Split StringMapEntry construction out of StringMap, into StringMapEntry.
[oota-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 was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source 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/Support/Allocator.h"
18 #include <cstring>
19
20 namespace llvm {
21   
22 /// StringMapEntryBase - Shared base class of StringMapEntry instances.
23 class StringMapEntryBase {
24   unsigned StrLen;
25 public:
26   StringMapEntryBase(unsigned Len) : StrLen(Len) {}
27   
28   unsigned getKeyLength() const { return StrLen; }
29 };
30   
31 /// StringMapVisitor - Subclasses of this class may be implemented to walk all
32 /// of the items in a StringMap.
33 class StringMapVisitor {
34 public:
35   virtual ~StringMapVisitor();
36   virtual void Visit(const char *Key, StringMapEntryBase *Value) const = 0;
37 };
38
39 /// StringMapImpl - This is the base class of StringMap that is shared among
40 /// all of its instantiations.
41 class StringMapImpl {
42 protected:
43   /// ItemBucket - The hash table consists of an array of these.  If Item is
44   /// non-null, this is an extant entry, otherwise, it is a hole.
45   struct ItemBucket {
46     /// FullHashValue - This remembers the full hash value of the key for
47     /// easy scanning.
48     unsigned FullHashValue;
49     
50     /// Item - This is a pointer to the actual item object.
51     StringMapEntryBase *Item;
52   };
53   
54   ItemBucket *TheTable;
55   unsigned NumBuckets;
56   unsigned NumItems;
57   unsigned ItemSize;
58 protected:
59   StringMapImpl(unsigned InitSize, unsigned ItemSize);
60   void RehashTable();
61   
62   /// LookupBucketFor - Look up the bucket that the specified string should end
63   /// up in.  If it already exists as a key in the map, the Item pointer for the
64   /// specified bucket will be non-null.  Otherwise, it will be null.  In either
65   /// case, the FullHashValue field of the bucket will be set to the hash value
66   /// of the string.
67   unsigned LookupBucketFor(const char *KeyStart, const char *KeyEnd);
68   
69 public:
70   unsigned getNumBuckets() const { return NumBuckets; }
71   unsigned getNumItems() const { return NumItems; }
72
73   void VisitEntries(const StringMapVisitor &Visitor) const;
74 };
75
76 /// StringMapEntry - This is used to represent one value that is inserted into
77 /// a StringMap.  It contains the Value itself and the key: the string length
78 /// and data.
79 template<typename ValueTy>
80 class StringMapEntry : public StringMapEntryBase {
81   ValueTy Val;
82 public:
83   StringMapEntry(unsigned StrLen)
84     : StringMapEntryBase(StrLen), Val() {}
85   StringMapEntry(unsigned StrLen, const ValueTy &V)
86     : StringMapEntryBase(StrLen), Val(V) {}
87
88   const ValueTy &getValue() const { return Val; }
89   ValueTy &getValue() { return Val; }
90   
91   void setValue(const ValueTy &V) { Val = V; }
92   
93   /// getKeyData - Return the start of the string data that is the key for this
94   /// value.  The string data is always stored immediately after the
95   /// StringMapEntry object.
96   const char *getKeyData() const {return reinterpret_cast<const char*>(this+1);}
97   
98   /// Create - Create a StringMapEntry for the specified key and default
99   /// construct the value.
100   template<typename AllocatorTy>
101   static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd,
102                                 AllocatorTy &Allocator) {
103     unsigned KeyLength = KeyEnd-KeyStart;
104     
105     // Okay, the item doesn't already exist, and 'Bucket' is the bucket to fill
106     // in.  Allocate a new item with space for the string at the end and a null
107     // terminator.
108     unsigned AllocSize = sizeof(StringMapEntry)+KeyLength+1;
109     
110 #ifdef __GNUC__
111     unsigned Alignment = __alignof__(StringMapEntry);
112 #else
113     // FIXME: ugly.
114     unsigned Alignment = 8;
115 #endif
116     StringMapEntry *NewItem = 
117       static_cast<StringMapEntry*>(Allocator.Allocate(AllocSize, Alignment));
118     
119     // Default construct the value.
120     new (NewItem) StringMapEntry(KeyLength);
121     
122     // Copy the string information.
123     char *StrBuffer = const_cast<char*>(NewItem->getKeyData());
124     memcpy(StrBuffer, KeyStart, KeyLength);
125     StrBuffer[KeyLength] = 0;  // Null terminate for convenience of clients.
126     return NewItem;
127   }
128   
129   /// Create - Create a StringMapEntry with normal malloc/free.
130   static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd) {
131     MallocAllocator A;
132     return Create(KeyStart, KeyEnd, A);
133   }
134
135   /// Destroy - Destroy this StringMapEntry, releasing memory back to the
136   /// specified allocator.
137   template<typename AllocatorTy>
138   void Destroy(AllocatorTy &Allocator) {
139     // Free memory referenced by the item.
140     this->~StringMapEntry();
141     Allocator.Deallocate(this);
142   }
143   
144   /// Destroy this object, releasing memory back to the malloc allocator.
145   void Destroy() {
146     MallocAllocator A;
147     Destroy(A);
148   }
149 };
150
151
152 /// StringMap - This is an unconventional map that is specialized for handling
153 /// keys that are "strings", which are basically ranges of bytes. This does some
154 /// funky memory allocation and hashing things to make it extremely efficient,
155 /// storing the string data *after* the value in the map.
156 template<typename ValueTy, typename AllocatorTy = MallocAllocator>
157 class StringMap : public StringMapImpl {
158   AllocatorTy Allocator;
159   typedef StringMapEntry<ValueTy> MapEntryTy;
160 public:
161   StringMap(unsigned InitialSize = 0)
162     : StringMapImpl(InitialSize, sizeof(MapEntryTy)) {}
163   
164   AllocatorTy &getAllocator() { return Allocator; }
165   const AllocatorTy &getAllocator() const { return Allocator; }
166
167   /// FindValue - Look up the specified key in the map.  If it exists, return a
168   /// pointer to the element, otherwise return null.
169   MapEntryTy *FindValue(const char *KeyStart, const char *KeyEnd) {
170     unsigned BucketNo = LookupBucketFor(KeyStart, KeyEnd);
171     return static_cast<MapEntryTy*>(TheTable[BucketNo].Item);
172   }
173   
174   /// GetOrCreateValue - Look up the specified key in the table.  If a value
175   /// exists, return it.  Otherwise, default construct a value, insert it, and
176   /// return.
177   StringMapEntry<ValueTy> &GetOrCreateValue(const char *KeyStart, 
178                                             const char *KeyEnd) {
179     unsigned BucketNo = LookupBucketFor(KeyStart, KeyEnd);
180     ItemBucket &Bucket = TheTable[BucketNo];
181     if (Bucket.Item)
182       return *static_cast<MapEntryTy*>(Bucket.Item);
183     
184     MapEntryTy *NewItem = MapEntryTy::Create(KeyStart, KeyEnd, Allocator);
185     ++NumItems;
186     
187     // Fill in the bucket for the hash table.  The FullHashValue was already
188     // filled in by LookupBucketFor.
189     Bucket.Item = NewItem;
190     
191     // If the hash table is now more than 3/4 full, rehash into a larger table.
192     if (NumItems > NumBuckets*3/4)
193       RehashTable();
194     return *NewItem;
195   }
196   
197   ~StringMap() {
198     for (ItemBucket *I = TheTable, *E = TheTable+NumBuckets; I != E; ++I) {
199       if (MapEntryTy *Id = static_cast<MapEntryTy*>(I->Item))
200         Id->Destroy(Allocator);
201     }
202     delete [] TheTable;
203   }
204 };
205   
206 }
207
208 #endif
209