aboutsummaryrefslogtreecommitdiffstats
path: root/include/llvm/ADT/FlatArrayMap.h
blob: b414cde7a163a58131b4dd0ebe5b109ddce0df3d (plain)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//===- llvm/ADT/FlatArrayMap.h - 'Normally small' pointer set ----*- C++ -*-==//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the FlatArrayMap class.
// See FlatArrayMap doxygen comments for more details.
//
//===----------------------------------------------------------------------===//

#ifndef FLATARRAYMAP_H_
#define FLATARRAYMAP_H_

#include <algorithm>
#include <utility>
#include "llvm/Support/type_traits.h"

namespace llvm {

  template <typename KeyTy, typename MappedTy>
  struct FlatArrayMapTypes {
    typedef KeyTy key_type;
    typedef MappedTy mapped_type;
    typedef typename std::pair<key_type, mapped_type> value_type;
  };

  template<typename KeyTy, typename MappedTy, bool IsConst = false>
  class FlatArrayMapIterator;

  //===--------------------------------------------------------------------===//
  /// FlatArrayMap presents map container interface.
  /// It uses flat array implementation inside:
  /// [ <key0, value0>, <key1, value1>, ... <keyN, valueN> ]
  /// It works fast for small amount of elements.
  /// User should pass key type, mapped type (type of value), and maximum
  /// number of elements.
  /// After maximum number of elements is reached, map declines any farther
  /// attempts to insert new elements ("insert" method returns <end(),false>).
  ///
  template <typename KeyTy, typename MappedTy, unsigned MaxArraySize>
  class FlatArrayMap {
  public:
    typedef FlatArrayMapTypes<KeyTy, MappedTy> Types;

    typedef typename Types::key_type key_type;
    typedef typename Types::mapped_type mapped_type;
    typedef typename Types::value_type value_type;

    typedef FlatArrayMapIterator<KeyTy, MappedTy> iterator;
    typedef FlatArrayMapIterator<KeyTy, MappedTy, true> const_iterator;

    typedef FlatArrayMap<KeyTy, MappedTy, MaxArraySize> self;

  private:

    enum { BadIndex = -1U };

    key_type EmptyKey;
    mapped_type EmptyValue;

    value_type Array[MaxArraySize + 1];
    unsigned NumElements;

  unsigned findFor(const KeyTy Ptr) const {
    // Linear search for the item.
    for (const value_type *APtr = Array, *E = Array + NumElements;
               APtr != E; ++APtr) {
      if (APtr->first == Ptr) {
        return APtr - Array;
      }
    }
    return BadIndex;
  }

  bool lookupFor(const KeyTy &Ptr, const value_type*& Found) const {
    unsigned FoundIdx = findFor(Ptr);
    if (FoundIdx != BadIndex) {
      Found = Array + FoundIdx;
      return true;
    }
    return false;
  }

  bool lookupFor(const KeyTy &Ptr, value_type*& Found) {
    unsigned FoundIdx = findFor(Ptr);
    if (FoundIdx != BadIndex) {
      Found = Array + FoundIdx;
      return true;
    }
    return false;
  }


  void copyFrom(const self &RHS) {
    memcpy(Array, RHS.Array, sizeof(value_type) * (MaxArraySize + 1));
    NumElements = RHS.NumElements;
  }

  void init () {
    memset(Array + MaxArraySize, 0, sizeof(value_type));
    NumElements = 0;
  }

  bool insertInternal(KeyTy Ptr, MappedTy Val, value_type*& Item) {
    // Check to see if it is already in the set.
    value_type *Found;
    if (lookupFor(Ptr, Found)) {
      Item = Found;
      return false;
    }
    if (NumElements < MaxArraySize) {
      unsigned Idx = NumElements++;
      Array[Idx] = std::make_pair(Ptr, Val);
      Item = Array + Idx;
      return true;
    }
    Item = Array + MaxArraySize; // return end()
    return false;
  }

  public:

    // Constructors

    FlatArrayMap() : EmptyKey(), EmptyValue() {
      init();
    }

    FlatArrayMap(const self &that) :
      EmptyKey(), EmptyValue() {
      copyFrom(that);
    }

    template<typename It>
    FlatArrayMap(It I, It E) :
      EmptyKey(), EmptyValue() {
      init();
      insert(I, E);
    }

    // Size

    unsigned size() const {
      return NumElements;
    }

    bool empty() const {
      return !NumElements;
    }

    // Iterators

    iterator begin() {
      return iterator(Array);
    }
    const_iterator begin() const {
      return const_iterator(Array);
    }

