feisty meow concerns codebase  2.140
bit_vector.h
Go to the documentation of this file.
1 #ifndef BIT_VECTOR_CLASS
2 #define BIT_VECTOR_CLASS
3 
4 /*****************************************************************************\
5 * *
6 * Name : bit_vector *
7 * Author : Chris Koeritz *
8 * *
9 *******************************************************************************
10 * Copyright (c) 1990-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
17 
18 #include <basis/astring.h>
19 #include <basis/definitions.h>
20 
21 namespace structures {
22 
24 
26 {
27 public:
28  bit_vector();
30 
31  bit_vector(int size, const basis::abyte *initial = NULL_POINTER);
33 
37  bit_vector(const bit_vector &to_copy);
38 
39  ~bit_vector();
40 
41  bit_vector &operator =(const bit_vector &to_copy);
42 
43  int bits() const;
45 
46  bool operator [] (int position) const;
48 
49  bool on(int position) const;
51 
52  bool off(int position) const;
54 
55  void set_bit(int position, bool value);
57 
58  bool whole() const;
60 
61  bool empty() const;
63 
64  int find_first(bool to_find) const;
66 
69  void light(int position);
71 
72  void clear(int position);
74 
75  void resize(int size);
77 
79  void reset(int size);
81 
82  bool compare(const bit_vector &that, int start, int stop) const;
84 
85  bool operator == (const bit_vector &that) const;
87 
90  basis::astring text_form() const;
92 
93  bit_vector subvector(int start, int end) const;
95 
99  bool overwrite(int start, const bit_vector &to_write);
101 
106  basis::un_int get(int start, int size) const;
108 
113  bool set(int start, int size, basis::un_int source);
115 
121  operator const basis::byte_array &() const;
123 
125 private:
126  basis::byte_array *_implementation;
127  int _number_of_bits;
128 
129  struct two_dim_location { int c_byte; int c_offset; };
131 
132  two_dim_location into_two_dim(int position) const;
135  bool get_bit(const two_dim_location &pos_in2) const;
137  void set_bit(const two_dim_location &pos_in2, bool value);
139 };
140 
142 
143 // NOTE: these are operations on numbers, NOT on bit_vectors.
144 
146 template <class type>
147 void SET(type &to_modify, type bits) { to_modify |= bits; }
148 
150 template <class type>
151 void CLEAR(type &to_modify, type bits) { to_modify &= (type)~bits; }
152 
154 template <class type>
155 bool TEST(type to_test, type bits) { return bool(!(!(to_test & bits))); }
156 
158 
159 } //namespace.
160 
161 #endif
162 
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
A very common template for a dynamic array of bytes.
Definition: byte_array.h:36
An array of bits with operations for manipulating and querying individual bits.
Definition: bit_vector.h:26
void clear(int position)
clears the value of the bit at "position".
Definition: bit_vector.cpp:152
bool set(int start, int size, basis::un_int source)
puts the "source" value into the vector at "start".
Definition: bit_vector.cpp:236
bool whole() const
returns true if entire vector is set.
Definition: bit_vector.cpp:68
bool on(int position) const
returns true if the bit at "position" is set.
Definition: bit_vector.cpp:75
int find_first(bool to_find) const
Seeks the first occurrence of "to_find".
Definition: bit_vector.cpp:158
basis::astring text_form() const
prints a nicely formatted list of bits to a string.
Definition: bit_vector.cpp:184
bool empty() const
returns true if entire vector is unset.
Definition: bit_vector.cpp:70
int bits() const
returns the number of bits in the vector.
Definition: bit_vector.cpp:66
bool operator==(const bit_vector &that) const
returns true if "this" is equal to "that".
Definition: bit_vector.cpp:72
void light(int position)
sets the value of the bit at "position".
Definition: bit_vector.cpp:146
basis::un_int get(int start, int size) const
gets a portion of the vector as an unsigned integer.
Definition: bit_vector.cpp:252
void reset(int size)
resizes the bit_vector and clears all bits in it.
Definition: bit_vector.cpp:105
bool compare(const bit_vector &that, int start, int stop) const
true if "this" is the same as "that" between "start" and "stop".
Definition: bit_vector.cpp:176
void set_bit(int position, bool value)
sets the bit at "position" to a particular "value".
Definition: bit_vector.cpp:127
bit_vector()
creates a zero length bit_vector.
Definition: bit_vector.cpp:36
bool off(int position) const
returns true if the bit at "position" is clear.
Definition: bit_vector.cpp:78
bit_vector & operator=(const bit_vector &to_copy)
Definition: bit_vector.cpp:56
bool operator[](int position) const
returns the value of the bit at the position specified.
Definition: bit_vector.cpp:140
bool overwrite(int start, const bit_vector &to_write)
Stores bits from "to_write" into "this" at "start".
Definition: bit_vector.cpp:220
bit_vector subvector(int start, int end) const
Extracts a chunk of the vector between "start" and "end".
Definition: bit_vector.cpp:210
void resize(int size)
Changes the size of the bit_vector to "size" bits.
Definition: bit_vector.cpp:80
Constants and objects used throughout HOOPLE.
#define NULL_POINTER
The value representing a pointer to nothing.
Definition: definitions.h:32
unsigned char abyte
A fairly important unit which is seldom defined...
Definition: definitions.h:51
unsigned int un_int
Abbreviated name for unsigned integers.
Definition: definitions.h:62
A dynamic container class that holds any kind of object via pointers.
Definition: amorph.h:55
bool TEST(type to_test, type bits)
returns non-zero if the "bits" bit pattern is turned on in "to_test".
Definition: bit_vector.h:155
void SET(type &to_modify, type bits)
returns a number based on "to_modify" but with "bits" turned on.
Definition: bit_vector.h:147
void CLEAR(type &to_modify, type bits)
returns a number based on "to_modify" but with "bits" turned off.
Definition: bit_vector.h:151