Sometimes a program needs to organize an array of boolean flags of a large (and sometimes very large) dimension. A specially designed TBits object (in the KOLadd module) will help you to accomplish this task. Flags are stored in it as a single continuous array of bits (packed by 8 bits per byte). At the same time, a sufficiently high speed of work with flags is provided, and there are a number of quick operations that can increase the efficiency of work when performing some traditional tasks for which such arrays are usually used.
Constructor:
NewBits - creates an empty object to store a dynamic array of flags, returns a pointer of the PBits type to this object.
Methods and properties:
Bits[i]- access to individual flags of the array. Reading outside the array always returns false. Writing outside the array ensures that the array grows automatically (if written to true);
Count - the number of flags in the array (read-only);
Size - the size of the flags array in bytes (also read-only);
Capacity - the maximum number of flags for the storage of which memory is reserved. If the number of flags when adding new ones begins to exceed the reserved memory, then this value increases, and the memory for storing the flags is reallocated. In this case, if required, the accumulated flags are moved to a new location - in the same way as for lists. To prevent too frequent reallocation of memory, you should set the value of the Capacity property in the program, sufficient to work for a long time without the need to reallocate memory;
IndexOf(b) - returns the index of the first flag with the specified boolean value;
Openbit - similar to the previous one, but it returns the index of the first flag false in the array;
InstallBits(i, j, b) - for a continuous group of flags starting from index i and length j sets the value b;
Clear - clears the array of flags, freeing the occupied memory. If the array is assumed to be infinitely expanding to the right, then this operation is semantically equivalent to writing false to all the flags of the array;
Copy(i, j) - creates a new object TBits on the basis of this (returning a pointer to it of the PBits type), copying j flags into it, starting from index i;
Range(i, j) - a function equivalent to the previous one;
AssignBits(to_i, from_bits, from_i, j) - copies j flags from another TBits object from position from_i;
SaveToStream(strm)- saves an array of flags to the stream from the current position. First, the number of bits in the array is written, then the bits themselves;
LoadFromStream(strm)- loads an array of bits from a stream. the data must have been saved to the stream earlier by the SaveToStream method.