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.
7 // SparseSet<T>(m) is a set of integers in [0, m).
8 // It requires sizeof(int)*m memory, but it provides
9 // fast iteration through the elements in the set and fast clearing
12 // Insertion and deletion are constant time operations.
14 // Allocating the set is a constant time operation
15 // when memory allocation is a constant time operation.
17 // Clearing the set is a constant time operation (unusual!).
19 // Iterating through the set is an O(n) operation, where n
20 // is the number of items in the set (not O(m)).
22 // The set iterator visits entries in the order they were first
23 // inserted into the array. It is safe to add items to the set while
24 // using an iterator: the iterator will visit indices added to the set
25 // during the iteration, but will not re-visit indices whose values
26 // change after visiting. Thus SparseSet can be a convenient
27 // implementation of a work queue.
29 // The SparseSet implementation is NOT thread-safe. It is up to the
30 // caller to make sure only one thread is accessing the set. (Typically
31 // these sets are temporary values and used in situations where speed is
34 // The SparseSet interface does not present all the usual STL bells and
37 // Implemented with reference to Briggs & Torczon, An Efficient
38 // Representation for Sparse Sets, ACM Letters on Programming Languages
39 // and Systems, Volume 2, Issue 1-4 (March-Dec. 1993), pp. 59-69.
41 // For a generalization to sparse array, see sparse_array.h.
45 // See sparse_array.h for implementation details
47 #ifndef RE2_UTIL_SPARSE_SET_H__
48 #define RE2_UTIL_SPARSE_SET_H__
50 #include "util/util.h"
57 : size_(0), max_size_(0), sparse_to_dense_(NULL), dense_(NULL), valgrind_(RunningOnValgrind()) {}
59 SparseSet(int max_size) {
61 sparse_to_dense_ = new int[max_size];
62 dense_ = new int[max_size];
63 valgrind_ = RunningOnValgrind();
64 // Don't need to zero the memory, but do so anyway
65 // to appease Valgrind.
67 for (int i = 0; i < max_size; i++) {
68 dense_[i] = 0xababababU;
69 sparse_to_dense_[i] = 0xababababU;
76 delete[] sparse_to_dense_;
80 typedef int* iterator;
81 typedef const int* const_iterator;
83 int size() const { return size_; }
84 iterator begin() { return dense_; }
85 iterator end() { return dense_ + size_; }
86 const_iterator begin() const { return dense_; }
87 const_iterator end() const { return dense_ + size_; }
89 // Change the maximum size of the array.
90 // Invalidates all iterators.
91 void resize(int new_max_size) {
92 if (size_ > new_max_size)
94 if (new_max_size > max_size_) {
95 int* a = new int[new_max_size];
96 if (sparse_to_dense_) {
97 memmove(a, sparse_to_dense_, max_size_*sizeof a[0]);
99 for (int i = max_size_; i < new_max_size; i++)
102 delete[] sparse_to_dense_;
104 sparse_to_dense_ = a;
106 a = new int[new_max_size];
108 memmove(a, dense_, size_*sizeof a[0]);
110 for (int i = size_; i < new_max_size; i++)
117 max_size_ = new_max_size;
120 // Return the maximum size of the array.
121 // Indices can be in the range [0, max_size).
122 int max_size() const { return max_size_; }
125 void clear() { size_ = 0; }
127 // Check whether i is in the array.
128 bool contains(int i) const {
130 DCHECK_LT(i, max_size_);
131 if (static_cast<uint>(i) >= max_size_) {
134 // Unsigned comparison avoids checking sparse_to_dense_[i] < 0.
135 return (uint)sparse_to_dense_[i] < (uint)size_ &&
136 dense_[sparse_to_dense_[i]] == i;
139 // Adds i to the set.
145 // Set the value at the new index i to v.
146 // Fast but unsafe: only use if contains(i) is false.
147 void insert_new(int i) {
148 if (static_cast<uint>(i) >= max_size_) {
149 // Semantically, end() would be better here, but we already know
150 // the user did something stupid, so begin() insulates them from
151 // dereferencing an invalid pointer.
154 DCHECK(!contains(i));
155 DCHECK_LT(size_, max_size_);
156 sparse_to_dense_[i] = size_;
161 // Comparison function for sorting.
162 // Can sort the sparse array so that future iterations
163 // will visit indices in increasing order using
164 // sort(arr.begin(), arr.end(), arr.less);
165 static bool less(int a, int b) { return a < b; }
170 int* sparse_to_dense_;
174 DISALLOW_EVIL_CONSTRUCTORS(SparseSet);
179 #endif // RE2_UTIL_SPARSE_SET_H__