diff options
author | Brendan Hickey <brendan@bhickey.net> | 2013-07-28 16:44:21 -0700 |
---|---|---|
committer | Brendan Hickey <brendan@bhickey.net> | 2013-07-28 16:44:21 -0700 |
commit | 5c2f3ca684a3e289b6db8dbfa54d662a7a4309ca (patch) | |
tree | 080cf13e708206cf0ee836f486333129c92601b1 /crawl-ref/source/bitary.h | |
parent | 23a74840f4654b120be714fef93c848aea82e63b (diff) | |
download | crawl-ref-5c2f3ca684a3e289b6db8dbfa54d662a7a4309ca.tar.gz crawl-ref-5c2f3ca684a3e289b6db8dbfa54d662a7a4309ca.zip |
Dispatch to std::bitset for FixedBitVector.
This patch reimplements 1D and 2D FixedBitVectors in terms of std::bitset.
The 1D FixedBitVector can probably be deleted in its entirety.
bit_vector could be replaced with boost::dynamic_bitset, if we were willing
to introduce a dependency on Boost.
Stress tests demonstrate a tiny improvement in timings.
Diffstat (limited to 'crawl-ref/source/bitary.h')
-rw-r--r-- | crawl-ref/source/bitary.h | 54 |
1 files changed, 24 insertions, 30 deletions
diff --git a/crawl-ref/source/bitary.h b/crawl-ref/source/bitary.h index 97a02ae418..5190365bbc 100644 --- a/crawl-ref/source/bitary.h +++ b/crawl-ref/source/bitary.h @@ -11,6 +11,8 @@ #include "defines.h" +#include <bitset> + class bit_vector { public: @@ -41,17 +43,15 @@ protected: template <unsigned int SIZE> class FixedBitVector { protected: - unsigned long data[(SIZE + LONGSIZE - 1) / LONGSIZE]; + bitset<SIZE> data; public: void reset() { - for (unsigned int i = 0; i < ARRAYSZ(data); i++) - data[i] = 0; + data.reset(); } FixedBitVector() { - reset(); } inline bool get(unsigned int i) const @@ -61,7 +61,7 @@ public: if (i >= SIZE) die("bit vector range error: %d / %u", (int)i, SIZE); #endif - return data[i / LONGSIZE] & 1UL << i % LONGSIZE; + return data[i]; } inline bool operator[](unsigned int i) const @@ -75,49 +75,48 @@ public: if (i >= SIZE) die("bit vector range error: %d / %u", (int)i, SIZE); #endif - if (value) - data[i / LONGSIZE] |= 1UL << i % LONGSIZE; - else - data[i / LONGSIZE] &= ~(1UL << i % LONGSIZE); + data[i] = value; } inline FixedBitVector<SIZE>& operator|=(const FixedBitVector<SIZE>&x) { - for (unsigned int i = 0; i < ARRAYSZ(data); i++) - data[i] |= x.data[i]; + data |= x.data; return *this; } inline FixedBitVector<SIZE>& operator&=(const FixedBitVector<SIZE>&x) { - for (unsigned int i = 0; i < ARRAYSZ(data); i++) - data[i] &= x.data[i]; + data &= x.data; return *this; } void init(bool value) { - long fill = value ? ~(long)0 : 0; - for (unsigned int i = 0; i < ARRAYSZ(data); i++) - data[i] = fill; + data.reset(); + if (value) + { + data.flip(); + } } }; template <unsigned int SIZEX, unsigned int SIZEY> class FixedBitArray { protected: - unsigned long data[(SIZEX*SIZEY + LONGSIZE - 1) / LONGSIZE]; + std::bitset<SIZEX*SIZEY> data; public: void reset() { - for (unsigned int i = 0; i < ARRAYSZ(data); i++) - data[i] = 0; + data.reset(); } void init(bool def) { - for (unsigned int i = 0; i < ARRAYSZ(data); i++) - data[i] = def ? ULONG_MAX : 0; + data.reset(); + if (def) + { + data.flip(); + } } FixedBitArray() @@ -138,7 +137,7 @@ public: die("bit array range error: %d,%d / %u,%u", x, y, SIZEX, SIZEY); #endif unsigned int i = y * SIZEX + x; - return data[i / LONGSIZE] & 1UL << i % LONGSIZE; + return data[i]; } template<class Indexer> inline bool get(const Indexer &i) const @@ -163,10 +162,7 @@ public: die("bit array range error: %d,%d / %u,%u", x, y, SIZEX, SIZEY); #endif unsigned int i = y * SIZEX + x; - if (value) - data[i / LONGSIZE] |= 1UL << i % LONGSIZE; - else - data[i / LONGSIZE] &= ~(1UL << i % LONGSIZE); + data[i] = value; } template<class Indexer> inline void set(const Indexer &i, bool value = true) @@ -176,15 +172,13 @@ public: inline FixedBitArray<SIZEX, SIZEY>& operator|=(const FixedBitArray<SIZEX, SIZEY>&x) { - for (unsigned int i = 0; i < ARRAYSZ(data); i++) - data[i] |= x.data[i]; + data |= x.data; return *this; } inline FixedBitArray<SIZEX, SIZEY>& operator&=(const FixedBitArray<SIZEX, SIZEY>&x) { - for (unsigned int i = 0; i < ARRAYSZ(data); i++) - data[i] &= x.data[i]; + data &= x.data; return *this; } }; |