summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/bitary.cc
diff options
context:
space:
mode:
authorRobert Vollmert <rvollmert@gmx.net>2009-10-10 21:32:50 +0200
committerRobert Vollmert <rvollmert@gmx.net>2009-10-10 23:49:25 +0200
commit13aacae3ac6afc8437f84527d0b5f564d7558cd1 (patch)
tree9accb25b77e4f2e40dd828ce5d9823b6a2ad37fe /crawl-ref/source/bitary.cc
parent370d19ae9228843151b2d4dc56e549274040b991 (diff)
downloadcrawl-ref-13aacae3ac6afc8437f84527d0b5f564d7558cd1.tar.gz
crawl-ref-13aacae3ac6afc8437f84527d0b5f564d7558cd1.zip
Implement bit array class.
Diffstat (limited to 'crawl-ref/source/bitary.cc')
-rw-r--r--crawl-ref/source/bitary.cc76
1 files changed, 76 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);
+}