An array of many bool variables takes much space. Bool is such a variable, that can be saved using one bit only. Under x86 architecture smallest memory chunk, that can be allocated, is one byte. We are losing 7 bits in every bool variable. in 1000 - element array that’s 7000 bits - 875 bytes. We are wasting 87.5% of memory.
My BoolArray class solves the problem. It can be pretty much improved (especially by overloading operators), but now it works properly.
Three of its features are quite remarkable: the extended functions, customizable size and file functions.
1. Extended Functions - Turned on by:
#define EXTENDED_BOOL_FUNCTIONS
Without the preprocessor instruction the class is minimized to simplest methods only, such as getValue(), setValue(). When the instruction is enabled, the class increases its complexity, covering many possible uses.
2. Customizable size - set by:
#define SIZE_SMALL
(or SIZE_MEDIUM, or SIZE_BIG)
SIZE_SMALL - size is stored in a char variable
SIZE_MEDIUM - size is stored in a int variable
SIZE_BIG - size is stored in a long variable
By default the size is set to SIZE_SMALL.
3. File functions - enable saving and reading from a file. Enabled by including <fstream> BEFORE including “boolean.h”. Tested in MinGW, should work in gcc & DJGPP, in other compilers the programmer may need to check the constant name defined in fstream - here it’s _GLIBCXX_FSTREAM.
Especially the toFileAndFree function is worth mentioning. It saves the array contents on hard disk, and frees the memory. It could be very useful in resources managing.
BoolArray - here’s the download link.
Exemplary Use - a source file with exemplary use of the class.
Please send me the information about found bugs & possible improvements.