File: BitSet.cpp

package info (click to toggle)
grcompiler 4.2-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,076 kB
  • ctags: 5,163
  • sloc: cpp: 45,565; sh: 4,451; ansic: 4,377; makefile: 185; xml: 175; perl: 127
file content (68 lines) | stat: -rw-r--r-- 1,726 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "Antlr/BitSet.hpp"

/**A BitSet to replace java.util.BitSet.
 * Primary differences are that most set operators return new sets
 * as opposed to oring and anding "in place".  Further, a number of
 * operations were added.  I cannot contain a BitSet because there
 * is no way to access the internal bits (which I need for speed)
 * and, because it is final, I cannot subclass to add functionality.
 * Consider defining set degree.  Without access to the bits, I must
 * call a method n times to test the ith bit...ack!
 *
 * Also seems like or() from util is wrong when size of incoming set is bigger
 * than this.length.
 *
 *
 * This is a C++ version of the Java class described above, with only
 * a handful of the methods implemented, because we don't need the
 * others at runtime. It's really just a wrapper around vector<bool>,
 * which should probably be changed to a wrapper around bitset, once
 * bitset is more widely available.
 *
 * @author Terence Parr, MageLang Institute
 * @author <br><a href="mailto:pete@yamuna.demon.co.uk">Pete Wells</a>
 */
BitSet::BitSet(int nbits)
	: storage(nbits)
{
	for (int i=0;i<nbits;i++) {
		storage[i] = false;
	}
}

BitSet::BitSet(const unsigned long* bits_,int nlongs)
	: storage(nlongs*32)
{
	for (int i=0;i<nlongs*32;i++) {
		storage[i] = (bits_[i>>5] & (1UL << (i&31))) ? true : false;
	}
}

BitSet::~BitSet()
{
}

void BitSet::add(int el)
{
	storage[el] = true;
}

bool BitSet::member(int el) const
{
	if ( el < 0 || el >= (int)storage.size())
		return false;

	return storage[el];
}

std::vector<int> BitSet::toArray() const
{
	std::vector<int> elems;
	for (unsigned int i=0;i<storage.size();i++) {
		if (storage[i])
			elems.push_back(i);
	}

	return elems;
}