Freeciv-3.3
Loading...
Searching...
No Matches
Data Structures | Macros | Functions
bitvector.h File Reference
#include <stdlib.h>
#include <string.h>
#include "log.h"
#include "support.h"

Go to the source code of this file.

Data Structures

struct  dbv
 

Macros

#define TEST_BIT(val, bit_no)    (((val) & (1u << (bit_no))) == (1u << (bit_no)))
 
#define MAX_DBV_LENGTH   (4 * 1024 * 1024)
 
#define _BV_BYTES(bits)   ((((bits) - 1) / 8) + 1)
 
#define _BV_BYTE_INDEX(bits)   ((bits) / 8)
 
#define _BV_BITMASK(bit)   (1u << ((bit) & 0x7))
 
#define _BV_ASSERT(bv, bit)   (void)0
 
#define BV_ISSET(bv, bit)
 
#define BV_SET(bv, bit)
 
#define BV_CLR(bv, bit)
 
#define BV_SET_VAL(bv, bit, val)
 
#define BV_CLR_ALL(bv)
 
#define BV_SET_ALL(bv)
 
#define BV_CHECK_MASK(vec1, vec2)
 
#define BV_ISSET_ANY(vec)   BV_CHECK_MASK(vec, vec)
 
#define BV_ARE_EQUAL(vec1, vec2)
 
#define BV_SET_ALL_FROM(vec_to, vec_from)
 
#define BV_CLR_ALL_FROM(vec_to, vec_from)
 
#define BV_DEFINE(name, bits)    typedef struct { unsigned char vec[_BV_BYTES(bits)]; } name
 

Functions

void dbv_init (struct dbv *pdbv, int bits)
 
void dbv_resize (struct dbv *pdbv, int bits)
 
void dbv_free (struct dbv *pdbv)
 
int dbv_bits (struct dbv *pdbv)
 
bool dbv_isset (const struct dbv *pdbv, int bit) fc__attribute((nonnull(1)))
 
bool dbv_isset_any (const struct dbv *pdbv) fc__attribute((nonnull(1)))
 
void dbv_set (struct dbv *pdbv, int bit) fc__attribute((nonnull(1)))
 
void dbv_set_all (struct dbv *pdbv) fc__attribute((nonnull(1)))
 
void dbv_clr (struct dbv *pdbv, int bit) fc__attribute((nonnull(1)))
 
void dbv_clr_all (struct dbv *pdbv) fc__attribute((nonnull(1)))
 
bool dbv_are_equal (const struct dbv *pdbv1, const struct dbv *pdbv2) fc__attribute((nonnull(1
 
bool bool bv_match_dbv (const struct dbv *match, const unsigned char *src)
 
void dbv_copy (struct dbv *dest, const struct dbv *src)
 
void dbv_to_bv (unsigned char *dest, const struct dbv *src)
 
void bv_to_dbv (struct dbv *dest, const unsigned char *src)
 
void dbv_debug (struct dbv *pdbv) fc__attribute((nonnull(1)))
 
bool bv_check_mask (const unsigned char *vec1, const unsigned char *vec2, size_t size1, size_t size2)
 
bool bv_are_equal (const unsigned char *vec1, const unsigned char *vec2, size_t size1, size_t size2)
 
void bv_set_all_from (unsigned char *vec_to, const unsigned char *vec_from, size_t size_to, size_t size_from)
 
void bv_clr_all_from (unsigned char *vec_to, const unsigned char *vec_from, size_t size_to, size_t size_from)
 

Macro Definition Documentation

◆ _BV_ASSERT

#define _BV_ASSERT (   bv,
  bit 
)    (void)0

Definition at line 83 of file bitvector.h.

◆ _BV_BITMASK

#define _BV_BITMASK (   bit)    (1u << ((bit) & 0x7))

Definition at line 76 of file bitvector.h.

◆ _BV_BYTE_INDEX

#define _BV_BYTE_INDEX (   bits)    ((bits) / 8)

Definition at line 75 of file bitvector.h.

◆ _BV_BYTES

#define _BV_BYTES (   bits)    ((((bits) - 1) / 8) + 1)

Definition at line 74 of file bitvector.h.

◆ BV_ARE_EQUAL

#define BV_ARE_EQUAL (   vec1,
  vec2 
)
Value:
bv_are_equal((vec1).vec, (vec2).vec, sizeof((vec1).vec), \
sizeof((vec2).vec))
bool bv_are_equal(const unsigned char *vec1, const unsigned char *vec2, size_t size1, size_t size2)
Definition bitvector.c:293
char * incite_cost
Definition comments.c:76

Definition at line 121 of file bitvector.h.

◆ BV_CHECK_MASK

#define BV_CHECK_MASK (   vec1,
  vec2 
)
Value:
bv_check_mask((vec1).vec, (vec2).vec, sizeof((vec1).vec), \
sizeof((vec2).vec))
bool bv_check_mask(const unsigned char *vec1, const unsigned char *vec2, size_t size1, size_t size2)
Definition bitvector.c:271

