Subversion Repositories tendra.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
7 7u83 2
 * Copyright (c) 2002-2005 The TenDRA Project <http://www.tendra.org/>.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. Neither the name of The TenDRA Project nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific, prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
18
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
 * EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 * $Id$
30
 */
31
/*
2 7u83 32
    		 Crown Copyright (c) 1997
7 7u83 33
 
2 7u83 34
    This TenDRA(r) Computer Program is subject to Copyright
35
    owned by the United Kingdom Secretary of State for Defence
36
    acting through the Defence Evaluation and Research Agency
37
    (DERA).  It is made available to Recipients with a
38
    royalty-free licence for its use, reproduction, transfer
39
    to other parties and amendment for any purpose not excluding
40
    product development provided that any such use et cetera
41
    shall be deemed to be acceptance of the following conditions:-
7 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
7 7u83 45
 
2 7u83 46
        (2) Any amended version of it shall be clearly marked to
47
        show both the nature of and the organisation responsible
48
        for the relevant amendment or amendments;
7 7u83 49
 
2 7u83 50
        (3) Its onward transfer from a recipient to another
51
        party shall be deemed to be that party's acceptance of
52
        these conditions;
7 7u83 53
 
2 7u83 54
        (4) DERA gives no warranty or assurance as to its
55
        quality or suitability for any purpose and DERA accepts
56
        no liability whatsoever in relation to any use to which
57
        it may be put.
58
*/
59
 
60
 
61
/**** bitvec.c --- Bit vector manipulation.
62
 *
63
 ** Author: Steve Folkes <smf@hermes.mod.uk>
64
 *
65
 **** Commentary:
66
 *
67
 * This file implements the bit vector manipulation routines specified in
68
 * "bitvec.h".  See that file for more details.
69
 *
70
 **** Change Log:
71
 * $Log: bitvec.c,v $
72
 * Revision 1.1.1.1  1998/01/17  15:57:43  release
73
 * First version to be checked into rolling release.
74
 *
75
 * Revision 1.2  1994/12/15  09:56:59  smf
76
 * Brought into line with OSSG C Coding Standards Document, as per
77
 * "CR94_178.sid+tld-update".
78
 *
79
 * Revision 1.1.1.1  1994/07/25  16:05:47  smf
80
 * Initial import of library shared files.
81
 *
82
**/
83
 
84
/****************************************************************************/
85
 
86
#include "bitvec.h"
87
 
88
/*--------------------------------------------------------------------------*/
89
 
7 7u83 90
static unsigned		bitvec_size;
91
static unsigned		bitvec_valid_bits;
92
static ByteT		bitvec_mask;
2 7u83 93
 
7 7u83 94
#define NUM_BITS	((unsigned)(CHAR_BIT))
2 7u83 95
 
96
/*--------------------------------------------------------------------------*/
97
 
98
void
7 7u83 99
bitvec_set_size(unsigned size)
2 7u83 100
{
101
    bitvec_valid_bits = size;
7 7u83 102
    bitvec_size       = ((size + NUM_BITS - (unsigned)1) / NUM_BITS);
103
    bitvec_mask       = (ByteT)0;
2 7u83 104
    if (size % NUM_BITS) {
105
	unsigned i;
106
	unsigned mask =0;
107
 
7 7u83 108
	for (i = (NUM_BITS - (size % NUM_BITS)); i; i--) {
2 7u83 109
	    mask >>= 1;
7 7u83 110
	    mask  |= ((unsigned)1 << (NUM_BITS - (unsigned)1));
2 7u83 111
	}
7 7u83 112
	bitvec_mask = (ByteT)mask;
2 7u83 113
    }
114
    bitvec_mask = (~bitvec_mask);
115
}
116
 
117
void
7 7u83 118
bitvec_init(BitVecP bitvec)
2 7u83 119
{
7 7u83 120
    bitvec->bits = ALLOCATE_VECTOR(ByteT, bitvec_size);
2 7u83 121
}
122
 
