• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2006 The RE2 Authors.  All Rights Reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4 
5 #ifndef RE2_SPARSE_ARRAY_H_
6 #define RE2_SPARSE_ARRAY_H_
7 
8 // DESCRIPTION
9 //
10 // SparseArray<T>(m) is a map from integers in [0, m) to T values.
11 // It requires (sizeof(T)+sizeof(int))*m memory, but it provides
12 // fast iteration through the elements in the array and fast clearing
13 // of the array.  The array has a concept of certain elements being
14 // uninitialized (having no value).
15 //
16 // Insertion and deletion are constant time operations.
17 //
18 // Allocating the array is a constant time operation
19 // when memory allocation is a constant time operation.
20 //
21 // Clearing the array is a constant time operation (unusual!).
22 //
23 // Iterating through the array is an O(n) operation, where n
24 // is the number of items in the array (not O(m)).
25 //
26 // The array iterator visits entries in the order they were first
27 // inserted into the array.  It is safe to add items to the array while
28 // using an iterator: the iterator will visit indices added to the array
29 // during the iteration, but will not re-visit indices whose values
30 // change after visiting.  Thus SparseArray can be a convenient
31 // implementation of a work queue.
32 //
33 // The SparseArray implementation is NOT thread-safe.  It is up to the
34 // caller to make sure only one thread is accessing the array.  (Typically
35 // these arrays are temporary values and used in situations where speed is
36 // important.)
37 //
38 // The SparseArray interface does not present all the usual STL bells and
39 // whistles.
40 //
41 // Implemented with reference to Briggs & Torczon, An Efficient
42 // Representation for Sparse Sets, ACM Letters on Programming Languages
43 // and Systems, Volume 2, Issue 1-4 (March-Dec.  1993), pp.  59-69.
44 //
45 // Briggs & Torczon popularized this technique, but it had been known
46 // long before their paper.  They point out that Aho, Hopcroft, and
47 // Ullman's 1974 Design and Analysis of Computer Algorithms and Bentley's
48 // 1986 Programming Pearls both hint at the technique in exercises to the
49 // reader (in Aho & Hopcroft, exercise 2.12; in Bentley, column 1
50 // exercise 8).
51 //
52 // Briggs & Torczon describe a sparse set implementation.  I have
53 // trivially generalized it to create a sparse array (actually the original
54 // target of the AHU and Bentley exercises).
55 
56 // IMPLEMENTATION
57 //
58 // SparseArray is an array dense_ and an array sparse_ of identical size.
59 // At any point, the number of elements in the sparse array is size_.
60 //
61 // The array dense_ contains the size_ elements in the sparse array (with
62 // their indices),
63 // in the order that the elements were first inserted.  This array is dense:
64 // the size_ pairs are dense_[0] through dense_[size_-1].
65 //
66 // The array sparse_ maps from indices in [0,m) to indices in [0,size_).
67 // For indices present in the array, dense_[sparse_[i]].index_ == i.
68 // For indices not present in the array, sparse_ can contain any value at all,
69 // perhaps outside the range [0, size_) but perhaps not.
70 //
71 // The lax requirement on sparse_ values makes clearing the array very easy:
72 // set size_ to 0.  Lookups are slightly more complicated.
73 // An index i has a value in the array if and only if:
74 //   sparse_[i] is in [0, size_) AND
75 //   dense_[sparse_[i]].index_ == i.
76 // If both these properties hold, only then it is safe to refer to
77 //   dense_[sparse_[i]].value_
78 // as the value associated with index i.
79 //
80 // To insert a new entry, set sparse_[i] to size_,
81 // initialize dense_[size_], and then increment size_.
82 //
83 // To make the sparse array as efficient as possible for non-primitive types,
84 // elements may or may not be destroyed when they are deleted from the sparse
85 // array through a call to resize(). They immediately become inaccessible, but
86 // they are only guaranteed to be destroyed when the SparseArray destructor is
87 // called.
88 //
89 // A moved-from SparseArray will be empty.
90 
91 #include <assert.h>
92 #include <stdint.h>
93 
94 #include <algorithm>
95 #include <memory>
96 #include <utility>
97 
98 #include "re2/pod_array.h"
99 
100 // Doing this simplifies the logic below.
101 #ifndef __has_feature
102 #define __has_feature(x) 0
103 #endif
104 
105 #if __has_feature(memory_sanitizer)
106 #include <sanitizer/msan_interface.h>
107 #endif
108 
109 namespace re2 {
110 
111 template<typename Value>
112 class SparseArray {
113  public:
114   SparseArray();
115   explicit SparseArray(int max_size);
116   ~SparseArray();
117 
118   // IndexValue pairs: exposed in SparseArray::iterator.
119   class IndexValue;
120 
121   typedef IndexValue* iterator;
122   typedef const IndexValue* const_iterator;
123 
124   SparseArray(const SparseArray& src);
125   SparseArray(SparseArray&& src);
126 
127   SparseArray& operator=(const SparseArray& src);
128   SparseArray& operator=(SparseArray&& src);
129 
130   // Return the number of entries in the array.
size()131   int size() const {
132     return size_;
133   }
134 
135   // Indicate whether the array is empty.
empty()136   int empty() const {
137     return size_ == 0;
138   }
139 
140   // Iterate over the array.
begin()141   iterator begin() {
142     return dense_.data();
143   }
end()144   iterator end() {
145     return dense_.data() + size_;
146   }
147 
begin()148   const_iterator begin() const {
149     return dense_.data();
150   }
end()151   const_iterator end() const {
152     return dense_.data() + size_;
153   }
154 
155   // Change the maximum size of the array.
156   // Invalidates all iterators.
157   void resize(int new_max_size);
158 
159   // Return the maximum size of the array.
160   // Indices can be in the range [0, max_size).
max_size()161   int max_size() const {
162     if (dense_.data() != NULL)
163       return dense_.size();
164     else
165       return 0;
166   }
167 
168   // Clear the array.
clear()169   void clear() {
170     size_ = 0;
171   }
172 
173   // Check whether index i is in the array.
174   bool has_index(int i) const;
175 
176   // Comparison function for sorting.
177   // Can sort the sparse array so that future iterations
178   // will visit indices in increasing order using
179   // std::sort(arr.begin(), arr.end(), arr.less);
180   static bool less(const IndexValue& a, const IndexValue& b);
181 
182  public:
183   // Set the value at index i to v.
set(int i,const Value & v)184   iterator set(int i, const Value& v) {
185     return SetInternal(true, i, v);
186   }
187 
188   // Set the value at new index i to v.
189   // Fast but unsafe: only use if has_index(i) is false.
set_new(int i,const Value & v)190   iterator set_new(int i, const Value& v) {
191     return SetInternal(false, i, v);
192   }
193 
194   // Set the value at index i to v.
195   // Fast but unsafe: only use if has_index(i) is true.
set_existing(int i,const Value & v)196   iterator set_existing(int i, const Value& v) {
197     return SetExistingInternal(i, v);
198   }
199 
200   // Get the value at index i.
201   // Fast but unsafe: only use if has_index(i) is true.
get_existing(int i)202   Value& get_existing(int i) {
203     assert(has_index(i));
204     return dense_[sparse_[i]].value_;
205   }
get_existing(int i)206   const Value& get_existing(int i) const {
207     assert(has_index(i));
208     return dense_[sparse_[i]].value_;
209   }
210 
211  private:
SetInternal(bool allow_existing,int i,const Value & v)212   iterator SetInternal(bool allow_existing, int i, const Value& v) {
213     DebugCheckInvariants();
214     if (static_cast<uint32_t>(i) >= static_cast<uint32_t>(max_size())) {
215       assert(false && "illegal index");
216       // Semantically, end() would be better here, but we already know
217       // the user did something stupid, so begin() insulates them from
218       // dereferencing an invalid pointer.
219       return begin();
220     }
221     if (!allow_existing) {
222       assert(!has_index(i));
223       create_index(i);
224     } else {
225       if (!has_index(i))
226         create_index(i);
227     }
228     return SetExistingInternal(i, v);
229   }
230 
SetExistingInternal(int i,const Value & v)231   iterator SetExistingInternal(int i, const Value& v) {
232     DebugCheckInvariants();
233     assert(has_index(i));
234     dense_[sparse_[i]].value_ = v;
235     DebugCheckInvariants();
236     return dense_.data() + sparse_[i];
237   }
238 
239   // Add the index i to the array.
240   // Only use if has_index(i) is known to be false.
241   // Since it doesn't set the value associated with i,
242   // this function is private, only intended as a helper
243   // for other methods.
244   void create_index(int i);
245 
246   // In debug mode, verify that some invariant properties of the class
247   // are being maintained. This is called at the end of the constructor
248   // and at the beginning and end of all public non-const member functions.
249   void DebugCheckInvariants() const;
250 
251   // Initializes memory for elements [min, max).
MaybeInitializeMemory(int min,int max)252   void MaybeInitializeMemory(int min, int max) {
253 #if __has_feature(memory_sanitizer)
254     __msan_unpoison(sparse_.data() + min, (max - min) * sizeof sparse_[0]);
255 #elif defined(RE2_ON_VALGRIND)
256     for (int i = min; i < max; i++) {
257       sparse_[i] = 0xababababU;
258     }
259 #endif
260   }
261 
262   int size_ = 0;
263   PODArray<int> sparse_;
264   PODArray<IndexValue> dense_;
265 };
266 
267 template<typename Value>
268 SparseArray<Value>::SparseArray() = default;
269 
270 template<typename Value>
SparseArray(const SparseArray & src)271 SparseArray<Value>::SparseArray(const SparseArray& src)
272     : size_(src.size_),
273       sparse_(src.max_size()),
274       dense_(src.max_size()) {
275   std::copy_n(src.sparse_.data(), src.max_size(), sparse_.data());
276   std::copy_n(src.dense_.data(), src.max_size(), dense_.data());
277 }
278 
279 template<typename Value>
SparseArray(SparseArray && src)280 SparseArray<Value>::SparseArray(SparseArray&& src)
281     : size_(src.size_),
282       sparse_(std::move(src.sparse_)),
283       dense_(std::move(src.dense_)) {
284   src.size_ = 0;
285 }
286 
287 template<typename Value>
288 SparseArray<Value>& SparseArray<Value>::operator=(const SparseArray& src) {
289   // Construct these first for exception safety.
290   PODArray<int> a(src.max_size());
291   PODArray<IndexValue> b(src.max_size());
292 
293   size_ = src.size_;
294   sparse_ = std::move(a);
295   dense_ = std::move(b);
296   std::copy_n(src.sparse_.data(), src.max_size(), sparse_.data());
297   std::copy_n(src.dense_.data(), src.max_size(), dense_.data());
298   return *this;
299 }
300 
301 template<typename Value>
302 SparseArray<Value>& SparseArray<Value>::operator=(SparseArray&& src) {
303   size_ = src.size_;
304   sparse_ = std::move(src.sparse_);
305   dense_ = std::move(src.dense_);
306   src.size_ = 0;
307   return *this;
308 }
309 
310 // IndexValue pairs: exposed in SparseArray::iterator.
311 template<typename Value>
312 class SparseArray<Value>::IndexValue {
313  public:
index()314   int index() const { return index_; }
value()315   Value& value() { return value_; }
value()316   const Value& value() const { return value_; }
317 
318  private:
319   friend class SparseArray;
320   int index_;
321   Value value_;
322 };
323 
324 // Change the maximum size of the array.
325 // Invalidates all iterators.
326 template<typename Value>
resize(int new_max_size)327 void SparseArray<Value>::resize(int new_max_size) {
328   DebugCheckInvariants();
329   if (new_max_size > max_size()) {
330     const int old_max_size = max_size();
331 
332     // Construct these first for exception safety.
333     PODArray<int> a(new_max_size);
334     PODArray<IndexValue> b(new_max_size);
335 
336     std::copy_n(sparse_.data(), old_max_size, a.data());
337     std::copy_n(dense_.data(), old_max_size, b.data());
338 
339     sparse_ = std::move(a);
340     dense_ = std::move(b);
341 
342     MaybeInitializeMemory(old_max_size, new_max_size);
343   }
344   if (size_ > new_max_size)
345     size_ = new_max_size;
346   DebugCheckInvariants();
347 }
348 
349 // Check whether index i is in the array.
350 template<typename Value>
has_index(int i)351 bool SparseArray<Value>::has_index(int i) const {
352   assert(i >= 0);
353   assert(i < max_size());
354   if (static_cast<uint32_t>(i) >= static_cast<uint32_t>(max_size())) {
355     return false;
356   }
357   // Unsigned comparison avoids checking sparse_[i] < 0.
358   return (uint32_t)sparse_[i] < (uint32_t)size_ &&
359          dense_[sparse_[i]].index_ == i;
360 }
361 
362 template<typename Value>
create_index(int i)363 void SparseArray<Value>::create_index(int i) {
364   assert(!has_index(i));
365   assert(size_ < max_size());
366   sparse_[i] = size_;
367   dense_[size_].index_ = i;
368   size_++;
369 }
370 
SparseArray(int max_size)371 template<typename Value> SparseArray<Value>::SparseArray(int max_size) :
372     sparse_(max_size), dense_(max_size) {
373   MaybeInitializeMemory(size_, max_size);
374   DebugCheckInvariants();
375 }
376 
~SparseArray()377 template<typename Value> SparseArray<Value>::~SparseArray() {
378   DebugCheckInvariants();
379 }
380 
DebugCheckInvariants()381 template<typename Value> void SparseArray<Value>::DebugCheckInvariants() const {
382   assert(0 <= size_);
383   assert(size_ <= max_size());
384 }
385 
386 // Comparison function for sorting.
less(const IndexValue & a,const IndexValue & b)387 template<typename Value> bool SparseArray<Value>::less(const IndexValue& a,
388                                                        const IndexValue& b) {
389   return a.index_ < b.index_;
390 }
391 
392 }  // namespace re2
393 
394 #endif  // RE2_SPARSE_ARRAY_H_
395