00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "w_defines.h"
00031
00032
00033
00034 #ifdef __GNUC__
00035 # pragma implementation
00036 #endif
00037
00038 #include "w_base.h"
00039 #include <cstring>
00040 #include "w_bitmap.h"
00041
00042 inline int div8(long x) { return x >> 3; }
00043 inline int mod8(long x) { return x & 7; }
00044 inline int div32(long x) { return x >> 5; }
00045 inline int mod32(long x) { return x & 31; }
00046
00047 int
00048 w_bitmap_t::bytesForBits(uint4_t numBits)
00049 {
00050 return (div8(numBits -1) + 1);
00051 }
00052
00053
00054 NORET
00055 w_bitmap_t::w_bitmap_t(uint4_t size)
00056 : sz(size), mem_alloc(true)
00057 {
00058 int n = bytesForBits(size);
00059 ptr = new uint1_t[n] ;
00060 if (!ptr) W_FATAL(fcOUTOFMEMORY) ;
00061 }
00062
00063 void
00064 w_bitmap_t::zero()
00065 {
00066 int n = bytesForBits(sz);
00067 memset(ptr, 0, n);
00068 }
00069
00070 void
00071 w_bitmap_t::fill()
00072 {
00073 int n = bytesForBits(sz);
00074 memset(ptr, 0xff, n);
00075 }
00076
00077 bool
00078 w_bitmap_t::is_set(uint4_t offset) const
00079 {
00080 return (ptr[div8(offset)] & (1 << mod8(offset))) != 0;
00081 }
00082
00083 void
00084 w_bitmap_t::set(uint4_t offset)
00085 {
00086 ptr[div8(offset)] |= (1 << mod8(offset));
00087 }
00088
00089 void
00090 w_bitmap_t::clr(uint4_t offset)
00091 {
00092 ptr[div8(offset)] &= ~(1 << mod8(offset));
00093 }
00094
00095 w_base_t::int4_t
00096 w_bitmap_t::first_set(uint4_t start) const
00097 {
00098 w_assert9(start < sz);
00099 register uint1_t* p = ptr + div8(start);
00100 register uint4_t mask = 1 << mod8(start);
00101 register uint4_t size = sz;
00102 for (size -= start; size; start++, size--) {
00103 if (*p & mask) {
00104 w_assert9(is_set(start));
00105 return start;
00106 }
00107 if ((mask <<= 1) == 0x100) {
00108 mask = 1;
00109 p++;
00110 }
00111 }
00112
00113 return -1;
00114 }
00115
00116 w_base_t::int4_t
00117 w_bitmap_t::first_clr(uint4_t start) const
00118 {
00119 w_assert9(start < sz);
00120 register uint1_t* p = ptr + div8(start);
00121 register uint4_t mask = 1 << mod8(start);
00122 register uint4_t size = sz;
00123 for (size -= start; size; start++, size--) {
00124 if ((*p & mask) == 0) {
00125 return start;
00126 }
00127 if ((mask <<= 1) == 0x100) {
00128 mask = 1;
00129 p++;
00130 }
00131 }
00132
00133 return -1;
00134 }
00135
00136 w_base_t::uint4_t
00137 w_bitmap_t::num_set() const
00138 {
00139 uint1_t* p = ptr;
00140 uint4_t size = sz;
00141 int count;
00142 int mask;
00143 for (count = 0, mask = 1; size; size--) {
00144 if (*p & mask) count++;
00145 if ((mask <<= 1) == 0x100) {
00146 mask = 1;
00147 p++;
00148 }
00149 }
00150 return count;
00151 }
00152
00153 ostream& operator<<(ostream& o, const w_bitmap_t& obj)
00154 {
00155 for (register unsigned i = 0; i < obj.sz; i++) {
00156 o << (obj.is_set(i) != 0);
00157 }
00158 return o;
00159 }
00160