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
21namespace structures {
22
24
26{
27public:
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
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
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
125private:
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
146template <class type>
147void SET(type &to_modify, type bits) { to_modify |= bits; }
148
150template <class type>
151void CLEAR(type &to_modify, type bits) { to_modify &= (type)~bits; }
152
154template <class type>
155bool 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".
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.
Emulates a mathematical set, providing several standard set operations.
Definition set.h:36
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
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
#define TEST(action)