    iterator end() {
      return iterator(Array + NumElements);
    }
    const_iterator end() const {
      return const_iterator(Array + NumElements);
    }

    // Modifiers

    void clear() {
      for (unsigned i = 0; i < NumElements; ++i) {
        Array[i].first = EmptyKey;
        Array[i].second = EmptyValue;
      }
      NumElements = 0;
    }

    // The map container is extended by inserting a single new element.
    // The behavior is the same as the std::map::insert, except the
    // case when maximum number of elements is reached;
    // in this case map declines any farther attempts
    // to insert new elements ("insert" method returns <end(),false>).
    std::pair<iterator, bool> insert(const value_type& KV) {
      value_type* Item;
      bool Res = insertInternal(KV.first, KV.second, Item);
      return std::make_pair(iterator(Item), Res);
    }

    template <typename IterT>
    void insert(IterT I, IterT E) {
      for (; I != E; ++I)
        insert(*I);
    }

    void erase(key_type K) {
      unsigned Found = findFor(K);
      if (Found != BadIndex) {
        value_type *APtr = Array + Found;
        value_type *E = Array + NumElements;
        *APtr = E[-1];
        E[-1].first.~key_type();
        E[-1].second.~mapped_type();
        --NumElements;
      }
    }

    void erase(iterator i) {
      erase(i->first);
    }

    void swap(self& RHS) {
      std::swap_ranges(Array, Array+MaxArraySize,  RHS.Array);
      std::swap(this->NumElements, RHS.NumElements);
    }

    // Search operations

    iterator find(const key_type& K) {
      value_type *Found;
      if (lookupFor(K, Found))
        return iterator(Found);
      return end();
    }

    const_iterator find(const key_type& K) const {
      const value_type *Found;
      if (lookupFor(K, Found))
        return const_iterator(Found);
      return end();
    }

    bool count(const key_type& K) const {
      return find(K) != end();
    }

    mapped_type &operator[](const key_type &Key) {
      std::pair<iterator, bool> res = insert(Key, mapped_type());
      return res.first->second;
    }

    // Other operations

    self& operator=(const self& other) {
      clear();
      copyFrom(other);
      return *this;
    }

    /// isPointerIntoBucketsArray - Return true if the specified pointer points
    /// somewhere into the map's array of buckets (i.e. either to a key or
    /// value).
    bool isPointerIntoBucketsArray(const void *Ptr) const {
      return Ptr >= Array && Ptr < Array + NumElements;
    }

    /// getPointerIntoBucketsArray() - Return an opaque pointer into the buckets
    /// array.
    const void *getPointerIntoBucketsArray() const { return Array; }
  };

  template<typename KeyTy, typename MappedTy, bool IsConst>
  class FlatArrayMapIterator {

    typedef FlatArrayMapTypes<KeyTy, MappedTy> Types;

    typedef typename conditional<IsConst,
                                 const typename Types::value_type,
                                 typename Types::value_type>::type value_type;
    typedef value_type *pointer;
    typedef value_type &reference;

    typedef FlatArrayMapIterator<KeyTy, MappedTy, IsConst> self;
    typedef FlatArrayMapIterator<KeyTy, MappedTy, false> non_const_self;
    typedef FlatArrayMapIterator<KeyTy, MappedTy, true> const_self;

    friend class FlatArrayMapIterator<KeyTy, MappedTy, false>;
    friend class FlatArrayMapIterator<KeyTy, MappedTy, true>;

    pointer TheBucket;

  public:

    FlatArrayMapIterator() : TheBucket(0) {}

    explicit FlatArrayMapIterator(pointer BP) :
        TheBucket(BP) {}

    // If IsConst is true this is a converting constructor from iterator to
    // const_iterator and the default copy constructor is used.
    // Otherwise this is a copy constructor for iterator.
    FlatArrayMapIterator(const non_const_self& I)
      : TheBucket(I.TheBucket) {}

    bool operator==(const const_self &RHS) const {
      return TheBucket->first == RHS.TheBucket->first;
    }
    bool operator!=(const const_self &RHS) const {
      return TheBucket->first != RHS.TheBucket->first;
    }

    reference operator*() const {
      return *TheBucket;
    }

    pointer operator->() const {
      return TheBucket;
    }

    inline self& operator++() {   // Preincrement
      ++TheBucket;
      return *this;
    }

    self operator++(int) {        // Postincrement
      FlatArrayMapIterator tmp = *this; ++*this; return tmp;
    }
  };
}

#endif /* FLATARRAYMAP_H_ */