Freeciv-3.1
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)
 
bool dbv_isset_any (const struct dbv *pdbv)
 
void dbv_set (struct dbv *pdbv, int bit)
 
void dbv_set_all (struct dbv *pdbv)
 
void dbv_clr (struct dbv *pdbv, int bit)
 
void dbv_clr_all (struct dbv *pdbv)
 
bool dbv_are_equal (const struct dbv *pdbv1, const struct dbv *pdbv2)
 
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)
 
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 75 of file bitvector.h.

◆ _BV_BITMASK

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

Definition at line 68 of file bitvector.h.

◆ _BV_BYTE_INDEX

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

Definition at line 67 of file bitvector.h.

◆ _BV_BYTES

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

Definition at line 66 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:302

Definition at line 113 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:280

Definition at line 106 of file bitvector.h.

◆ BV_CLR

#define BV_CLR (   bv,
  bit 
)
Value:
do { \
_BV_ASSERT(bv, bit); \
(bv).vec[_BV_BYTE_INDEX(bit)] &= ~_BV_BITMASK(bit); \
} while (FALSE)
#define _BV_BYTE_INDEX(bits)
Definition bitvector.h:67
#define FALSE
Definition support.h:47

Definition at line 86 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 95 of file bitvector.h.

◆ BV_CLR_ALL_FROM

#define BV_CLR_ALL_FROM (   vec_to,
  vec_from 
)
Value:
bv_clr_all_from((vec_to).vec, (vec_from).vec, \
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:351

Definition at line 127 of file bitvector.h.

◆ BV_DEFINE

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

Definition at line 132 of file bitvector.h.

◆ BV_ISSET

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

Definition at line 78 of file bitvector.h.

◆ BV_ISSET_ANY

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

Definition at line 109 of file bitvector.h.

◆ BV_SET

#define BV_SET (   bv,
  bit 
)
Value:
do { \
_BV_ASSERT(bv, bit); \
(bv).vec[_BV_BYTE_INDEX(bit)] |= _BV_BITMASK(bit); \
} while (FALSE)

Definition at line 81 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 99 of file bitvector.h.

◆ BV_SET_ALL_FROM

#define BV_SET_ALL_FROM (   vec_to,
  vec_from 
)
Value:
bv_set_all_from((vec_to).vec, (vec_from).vec, \
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:329

Definition at line 120 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:81
#define BV_CLR(bv, bit)
Definition bitvector.h:86

Definition at line 91 of file bitvector.h.

◆ MAX_DBV_LENGTH

#define MAX_DBV_LENGTH   (4 * 1024 * 1024)

Definition at line 63 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 302 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 280 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 351 of file bitvector.c.

◆ bv_match_dbv()

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 205 of file bitvector.c.

◆ 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 329 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 244 of file bitvector.c.

◆ dbv_are_equal()

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

Check if the two dynamic bitvectors are equal.

Definition at line 190 of file bitvector.c.

◆ 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.

◆ dbv_clr()

void dbv_clr ( struct dbv pdbv,
int  bit 
)

Clear the bit given by 'bit'.

Definition at line 167 of file bitvector.c.

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

◆ dbv_clr_all()

void dbv_clr_all ( struct dbv pdbv)

Clear all bits.

Definition at line 179 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 222 of file bitvector.c.

◆ dbv_debug()

void dbv_debug ( struct dbv pdbv)

Debug a dynamic bitvector.

Definition at line 252 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(), 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(), 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 132 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 156 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 235 of file bitvector.c.