123
void
7 7u83 124
bitvec_copy(BitVecP to, BitVecP from)
2 7u83 125
{
7 7u83 126
    to->bits = ALLOCATE_VECTOR(ByteT, bitvec_size);
127
   (void)memcpy((GenericP)(to->bits), (GenericP)(from->bits),
128
		(SizeT)bitvec_size);
2 7u83 129
}
130
 
131
void
7 7u83 132
bitvec_replace(BitVecP to, BitVecP from)
2 7u83 133
{
7 7u83 134
   (void)memcpy((GenericP)(to->bits), (GenericP)(from->bits),
135
		(SizeT)bitvec_size);
2 7u83 136
}
137
 
138
void
7 7u83 139
bitvec_empty(BitVecP bitvec)
2 7u83 140
{
7 7u83 141
   (void)memset((GenericP)(bitvec->bits), 0, (SizeT)bitvec_size);
2 7u83 142
}
143
 
144
BoolT
7 7u83 145
bitvec_is_empty(BitVecP bitvec)
2 7u83 146
{
147
    ByteP    bitvec_bits = (bitvec->bits);
148
    unsigned bytes       = bitvec_size;
149
 
7 7u83 150
    while (bytes--) {
151
	if (*bitvec_bits++) {
152
	    return(FALSE);
2 7u83 153
	}
154
    }
7 7u83 155
    return(TRUE);
2 7u83 156
}
157
 
158
BoolT
7 7u83 159
bitvec_is_full(BitVecP bitvec)
2 7u83 160
{
161
    ByteP    bitvec_bits = (bitvec->bits);
162
    unsigned bytes       = bitvec_size;
163
 
7 7u83 164
    while (bytes--) {
165
	ByteT byte = (*bitvec_bits++);
2 7u83 166
 
167
	if (bytes == 0) {
7 7u83 168
	    byte |= (ByteT)~bitvec_mask;
2 7u83 169
	}
170
	byte = ~byte;
171
	if (byte) {
7 7u83 172
	    return(FALSE);
2 7u83 173
	}
174
    }
7 7u83 175
    return(TRUE);
2 7u83 176
}
177
 
178
void
7 7u83 179
bitvec_set(BitVecP bitvec, unsigned bit)
2 7u83 180
{
7 7u83 181
    ASSERT(bit < bitvec_valid_bits);
182
   (bitvec->bits)[bit / NUM_BITS] |= (ByteT)(1 << (bit % NUM_BITS));
2 7u83 183
}
184
 
185
BoolT
7 7u83 186
bitvec_is_set(BitVecP bitvec, unsigned bit)
2 7u83 187
{
7 7u83 188
    ASSERT(bit < bitvec_valid_bits);
189
    return((bitvec->bits)[bit / NUM_BITS] & ((ByteT)1 << (bit % NUM_BITS)));
2 7u83 190
}
191
 
192
void
7 7u83 193
bitvec_or(BitVecP to, BitVecP from)
2 7u83 194
{
195
    ByteP    to_bits   = (to->bits);
196
    ByteP    from_bits = (from->bits);
197
    unsigned bytes     = bitvec_size;
198
 
7 7u83 199
    while (bytes--) {
200
	(*to_bits++) |= (*from_bits++);
2 7u83 201
    }
202
}
203
 
204
void
7 7u83 205
bitvec_and(BitVecP to, BitVecP from)
2 7u83 206
{
207
    ByteP    to_bits   = (to->bits);
208
    ByteP    from_bits = (from->bits);
209
    unsigned bytes     = bitvec_size;
210
 
7 7u83 211
    while (bytes--) {
212
	(*to_bits++) &= (*from_bits++);
2 7u83 213
    }
214
}
215
 
216
void
7 7u83 217
bitvec_not(BitVecP to)
2 7u83 218
{
219
    ByteP    to_bits = (to->bits);
220
    unsigned bytes   = bitvec_size;
221
 
7 7u83 222
    while (bytes--) {
2 7u83 223
	(*to_bits) = (~(*to_bits));
7 7u83 224
	to_bits++;
2 7u83 225
    }
7 7u83 226
   (to->bits)[bitvec_size - 1] &= bitvec_mask;
2 7u83 227
}
228
 
