From 13aacae3ac6afc8437f84527d0b5f564d7558cd1 Mon Sep 17 00:00:00 2001 From: Robert Vollmert Date: Sat, 10 Oct 2009 21:32:50 +0200 Subject: Implement bit array class. --- crawl-ref/source/bitary.cc | 76 +++++++++++++++++++++++++++++++++++++++++++ crawl-ref/source/bitary.h | 35 ++++++++++++++++++++ crawl-ref/source/makefile.obj | 1 + 3 files changed, 112 insertions(+) create mode 100644 crawl-ref/source/bitary.cc create mode 100644 crawl-ref/source/bitary.h (limited to 'crawl-ref/source') diff --git a/crawl-ref/source/bitary.cc b/crawl-ref/source/bitary.cc new file mode 100644 index 0000000000..d822dcd327 --- /dev/null +++ b/crawl-ref/source/bitary.cc @@ -0,0 +1,76 @@ +/* + * File: bitary.cc + * Summary: Bit array data type. + * Created by: Robert Vollmert + */ + +#include "AppHdr.h" + +#include "bitary.h" + +#include "debug.h" + +#define LONGSIZE (sizeof(unsigned long)*8) + +bit_array::bit_array(unsigned long s) + : size(s) +{ + nwords = (int) ((size + LONGSIZE - 1) / LONGSIZE); + data = new unsigned long[nwords]; + reset(); +} + +bit_array::~bit_array() +{ + delete[] data; +} + +void bit_array::reset() +{ + for (int w = 0; w < nwords; ++w) + data[w] = 0; +} + +bool bit_array::get(unsigned long index) const +{ + ASSERT(index < size); + int w = index / LONGSIZE; + int b = index % LONGSIZE; + return (data[w] & (1UL << b)); +} + +void bit_array::set(unsigned long index, bool value) +{ + ASSERT(index < size); + int w = index / LONGSIZE; + int b = index % LONGSIZE; + if (value) + data[w] |= (1UL << b); + else + data[w] &= ~(1UL << b); +} + +bit_array& bit_array::operator |= (const bit_array& other) +{ + ASSERT(size == other.size); + for (int w = 0; w < nwords; ++w) + data[w] |= other.data[w]; + return (*this); +} + +bit_array& bit_array::operator &= (const bit_array& other) +{ + ASSERT(size == other.size); + for (int w = 0; w < nwords; ++w) + data[w] &= other.data[w]; + return (*this); +} + +bit_array bit_array::operator & (const bit_array& other) const +{ + ASSERT(size == other.size); + bit_array res = bit_array(size); + for (int w = 0; w < nwords; ++w) + res.data[w] = data[w] & other.data[w]; + return (res); +} diff --git a/crawl-ref/source/bitary.h b/crawl-ref/source/bitary.h new file mode 100644 index 0000000000..110a9e846c --- /dev/null +++ b/crawl-ref/source/bitary.h @@ -0,0 +1,35 @@ +/* + * File: bitary.h + * Summary: Bit array data type. + * Created by: Robert Vollmert + * + * Just contains the operations required by los.cc + * for the moment. + */ + +#ifndef BITARY_H +#define BITARY_H + +struct bit_array +{ +public: + bit_array(unsigned long size = 0); + ~bit_array(); + + void reset(); + + bool get(unsigned long index) const; + void set(unsigned long index, bool value = true); + + bit_array& operator |= (const bit_array& other); + bit_array& operator &= (const bit_array& other); + bit_array operator & (const bit_array& other) const; + +protected: + unsigned long size; + int nwords; + unsigned long *data; +}; + +#endif + diff --git a/crawl-ref/source/makefile.obj b/crawl-ref/source/makefile.obj index aaa4f4d168..d2e6447573 100644 --- a/crawl-ref/source/makefile.obj +++ b/crawl-ref/source/makefile.obj @@ -5,6 +5,7 @@ artefact.o \ acr.o \ arena.o \ beam.o \ +bitary.o \ branch.o \ chardump.o \ cio.o \ -- cgit v1.2.3-54-g00ecf