StringMap.h revision ddcdcc88631c6bd4ad43d9198b98bc9a829be036
1//===--- StringMap.h - String Hash table map interface ----------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the StringMap class.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ADT_STRINGMAP_H
15#define LLVM_ADT_STRINGMAP_H
16
17#include "llvm/ADT/StringRef.h"
18#include "llvm/Support/Allocator.h"
19#include <cstring>
20
21namespace llvm {
22  template<typename ValueT>
23  class StringMapConstIterator;
24  template<typename ValueT>
25  class StringMapIterator;
26  template<typename ValueTy>
27  class StringMapEntry;
28
29/// StringMapEntryInitializer - This datatype can be partially specialized for
30/// various datatypes in a stringmap to allow them to be initialized when an
31/// entry is default constructed for the map.
32template<typename ValueTy>
33class StringMapEntryInitializer {
34public:
35  template <typename InitTy>
36  static void Initialize(StringMapEntry<ValueTy> &T, InitTy InitVal) {
37    T.second = InitVal;
38  }
39};
40
41
42/// StringMapEntryBase - Shared base class of StringMapEntry instances.
43class StringMapEntryBase {
44  unsigned StrLen;
45public:
46  explicit StringMapEntryBase(unsigned Len) : StrLen(Len) {}
47
48  unsigned getKeyLength() const { return StrLen; }
49};
50
51/// StringMapImpl - This is the base class of StringMap that is shared among
52/// all of its instantiations.
53class StringMapImpl {
54public:
55  /// ItemBucket - The hash table consists of an array of these.  If Item is
56  /// non-null, this is an extant entry, otherwise, it is a hole.
57  struct ItemBucket {
58    /// FullHashValue - This remembers the full hash value of the key for
59    /// easy scanning.
60    unsigned FullHashValue;
61
62    /// Item - This is a pointer to the actual item object.
63    StringMapEntryBase *Item;
64  };
65
66protected:
67  ItemBucket *TheTable;
68  unsigned NumBuckets;
69  unsigned NumItems;
70  unsigned NumTombstones;
71  unsigned ItemSize;
72protected:
73  explicit StringMapImpl(unsigned itemSize) : ItemSize(itemSize) {
74    // Initialize the map with zero buckets to allocation.
75    TheTable = 0;
76    NumBuckets = 0;
77    NumItems = 0;
78    NumTombstones = 0;
79  }
80  StringMapImpl(unsigned InitSize, unsigned ItemSize);
81  void RehashTable();
82
83  /// LookupBucketFor - Look up the bucket that the specified string should end
84  /// up in.  If it already exists as a key in the map, the Item pointer for the
85  /// specified bucket will be non-null.  Otherwise, it will be null.  In either
86  /// case, the FullHashValue field of the bucket will be set to the hash value
87  /// of the string.
88  unsigned LookupBucketFor(StringRef Key);
89
90  /// FindKey - Look up the bucket that contains the specified key. If it exists
91  /// in the map, return the bucket number of the key.  Otherwise return -1.
92  /// This does not modify the map.
93  int FindKey(StringRef Key) const;
94
95  /// RemoveKey - Remove the specified StringMapEntry from the table, but do not
96  /// delete it.  This aborts if the value isn't in the table.
97  void RemoveKey(StringMapEntryBase *V);
98
99  /// RemoveKey - Remove the StringMapEntry for the specified key from the
100  /// table, returning it.  If the key is not in the table, this returns null.
101  StringMapEntryBase *RemoveKey(StringRef Key);
102private:
103  void init(unsigned Size);
104public:
105  static StringMapEntryBase *getTombstoneVal() {
106    return (StringMapEntryBase*)-1;
107  }
108
109  unsigned getNumBuckets() const { return NumBuckets; }
110  unsigned getNumItems() const { return NumItems; }
111
112  bool empty() const { return NumItems == 0; }
113  unsigned size() const { return NumItems; }
114};
115
116/// StringMapEntry - This is used to represent one value that is inserted into
117/// a StringMap.  It contains the Value itself and the key: the string length
118/// and data.
119template<typename ValueTy>
120class StringMapEntry : public StringMapEntryBase {
121public:
122  ValueTy second;
123
124  explicit StringMapEntry(unsigned strLen)
125    : StringMapEntryBase(strLen), second() {}
126  StringMapEntry(unsigned strLen, const ValueTy &V)
127    : StringMapEntryBase(strLen), second(V) {}
128
129  StringRef getKey() const {
130    return StringRef(getKeyData(), getKeyLength());
131  }
132
133  const ValueTy &getValue() const { return second; }
134  ValueTy &getValue() { return second; }
135
136  void setValue(const ValueTy &V) { second = V; }
137
138  /// getKeyData - Return the start of the string data that is the key for this
139  /// value.  The string data is always stored immediately after the
140  /// StringMapEntry object.
141  const char *getKeyData() const {return reinterpret_cast<const char*>(this+1);}
142
143  const char *first() const { return getKeyData(); }
144
145  /// Create - Create a StringMapEntry for the specified key and default
146  /// construct the value.
147  template<typename AllocatorTy, typename InitType>
148  static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd,
149                                AllocatorTy &Allocator,
150                                InitType InitVal) {
151    unsigned KeyLength = static_cast<unsigned>(KeyEnd-KeyStart);
152
153    // Okay, the item doesn't already exist, and 'Bucket' is the bucket to fill
154    // in.  Allocate a new item with space for the string at the end and a null
155    // terminator.
156
157    unsigned AllocSize = static_cast<unsigned>(sizeof(StringMapEntry))+
158      KeyLength+1;
159    unsigned Alignment = alignOf<StringMapEntry>();
160
161    StringMapEntry *NewItem =
162      static_cast<StringMapEntry*>(Allocator.Allocate(AllocSize,Alignment));
163
164    // Default construct the value.
165    new (NewItem) StringMapEntry(KeyLength);
166
167    // Copy the string information.
168    char *StrBuffer = const_cast<char*>(NewItem->getKeyData());
169    memcpy(StrBuffer, KeyStart, KeyLength);
170    StrBuffer[KeyLength] = 0;  // Null terminate for convenience of clients.
171
172    // Initialize the value if the client wants to.
173    StringMapEntryInitializer<ValueTy>::Initialize(*NewItem, InitVal);
174    return NewItem;
175  }
176
177  template<typename AllocatorTy>
178  static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd,
179                                AllocatorTy &Allocator) {
180    return Create(KeyStart, KeyEnd, Allocator, 0);
181  }
182
183
184  /// Create - Create a StringMapEntry with normal malloc/free.
185  template<typename InitType>
186  static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd,
187                                InitType InitVal) {
188    MallocAllocator A;
189    return Create(KeyStart, KeyEnd, A, InitVal);
190  }
191
192  static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd) {
193    return Create(KeyStart, KeyEnd, ValueTy());
194  }
195
196  /// GetStringMapEntryFromValue - Given a value that is known to be embedded
197  /// into a StringMapEntry, return the StringMapEntry itself.
198  static StringMapEntry &GetStringMapEntryFromValue(ValueTy &V) {
199    StringMapEntry *EPtr = 0;
200    char *Ptr = reinterpret_cast<char*>(&V) -
201                  (reinterpret_cast<char*>(&EPtr->second) -
202                   reinterpret_cast<char*>(EPtr));
203    return *reinterpret_cast<StringMapEntry*>(Ptr);
204  }
205  static const StringMapEntry &GetStringMapEntryFromValue(const ValueTy &V) {
206    return GetStringMapEntryFromValue(const_cast<ValueTy&>(V));
207  }
208
209  /// GetStringMapEntryFromKeyData - Given key data that is known to be embedded
210  /// into a StringMapEntry, return the StringMapEntry itself.
211  static StringMapEntry &GetStringMapEntryFromKeyData(const char *KeyData) {
212    char *Ptr = const_cast<char*>(KeyData) - sizeof(StringMapEntry<ValueTy>);
213    return *reinterpret_cast<StringMapEntry*>(Ptr);
214  }
215
216
217  /// Destroy - Destroy this StringMapEntry, releasing memory back to the
218  /// specified allocator.
219  template<typename AllocatorTy>
220  void Destroy(AllocatorTy &Allocator) {
221    // Free memory referenced by the item.
222    this->~StringMapEntry();
223    Allocator.Deallocate(this);
224  }
225
226  /// Destroy this object, releasing memory back to the malloc allocator.
227  void Destroy() {
228    MallocAllocator A;
229    Destroy(A);
230  }
231};
232
233
234/// StringMap - This is an unconventional map that is specialized for handling
235/// keys that are "strings", which are basically ranges of bytes. This does some
236/// funky memory allocation and hashing things to make it extremely efficient,
237/// storing the string data *after* the value in the map.
238template<typename ValueTy, typename AllocatorTy = MallocAllocator>
239class StringMap : public StringMapImpl {
240  AllocatorTy Allocator;
241  typedef StringMapEntry<ValueTy> MapEntryTy;
242public:
243  StringMap() : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))) {}
244  explicit StringMap(unsigned InitialSize)
245    : StringMapImpl(InitialSize, static_cast<unsigned>(sizeof(MapEntryTy))) {}
246
247  explicit StringMap(AllocatorTy A)
248    : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))), Allocator(A) {}
249
250  explicit StringMap(const StringMap &RHS)
251    : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))) {
252    assert(RHS.empty() &&
253           "Copy ctor from non-empty stringmap not implemented yet!");
254    (void)RHS;
255  }
256  void operator=(const StringMap &RHS) {
257    assert(RHS.empty() &&
258           "assignment from non-empty stringmap not implemented yet!");
259    (void)RHS;
260    clear();
261  }
262
263  typedef typename ReferenceAdder<AllocatorTy>::result AllocatorRefTy;
264  typedef typename ReferenceAdder<const AllocatorTy>::result AllocatorCRefTy;
265  AllocatorRefTy getAllocator() { return Allocator; }
266  AllocatorCRefTy getAllocator() const { return Allocator; }
267
268  typedef const char* key_type;
269  typedef ValueTy mapped_type;
270  typedef StringMapEntry<ValueTy> value_type;
271  typedef size_t size_type;
272
273  typedef StringMapConstIterator<ValueTy> const_iterator;
274  typedef StringMapIterator<ValueTy> iterator;
275
276  iterator begin() {
277    return iterator(TheTable, NumBuckets == 0);
278  }
279  iterator end() {
280    return iterator(TheTable+NumBuckets, true);
281  }
282  const_iterator begin() const {
283    return const_iterator(TheTable, NumBuckets == 0);
284  }
285  const_iterator end() const {
286    return const_iterator(TheTable+NumBuckets, true);
287  }
288
289  iterator find(StringRef Key) {
290    int Bucket = FindKey(Key);
291    if (Bucket == -1) return end();
292    return iterator(TheTable+Bucket);
293  }
294
295  const_iterator find(StringRef Key) const {
296    int Bucket = FindKey(Key);
297    if (Bucket == -1) return end();
298    return const_iterator(TheTable+Bucket);
299  }
300
301   /// lookup - Return the entry for the specified key, or a default
302  /// constructed value if no such entry exists.
303  ValueTy lookup(StringRef Key) const {
304    const_iterator it = find(Key);
305    if (it != end())
306      return it->second;
307    return ValueTy();
308  }
309
310  ValueTy& operator[](StringRef Key) {
311    return GetOrCreateValue(Key).getValue();
312  }
313
314  size_type count(StringRef Key) const {
315    return find(Key) == end() ? 0 : 1;
316  }
317
318  /// insert - Insert the specified key/value pair into the map.  If the key
319  /// already exists in the map, return false and ignore the request, otherwise
320  /// insert it and return true.
321  bool insert(MapEntryTy *KeyValue) {
322    unsigned BucketNo = LookupBucketFor(KeyValue->getKey());
323    ItemBucket &Bucket = TheTable[BucketNo];
324    if (Bucket.Item && Bucket.Item != getTombstoneVal())
325      return false;  // Already exists in map.
326
327    if (Bucket.Item == getTombstoneVal())
328      --NumTombstones;
329    Bucket.Item = KeyValue;
330    ++NumItems;
331    assert(NumItems + NumTombstones <= NumBuckets);
332
333    RehashTable();
334    return true;
335  }
336
337  // clear - Empties out the StringMap
338  void clear() {
339    if (empty()) return;
340
341    // Zap all values, resetting the keys back to non-present (not tombstone),
342    // which is safe because we're removing all elements.
343    for (ItemBucket *I = TheTable, *E = TheTable+NumBuckets; I != E; ++I) {
344      if (I->Item && I->Item != getTombstoneVal()) {
345        static_cast<MapEntryTy*>(I->Item)->Destroy(Allocator);
346        I->Item = 0;
347      }
348    }
349
350    NumItems = 0;
351    NumTombstones = 0;
352  }
353
354  /// GetOrCreateValue - Look up the specified key in the table.  If a value
355  /// exists, return it.  Otherwise, default construct a value, insert it, and
356  /// return.
357  template <typename InitTy>
358  StringMapEntry<ValueTy> &GetOrCreateValue(StringRef Key,
359                                            InitTy Val) {
360    unsigned BucketNo = LookupBucketFor(Key);
361    ItemBucket &Bucket = TheTable[BucketNo];
362    if (Bucket.Item && Bucket.Item != getTombstoneVal())
363      return *static_cast<MapEntryTy*>(Bucket.Item);
364
365    MapEntryTy *NewItem =
366      MapEntryTy::Create(Key.begin(), Key.end(), Allocator, Val);
367
368    if (Bucket.Item == getTombstoneVal())
369      --NumTombstones;
370    ++NumItems;
371    assert(NumItems + NumTombstones <= NumBuckets);
372
373    // Fill in the bucket for the hash table.  The FullHashValue was already
374    // filled in by LookupBucketFor.
375    Bucket.Item = NewItem;
376
377    RehashTable();
378    return *NewItem;
379  }
380
381  StringMapEntry<ValueTy> &GetOrCreateValue(StringRef Key) {
382    return GetOrCreateValue(Key, ValueTy());
383  }
384
385  template <typename InitTy>
386  StringMapEntry<ValueTy> &GetOrCreateValue(const char *KeyStart,
387                                            const char *KeyEnd,
388                                            InitTy Val) {
389    return GetOrCreateValue(StringRef(KeyStart, KeyEnd - KeyStart), Val);
390  }
391
392  StringMapEntry<ValueTy> &GetOrCreateValue(const char *KeyStart,
393                                            const char *KeyEnd) {
394    return GetOrCreateValue(StringRef(KeyStart, KeyEnd - KeyStart));
395  }
396
397  /// remove - Remove the specified key/value pair from the map, but do not
398  /// erase it.  This aborts if the key is not in the map.
399  void remove(MapEntryTy *KeyValue) {
400    RemoveKey(KeyValue);
401  }
402
403  void erase(iterator I) {
404    MapEntryTy &V = *I;
405    remove(&V);
406    V.Destroy(Allocator);
407  }
408
409  bool erase(StringRef Key) {
410    iterator I = find(Key);
411    if (I == end()) return false;
412    erase(I);
413    return true;
414  }
415
416  ~StringMap() {
417    clear();
418    free(TheTable);
419  }
420};
421
422
423template<typename ValueTy>
424class StringMapConstIterator {
425protected:
426  StringMapImpl::ItemBucket *Ptr;
427public:
428  typedef StringMapEntry<ValueTy> value_type;
429
430  explicit StringMapConstIterator(StringMapImpl::ItemBucket *Bucket,
431                                  bool NoAdvance = false)
432  : Ptr(Bucket) {
433    if (!NoAdvance) AdvancePastEmptyBuckets();
434  }
435
436  const value_type &operator*() const {
437    return *static_cast<StringMapEntry<ValueTy>*>(Ptr->Item);
438  }
439  const value_type *operator->() const {
440    return static_cast<StringMapEntry<ValueTy>*>(Ptr->Item);
441  }
442
443  bool operator==(const StringMapConstIterator &RHS) const {
444    return Ptr == RHS.Ptr;
445  }
446  bool operator!=(const StringMapConstIterator &RHS) const {
447    return Ptr != RHS.Ptr;
448  }
449
450  inline StringMapConstIterator& operator++() {          // Preincrement
451    ++Ptr;
452    AdvancePastEmptyBuckets();
453    return *this;
454  }
455  StringMapConstIterator operator++(int) {        // Postincrement
456    StringMapConstIterator tmp = *this; ++*this; return tmp;
457  }
458
459private:
460  void AdvancePastEmptyBuckets() {
461    while (Ptr->Item == 0 || Ptr->Item == StringMapImpl::getTombstoneVal())
462      ++Ptr;
463  }
464};
465
466template<typename ValueTy>
467class StringMapIterator : public StringMapConstIterator<ValueTy> {
468public:
469  explicit StringMapIterator(StringMapImpl::ItemBucket *Bucket,
470                             bool NoAdvance = false)
471    : StringMapConstIterator<ValueTy>(Bucket, NoAdvance) {
472  }
473  StringMapEntry<ValueTy> &operator*() const {
474    return *static_cast<StringMapEntry<ValueTy>*>(this->Ptr->Item);
475  }
476  StringMapEntry<ValueTy> *operator->() const {
477    return static_cast<StringMapEntry<ValueTy>*>(this->Ptr->Item);
478  }
479};
480
481}
482
483#endif
484