Debian Codegraph @

membership/ccm/ccmbitmap.c

raw
1 /*
2 * ccmbitmap.c: functions that manipulate bitmaps
3 *
4 * Copyright (C) 2001 Aatash Patel <[email protected]>
5 * Copyright (C) 2001 Dr Xu <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 */
22 /* bitmap.c */
23 /* Routines to manage a bitmap -- an array of bits each of which */
24 /* can be either on or off. Represented as an array of integers. */
25 #include <lha_internal.h>
26 #include <ccm.h>
27
28 #ifndef TRUE
29 # define TRUE 1
30 # define FALSE 0
31 #endif
32
33
34
35 /* Initialize a bitmap with "nitems" bits, so that every bit is clear. */
36 /* it can be added somewhere on a list. */
37 int
38 bitmap_create(char **map, int numBits)
39 {
40 int i, numBytes;
41
42 if (numBits%BitsInByte == 0) {
43 numBytes = numBits/BitsInByte;
44 } else {
45 numBytes = numBits/BitsInByte+1;
46 }
47 *map = g_malloc(sizeof(char)*numBytes);
48 assert(*map);
49 for ( i = 0 ; i < numBytes; i++ )
50 (*map)[i] = 0;
51 return(numBytes);
52 }
53
54 /* return the number of bytes required to represent numBits */
55 int
56 bitmap_size(int numBits)
57 {
58 int numBytes;
59 if (numBits%BitsInByte == 0) {
60 numBytes = numBits/BitsInByte;
61 } else {
62 numBytes = numBits/BitsInByte+1;
63 }
64 return(numBytes);
65 }
66
67
68 /* delete bitmap */
69 void
70 bitmap_delete(char *map) {
71 g_free(map);
72 }
73
74 /* mark the which bit as set */
75 void
76 bitmap_mark(int which, char *map, int numBits) {
77 assert(which >= 0 && which < numBits);
78 map[which / BitsInByte] |= 1 << (which % BitsInByte);
79 }
80
81
82 /* Clear the "which" bit in a bitmap. */
83 void
84 bitmap_clear(int which, char *map, int numBits) {
85 assert(which >= 0 && which < numBits);
86 map[which / BitsInByte] &= ~(1 << (which % BitsInByte));
87 }
88
89
90 /* Return TRUE if the "which" bit is set. */
91 int
92 bitmap_test(int which, const char *map, int numBits)
93 {
94 assert(which >= 0 && which < numBits);
95 if (map[which / BitsInByte] & (1 << (which % BitsInByte)))
96 return(TRUE);
97 else
98 return(FALSE);
99 }
100
101 /* Return total number of bits already set */
102 int
103 bitmap_count(const char *map, int numBits)
104 {
105 int count, i;
106
107 count = 0;
108 for (i = 0; i < numBits; i++)
109 if (bitmap_test(i, map, numBits))
110 count++;
111 return count;
112 }
113
114
115 /* Print the contents of the bitmap, for debugging. */
116 void
117 bitmap_print(char *map, int numBits, char * comments)
118 {
119 int i;
120
121 fprintf(stderr, "%s\n", comments);
122 for (i = 0; i < numBits; i++)
123 if (bitmap_test(i, map, numBits))
124 fprintf(stderr, "%d, ", i);
125 fprintf(stderr, "\n");
126 }
127
128 /* Reset the bitmap. */
129 void
130 bitmap_reset(char *map, int numBits)
131 {
132 int i;
133
134 for(i=0; i<numBits; i++) {
135 bitmap_clear(i, map, numBits);
136 }
137 }