Freeciv-3.3: /fast/freeciv/nightly/main/utility/bitvector.h Source File

Freeciv-3.3

Loading...
Searching...
No Matches
bitvector.h
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13#ifndef FC__BITVECTOR_H
14#define FC__BITVECTOR_H
15
16#ifdef __cplusplus
17extern "C" {
18#endif /* __cplusplus */
19
20#include <stdlib.h> /* size_t */
21#include <string.h> /* memset */
22
23/* utility */
24#include "log.h"
25#include "support.h" /* bool, fc__attribute */
26
27/* Yields TRUE iff the bit bit_no is set in val. */
28#define TEST_BIT(val, bit_no) \
29 (((val) & (1u << (bit_no))) == (1u << (bit_no)))
30
31/* Dynamic bitvectors */
32 struct dbv {
33 int bits;
34 unsigned char *vec;
35};
36
37void dbv_init(struct dbv *pdbv, int bits);
38void dbv_resize(struct dbv *pdbv, int bits);
39void dbv_free(struct dbv *pdbv);
40
41int dbv_bits(struct dbv *pdbv);
42
43bool dbv_isset(const struct dbv *pdbv, int bit);
44bool dbv_isset_any(const struct dbv *pdbv);
45
46void dbv_set(struct dbv *pdbv, int bit);
47void dbv_set_all(struct dbv *pdbv);
48
49void dbv_clr(struct dbv *pdbv, int bit);
50void dbv_clr_all(struct dbv *pdbv);
51
52bool dbv_are_equal(const struct dbv *pdbv1, const struct dbv *pdbv2);
53bool bv_match_dbv(const struct dbv *match, const unsigned char *src);
54void dbv_copy(struct dbv *dest, const struct dbv *src);
55
56void dbv_to_bv(unsigned char *dest, const struct dbv *src);
57void bv_to_dbv(struct dbv *dest, const unsigned char *src);
58
59void dbv_debug(struct dbv *pdbv);
60
61/* Maximal size of a dynamic bitvector.
62 Use a large value to be on the safe side (4Mbits = 512kbytes). */
63#define MAX_DBV_LENGTH (4 * 1024 * 1024)
64
65/* Static bitvectors. */
66#define _BV_BYTES(bits) ((((bits) - 1) / 8) + 1)
67#define _BV_BYTE_INDEX(bits) ((bits) / 8)
68#define _BV_BITMASK(bit) (1u << ((bit) & 0x7))
69
70#ifdef FREECIV_DEBUG
71#define _BV_ASSERT(bv, bit) \
72 fc_assert((signed int)(bit) >= 0 \
73 && (signed int)(bit) < (signed int) sizeof((bv).vec) * 8)
74#else
75#define _BV_ASSERT(bv, bit) (void)0
76#endif
77
78#define BV_ISSET(bv, bit) \
79 (_BV_ASSERT(bv, bit), \
80 ((bv).vec[_BV_BYTE_INDEX(bit)] & _BV_BITMASK(bit)) != 0)
81#define BV_SET(bv, bit) \
82 do { \
83 _BV_ASSERT(bv, bit); \
84 (bv).vec[_BV_BYTE_INDEX(bit)] |= _BV_BITMASK(bit); \
85 } while (FALSE)
86#define BV_CLR(bv, bit) \
87 do { \
88 _BV_ASSERT(bv, bit); \
89 (bv).vec[_BV_BYTE_INDEX(bit)] &= ~_BV_BITMASK(bit); \
90 } while (FALSE)
91#define BV_SET_VAL(bv, bit, val) \
92 do { \
93 if (val) { BV_SET(bv, bit); } else { BV_CLR(bv, bit); } \
94 } while (FALSE);
95#define BV_CLR_ALL(bv) \
96 do { \
97 memset((bv).vec, 0, sizeof((bv).vec)); \
98 } while (FALSE)
99#define BV_SET_ALL(bv) \
100 do { \
101 memset((bv).vec, 0xff, sizeof((bv).vec)); \
102 } while (FALSE)
103
104bool bv_check_mask(const unsigned char *vec1, const unsigned char *vec2,
105 size_t size1, size_t size2);
106#define BV_CHECK_MASK(vec1, vec2) \
107 bv_check_mask((vec1).vec, (vec2).vec, sizeof((vec1).vec), \
108 sizeof((vec2).vec))
109#define BV_ISSET_ANY(vec) BV_CHECK_MASK(vec, vec)
110
111bool bv_are_equal(const unsigned char *vec1, const unsigned char *vec2,
112 size_t size1, size_t size2);
113#define BV_ARE_EQUAL(vec1, vec2) \
114 bv_are_equal((vec1).vec, (vec2).vec, sizeof((vec1).vec), \
115 sizeof((vec2).vec))
116
117void bv_set_all_from(unsigned char *vec_to,
118 const unsigned char *vec_from,
119 size_t size_to, size_t size_from);
120#define BV_SET_ALL_FROM(vec_to, vec_from) \
121 bv_set_all_from((vec_to).vec, (vec_from).vec, \
122 sizeof((vec_to).vec), sizeof((vec_from).vec))
123
124void bv_clr_all_from(unsigned char *vec_to,
125 const unsigned char *vec_from,
126 size_t size_to, size_t size_from);
127#define BV_CLR_ALL_FROM(vec_to, vec_from) \
128 bv_clr_all_from((vec_to).vec, (vec_from).vec, \
129 sizeof((vec_to).vec), sizeof((vec_from).vec))
130
131/* Used to make a BV typedef. Such types are usually called "bv_foo". */
132#define BV_DEFINE(name, bits) \
133 typedef struct { unsigned char vec[_BV_BYTES(bits)]; } name
134
135#ifdef __cplusplus
136}
137#endif /* __cplusplus */
138
139#endif /* FC__BITVECTOR_H */
bv_check_mask
bool bv_check_mask(const unsigned char *vec1, const unsigned char *vec2, size_t size1, size_t size2)
Definition bitvector.c:280
bv_to_dbv
void bv_to_dbv(struct dbv *dest, const unsigned char *src)
Definition bitvector.c:244
dbv_isset_any
bool dbv_isset_any(const struct dbv *pdbv)
Definition bitvector.c:132
dbv_resize
void dbv_resize(struct dbv *pdbv, int bits)
Definition bitvector.c:71
bv_match_dbv
bool bv_match_dbv(const struct dbv *match, const unsigned char *src)
Definition bitvector.c:205
dbv_init
void dbv_init(struct dbv *pdbv, int bits)
Definition bitvector.c:50
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)
Definition bitvector.c:351
dbv_bits
int dbv_bits(struct dbv *pdbv)
Definition bitvector.c:108
dbv_set_all
void dbv_set_all(struct dbv *pdbv)
Definition bitvector.c:156
dbv_set
void dbv_set(struct dbv *pdbv, int bit)
Definition bitvector.c:144
bv_are_equal
bool bv_are_equal(const unsigned char *vec1, const unsigned char *vec2, size_t size1, size_t size2)
Definition bitvector.c:302
dbv_isset
bool dbv_isset(const struct dbv *pdbv, int bit)
Definition bitvector.c:120
dbv_copy
void dbv_copy(struct dbv *dest, const struct dbv *src)
Definition bitvector.c:222
dbv_debug
void dbv_debug(struct dbv *pdbv)
Definition bitvector.c:252
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)
Definition bitvector.c:329
dbv_free
void dbv_free(struct dbv *pdbv)
Definition bitvector.c:95
dbv_clr_all
void dbv_clr_all(struct dbv *pdbv)
Definition bitvector.c:179
dbv_clr
void dbv_clr(struct dbv *pdbv, int bit)
Definition bitvector.c:167
dbv_are_equal
bool dbv_are_equal(const struct dbv *pdbv1, const struct dbv *pdbv2)
Definition bitvector.c:190
dbv_to_bv
void dbv_to_bv(unsigned char *dest, const struct dbv *src)
Definition bitvector.c:235
incite_cost
char * incite_cost
Definition comments.c:74
log.h
dbv
Definition bitvector.h:32
dbv::bits
int bits
Definition bitvector.h:33
dbv::vec
unsigned char * vec
Definition bitvector.h:34
support.h