]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - opencl/llvm.git/blob - include/llvm/ADT/StringMap.h
Add methods to StringMap to erase entries by key.
[opencl/llvm.git] / include / llvm / ADT / StringMap.h
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 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_STRINGMAP_H
15 #define LLVM_ADT_STRINGMAP_H
17 #include "llvm/Support/Allocator.h"
18 #include <cstring>
19 #include <string>
21 namespace llvm {
22   template<typename ValueT>
23   class StringMapConstIterator;
24   template<typename ValueT>
25   class StringMapIterator;
26   template<typename ValueTy>
27   class StringMapEntry;
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.
32 template<typename ValueTy>
33 class StringMapEntryInitializer {
34 public:
35   template <typename InitTy>
36   static void Initialize(StringMapEntry<ValueTy> &T, InitTy InitVal) {
37   }
38 };
41 /// StringMapEntryBase - Shared base class of StringMapEntry instances.
42 class StringMapEntryBase {
43   unsigned StrLen;
44 public:
45   explicit StringMapEntryBase(unsigned Len) : StrLen(Len) {}
47   unsigned getKeyLength() const { return StrLen; }
48 };
50 /// StringMapImpl - This is the base class of StringMap that is shared among
51 /// all of its instantiations.
52 class StringMapImpl {
53 public:
54   /// ItemBucket - The hash table consists of an array of these.  If Item is
55   /// non-null, this is an extant entry, otherwise, it is a hole.
56   struct ItemBucket {
57     /// FullHashValue - This remembers the full hash value of the key for
58     /// easy scanning.
59     unsigned FullHashValue;
61     /// Item - This is a pointer to the actual item object.
62     StringMapEntryBase *Item;
63   };
65 protected:
66   ItemBucket *TheTable;
67   unsigned NumBuckets;
68   unsigned NumItems;
69   unsigned NumTombstones;
70   unsigned ItemSize;
71 protected:
72   explicit StringMapImpl(unsigned itemSize) : ItemSize(itemSize) {
73     // Initialize the map with zero buckets to allocation.
74     TheTable = 0;
75     NumBuckets = 0;
76     NumItems = 0;
77     NumTombstones = 0;
78   }
79   StringMapImpl(unsigned InitSize, unsigned ItemSize);
80   void RehashTable();
82   /// ShouldRehash - Return true if the table should be rehashed after a new
83   /// element was recently inserted.
84   bool ShouldRehash() const {
85     // If the hash table is now more than 3/4 full, or if fewer than 1/8 of
86     // the buckets are empty (meaning that many are filled with tombstones),
87     // grow the table.
88     return NumItems*4 > NumBuckets*3 ||
89            NumBuckets-(NumItems+NumTombstones) < NumBuckets/8;
90   }
92   /// LookupBucketFor - Look up the bucket that the specified string should end
93   /// up in.  If it already exists as a key in the map, the Item pointer for the
94   /// specified bucket will be non-null.  Otherwise, it will be null.  In either
95   /// case, the FullHashValue field of the bucket will be set to the hash value
96   /// of the string.
97   unsigned LookupBucketFor(const char *KeyStart, const char *KeyEnd);
99   /// FindKey - Look up the bucket that contains the specified key. If it exists
100   /// in the map, return the bucket number of the key.  Otherwise return -1.
101   /// This does not modify the map.
102   int FindKey(const char *KeyStart, const char *KeyEnd) const;
104   /// RemoveKey - Remove the specified StringMapEntry from the table, but do not
105   /// delete it.  This aborts if the value isn't in the table.
106   void RemoveKey(StringMapEntryBase *V);
108   /// RemoveKey - Remove the StringMapEntry for the specified key from the
109   /// table, returning it.  If the key is not in the table, this returns null.
110   StringMapEntryBase *RemoveKey(const char *KeyStart, const char *KeyEnd);
111 private:
112   void init(unsigned Size);
113 public:
114   static StringMapEntryBase *getTombstoneVal() {
115     return (StringMapEntryBase*)-1;
116   }
118   unsigned getNumBuckets() const { return NumBuckets; }
119   unsigned getNumItems() const { return NumItems; }
121   bool empty() const { return NumItems == 0; }
122   unsigned size() const { return NumItems; }
123 };
125 /// StringMapEntry - This is used to represent one value that is inserted into
126 /// a StringMap.  It contains the Value itself and the key: the string length
127 /// and data.
128 template<typename ValueTy>
129 class StringMapEntry : public StringMapEntryBase {
130 public:
131   ValueTy second;
133   explicit StringMapEntry(unsigned StrLen)
134     : StringMapEntryBase(StrLen), second() {}
135   StringMapEntry(unsigned StrLen, const ValueTy &V)
136     : StringMapEntryBase(StrLen), second(V) {}
138   const ValueTy &getValue() const { return second; }
139   ValueTy &getValue() { return second; }
141   void setValue(const ValueTy &V) { second = V; }
143   /// getKeyData - Return the start of the string data that is the key for this
144   /// value.  The string data is always stored immediately after the
145   /// StringMapEntry object.
146   const char *getKeyData() const {return reinterpret_cast<const char*>(this+1);}
148   const char *first() const { return getKeyData(); }
150   /// Create - Create a StringMapEntry for the specified key and default
151   /// construct the value.
152   template<typename AllocatorTy, typename InitType>
153   static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd,
154                                 AllocatorTy &Allocator,
155                                 InitType InitVal) {
156     unsigned KeyLength = static_cast<unsigned>(KeyEnd-KeyStart);
158     // Okay, the item doesn't already exist, and 'Bucket' is the bucket to fill
159     // in.  Allocate a new item with space for the string at the end and a null
160     // terminator.
162     unsigned AllocSize = static_cast<unsigned>(sizeof(StringMapEntry))+
163       KeyLength+1;
164     unsigned Alignment = alignof<StringMapEntry>();
166     StringMapEntry *NewItem =
167       static_cast<StringMapEntry*>(Allocator.Allocate(AllocSize,Alignment));
169     // Default construct the value.
170     new (NewItem) StringMapEntry(KeyLength);
172     // Copy the string information.
173     char *StrBuffer = const_cast<char*>(NewItem->getKeyData());
174     memcpy(StrBuffer, KeyStart, KeyLength);
175     StrBuffer[KeyLength] = 0;  // Null terminate for convenience of clients.
177     // Initialize the value if the client wants to.
178     StringMapEntryInitializer<ValueTy>::Initialize(*NewItem, InitVal);
179     return NewItem;
180   }
182   template<typename AllocatorTy>
183   static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd,
184                                 AllocatorTy &Allocator) {
185     return Create(KeyStart, KeyEnd, Allocator, (void*)0);
186   }
189   /// Create - Create a StringMapEntry with normal malloc/free.
190   template<typename InitType>
191   static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd,
192                                 InitType InitVal) {
193     MallocAllocator A;
194     return Create(KeyStart, KeyEnd, A, InitVal);
195   }
197   static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd) {
198     return Create(KeyStart, KeyEnd, (void*)0);
199   }
201   /// GetStringMapEntryFromValue - Given a value that is known to be embedded
202   /// into a StringMapEntry, return the StringMapEntry itself.
203   static StringMapEntry &GetStringMapEntryFromValue(ValueTy &V) {
204     StringMapEntry *EPtr = 0;
205     char *Ptr = reinterpret_cast<char*>(&V) -
206                   (reinterpret_cast<char*>(&EPtr->second) -
207                    reinterpret_cast<char*>(EPtr));
208     return *reinterpret_cast<StringMapEntry*>(Ptr);
209   }
210   static const StringMapEntry &GetStringMapEntryFromValue(const ValueTy &V) {
211     return GetStringMapEntryFromValue(const_cast<ValueTy&>(V));
212   }
214   /// Destroy - Destroy this StringMapEntry, releasing memory back to the
215   /// specified allocator.
216   template<typename AllocatorTy>
217   void Destroy(AllocatorTy &Allocator) {
218     // Free memory referenced by the item.
219     this->~StringMapEntry();
220     Allocator.Deallocate(this);
221   }
223   /// Destroy this object, releasing memory back to the malloc allocator.
224   void Destroy() {
225     MallocAllocator A;
226     Destroy(A);
227   }
228 };
231 /// StringMap - This is an unconventional map that is specialized for handling
232 /// keys that are "strings", which are basically ranges of bytes. This does some
233 /// funky memory allocation and hashing things to make it extremely efficient,
234 /// storing the string data *after* the value in the map.
235 template<typename ValueTy, typename AllocatorTy = MallocAllocator>
236 class StringMap : public StringMapImpl {
237   AllocatorTy Allocator;
238   typedef StringMapEntry<ValueTy> MapEntryTy;
239 public:
240   StringMap() : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))) {}
241   explicit StringMap(unsigned InitialSize)
242     : StringMapImpl(InitialSize, static_cast<unsigned>(sizeof(MapEntryTy))) {}
244   AllocatorTy &getAllocator() { return Allocator; }
245   const AllocatorTy &getAllocator() const { return Allocator; }
247   typedef const char* key_type;
248   typedef ValueTy mapped_type;
249   typedef StringMapEntry<ValueTy> value_type;
250   typedef size_t size_type;
252   typedef StringMapConstIterator<ValueTy> const_iterator;
253   typedef StringMapIterator<ValueTy> iterator;
255   iterator begin() {
256     return iterator(TheTable, NumBuckets == 0);
257   }
258   iterator end() {
259     return iterator(TheTable+NumBuckets, true);
260   }
261   const_iterator begin() const {
262     return const_iterator(TheTable, NumBuckets == 0);
263   }
264   const_iterator end() const {
265     return const_iterator(TheTable+NumBuckets, true);
266   }
268   iterator find(const char *KeyStart, const char *KeyEnd) {
269     int Bucket = FindKey(KeyStart, KeyEnd);
270     if (Bucket == -1) return end();
271     return iterator(TheTable+Bucket);
272   }
273   iterator find(const char *Key) {
274     return find(Key, Key + strlen(Key));
275   }
276   iterator find(const std::string &Key) {
277     const char* key_start = (Key.empty() ? NULL : &Key[0]);
278     return find(key_start, key_start + Key.size());
279   }
281   const_iterator find(const char *KeyStart, const char *KeyEnd) const {
282     int Bucket = FindKey(KeyStart, KeyEnd);
283     if (Bucket == -1) return end();
284     return const_iterator(TheTable+Bucket);
285   }
286   const_iterator find(const char *Key) const {
287     return find(Key, Key + strlen(Key));
288   }
289   const_iterator find(const std::string &Key) const {
290     const char* key_start = (Key.empty() ? NULL : &Key[0]);
291     return find(key_start, key_start + Key.size());
292   }
294   ValueTy& operator[](const char *Key) {
295     value_type& entry = GetOrCreateValue(Key, Key + strlen(Key));
296     return entry.getValue();
297   }
298   ValueTy& operator[](const std::string &Key) {
299     const char* key_start = (Key.empty() ? NULL : &Key[0]);
300     value_type& entry = GetOrCreateValue(key_start, key_start + Key.size());
301     return entry.getValue();
302   }
304   size_type count(const char *KeyStart, const char *KeyEnd) const {
305     return find(KeyStart, KeyEnd) == end() ? 0 : 1;
306   }
307   size_type count(const char *Key) const {
308     return count(Key, Key + strlen(Key));
309   }
310   size_type count(const std::string &Key) const {
311     const char* key_start = (Key.empty() ? NULL : &Key[0]);
312     return count(key_start, key_start + Key.size());
313   }
315   /// insert - Insert the specified key/value pair into the map.  If the key
316   /// already exists in the map, return false and ignore the request, otherwise
317   /// insert it and return true.
318   bool insert(MapEntryTy *KeyValue) {
319     unsigned BucketNo =
320       LookupBucketFor(KeyValue->getKeyData(),
321                       KeyValue->getKeyData()+KeyValue->getKeyLength());
322     ItemBucket &Bucket = TheTable[BucketNo];
323     if (Bucket.Item && Bucket.Item != getTombstoneVal())
324       return false;  // Already exists in map.
326     if (Bucket.Item == getTombstoneVal())
327       --NumTombstones;
328     Bucket.Item = KeyValue;
329     ++NumItems;
331     if (ShouldRehash())
332       RehashTable();
333     return true;
334   }
336   /// GetOrCreateValue - Look up the specified key in the table.  If a value
337   /// exists, return it.  Otherwise, default construct a value, insert it, and
338   /// return.
339   template <typename InitTy>
340   StringMapEntry<ValueTy> &GetOrCreateValue(const char *KeyStart,
341                                             const char *KeyEnd,
342                                             InitTy Val) {
343     unsigned BucketNo = LookupBucketFor(KeyStart, KeyEnd);
344     ItemBucket &Bucket = TheTable[BucketNo];
345     if (Bucket.Item && Bucket.Item != getTombstoneVal())
346       return *static_cast<MapEntryTy*>(Bucket.Item);
348     MapEntryTy *NewItem = MapEntryTy::Create(KeyStart, KeyEnd, Allocator, Val);
350     if (Bucket.Item == getTombstoneVal())
351       --NumTombstones;
352     ++NumItems;
354     // Fill in the bucket for the hash table.  The FullHashValue was already
355     // filled in by LookupBucketFor.
356     Bucket.Item = NewItem;
358     if (ShouldRehash())
359       RehashTable();
360     return *NewItem;
361   }
363   StringMapEntry<ValueTy> &GetOrCreateValue(const char *KeyStart,
364                                             const char *KeyEnd) {
365     return GetOrCreateValue(KeyStart, KeyEnd, (void*)0);
366   }
368   /// remove - Remove the specified key/value pair from the map, but do not
369   /// erase it.  This aborts if the key is not in the map.
370   void remove(MapEntryTy *KeyValue) {
371     RemoveKey(KeyValue);
372   }
374   void erase(iterator I) {
375     MapEntryTy &V = *I;
376     remove(&V);
377     V.Destroy(Allocator);
378   }
380   bool erase(const char *Key) {
381     iterator I = find(Key);
382     if (I == end()) return false;
383     erase(I);
384     return true;
385   }
387   bool erase(std::string Key) {
388     iterator I = find(Key);
389     if (I == end()) return false;
390     erase(I);
391     return true;
392   }
394   ~StringMap() {
395     for (ItemBucket *I = TheTable, *E = TheTable+NumBuckets; I != E; ++I) {
396       if (I->Item && I->Item != getTombstoneVal())
397         static_cast<MapEntryTy*>(I->Item)->Destroy(Allocator);
398     }
399     free(TheTable);
400   }
401 private:
402   StringMap(const StringMap &);  // FIXME: Implement.
403   void operator=(const StringMap &);  // FIXME: Implement.
404 };
407 template<typename ValueTy>
408 class StringMapConstIterator {
409 protected:
410   StringMapImpl::ItemBucket *Ptr;
411 public:
412   explicit StringMapConstIterator(StringMapImpl::ItemBucket *Bucket,
413                                   bool NoAdvance = false)
414   : Ptr(Bucket) {
415     if (!NoAdvance) AdvancePastEmptyBuckets();
416   }
418   const StringMapEntry<ValueTy> &operator*() const {
419     return *static_cast<StringMapEntry<ValueTy>*>(Ptr->Item);
420   }
421   const StringMapEntry<ValueTy> *operator->() const {
422     return static_cast<StringMapEntry<ValueTy>*>(Ptr->Item);
423   }
425   bool operator==(const StringMapConstIterator &RHS) const {
426     return Ptr == RHS.Ptr;
427   }
428   bool operator!=(const StringMapConstIterator &RHS) const {
429     return Ptr != RHS.Ptr;
430   }
432   inline StringMapConstIterator& operator++() {          // Preincrement
433     ++Ptr;
434     AdvancePastEmptyBuckets();
435     return *this;
436   }
437   StringMapConstIterator operator++(int) {        // Postincrement
438     StringMapConstIterator tmp = *this; ++*this; return tmp;
439   }
441 private:
442   void AdvancePastEmptyBuckets() {
443     while (Ptr->Item == 0 || Ptr->Item == StringMapImpl::getTombstoneVal())
444       ++Ptr;
445   }
446 };
448 template<typename ValueTy>
449 class StringMapIterator : public StringMapConstIterator<ValueTy> {
450 public:
451   explicit StringMapIterator(StringMapImpl::ItemBucket *Bucket,
452                              bool NoAdvance = false)
453     : StringMapConstIterator<ValueTy>(Bucket, NoAdvance) {
454   }
455   StringMapEntry<ValueTy> &operator*() const {
456     return *static_cast<StringMapEntry<ValueTy>*>(this->Ptr->Item);
457   }
458   StringMapEntry<ValueTy> *operator->() const {
459     return static_cast<StringMapEntry<ValueTy>*>(this->Ptr->Item);
460   }
461 };
465 #endif