Xavax C++ Library Class Index   FAQ   Overview

Class xavax::Bitmap

Bitmap manages a bitmap of arbitrary size. Space for the bitmap is allocated and deleted automatically, and bounds checking is performed by operator[] and the get and set methods.

Constructor Summary
Bitmap(size_t n)
         Create a new bitmap of the specified size.

Operator Summary
Bit operator[](size_t p)
         Returns a reference to the bit at position p.

Method Summary
bool findFirst(bool state, size_t& position)
         Find the first bit in the specified state.
bool get(size_t position)
         Returns the value of the specified bit.
void reset()
         Reset all bits in the bitmap to false.
void set(size_t position, bool value)
         Set the value of the specified bit.

Related Classes
Bit

Constructor Detail

Bitmap

Bitmap(size_t n)
Construct a bitmap of size n.
Parameters:
n - the number of bits in the bitmap.

Operator Detail

operator[]

Bit operator[](size_t p)
Returns a Bit object which is a reference to the bit at position p.   Bit::operator bool and Bit::operator= can be used to get and set the bit.
Parameters:
p - the position of the bit to be referenced.
Returns:
a reference to the bit at position p.
Throws:
RangeException if p is greater than or equal to the bitmap size.

Method Detail

findFirst

bool findFirst(bool state, size_t& position)
First the first bit in the specified state. If a bit in the specified state is found, store the bit position and return true.
Parameters:
state - the state of the bit to find.
position - the position of the bit found.
Returns:
true if a bit was found.

get

bool get(size_t position)
Return the value of the bit at the specified position.
Parameters:
position - the position of the bit to be retrieved.
Returns:
the value of the specified bit.
Throws:
RangeException if p is greater than or equal to the bitmap size.

reset

void reset()
Reset all bits in the bitmap to false.

set

void set(size_t position, bool value)
Set the value of the bit at position p to value.
Parameters:
position - the position of the bit to be set.
value - the new boolean value of the bit.
Throws:
RangeException if position is greater than or equal to the bitmap size.

Example Code

#include <iostream.h>
#include "Bitmap.h"

int main(int argc, char** argv)
{
  //
  // Create a bitmap and set each bit with a position
  // that is a multiple of 3 (except 0).
  //
  Bitmap b(40);
  b[21] = b[24] = b[27] = b[30] = b[33] = b[36] = b[39] = true;
  b[3]  = b[6]  = b[9]  = b[12] = b[15] = b[18] = b[21];

  //
  // While any bit is set, find the first one bit
  // and print the bit position, then clear the bit.
  //
  size_t position;
  while ( b.findFirst(true, position) ) {
    cout << "first one = " << position << endl;
    b.set(position, false);
  }

  //
  // Reset the bitmap to all ones, then clear each bit
  // with a position that is a multiple of 3 (except 0).
  //
  cout << "Resetting bitmap to all ones..." << endl;
  b.reset(true);
  cout << "Clearing some bits..." << endl;
  b[21] = b[24] = b[27] = b[30] = b[33] = b[36] = b[39] = false;
  b[3]  = b[6]  = b[9]  = b[12] = b[15] = b[18] = b[21];

  //
  // While any bit is clear, find the first zero bit
  // and print the bit position, then set the bit.
  //
  while ( b.findFirst(false, position) ) {
    cout << "first zero = " << position << endl;
    b.set(position, true);
  }
}

Copyright © 2003 Xavax Inc. -- All Rights Reserved