26 #ifdef DEBUG_BIT_VECTOR
27 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
32 using namespace basis;
36 bit_vector::bit_vector()
43 reset(number_of_bits);
50 : _implementation(new
byte_array(*to_copy._implementation)),
51 _number_of_bits(to_copy._number_of_bits)
58 if (
this == &to_copy)
return *
this;
59 *_implementation = *to_copy._implementation;
60 _number_of_bits = to_copy._number_of_bits;
64 bit_vector::operator
const byte_array & ()
const {
return *_implementation; }
73 {
return compare(that, 0, _number_of_bits); }
76 {
return get_bit(into_two_dim(position)); }
83 if (
negative(number_of_bits))
return;
84 if (
bits() == number_of_bits)
return;
85 int old_size =
bits();
86 _number_of_bits = number_of_bits;
88 _implementation->
resize(number_of_bytes);
90 if (old_size < number_of_bits) {
92 for (
int i = old_size; i < old_size + 8; i++) {
98 for (
int j = old_bytes; j < number_of_bytes; j++) {
100 _implementation->
use(j) = 0;
108 memset(_implementation->
access(), 0, _implementation->
length());
111 bit_vector::two_dim_location bit_vector::into_two_dim(
int position)
const
113 two_dim_location to_return;
119 bool bit_vector::get_bit(
const two_dim_location &pos_in2)
const
122 _number_of_bits - 1,
false);
123 abyte test_mask =
abyte(1 << pos_in2.c_offset);
124 return TEST(
abyte(_implementation->
get(pos_in2.c_byte)), test_mask);
130 set_bit(into_two_dim(position), value);
135 abyte test_mask =
abyte(1 << pos_in2.c_offset);
136 if (set_it)
SET(_implementation->
use(pos_in2.c_byte), test_mask);
137 else CLEAR((
abyte &)_implementation->
get(pos_in2.c_byte), test_mask);
143 return get_bit(into_two_dim(position));
149 set_bit(into_two_dim(position),
true);
155 set_bit(into_two_dim(position),
false);
162 for (
int full_byte = 0; full_byte < _implementation->
length(); full_byte++) {
163 if ( (to_find && _implementation->
get(full_byte))
164 || (!to_find && (_implementation->
get(full_byte) != whole_set)) ) {
168 if (
on(i) == to_find)
return i;
170 return common::NOT_FOUND;
173 return common::NOT_FOUND;
178 for (
int i = start; i <= stop; i++) {
179 if (
on(i) != that.
on(i))
return false;
187 int bits_on_line = 0;
188 const int estimated_elements_on_line = 64;
189 for (
int i = 0; i < _number_of_bits; i++) {
191 if (bits_on_line == 0) {
192 if (i != 0) to_return +=
"\n";
193 if (i < 10000) to_return +=
" ";
194 if (i < 1000) to_return +=
" ";
195 if (i < 100) to_return +=
" ";
196 if (i < 10) to_return +=
" ";
200 if (
on(i)) to_return +=
"1";
201 else to_return +=
"0";
203 if (bits_on_line >= estimated_elements_on_line) bits_on_line = 0;
204 else if ( !(bits_on_line %
BITS_PER_BYTE) ) to_return +=
" ";
214 int size = end - start + 1;
216 for (
int i = start; i <= end; i++) to_return.
set_bit(i - start,
on(i));
223 int end = start + to_write.
bits() - 1;
225 for (
int i = start; i <= end; i++)
set_bit(i, to_write[i - start]);
239 int end = start + size - 1;
246 from_int._implementation->
resize(size, byte_array::NEW_AT_BEGINNING);
247 else from_int.
resize(size);
254 int end = start + size - 1;
257 int new_length = segment.
bits();
258 if (new_length % 8) {
259 new_length = ( (new_length+8) / 8) * 8;
262 segment.
resize(new_length);
266 for (
int i = 0; i < segment.
bits(); i += 8)
268 if (j < segment.
bits())
269 new_segment.
set_bit(i + (7 - (j - i)), segment[j]);
270 segment = new_segment;
273 LOG(
"new seg after bit copy:");
280 for (
int i =
minimum(4, bytes_used) - 1; i >= 0; i--) {
281 #ifdef DEBUG_BIT_VECTOR
286 #ifdef DEBUG_BIT_VECTOR
291 else to_return >>= 8;
292 #ifdef DEBUG_BIT_VECTOR
299 #ifdef DEBUG_BIT_VECTOR
303 SET(to_return, mask);
304 #ifdef DEBUG_BIT_VECTOR
310 #ifdef DEBUG_BIT_VECTOR
a_sprintf is a specialization of astring that provides printf style support.
void reset(int number=0, const contents *initial_contents=NULL_POINTER)
Resizes this array and sets the contents from an array of contents.
const contents & get(int index) const
Accesses individual objects stored in "this" at the "index" position.
contents * access()
A non-constant access of the underlying C-array. BE REALLY CAREFUL.
outcome resize(int new_size, how_to_copy way=NEW_AT_END)
Changes the size of the C array to "new_size".
int length() const
Returns the current reported length of the allocated C array.
contents & use(int index)
A non-constant version of get(); the returned object can be modified.
Provides a dynamically resizable ASCII character string.
const char * s() const
synonym for observe. the 's' stands for "string", if that helps.
A very common template for a dynamic array of bytes.
An array of bits with operations for manipulating and querying individual bits.
void clear(int position)
clears the value of the bit at "position".
bool set(int start, int size, basis::un_int source)
puts the "source" value into the vector at "start".
bool whole() const
returns true if entire vector is set.
bool on(int position) const
returns true if the bit at "position" is set.
int find_first(bool to_find) const
Seeks the first occurrence of "to_find".
basis::astring text_form() const
prints a nicely formatted list of bits to a string.
bool empty() const
returns true if entire vector is unset.
int bits() const
returns the number of bits in the vector.
bool operator==(const bit_vector &that) const
returns true if "this" is equal to "that".
void light(int position)
sets the value of the bit at "position".
basis::un_int get(int start, int size) const
gets a portion of the vector as an unsigned integer.
void reset(int size)
resizes the bit_vector and clears all bits in it.
bool compare(const bit_vector &that, int start, int stop) const
true if "this" is the same as "that" between "start" and "stop".
void set_bit(int position, bool value)
sets the bit at "position" to a particular "value".
bit_vector()
creates a zero length bit_vector.
bool off(int position) const
returns true if the bit at "position" is clear.
bit_vector & operator=(const bit_vector &to_copy)
bool operator[](int position) const
returns the value of the bit at the position specified.
bool overwrite(int start, const bit_vector &to_write)
Stores bits from "to_write" into "this" at "start".
bit_vector subvector(int start, int end) const
Extracts a chunk of the vector between "start" and "end".
void resize(int size)
Changes the size of the bit_vector to "size" bits.
Constants and objects used throughout HOOPLE.
#define BITS_PER_BYTE
A fundamental constant measuring the number of bits in a byte.
#define NULL_POINTER
The value representing a pointer to nothing.
#define bounds_return(value, low, high, to_return)
Verifies that "value" is between "low" and "high", inclusive.
The guards collection helps in testing preconditions and reporting errors.
type number_of_packets(type message_size, type packet_size)
Reports number of packets needed given a total size and the packet size.
void WHACK(contents *&ptr)
deletion with clearing of the pointer.
unsigned char abyte
A fairly important unit which is seldom defined...
unsigned int un_int
Abbreviated name for unsigned integers.
type minimum(type a, type b)
maximum returns the greater of two values.
bool negative(const type &a)
negative returns true if "a" is less than zero.
A dynamic container class that holds any kind of object via pointers.
bool TEST(type to_test, type bits)
returns non-zero if the "bits" bit pattern is turned on in "to_test".
void SET(type &to_modify, type bits)
returns a number based on "to_modify" but with "bits" turned on.
void CLEAR(type &to_modify, type bits)
returns a number based on "to_modify" but with "bits" turned off.