1 //===- llvm/ADT/FlatArrayMap.h - 'Normally small' pointer set ----*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the FlatArrayMap class.
11 // See FlatArrayMap doxygen comments for more details.
13 //===----------------------------------------------------------------------===//
15 #ifndef FLATARRAYMAP_H_
16 #define FLATARRAYMAP_H_
20 #include "llvm/Support/type_traits.h"
24 template <typename KeyTy, typename MappedTy>
25 struct FlatArrayMapTypes {
26 typedef KeyTy key_type;
27 typedef MappedTy mapped_type;
28 typedef typename std::pair<key_type, mapped_type> value_type;
31 template<typename KeyTy, typename MappedTy, bool IsConst = false>
32 class FlatArrayMapIterator;
34 //===--------------------------------------------------------------------===//
35 /// FlatArrayMap presents map container interface.
36 /// It uses flat array implementation inside:
37 /// [ <key0, value0>, <key1, value1>, ... <keyN, valueN> ]
38 /// It works fast for small amount of elements.
39 /// User should pass key type, mapped type (type of value), and maximum
40 /// number of elements.
41 /// After maximum number of elements is reached, map declines any farther
42 /// attempts to insert new elements ("insert" method returns <end(),false>).
44 template <typename KeyTy, typename MappedTy, unsigned MaxArraySize>
47 typedef FlatArrayMapTypes<KeyTy, MappedTy> Types;
49 typedef typename Types::key_type key_type;
50 typedef typename Types::mapped_type mapped_type;
51 typedef typename Types::value_type value_type;
53 typedef FlatArrayMapIterator<KeyTy, MappedTy> iterator;
54 typedef FlatArrayMapIterator<KeyTy, MappedTy, true> const_iterator;
56 typedef FlatArrayMap<KeyTy, MappedTy, MaxArraySize> self;
60 enum { BadIndex = -1U };
63 mapped_type EmptyValue;
65 value_type Array[MaxArraySize + 1];
68 unsigned findFor(const KeyTy Ptr) const {
69 // Linear search for the item.
70 for (const value_type *APtr = Array, *E = Array + NumElements;
72 if (APtr->first == Ptr) {
79 bool lookupFor(const KeyTy &Ptr, const value_type*& Found) const {
80 unsigned FoundIdx = findFor(Ptr);
81 if (FoundIdx != BadIndex) {
82 Found = Array + FoundIdx;
88 bool lookupFor(const KeyTy &Ptr, value_type*& Found) {
89 unsigned FoundIdx = findFor(Ptr);
90 if (FoundIdx != BadIndex) {
91 Found = Array + FoundIdx;
98 void copyFrom(const self &RHS) {
99 memcpy(Array, RHS.Array, sizeof(value_type) * (MaxArraySize + 1));
100 NumElements = RHS.NumElements;
104 memset(Array + MaxArraySize, 0, sizeof(value_type));
108 bool insertInternal(KeyTy Ptr, MappedTy Val, value_type*& Item) {
109 // Check to see if it is already in the set.
111 if (lookupFor(Ptr, Found)) {
115 if (NumElements < MaxArraySize) {
116 unsigned Idx = NumElements++;
117 Array[Idx] = std::make_pair(Ptr, Val);
121 Item = Array + MaxArraySize; // return end()
129 FlatArrayMap() : EmptyKey(), EmptyValue() {
133 FlatArrayMap(const self &that) :
134 EmptyKey(), EmptyValue() {
138 template<typename It>
139 FlatArrayMap(It I, It E) :
140 EmptyKey(), EmptyValue() {
147 unsigned size() const {
158 return iterator(Array);
160 const_iterator begin() const {
161 return const_iterator(Array);
165 return iterator(Array + NumElements);
167 const_iterator end() const {
168 return const_iterator(Array + NumElements);
174 for (unsigned i = 0; i < NumElements; ++i) {
175 Array[i].first = EmptyKey;
176 Array[i].second = EmptyValue;
181 // The map container is extended by inserting a single new element.
182 // The behavior is the same as the std::map::insert, except the
183 // case when maximum number of elements is reached;
184 // in this case map declines any farther attempts
185 // to insert new elements ("insert" method returns <end(),false>).
186 std::pair<iterator, bool> insert(const value_type& KV) {
188 bool Res = insertInternal(KV.first, KV.second, Item);
189 return std::make_pair(iterator(Item), Res);
192 template <typename IterT>
193 void insert(IterT I, IterT E) {
198 void erase(key_type K) {
199 unsigned Found = findFor(K);
200 if (Found != BadIndex) {
201 value_type *APtr = Array + Found;
202 value_type *E = Array + NumElements;
204 E[-1].first.~key_type();
205 E[-1].second.~mapped_type();
210 void erase(iterator i) {
214 void swap(self& RHS) {
215 std::swap_ranges(Array, Array+MaxArraySize, RHS.Array);
216 std::swap(this->NumElements, RHS.NumElements);
221 iterator find(const key_type& K) {
223 if (lookupFor(K, Found))
224 return iterator(Found);
228 const_iterator find(const key_type& K) const {
229 const value_type *Found;
230 if (lookupFor(K, Found))
231 return const_iterator(Found);
235 bool count(const key_type& K) const {
236 return find(K) != end();
239 mapped_type &operator[](const key_type &Key) {
240 std::pair<iterator, bool> res = insert(Key, mapped_type());
241 return res.first->second;
246 self& operator=(const self& other) {
252 /// isPointerIntoBucketsArray - Return true if the specified pointer points
253 /// somewhere into the map's array of buckets (i.e. either to a key or
255 bool isPointerIntoBucketsArray(const void *Ptr) const {
256 return Ptr >= Array && Ptr < Array + NumElements;
259 /// getPointerIntoBucketsArray() - Return an opaque pointer into the buckets
261 const void *getPointerIntoBucketsArray() const { return Array; }
264 template<typename KeyTy, typename MappedTy, bool IsConst>
265 class FlatArrayMapIterator {
267 typedef FlatArrayMapTypes<KeyTy, MappedTy> Types;
269 typedef typename conditional<IsConst,
270 const typename Types::value_type,
271 typename Types::value_type>::type value_type;
272 typedef value_type *pointer;
273 typedef value_type &reference;
275 typedef FlatArrayMapIterator<KeyTy, MappedTy, IsConst> self;
276 typedef FlatArrayMapIterator<KeyTy, MappedTy, false> non_const_self;
277 typedef FlatArrayMapIterator<KeyTy, MappedTy, true> const_self;
279 friend class FlatArrayMapIterator<KeyTy, MappedTy, false>;
280 friend class FlatArrayMapIterator<KeyTy, MappedTy, true>;
286 FlatArrayMapIterator() : TheBucket(0) {}
288 explicit FlatArrayMapIterator(pointer BP) :
291 // If IsConst is true this is a converting constructor from iterator to
292 // const_iterator and the default copy constructor is used.
293 // Otherwise this is a copy constructor for iterator.
294 FlatArrayMapIterator(const non_const_self& I)
295 : TheBucket(I.TheBucket) {}
297 bool operator==(const const_self &RHS) const {
298 return TheBucket->first == RHS.TheBucket->first;
300 bool operator!=(const const_self &RHS) const {
301 return TheBucket->first != RHS.TheBucket->first;
304 reference operator*() const {
308 pointer operator->() const {
312 inline self& operator++() { // Preincrement
317 self operator++(int) { // Postincrement
318 FlatArrayMapIterator tmp = *this; ++*this; return tmp;
323 #endif /* FLATARRAYMAP_H_ */