Definition at line 114 of file bitvector.h.

◆ BV_CLR

#define BV_CLR (   bv,
  bit 
)
Value:
do { \
} while (FALSE)
#define _BV_BYTE_INDEX(bits)
Definition bitvector.h:75
#define FALSE
Definition support.h:47

Definition at line 94 of file bitvector.h.

◆ BV_CLR_ALL

#define BV_CLR_ALL (   bv)
Value:
do { \
memset((bv).vec, 0, sizeof((bv).vec)); \
} while (FALSE)

Definition at line 103 of file bitvector.h.

◆ BV_CLR_ALL_FROM

#define BV_CLR_ALL_FROM (   vec_to,
  vec_from 
)
Value:
sizeof((vec_to).vec), sizeof((vec_from).vec))
void bv_clr_all_from(unsigned char *vec_to, const unsigned char *vec_from, size_t size_to, size_t size_from)
Definition bitvector.c:342

Definition at line 135 of file bitvector.h.

◆ BV_DEFINE

#define BV_DEFINE (   name,
  bits 
)     typedef struct { unsigned char vec[_BV_BYTES(bits)]; } name

Definition at line 140 of file bitvector.h.

◆ BV_ISSET

#define BV_ISSET (   bv,
  bit 
)
Value:
((bv).vec[_BV_BYTE_INDEX(bit)] & _BV_BITMASK(bit)) != 0)
#define _BV_ASSERT(bv, bit)
Definition bitvector.h:83
#define _BV_BITMASK(bit)
Definition bitvector.h:76

Definition at line 86 of file bitvector.h.

◆ BV_ISSET_ANY

#define BV_ISSET_ANY (   vec)    BV_CHECK_MASK(vec, vec)

Definition at line 117 of file bitvector.h.

◆ BV_SET

#define BV_SET (   bv,
  bit 
)
Value:
do { \
} while (FALSE)

Definition at line 89 of file bitvector.h.

◆ BV_SET_ALL

#define BV_SET_ALL (   bv)
Value:
do { \
memset((bv).vec, 0xff, sizeof((bv).vec)); \
} while (FALSE)

Definition at line 107 of file bitvector.h.

◆ BV_SET_ALL_FROM

#define BV_SET_ALL_FROM (   vec_to,
  vec_from 
)
Value:
sizeof((vec_to).vec), sizeof((vec_from).vec))
void bv_set_all_from(unsigned char *vec_to, const unsigned char *vec_from, size_t size_to, size_t size_from)
Definition bitvector.c:320

Definition at line 128 of file bitvector.h.

◆ BV_SET_VAL

#define BV_SET_VAL (   bv,
  bit,
  val 
)
Value:
do { \
if (val) { BV_SET(bv, bit); } else { BV_CLR(bv, bit); } \
} while (FALSE);
#define BV_SET(bv, bit)
Definition bitvector.h:89
#define BV_CLR(bv, bit)
Definition bitvector.h:94

Definition at line 99 of file bitvector.h.

◆ MAX_DBV_LENGTH

#define MAX_DBV_LENGTH   (4 * 1024 * 1024)

Definition at line 71 of file bitvector.h.

◆ TEST_BIT

#define TEST_BIT (   val,
  bit_no 
)     (((val) & (1u << (bit_no))) == (1u << (bit_no)))

Definition at line 28 of file bitvector.h.

Function Documentation

◆ bv_are_equal()

bool bv_are_equal ( const unsigned char vec1,
const unsigned char vec2,
size_t  size1,
size_t  size2 
)

Compare elements of two bitvectors. Both vectors are expected to have same number of elements, i.e. , size1 must be equal to size2.

Definition at line 293 of file bitvector.c.

Referenced by dbv_are_equal().

◆ bv_check_mask()

bool bv_check_mask ( const unsigned char vec1,
const unsigned char vec2,
size_t  size1,
size_t  size2 
)

Return whether two vectors: vec1 and vec2 have common bits. I.e. (vec1 & vec2) != 0.

Don't call this function directly, use BV_CHECK_MASK macro instead. Don't call this function with two different bitvectors.

Definition at line 271 of file bitvector.c.

Referenced by dbv_isset_any().

◆ bv_clr_all_from()

void bv_clr_all_from ( unsigned char vec_to,
const unsigned char vec_from,
size_t  size_to,
size_t  size_from 
)

