view anagram/support/bitmap.cpp @ 17:12171da8943f

Don't refer to CVS.
author David A. Holland
date Tue, 31 May 2022 01:56:37 -0400
parents 13d2b8934445
children
line wrap: on
line source

/*
 * AnaGram, A System for Syntax Directed Programming
 * Copyright 1993-2000 Parsifal Software. All Rights Reserved.
 * See the file COPYING for license and usage terms.
 *
 * bitmap.cpp
 */

#include "assert.h"
#include "bitmap.h"

//#define INCLUDE_LOGGING
#include "log.h"


void CharBitmap::blurCase(int x) {
  assert((unsigned) x < nBits);
  unsigned mask = 1 << (x%8);
  int index = x/8;
  int truthValue = (map[index] | map[index ^ 4]) & mask;
  map[index] |= (unsigned char) truthValue;
  map[index ^ 4] |= (unsigned char) truthValue;
}

void CharBitmap::blurCase() {
  LOGSECTION("CharBitmap::blurCase()");
  int i;
  for (i = 'A'; i <= 'Z'; i++) {
    blurCase(i);
  }
  for (i = 0xc0; i <= 0xde; i++) {
    if (i != 0xd7) {
      blurCase(i);
    }
  }
  LOGS("blurCase complete");
}


int Bitmap::setBit(int x) {
  LOGSECTION("Bitmap::setBit");
  LOGV(x) LCV(nBits);
  assert((unsigned) x < nBits);
  unsigned mask = 1 << (x %8);
  int truthValue = (map[x/8] & mask) != 0;
  map[x/8] |= (unsigned char) mask;
  return truthValue;
}

int CharBitmap::setBit(int x) {
  return Bitmap::setBit(x - min_char_number);
}

int Bitmap::getBit(int x) {
  assert((unsigned) x < nBits);
  return (map[x/8] & (1 << (x % 8))) != 0;
}

int CharBitmap::getBit(int x) {
  return Bitmap::getBit(x - min_char_number);
}

int Bitmap::list(int *x) {
  LOGSECTION("Bitmap::list");
  unsigned char *p = (unsigned char *) map;
  unsigned i;
  int count = 0;
  for (i = 0; i < nBits; i++) {
    if ((p[i/8] & (1 << (i % 8))) == 0) {
      continue;
    }
    x[count++] = i;
  }
  return count;
}

int CharBitmap::list(int *x) {
  int count = Bitmap::list(x);
  for (int i = 0; i < count; i++) {
    x[i] += min_char_number;
  }
  return count;
}

Bitmap &Bitmap::setRange(unsigned first, unsigned last) {
  LOGSECTION("Bitmap::setRange");
  LOGV(first) LCV(last);
  assert(first <= last && last < nBits);
  int n = last/8;
  unsigned char bit = (unsigned char) (1 << (last %8));
  LOGV(n) LCV(bit);
  while (bit && first <= last) {
    map[n] |= bit;
    bit >>= 1;
    last--;
  }
  LOGV(bit) LCV(last);
  int x = first/8;
  bit = (unsigned char) (1 << (first % 8));
  LOGV(x) LCV(bit);
  while (bit && first <= last) {
    map[x] |= bit;
    bit <<= 1;
    first++;
  }
  LOGV(x) LCV(bit);
  x++;
  while (x < n) {
    map[x++] = 0xff;
  }
  return *this;
}

#ifdef _MSC_VER
Bitmap &CharBitmap::setRange(unsigned first, unsigned last) {
#else
CharBitmap &CharBitmap::setRange(unsigned first, unsigned last) {
#endif
  LOGSECTION("CharBitmap::setRange");
  LOGV(first) LCV(last);
  Bitmap::setRange(first - min_char_number, last - min_char_number);
  return *this;
}

Bitmap &Bitmap::operator |= (const Bitmap &b) {
  assert(nChars == b.nChars);
  int x = nChars;
  while (x--) {
    map[x] |= b.map[x];
  }
  return *this;
}

Bitmap &Bitmap::operator &= (const Bitmap &b) {
  assert(nChars == b.nChars);
  int x = nChars;
  while (x--) {
    map[x] &= b.map[x];
  }
  return *this;
}

Bitmap &Bitmap::operator -= (const Bitmap &b) {
  LOGSECTION("CharBitmap::operator -=");
  LOGV(nChars) LCV(b.nChars);
  assert(nChars == b.nChars);
  int x = nChars;
  while (x--) {
    map[x] &= (unsigned char) ~b.map[x];
  }
  return *this;
}

Bitmap &Bitmap::operator ~() {
  int x = nChars;
  while (x--) {
    map[x] = (unsigned char) ~map[x];
  }
  return *this;
}