diff options
author | Adam Borowski <kilobyte@angband.pl> | 2012-07-26 14:31:58 +0200 |
---|---|---|
committer | Adam Borowski <kilobyte@angband.pl> | 2012-07-26 14:31:58 +0200 |
commit | 42facf2424f12ef1be983b9b9f6ec2996d3981bf (patch) | |
tree | 98df9e77ddca47570e5acde154e4eb86fa329002 /crawl-ref/source/bitary.h | |
parent | 705162b1193ce341608cbbe7017119e366760a1a (diff) | |
download | crawl-ref-42facf2424f12ef1be983b9b9f6ec2996d3981bf.tar.gz crawl-ref-42facf2424f12ef1be983b9b9f6ec2996d3981bf.zip |
A two-dimensional FixedBitArray template.
Diffstat (limited to 'crawl-ref/source/bitary.h')
-rw-r--r-- | crawl-ref/source/bitary.h | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/crawl-ref/source/bitary.h b/crawl-ref/source/bitary.h index 98a33bef6f..bd125b35ee 100644 --- a/crawl-ref/source/bitary.h +++ b/crawl-ref/source/bitary.h @@ -92,4 +92,64 @@ public: } }; +template <unsigned int SIZEX, unsigned int SIZEY> class FixedBitArray +{ +protected: + unsigned long data[(SIZEX*SIZEY + LONGSIZE - 1) / LONGSIZE]; +public: + void reset() + { + for (unsigned int i = 0; i < ARRAYSZ(data); i++) + data[i] = 0; + } + + FixedBitArray() + { + reset(); + } + + inline bool get(int x, int y) const + { +#ifdef ASSERTS + // printed as signed, as in FixedArray + if (x < 0 || y < 0 || x >= (int)SIZEX || y >= (int)SIZEY) + 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; + } + + inline bool get(coord_def c) const + { + return get(c.x, c.y); + } + + inline void set(int x, int y, bool value = true) + { +#ifdef ASSERTS + if (x < 0 || y < 0 || x >= (int)SIZEX || y >= (int)SIZEY) + 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); + } + + inline FixedBitArray<SIZEX, SIZEY>& operator|=(const FixedBitArray<SIZEX, SIZEY>&x) + { + for (unsigned int i = 0; i < ARRAYSZ(data); i++) + data[i] |= x.data[i]; + 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]; + return *this; + } +}; + #endif |