File: cow_trie.hh

package info (click to toggle)
monotone 0.48-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 20,096 kB
  • ctags: 8,077
  • sloc: cpp: 81,000; sh: 6,402; perl: 1,241; lisp: 1,045; makefile: 655; python: 566; sql: 112; ansic: 52
file content (240 lines) | stat: -rw-r--r-- 6,013 bytes parent folder | download | duplicates (4)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2010 Timothy Brownawell <tbrownaw@prjek.net>
// You may use, copy, modify, distribute, etc, this file with no other
// restriction than that this notice not be removed.

#include <boost/shared_ptr.hpp>
#include <vector>

// This is *NOT* a normal STL-compatible container! It pretends well enough
// to work with parallel_iter and such, but it does not have find(),
// insert(), erase().
//
// Because this is copy-on-write, and the copying is per-node instead of for
// the whole object, the nodes can not have parent pointers (also, having
// parent pointers would I think make the size 2^n+4 instead of 2^n, which
// AIUI would waste almost equal space with common memory allocators).
// This lack of parent pointers means that iterators are expensive, so they're
// not used except for, well, iteration.

template<typename _Key, typename _Value, int _Bits>
class cow_trie
{
public:
  typedef _Key key_type;
  //typedef _Value value_type;
  typedef std::pair<_Key, _Value> value_type;
private:
  enum { mask = (1<<_Bits)-1 };
  enum { levels = (sizeof(_Key) * 8 + _Bits - 1) / _Bits };

  struct middle_node_type
  {
    boost::shared_ptr<void> contents[(1<<_Bits)];
  };
  struct leaf_node_type
  {
    _Value contents[1<<_Bits];
  };

  _Value _empty_value;
  unsigned _count;
  boost::shared_ptr<void> _data;

  bool walk(boost::shared_ptr<void> & d, _Key key, int level, _Value **ret)
  {
    if (!d)
      {
	if (level > 0)
	  d.reset(new middle_node_type());
	else
	  d.reset(new leaf_node_type());
      }
    if (!d.unique())
      {
	if (level > 0)
	  d.reset(new middle_node_type(*boost::static_pointer_cast<middle_node_type>(d)));
	else
	  d.reset(new leaf_node_type(*boost::static_pointer_cast<leaf_node_type>(d)));
      }
    unsigned idx = (key >> (_Bits * level)) & mask;
    if (level > 0)
      return walk(boost::static_pointer_cast<middle_node_type>(d)->contents[idx],
		  key, level-1, ret);
    else
      {
	*ret = &boost::static_pointer_cast<leaf_node_type>(d)->contents[idx];
	return true;
      }
  }

  bool walk(boost::shared_ptr<void> const & d, _Key key, int level, _Value **ret) const
  {
    if (!d)
      {
	return false;
      }
    unsigned idx = (key >> (_Bits * level)) & mask;
    if (level > 0)
      return walk(boost::static_pointer_cast<middle_node_type>(d)->contents[idx],
		  key, level-1, ret);
    else
      {
	*ret = &boost::static_pointer_cast<leaf_node_type>(d)->contents[idx];
	return true;
      }
  }
public:
  cow_trie() : _count(0) { }
  unsigned size() const { return _count; }
  bool empty() const { return _count == 0; }
  void clear()
  {
    _count = 0;
    _data.reset();
  }
  _Value const & set(_Key key, _Value const & value) {
    _Value *p;
    walk(_data, key, levels-1, &p);
    bool b = (*p != _empty_value);
    bool a = (value != _empty_value);
    if (b && !a)
      --_count;
    else if (a && !b)
      ++_count;
    *p = value;
    return *p;
  }
  bool set_if_missing(_Key key, _Value const & value)
  {
    _Value *p;
    walk(_data, key, levels-1, &p);
    if (*p != _empty_value)
      return false;
    if (value != _empty_value)
      {
	++_count;
	*p = value;
      }
    return true;
  }
  void unset(_Key key) {
    set(key, _empty_value);
  }
  _Value const &get_if_present(_Key key) const {
    _Value *p;
    if (walk(_data, key, levels-1, &p))
      return *p;
    else
      return _empty_value;
  }
  // This is actually not the same as above.
  // It's non-const, so it calls the other walk().
  _Value const &get_unshared_if_present(_Key key)
  {
    _Value *p;
    if (walk(_data, key, levels-1, &p))
      return *p;
    else
      return _empty_value;
  }

  class const_iterator
  {
    struct stack_item
    {
      boost::shared_ptr<void> ptr;
      unsigned idx;
      bool operator==(stack_item const & other) const
      {
	return ptr == other.ptr && idx == other.idx;
      }
    };
    std::vector<stack_item> stack;
    friend class cow_trie;
    explicit const_iterator(cow_trie const & t)
    {
      if (t._data)
	{
	  stack_item item;
	  item.ptr = t._data;
	  item.idx = (unsigned)-1;
	  stack.push_back(item);
	  ++(*this);
	}
    }
    _Value _empty_value;
  private:
    value_type _ret;
  public:
    bool operator==(const_iterator const & other)
    {
      return stack == other.stack;
    }
    bool operator!=(const_iterator const & other)
    {
      return stack != other.stack;
    }
    const_iterator() { }
    const_iterator const & operator++()
    {
      while (!stack.empty())
	{
	  stack_item & item = stack.back();
	  boost::shared_ptr<middle_node_type> middle
	    = boost::static_pointer_cast<middle_node_type>(item.ptr);
	  boost::shared_ptr<leaf_node_type> leaf
	    = boost::static_pointer_cast<leaf_node_type>(item.ptr);
	  for (++item.idx; item.idx < (1<<_Bits); ++item.idx)
	    {
	      if (stack.size() == levels)
		{
		  if (leaf->contents[item.idx] != _empty_value)
		    {
		      _ret.first = (_ret.first & ~mask) | item.idx;
		      _ret.second = leaf->contents[item.idx];
		      return *this;
		    }
		}
	      else
		{
		  if (middle->contents[item.idx])
		    {
		      int shifts = levels - stack.size();
		      int bits = shifts * _Bits;
		      _ret.first = (_ret.first & ~(mask<<bits)) | (item.idx<<bits);
		      stack_item i;
		      i.ptr = middle->contents[item.idx];
		      i.idx = (unsigned)-1;
		      stack.push_back(i);
		      break;
		    }
		}
	    }
	  if (item.idx == (1 << _Bits))
	    stack.pop_back();
	}
      return *this;
    }
    value_type const & operator*() const
    {
      return _ret;
    }
    value_type const * operator->() const
    {
      return &_ret;
    }
  };
  friend class const_iterator;

  const_iterator begin() const { return const_iterator(*this); }
  const_iterator end() const { return const_iterator(); }
};


// Local Variables:
// mode: C++
// fill-column: 76
// c-file-style: "gnu"
// indent-tabs-mode: nil
// End:
// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s: