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 */
32struct 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)
45bool dbv_isset_any(const struct dbv *pdbv)
47
48void dbv_set(struct dbv *pdbv, int bit)
50void dbv_set_all(struct dbv *pdbv)
52
53void dbv_clr(struct dbv *pdbv, int bit)
55void dbv_clr_all(struct dbv *pdbv)
57
58bool dbv_are_equal(const struct dbv *pdbv1, const struct dbv *pdbv2)
59 fc__attribute((nonnull (1, 2)));
60bool bv_match_dbv(const struct dbv *match, const unsigned char *src);
61void dbv_copy(struct dbv *dest, const struct dbv *src);
62
63void dbv_to_bv(unsigned char *dest, const struct dbv *src);
64void bv_to_dbv(struct dbv *dest, const unsigned char *src);
65
66void dbv_debug(struct dbv *pdbv)
68
69/* Maximal size of a dynamic bitvector.
70 Use a large value to be on the safe side (4Mbits = 512kbytes). */
71#define MAX_DBV_LENGTH (4 * 1024 * 1024)
72
73/* Static bitvectors. */
74#define _BV_BYTES(bits) ((((bits) - 1) / 8) + 1)
75#define _BV_BYTE_INDEX(bits) ((bits) / 8)
76#define _BV_BITMASK(bit) (1u << ((bit) & 0x7))
77
78#ifdef FREECIV_DEBUG
79#define _BV_ASSERT(bv, bit) \
80 fc_assert((signed int)(bit) >= 0 \
81 && (signed int)(bit) < (signed int) sizeof((bv).vec) * 8)
82#else
83#define _BV_ASSERT(bv, bit) (void)0
84#endif
85
86#define BV_ISSET(bv, bit) \
87 (_BV_ASSERT(bv, bit), \
88 ((bv).vec[_BV_BYTE_INDEX(bit)] & _BV_BITMASK(bit)) != 0)
89#define BV_SET(bv, bit) \
90 do { \
91 _BV_ASSERT(bv, bit); \
92 (bv).vec[_BV_BYTE_INDEX(bit)] |= _BV_BITMASK(bit); \
93 } while (FALSE)
94#define BV_CLR(bv, bit) \
95 do { \
96 _BV_ASSERT(bv, bit); \
97 (bv).vec[_BV_BYTE_INDEX(bit)] &= ~_BV_BITMASK(bit); \
98 } while (FALSE)
99#define BV_SET_VAL(bv, bit, val) \
100 do { \
101 if (val) { BV_SET(bv, bit); } else { BV_CLR(bv, bit); } \
102 } while (FALSE);
103#define BV_CLR_ALL(bv) \
104 do { \
105 memset((bv).vec, 0, sizeof((bv).vec)); \
106 } while (FALSE)
107#define BV_SET_ALL(bv) \
108 do { \
109 memset((bv).vec, 0xff, sizeof((bv).vec)); \
110 } while (FALSE)
111
112bool bv_check_mask(const unsigned char *vec1, const unsigned char *vec2,
113 size_t size1, size_t size2);
114#define BV_CHECK_MASK(vec1, vec2) \
115 bv_check_mask((vec1).vec, (vec2).vec, sizeof((vec1).vec), \
116 sizeof((vec2).vec))
117#define BV_ISSET_ANY(vec) BV_CHECK_MASK(vec, vec)
118
119bool bv_are_equal(const unsigned char *vec1, const unsigned char *vec2,
120 size_t size1, size_t size2);
121#define BV_ARE_EQUAL(vec1, vec2) \
122 bv_are_equal((vec1).vec, (vec2).vec, sizeof((vec1).vec), \
123 sizeof((vec2).vec))
124
125void bv_set_all_from(unsigned char *vec_to,
126 const unsigned char *vec_from,
127 size_t size_to, size_t size_from);
128#define BV_SET_ALL_FROM(vec_to, vec_from) \
129 bv_set_all_from((vec_to).vec, (vec_from).vec, \
130 sizeof((vec_to).vec), sizeof((vec_from).vec))
131
132void bv_clr_all_from(unsigned char *vec_to,
133 const unsigned char *vec_from,
134 size_t size_to, size_t size_from);
135#define BV_CLR_ALL_FROM(vec_to, vec_from) \
136 bv_clr_all_from((vec_to).vec, (vec_from).vec, \
137 sizeof((vec_to).vec), sizeof((vec_from).vec))
138
139/* Used to make a BV typedef. Such types are usually called "bv_foo". */
140#define BV_DEFINE(name, bits) \
141 typedef struct { unsigned char vec[_BV_BYTES(bits)]; } name
142
143#ifdef __cplusplus
144}
145#endif /* __cplusplus */
146
147#endif /* FC__BITVECTOR_H */
bool bv_check_mask(const unsigned char *vec1, const unsigned char *vec2, size_t size1, size_t size2)
Definition bitvector.c:271
void bv_to_dbv(struct dbv *dest, const unsigned char *src)
Definition bitvector.c:236
void dbv_resize(struct dbv *pdbv, int bits)
Definition bitvector.c:71
void dbv_init(struct dbv *pdbv, int bits)
Definition bitvector.c:50
void dbv_clr(struct dbv *pdbv, int bit) fc__attribute((nonnull(1)))
Definition bitvector.c:163
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
bool dbv_isset(const struct dbv *pdbv, int bit) fc__attribute((nonnull(1)))
Definition bitvector.c:120
int dbv_bits(struct dbv *pdbv)
Definition bitvector.c:108
bool dbv_are_equal(const struct dbv *pdbv1, const struct dbv *pdbv2) fc__attribute((nonnull(1
bool bv_are_equal(const unsigned char *vec1, const unsigned char *vec2, size_t size1, size_t size2)
Definition bitvector.c:293
bool bool bv_match_dbv(const struct dbv *match, const unsigned char *src)
Definition bitvector.c:197
void dbv_set_all(struct dbv *pdbv) fc__attribute((nonnull(1)))
Definition bitvector.c:153
bool dbv_isset_any(const struct dbv *pdbv) fc__attribute((nonnull(1)))
Definition bitvector.c:131
void dbv_set(struct dbv *pdbv, int bit) fc__attribute((nonnull(1)))
Definition bitvector.c:142
void dbv_copy(struct dbv *dest, const struct dbv *src)
Definition bitvector.c:214
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
void dbv_debug(struct dbv *pdbv) fc__attribute((nonnull(1)))
Definition bitvector.c:244
void dbv_free(struct dbv *pdbv)
Definition bitvector.c:95
void dbv_clr_all(struct dbv *pdbv) fc__attribute((nonnull(1)))
Definition bitvector.c:174
void dbv_to_bv(unsigned char *dest, const struct dbv *src)
Definition bitvector.c:227
char * incite_cost
Definition comments.c:76
int bits
Definition bitvector.h:33
unsigned char * vec
Definition bitvector.h:34
#define fc__attribute(x)
Definition support.h:99