comparison anagram/support/bitmap.cpp @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:13d2b8934445
1 /*
2 * AnaGram, A System for Syntax Directed Programming
3 * Copyright 1993-2000 Parsifal Software. All Rights Reserved.
4 * See the file COPYING for license and usage terms.
5 *
6 * bitmap.cpp
7 */
8
9 #include "assert.h"
10 #include "bitmap.h"
11
12 //#define INCLUDE_LOGGING
13 #include "log.h"
14
15
16 void CharBitmap::blurCase(int x) {
17 assert((unsigned) x < nBits);
18 unsigned mask = 1 << (x%8);
19 int index = x/8;
20 int truthValue = (map[index] | map[index ^ 4]) & mask;
21 map[index] |= (unsigned char) truthValue;
22 map[index ^ 4] |= (unsigned char) truthValue;
23 }
24
25 void CharBitmap::blurCase() {
26 LOGSECTION("CharBitmap::blurCase()");
27 int i;
28 for (i = 'A'; i <= 'Z'; i++) {
29 blurCase(i);
30 }
31 for (i = 0xc0; i <= 0xde; i++) {
32 if (i != 0xd7) {
33 blurCase(i);
34 }
35 }
36 LOGS("blurCase complete");
37 }
38
39
40 int Bitmap::setBit(int x) {
41 LOGSECTION("Bitmap::setBit");
42 LOGV(x) LCV(nBits);
43 assert((unsigned) x < nBits);
44 unsigned mask = 1 << (x %8);
45 int truthValue = (map[x/8] & mask) != 0;
46 map[x/8] |= (unsigned char) mask;
47 return truthValue;
48 }
49
50 int CharBitmap::setBit(int x) {
51 return Bitmap::setBit(x - min_char_number);
52 }
53
54 int Bitmap::getBit(int x) {
55 assert((unsigned) x < nBits);
56 return (map[x/8] & (1 << (x % 8))) != 0;
57 }
58
59 int CharBitmap::getBit(int x) {
60 return Bitmap::getBit(x - min_char_number);
61 }
62
63 int Bitmap::list(int *x) {
64 LOGSECTION("Bitmap::list");
65 unsigned char *p = (unsigned char *) map;
66 unsigned i;
67 int count = 0;
68 for (i = 0; i < nBits; i++) {
69 if ((p[i/8] & (1 << (i % 8))) == 0) {
70 continue;
71 }
72 x[count++] = i;
73 }
74 return count;
75 }
76
77 int CharBitmap::list(int *x) {
78 int count = Bitmap::list(x);
79 for (int i = 0; i < count; i++) {
80 x[i] += min_char_number;
81 }
82 return count;
83 }
84
85 Bitmap &Bitmap::setRange(unsigned first, unsigned last) {
86 LOGSECTION("Bitmap::setRange");
87 LOGV(first) LCV(last);
88 assert(first <= last && last < nBits);
89 int n = last/8;
90 unsigned char bit = (unsigned char) (1 << (last %8));
91 LOGV(n) LCV(bit);
92 while (bit && first <= last) {
93 map[n] |= bit;
94 bit >>= 1;
95 last--;
96 }
97 LOGV(bit) LCV(last);
98 int x = first/8;
99 bit = (unsigned char) (1 << (first % 8));
100 LOGV(x) LCV(bit);
101 while (bit && first <= last) {
102 map[x] |= bit;
103 bit <<= 1;
104 first++;
105 }
106 LOGV(x) LCV(bit);
107 x++;
108 while (x < n) {
109 map[x++] = 0xff;
110 }
111 return *this;
112 }
113
114 #ifdef _MSC_VER
115 Bitmap &CharBitmap::setRange(unsigned first, unsigned last) {
116 #else
117 CharBitmap &CharBitmap::setRange(unsigned first, unsigned last) {
118 #endif
119 LOGSECTION("CharBitmap::setRange");
120 LOGV(first) LCV(last);
121 Bitmap::setRange(first - min_char_number, last - min_char_number);
122 return *this;
123 }
124
125 Bitmap &Bitmap::operator |= (const Bitmap &b) {
126 assert(nChars == b.nChars);
127 int x = nChars;
128 while (x--) {
129 map[x] |= b.map[x];
130 }
131 return *this;
132 }
133
134 Bitmap &Bitmap::operator &= (const Bitmap &b) {
135 assert(nChars == b.nChars);
136 int x = nChars;
137 while (x--) {
138 map[x] &= b.map[x];
139 }
140 return *this;
141 }
142
143 Bitmap &Bitmap::operator -= (const Bitmap &b) {
144 LOGSECTION("CharBitmap::operator -=");
145 LOGV(nChars) LCV(b.nChars);
146 assert(nChars == b.nChars);
147 int x = nChars;
148 while (x--) {
149 map[x] &= (unsigned char) ~b.map[x];
150 }
151 return *this;
152 }
153
154 Bitmap &Bitmap::operator ~() {
155 int x = nChars;
156 while (x--) {
157 map[x] = (unsigned char) ~map[x];
158 }
159 return *this;
160 }
161