Apply the new ground_level method.
[crawl.git] / crawl-ref / source / bitary.cc
blob52de022dc2b8b5684dc2f3ec151db231ca67d06d
1 /*
2 * File: bitary.cc
3 * Summary: Bit array data type.
4 * Created by: Robert Vollmert
5 */
7 #include "AppHdr.h"
9 #include "bitary.h"
11 #include "debug.h"
13 #define LONGSIZE (sizeof(unsigned long)*8)
15 bit_array::bit_array(unsigned long s)
16 : size(s)
18 nwords = static_cast<int>((size + LONGSIZE - 1) / LONGSIZE);
19 data = new unsigned long[nwords];
20 reset();
23 bit_array::~bit_array()
25 delete[] data;
28 void bit_array::reset()
30 for (int w = 0; w < nwords; ++w)
31 data[w] = 0;
34 bool bit_array::get(unsigned long index) const
36 ASSERT(index < size);
37 int w = index / LONGSIZE;
38 int b = index % LONGSIZE;
39 return (data[w] & (1UL << b));
42 void bit_array::set(unsigned long index, bool value)
44 ASSERT(index < size);
45 int w = index / LONGSIZE;
46 int b = index % LONGSIZE;
47 if (value)
48 data[w] |= (1UL << b);
49 else
50 data[w] &= ~(1UL << b);
53 bit_array& bit_array::operator |= (const bit_array& other)
55 ASSERT(size == other.size);
56 for (int w = 0; w < nwords; ++w)
57 data[w] |= other.data[w];
58 return (*this);
61 bit_array& bit_array::operator &= (const bit_array& other)
63 ASSERT(size == other.size);
64 for (int w = 0; w < nwords; ++w)
65 data[w] &= other.data[w];
66 return (*this);
69 bit_array bit_array::operator & (const bit_array& other) const
71 ASSERT(size == other.size);
72 bit_array res = bit_array(size);
73 for (int w = 0; w < nwords; ++w)
74 res.data[w] = data[w] & other.data[w];
75 return (res);