229
BoolT
7 7u83 230
bitvec_equal(BitVecP bitvec1, BitVecP bitvec2)
2 7u83 231
{
232
    ByteP    bitvec1_bits = (bitvec1->bits);
233
    ByteP    bitvec2_bits = (bitvec2->bits);
234
    unsigned bytes        = bitvec_size;
235
 
7 7u83 236
    while (bytes--) {
237
	if ((*bitvec1_bits++) != (*bitvec2_bits++)) {
238
	    return(FALSE);
2 7u83 239
	}
240
    }
7 7u83 241
    return(TRUE);
2 7u83 242
}
243
 
244
BoolT
7 7u83 245
bitvec_intersects(BitVecP bitvec1, BitVecP bitvec2)
2 7u83 246
{
247
    ByteP    bitvec1_bits = (bitvec1->bits);
248
    ByteP    bitvec2_bits = (bitvec2->bits);
249
    unsigned bytes        = bitvec_size;
250
 
7 7u83 251
    while (bytes--) {
252
	if ((*bitvec1_bits++) & (*bitvec2_bits++)) {
253
	    return(TRUE);
2 7u83 254
	}
255
    }
7 7u83 256
    return(FALSE);
2 7u83 257
}
258
 
259
unsigned
7 7u83 260
bitvec_num_bits(BitVecP bitvec)
2 7u83 261
{
262
    unsigned i;
263
    unsigned num_bits = 0;
264
 
7 7u83 265
    for (i = 0; i < bitvec_valid_bits; i++) {
266
	if (bitvec_is_set(bitvec, i)) {
267
	    num_bits++;
2 7u83 268
	}
269
    }
7 7u83 270
    return(num_bits);
2 7u83 271
}
272
 
273
unsigned
7 7u83 274
bitvec_first_bit(BitVecP bitvec)
2 7u83 275
{
276
    unsigned i;
277
 
7 7u83 278
    for (i = 0; i < bitvec_valid_bits; i++) {
279
	if (bitvec_is_set(bitvec, i)) {
280
	    return(i);
2 7u83 281
	}
282
    }
7 7u83 283
    return(bitvec_valid_bits);
2 7u83 284
}
285
 
286
BoolT
7 7u83 287
bitvec_next_bit(BitVecP bitvec, unsigned *next_ref)
2 7u83 288
{
289
    unsigned i;
290
 
7 7u83 291
    for (i = ((*next_ref) + 1); i < bitvec_valid_bits; i++) {
292
	if (bitvec_is_set(bitvec, i)) {
2 7u83 293
	    *next_ref = i;
7 7u83 294
	    return(TRUE);
2 7u83 295
	}
296
    }
7 7u83 297
    return(FALSE);
2 7u83 298
}
299
 
300
void
7 7u83 301
bitvec_destroy(BitVecP bitvec)
2 7u83 302
{
7 7u83 303
    DEALLOCATE(bitvec->bits);
2 7u83 304
}
305
 
306
void
7 7u83 307
write_bitvec_indices(OStreamP ostream, BitVecP bitvec)
2 7u83 308
{
309
    unsigned num_bits_set = 0;
310
    unsigned i;
311
 
7 7u83 312
    for (i = 0; i < bitvec_valid_bits; i++) {
313
	if (bitvec_is_set(bitvec, i)) {
314
	    num_bits_set++;
2 7u83 315
	}
316
    }
7 7u83 317
    for (i = 0; i < bitvec_valid_bits; i++) {
318
	if (bitvec_is_set(bitvec, i)) {
319
	    write_unsigned(ostream, i);
320
	    num_bits_set--;
2 7u83 321
	    if (num_bits_set == 1) {
7 7u83 322
		write_cstring(ostream, " & ");
2 7u83 323
	    } else if (num_bits_set != 0) {
7 7u83 324
		write_cstring(ostream, ", ");
2 7u83 325
	    }
326
	}
327
    }
328
}
329
 
330
/*
331
 * Local variables(smf):
332
 * eval: (include::add-path-entry "../os-interface" "../generated")
333
 * end:
334
**/