-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitstrmap.h
More file actions
104 lines (82 loc) · 1.73 KB
/
bitstrmap.h
File metadata and controls
104 lines (82 loc) · 1.73 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// bitstrmap.h
// map from finite bitstrings to some object
// This is not a BDD because:
// - I want to map to arbitrary objects, not just 0 or 1.
// - I do not expect much useful sharing.
#ifndef BITSTRMAP_H
#define BITSTRMAP_H
template <class T>
class BitStrMap {
public: // types
class Node {
public: // data
// domain object, or 0 if not mapped
T data;
// child if next bit in string is 0, or NULL if there are
// no mapped objects there (owner)
Node *zero;
// similar for 1
Node *one;
public:
Node() : data((T)0), zero(NULL), one(NULL) {}
~Node();
// get a successor, creating it if necessary
Node *getZero();
Node *getOne();
Node *getSucc(bool which)
{ return which? getOne() : getZero(); }
};
private: // data
// first node in tree, corresponding to empty bitstring
// (nullable owner)
Node *top;
public: // funcs
BitStrMap() : top(NULL) {}
~BitStrMap();
// get or create
Node *getTop();
};
// ------------------- Node implementation ----------------------
template <class T>
BitStrMap<T>::Node::~Node()
{
if (zero) {
delete zero;
}
if (one) {
delete one;
}
}
template <class T>
typename BitStrMap<T>::Node *BitStrMap<T>::Node::getZero()
{
if (!zero) {
zero = new Node;
}
return zero;
}
template <class T>
typename BitStrMap<T>::Node *BitStrMap<T>::Node::getOne()
{
if (!one) {
one = new Node;
}
return one;
}
// ------------------ BitStrMap implementation ------------------
template <class T>
BitStrMap<T>::~BitStrMap()
{
if (top) {
delete top;
}
}
template <class T>
typename BitStrMap<T>::Node *BitStrMap<T>::getTop()
{
if (!top) {
top = new Node;
}
return top;
}
#endif // BITSTRMAP_H