Clear everything that is true in vec_from in vec_to. Stuff that already is false in vec_to aren't touched.

Both vectors are expected to have same number of elements, i.e. , size1 must be equal to size2.

Don't call this function directly, use BV_CLR_ALL_FROM macro instead.

Definition at line 342 of file bitvector.c.

◆ bv_match_dbv()

bool bool bv_match_dbv ( const struct dbv match,
const unsigned char src 
)

Is content of static bitvector same as that of dynamic one. Comparison size is taken from the dynamic one.

Definition at line 197 of file bitvector.c.

Referenced by update_player_tile_knowledge().

◆ bv_set_all_from()

void bv_set_all_from ( unsigned char vec_to,
const unsigned char vec_from,
size_t  size_to,
size_t  size_from 
)

Set everything that is true in vec_from in vec_to. Stuff that already is true in vec_to aren't touched. (Bitwise inclusive OR assignment)

Both vectors are expected to have same number of elements, i.e. , size1 must be equal to size2.

Don't call this function directly, use BV_SET_ALL_FROM macro instead.

Definition at line 320 of file bitvector.c.

◆ bv_to_dbv()

void bv_to_dbv ( struct dbv dest,
const unsigned char src 
)

Copy static bit vector content to dynamic bitvector. Static vector is assumed to be at least same size as the dynamic one.

Definition at line 236 of file bitvector.c.

◆ dbv_are_equal()

bool dbv_are_equal ( const struct dbv pdbv1,
const struct dbv pdbv2 
)

◆ dbv_bits()

int dbv_bits ( struct dbv pdbv)

Returns the number of bits defined in a dynamic bitvector.

Definition at line 108 of file bitvector.c.

Referenced by sg_extras_get_dbv().

◆ dbv_clr()

void dbv_clr ( struct dbv pdbv,
int  bit 
)

Clear the bit given by 'bit'.

Definition at line 163 of file bitvector.c.

Referenced by api_edit_tile_hide(), handle_tile_info(), map_clear_known(), and update_player_tile_knowledge().

◆ dbv_clr_all()

void dbv_clr_all ( struct dbv pdbv)

Clear all bits.

Definition at line 174 of file bitvector.c.

Referenced by dbv_init(), dbv_resize(), make_rivers(), sg_load_map_known(), and sg_load_map_known().

◆ dbv_copy()

void dbv_copy ( struct dbv dest,
const struct dbv src 
)

Copy dynamic bit vector content from another.

Definition at line 214 of file bitvector.c.

Referenced by really_give_tile_info_from_player_to_player().

◆ dbv_debug()

void dbv_debug ( struct dbv pdbv)

Debug a dynamic bitvector.

Definition at line 244 of file bitvector.c.

◆ dbv_free()

void dbv_free ( struct dbv pdbv)

Destroy a dynamic bitvector.

Definition at line 95 of file bitvector.c.

Referenced by check_native_area(), is_city_channel_tile(), make_rivers(), player_destroy(), player_map_free(), player_tile_free(), and remove_city().

◆ dbv_init()

void dbv_init ( struct dbv pdbv,
int  bits 
)

Initialize a dynamic bitvector of size 'bits'. 'bits' must be greater than 0 and lower than the maximal size given by MAX_DBV_LENGTH. The bitvector is set to all clear.

Definition at line 50 of file bitvector.c.

Referenced by check_native_area(), dbv_resize(), is_city_channel_tile(), make_rivers(), player_map_init(), player_tile_init(), and remove_city().

◆ dbv_isset()

bool dbv_isset ( const struct dbv pdbv,
int  bit 
)

◆ dbv_isset_any()

bool dbv_isset_any ( const struct dbv pdbv)

Test if any bit is set.

Definition at line 131 of file bitvector.c.

◆ dbv_resize()

void dbv_resize ( struct dbv pdbv,
int  bits 
)

Resize a dynamic bitvector. Create it if needed.

Definition at line 71 of file bitvector.c.

Referenced by client_player_maps_reset(), and dbv_copy().

◆ dbv_set()

void dbv_set ( struct dbv pdbv,
int  bit 
)

◆ dbv_set_all()

void dbv_set_all ( struct dbv pdbv)

Set all bits.

Definition at line 153 of file bitvector.c.

◆ dbv_to_bv()

void dbv_to_bv ( unsigned char dest,
const struct dbv src 
)

Copy dynamic bit vector content to static bitvector. Static vector is assumed to be at least same size as the dynamic one.

Definition at line 227 of file bitvector.c.

Referenced by do_paradrop(), send_tile_info(), and sg_special_set_dbv().