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;
25 template<typename ValueTy>
28 /// StringMapEntryInitializer - This datatype can be partially specialized for
29 /// various datatypes in a stringmap to allow them to be initialized when an
30 /// entry is default constructed for the map.
31 template<typename ValueTy>
32 class StringMapEntryInitializer {
34 template <typename InitTy>
35 static void Initialize(StringMapEntry<ValueTy> &T, InitTy InitVal) {
40 /// StringMapEntryBase - Shared base class of StringMapEntry instances.
41 class StringMapEntryBase {
44 explicit StringMapEntryBase(unsigned Len) : StrLen(Len) {}
46 unsigned getKeyLength() const { return StrLen; }
49 /// StringMapImpl - This is the base class of StringMap that is shared among
50 /// all of its instantiations.
53 /// ItemBucket - The hash table consists of an array of these. If Item is
54 /// non-null, this is an extant entry, otherwise, it is a hole.
56 /// FullHashValue - This remembers the full hash value of the key for
58 unsigned FullHashValue;
60 /// Item - This is a pointer to the actual item object.
61 StringMapEntryBase *Item;
68 unsigned NumTombstones;
71 explicit StringMapImpl(unsigned itemSize) : ItemSize(itemSize) {
72 // Initialize the map with zero buckets to allocation.
78 StringMapImpl(unsigned InitSize, unsigned ItemSize);
81 /// ShouldRehash - Return true if the table should be rehashed after a new
82 /// element was recently inserted.
83 bool ShouldRehash() const {
84 // If the hash table is now more than 3/4 full, or if fewer than 1/8 of
85 // the buckets are empty (meaning that many are filled with tombstones),
87 return NumItems*4 > NumBuckets*3 ||
88 NumBuckets-(NumItems+NumTombstones) < NumBuckets/8;
91 /// LookupBucketFor - Look up the bucket that the specified string should end
92 /// up in. If it already exists as a key in the map, the Item pointer for the
93 /// specified bucket will be non-null. Otherwise, it will be null. In either
94 /// case, the FullHashValue field of the bucket will be set to the hash value
96 unsigned LookupBucketFor(const char *KeyStart, const char *KeyEnd);
98 /// FindKey - Look up the bucket that contains the specified key. If it exists
99 /// in the map, return the bucket number of the key. Otherwise return -1.
100 /// This does not modify the map.
101 int FindKey(const char *KeyStart, const char *KeyEnd) const;
103 /// RemoveKey - Remove the specified StringMapEntry from the table, but do not
104 /// delete it. This aborts if the value isn't in the table.
105 void RemoveKey(StringMapEntryBase *V);
107 /// RemoveKey - Remove the StringMapEntry for the specified key from the
108 /// table, returning it. If the key is not in the table, this returns null.
109 StringMapEntryBase *RemoveKey(const char *KeyStart, const char *KeyEnd);
111 void init(unsigned Size);
113 static StringMapEntryBase *getTombstoneVal() {
114 return (StringMapEntryBase*)-1;
117 unsigned getNumBuckets() const { return NumBuckets; }
118 unsigned getNumItems() const { return NumItems; }
120 bool empty() const { return NumItems == 0; }
121 unsigned size() const { return NumItems; }
124 /// StringMapEntry - This is used to represent one value that is inserted into
125 /// a StringMap. It contains the Value itself and the key: the string length
127 template<typename ValueTy>
128 class StringMapEntry : public StringMapEntryBase {
131 explicit StringMapEntry(unsigned StrLen)
132 : StringMapEntryBase(StrLen), Val() {}
133 StringMapEntry(unsigned StrLen, const ValueTy &V)
134 : StringMapEntryBase(StrLen), Val(V) {}
136 const ValueTy &getValue() const { return Val; }
137 ValueTy &getValue() { return Val; }
139 void setValue(const ValueTy &V) { Val = V; }
141 /// getKeyData - Return the start of the string data that is the key for this
142 /// value. The string data is always stored immediately after the
143 /// StringMapEntry object.
144 const char *getKeyData() const {return reinterpret_cast<const char*>(this+1);}
146 /// Create - Create a StringMapEntry for the specified key and default
147 /// construct the value.
148 template<typename AllocatorTy, typename InitType>
149 static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd,
150 AllocatorTy &Allocator,
152 unsigned KeyLength = KeyEnd-KeyStart;
154 // Okay, the item doesn't already exist, and 'Bucket' is the bucket to fill
155 // in. Allocate a new item with space for the string at the end and a null
158 unsigned AllocSize = sizeof(StringMapEntry)+KeyLength+1;
159 unsigned Alignment = alignof<StringMapEntry>();
161 StringMapEntry *NewItem =
162 static_cast<StringMapEntry*>(Allocator.Allocate(AllocSize,Alignment));
164 // Default construct the value.
165 new (NewItem) StringMapEntry(KeyLength);
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.
172 // Initialize the value if the client wants to.
173 StringMapEntryInitializer<ValueTy>::Initialize(*NewItem, InitVal);
177 template<typename AllocatorTy>
178 static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd,
179 AllocatorTy &Allocator) {
180 return Create(KeyStart, KeyEnd, Allocator, (void*)0);
184 /// Create - Create a StringMapEntry with normal malloc/free.
185 template<typename InitType>
186 static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd,
189 return Create(KeyStart, KeyEnd, A, InitVal);
192 static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd) {
193 return Create(KeyStart, KeyEnd, (void*)0);
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->Val) -
202 reinterpret_cast<char*>(EPtr));
203 return *reinterpret_cast<StringMapEntry*>(Ptr);
205 static const StringMapEntry &GetStringMapEntryFromValue(const ValueTy &V) {
206 return GetStringMapEntryFromValue(const_cast<ValueTy&>(V));
209 /// Destroy - Destroy this StringMapEntry, releasing memory back to the
210 /// specified allocator.
211 template<typename AllocatorTy>
212 void Destroy(AllocatorTy &Allocator) {
213 // Free memory referenced by the item.
214 this->~StringMapEntry();
215 Allocator.Deallocate(this);
218 /// Destroy this object, releasing memory back to the malloc allocator.
226 /// StringMap - This is an unconventional map that is specialized for handling
227 /// keys that are "strings", which are basically ranges of bytes. This does some
228 /// funky memory allocation and hashing things to make it extremely efficient,
229 /// storing the string data *after* the value in the map.
230 template<typename ValueTy, typename AllocatorTy = MallocAllocator>
231 class StringMap : public StringMapImpl {
232 AllocatorTy Allocator;
233 typedef StringMapEntry<ValueTy> MapEntryTy;
235 StringMap() : StringMapImpl(sizeof(MapEntryTy)) {}
236 explicit StringMap(unsigned InitialSize)
237 : StringMapImpl(InitialSize, sizeof(MapEntryTy)) {}
239 AllocatorTy &getAllocator() { return Allocator; }
240 const AllocatorTy &getAllocator() const { return Allocator; }
242 typedef StringMapConstIterator<ValueTy> const_iterator;
243 typedef StringMapIterator<ValueTy> iterator;
246 return iterator(TheTable, NumBuckets == 0);
249 return iterator(TheTable+NumBuckets, true);
251 const_iterator begin() const {
252 return const_iterator(TheTable, NumBuckets == 0);
254 const_iterator end() const {
255 return const_iterator(TheTable+NumBuckets, true);
258 iterator find(const char *KeyStart, const char *KeyEnd) {
259 int Bucket = FindKey(KeyStart, KeyEnd);
260 if (Bucket == -1) return end();
261 return iterator(TheTable+Bucket);
264 const_iterator find(const char *KeyStart, const char *KeyEnd) const {
265 int Bucket = FindKey(KeyStart, KeyEnd);
266 if (Bucket == -1) return end();
267 return const_iterator(TheTable+Bucket);
270 /// insert - Insert the specified key/value pair into the map. If the key
271 /// already exists in the map, return false and ignore the request, otherwise
272 /// insert it and return true.
273 bool insert(MapEntryTy *KeyValue) {
275 LookupBucketFor(KeyValue->getKeyData(),
276 KeyValue->getKeyData()+KeyValue->getKeyLength());
277 ItemBucket &Bucket = TheTable[BucketNo];
278 if (Bucket.Item && Bucket.Item != getTombstoneVal())
279 return false; // Already exists in map.
281 if (Bucket.Item == getTombstoneVal())
283 Bucket.Item = KeyValue;
291 /// GetOrCreateValue - Look up the specified key in the table. If a value
292 /// exists, return it. Otherwise, default construct a value, insert it, and
294 template <typename InitTy>
295 StringMapEntry<ValueTy> &GetOrCreateValue(const char *KeyStart,
298 unsigned BucketNo = LookupBucketFor(KeyStart, KeyEnd);
299 ItemBucket &Bucket = TheTable[BucketNo];
300 if (Bucket.Item && Bucket.Item != getTombstoneVal())
301 return *static_cast<MapEntryTy*>(Bucket.Item);
303 MapEntryTy *NewItem = MapEntryTy::Create(KeyStart, KeyEnd, Allocator, Val);
305 if (Bucket.Item == getTombstoneVal())
309 // Fill in the bucket for the hash table. The FullHashValue was already
310 // filled in by LookupBucketFor.
311 Bucket.Item = NewItem;
318 StringMapEntry<ValueTy> &GetOrCreateValue(const char *KeyStart,
319 const char *KeyEnd) {
320 return GetOrCreateValue(KeyStart, KeyEnd, (void*)0);
323 /// remove - Remove the specified key/value pair from the map, but do not
324 /// erase it. This aborts if the key is not in the map.
325 void remove(MapEntryTy *KeyValue) {
329 void erase(iterator I) {
332 V.Destroy(Allocator);
336 for (ItemBucket *I = TheTable, *E = TheTable+NumBuckets; I != E; ++I) {
337 if (I->Item && I->Item != getTombstoneVal())
338 static_cast<MapEntryTy*>(I->Item)->Destroy(Allocator);
343 StringMap(const StringMap &); // FIXME: Implement.
344 void operator=(const StringMap &); // FIXME: Implement.
348 template<typename ValueTy>
349 class StringMapConstIterator {
351 StringMapImpl::ItemBucket *Ptr;
353 explicit StringMapConstIterator(StringMapImpl::ItemBucket *Bucket,
354 bool NoAdvance = false)
356 if (!NoAdvance) AdvancePastEmptyBuckets();
359 const StringMapEntry<ValueTy> &operator*() const {
360 return *static_cast<StringMapEntry<ValueTy>*>(Ptr->Item);
362 const StringMapEntry<ValueTy> *operator->() const {
363 return static_cast<StringMapEntry<ValueTy>*>(Ptr->Item);
366 bool operator==(const StringMapConstIterator &RHS) const {
367 return Ptr == RHS.Ptr;
369 bool operator!=(const StringMapConstIterator &RHS) const {
370 return Ptr != RHS.Ptr;
373 inline StringMapConstIterator& operator++() { // Preincrement
375 AdvancePastEmptyBuckets();
378 StringMapConstIterator operator++(int) { // Postincrement
379 StringMapConstIterator tmp = *this; ++*this; return tmp;
383 void AdvancePastEmptyBuckets() {
384 while (Ptr->Item == 0 || Ptr->Item == StringMapImpl::getTombstoneVal())
389 template<typename ValueTy>
390 class StringMapIterator : public StringMapConstIterator<ValueTy> {
392 StringMapIterator(StringMapImpl::ItemBucket *Bucket,
393 bool NoAdvance = false)
394 : StringMapConstIterator<ValueTy>(Bucket, NoAdvance) {
396 StringMapEntry<ValueTy> &operator*() const {
397 return *static_cast<StringMapEntry<ValueTy>*>(this->Ptr->Item);
399 StringMapEntry<ValueTy> *operator->() const {
400 return static_cast<StringMapEntry<ValueTy>*>(this->Ptr->Item);