diff options
author | Robert Vollmert <rvollmert@gmx.net> | 2009-10-10 21:32:50 +0200 |
---|---|---|
committer | Robert Vollmert <rvollmert@gmx.net> | 2009-10-10 23:49:25 +0200 |
commit | 13aacae3ac6afc8437f84527d0b5f564d7558cd1 (patch) | |
tree | 9accb25b77e4f2e40dd828ce5d9823b6a2ad37fe /crawl-ref/source | |
parent | 370d19ae9228843151b2d4dc56e549274040b991 (diff) | |
download | crawl-ref-13aacae3ac6afc8437f84527d0b5f564d7558cd1.tar.gz crawl-ref-13aacae3ac6afc8437f84527d0b5f564d7558cd1.zip |
Implement bit array class.
Diffstat (limited to 'crawl-ref/source')
-rw-r--r-- | crawl-ref/source/bitary.cc | 76 | ||||
-rw-r--r-- | crawl-ref/source/bitary.h | 35 | ||||
-rw-r--r-- | crawl-ref/source/makefile.obj | 1 |
3 files changed, 112 insertions, 0 deletions
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 \ |