1 //===--- StringMap.h - String Hash table map interface ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file defines the StringMap class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_STRINGMAP_H
15 #define LLVM_ADT_STRINGMAP_H
17 #include "llvm/Support/Allocator.h"
21 template<typename ValueT>
22 class StringMapConstIterator;
23 template<typename ValueT>
24 class StringMapIterator;
27 /// StringMapEntryBase - Shared base class of StringMapEntry instances.
28 class StringMapEntryBase {
31 StringMapEntryBase(unsigned Len) : StrLen(Len) {}
33 unsigned getKeyLength() const { return StrLen; }
36 /// StringMapImpl - This is the base class of StringMap that is shared among
37 /// all of its instantiations.
40 /// ItemBucket - The hash table consists of an array of these. If Item is
41 /// non-null, this is an extant entry, otherwise, it is a hole.
43 /// FullHashValue - This remembers the full hash value of the key for
45 unsigned FullHashValue;
47 /// Item - This is a pointer to the actual item object.
48 StringMapEntryBase *Item;
55 unsigned NumTombstones;
58 StringMapImpl(unsigned InitSize, unsigned ItemSize);
61 /// LookupBucketFor - Look up the bucket that the specified string should end
62 /// up in. If it already exists as a key in the map, the Item pointer for the
63 /// specified bucket will be non-null. Otherwise, it will be null. In either
64 /// case, the FullHashValue field of the bucket will be set to the hash value
66 unsigned LookupBucketFor(const char *KeyStart, const char *KeyEnd);
68 /// FindKey - Look up the bucket that contains the specified key. If it exists
69 /// in the map, return the bucket number of the key. Otherwise return -1.
70 /// This does not modify the map.
71 int FindKey(const char *KeyStart, const char *KeyEnd) const;
73 /// RemoveKey - Remove the specified StringMapEntry from the table, but do not
74 /// delete it. This aborts if the value isn't in the table.
75 void RemoveKey(StringMapEntryBase *V);
77 /// RemoveKey - Remove the StringMapEntry for the specified key from the
78 /// table, returning it. If the key is not in the table, this returns null.
79 StringMapEntryBase *RemoveKey(const char *KeyStart, const char *KeyEnd);
82 static StringMapEntryBase *getTombstoneVal() {
83 return (StringMapEntryBase*)-1;
86 unsigned getNumBuckets() const { return NumBuckets; }
87 unsigned getNumItems() const { return NumItems; }
89 bool empty() const { return NumItems == 0; }
90 unsigned size() const { return NumItems; }
93 /// StringMapEntry - This is used to represent one value that is inserted into
94 /// a StringMap. It contains the Value itself and the key: the string length
96 template<typename ValueTy>
97 class StringMapEntry : public StringMapEntryBase {
100 StringMapEntry(unsigned StrLen)
101 : StringMapEntryBase(StrLen), Val() {}
102 StringMapEntry(unsigned StrLen, const ValueTy &V)
103 : StringMapEntryBase(StrLen), Val(V) {}
105 const ValueTy &getValue() const { return Val; }
106 ValueTy &getValue() { return Val; }
108 void setValue(const ValueTy &V) { Val = V; }
110 /// getKeyData - Return the start of the string data that is the key for this
111 /// value. The string data is always stored immediately after the
112 /// StringMapEntry object.
113 const char *getKeyData() const {return reinterpret_cast<const char*>(this+1);}
115 /// Create - Create a StringMapEntry for the specified key and default
116 /// construct the value.
117 template<typename AllocatorTy>
118 static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd,
119 AllocatorTy &Allocator) {
120 unsigned KeyLength = KeyEnd-KeyStart;
122 // Okay, the item doesn't already exist, and 'Bucket' is the bucket to fill
123 // in. Allocate a new item with space for the string at the end and a null
125 unsigned AllocSize = sizeof(StringMapEntry)+KeyLength+1;
128 unsigned Alignment = __alignof__(StringMapEntry);
131 unsigned Alignment = 8;
133 StringMapEntry *NewItem =
134 static_cast<StringMapEntry*>(Allocator.Allocate(AllocSize, Alignment));
136 // Default construct the value.
137 new (NewItem) StringMapEntry(KeyLength);
139 // Copy the string information.
140 char *StrBuffer = const_cast<char*>(NewItem->getKeyData());
141 memcpy(StrBuffer, KeyStart, KeyLength);
142 StrBuffer[KeyLength] = 0; // Null terminate for convenience of clients.
146 /// Create - Create a StringMapEntry with normal malloc/free.
147 static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd) {
149 return Create(KeyStart, KeyEnd, A);
152 /// Destroy - Destroy this StringMapEntry, releasing memory back to the
153 /// specified allocator.
154 template<typename AllocatorTy>
155 void Destroy(AllocatorTy &Allocator) {
156 // Free memory referenced by the item.
157 this->~StringMapEntry();
158 Allocator.Deallocate(this);
161 /// Destroy this object, releasing memory back to the malloc allocator.
169 /// StringMap - This is an unconventional map that is specialized for handling
170 /// keys that are "strings", which are basically ranges of bytes. This does some
171 /// funky memory allocation and hashing things to make it extremely efficient,
172 /// storing the string data *after* the value in the map.
173 template<typename ValueTy, typename AllocatorTy = MallocAllocator>
174 class StringMap : public StringMapImpl {
175 AllocatorTy Allocator;
176 typedef StringMapEntry<ValueTy> MapEntryTy;
178 StringMap(unsigned InitialSize = 0)
179 : StringMapImpl(InitialSize, sizeof(MapEntryTy)) {}
181 AllocatorTy &getAllocator() { return Allocator; }
182 const AllocatorTy &getAllocator() const { return Allocator; }
184 typedef StringMapConstIterator<ValueTy> const_iterator;
185 typedef StringMapIterator<ValueTy> iterator;
187 iterator begin() { return iterator(TheTable); }
188 iterator end() { return iterator(TheTable+NumBuckets); }
189 const_iterator begin() const { return const_iterator(TheTable); }
190 const_iterator end() const { return const_iterator(TheTable+NumBuckets); }
193 iterator find(const char *KeyStart, const char *KeyEnd) {
194 int Bucket = FindKey(KeyStart, KeyEnd);
195 if (Bucket == -1) return end();
196 return iterator(TheTable+Bucket);
199 const_iterator find(const char *KeyStart, const char *KeyEnd) const {
200 int Bucket = FindKey(KeyStart, KeyEnd);
201 if (Bucket == -1) return end();
202 return const_iterator(TheTable+Bucket);
205 /// insert - Insert the specified key/value pair into the map. If the key
206 /// already exists in the map, return false and ignore the request, otherwise
207 /// insert it and return true.
208 bool insert(MapEntryTy *KeyValue) {
210 LookupBucketFor(KeyValue->getKeyData(),
211 KeyValue->getKeyData()+KeyValue->getKeyLength());
212 ItemBucket &Bucket = TheTable[BucketNo];
213 if (Bucket.Item && Bucket.Item != getTombstoneVal())
214 return false; // Already exists in map.
216 if (Bucket.Item == getTombstoneVal())
218 Bucket.Item = KeyValue;
221 // If the hash table is now more than 3/4 full, rehash into a larger table.
222 if (NumItems > NumBuckets*3/4)
227 /// GetOrCreateValue - Look up the specified key in the table. If a value
228 /// exists, return it. Otherwise, default construct a value, insert it, and
230 StringMapEntry<ValueTy> &GetOrCreateValue(const char *KeyStart,
231 const char *KeyEnd) {
232 unsigned BucketNo = LookupBucketFor(KeyStart, KeyEnd);
233 ItemBucket &Bucket = TheTable[BucketNo];
234 if (Bucket.Item && Bucket.Item != getTombstoneVal())
235 return *static_cast<MapEntryTy*>(Bucket.Item);
237 MapEntryTy *NewItem = MapEntryTy::Create(KeyStart, KeyEnd, Allocator);
239 if (Bucket.Item == getTombstoneVal())
243 // Fill in the bucket for the hash table. The FullHashValue was already
244 // filled in by LookupBucketFor.
245 Bucket.Item = NewItem;
247 // If the hash table is now more than 3/4 full, rehash into a larger table.
248 if (NumItems > NumBuckets*3/4)
253 /// remove - Remove the specified key/value pair from the map, but do not
254 /// erase it. This aborts if the key is not in the map.
255 void remove(MapEntryTy *KeyValue) {
260 for (ItemBucket *I = TheTable, *E = TheTable+NumBuckets; I != E; ++I) {
261 if (MapEntryTy *Id = static_cast<MapEntryTy*>(I->Item))
262 Id->Destroy(Allocator);
269 template<typename ValueTy>
270 class StringMapConstIterator {
272 StringMapImpl::ItemBucket *Ptr;
274 StringMapConstIterator(StringMapImpl::ItemBucket *Bucket) : Ptr(Bucket) {
275 AdvancePastEmptyBuckets();
278 const StringMapEntry<ValueTy> &operator*() const {
279 return *static_cast<StringMapEntry<ValueTy>*>(Ptr->Item);
281 const StringMapEntry<ValueTy> *operator->() const {
282 return static_cast<StringMapEntry<ValueTy>*>(Ptr->Item);
285 bool operator==(const StringMapConstIterator &RHS) const {
286 return Ptr == RHS.Ptr;
288 bool operator!=(const StringMapConstIterator &RHS) const {
289 return Ptr != RHS.Ptr;
292 inline StringMapConstIterator& operator++() { // Preincrement
294 AdvancePastEmptyBuckets();
297 StringMapConstIterator operator++(int) { // Postincrement
298 StringMapConstIterator tmp = *this; ++*this; return tmp;
302 void AdvancePastEmptyBuckets() {
303 while (Ptr->Item == 0 || Ptr->Item == StringMapImpl::getTombstoneVal())
308 template<typename ValueTy>
309 class StringMapIterator : public StringMapConstIterator<ValueTy> {
311 StringMapIterator(StringMapImpl::ItemBucket *Bucket)
312 : StringMapConstIterator<ValueTy>(Bucket) {
314 StringMapEntry<ValueTy> &operator*() const {
315 return *static_cast<StringMapEntry<ValueTy>*>(this->Ptr->Item);
317 StringMapEntry<ValueTy> *operator->() const {
318 return static_cast<StringMapEntry<ValueTy>*>(this->Ptr->Item);