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;
26 /// StringMapEntryInitializer - This datatype can be partially specialized for
27 /// various datatypes in a stringmap to allow them to be initialized when an
28 /// entry is default constructed for the map.
29 template<typename ValueTy>
30 class StringMapEntryInitializer {
32 template <typename InitTy>
33 static void Initialize(ValueTy &T, InitTy InitVal) {
38 /// StringMapEntryBase - Shared base class of StringMapEntry instances.
39 class StringMapEntryBase {
42 explicit StringMapEntryBase(unsigned Len) : StrLen(Len) {}
44 unsigned getKeyLength() const { return StrLen; }
47 /// StringMapImpl - This is the base class of StringMap that is shared among
48 /// all of its instantiations.
51 /// ItemBucket - The hash table consists of an array of these. If Item is
52 /// non-null, this is an extant entry, otherwise, it is a hole.
54 /// FullHashValue - This remembers the full hash value of the key for
56 unsigned FullHashValue;
58 /// Item - This is a pointer to the actual item object.
59 StringMapEntryBase *Item;
66 unsigned NumTombstones;
69 explicit StringMapImpl(unsigned itemSize) : ItemSize(itemSize) {
70 // Initialize the map with zero buckets to allocation.
76 StringMapImpl(unsigned InitSize, unsigned ItemSize);
79 /// ShouldRehash - Return true if the table should be rehashed after a new
80 /// element was recently inserted.
81 bool ShouldRehash() const {
82 // If the hash table is now more than 3/4 full, or if fewer than 1/8 of
83 // the buckets are empty (meaning that many are filled with tombstones),
85 return NumItems*4 > NumBuckets*3 ||
86 NumBuckets-(NumItems+NumTombstones) < NumBuckets/8;
89 /// LookupBucketFor - Look up the bucket that the specified string should end
90 /// up in. If it already exists as a key in the map, the Item pointer for the
91 /// specified bucket will be non-null. Otherwise, it will be null. In either
92 /// case, the FullHashValue field of the bucket will be set to the hash value
94 unsigned LookupBucketFor(const char *KeyStart, const char *KeyEnd);
96 /// FindKey - Look up the bucket that contains the specified key. If it exists
97 /// in the map, return the bucket number of the key. Otherwise return -1.
98 /// This does not modify the map.
99 int FindKey(const char *KeyStart, const char *KeyEnd) const;
101 /// RemoveKey - Remove the specified StringMapEntry from the table, but do not
102 /// delete it. This aborts if the value isn't in the table.
103 void RemoveKey(StringMapEntryBase *V);
105 /// RemoveKey - Remove the StringMapEntry for the specified key from the
106 /// table, returning it. If the key is not in the table, this returns null.
107 StringMapEntryBase *RemoveKey(const char *KeyStart, const char *KeyEnd);
109 void init(unsigned Size);
111 static StringMapEntryBase *getTombstoneVal() {
112 return (StringMapEntryBase*)-1;
115 unsigned getNumBuckets() const { return NumBuckets; }
116 unsigned getNumItems() const { return NumItems; }
118 bool empty() const { return NumItems == 0; }
119 unsigned size() const { return NumItems; }
122 /// StringMapEntry - This is used to represent one value that is inserted into
123 /// a StringMap. It contains the Value itself and the key: the string length
125 template<typename ValueTy>
126 class StringMapEntry : public StringMapEntryBase {
129 explicit StringMapEntry(unsigned StrLen)
130 : StringMapEntryBase(StrLen), Val() {}
131 StringMapEntry(unsigned StrLen, const ValueTy &V)
132 : StringMapEntryBase(StrLen), Val(V) {}
134 const ValueTy &getValue() const { return Val; }
135 ValueTy &getValue() { return Val; }
137 void setValue(const ValueTy &V) { Val = V; }
139 /// getKeyData - Return the start of the string data that is the key for this
140 /// value. The string data is always stored immediately after the
141 /// StringMapEntry object.
142 const char *getKeyData() const {return reinterpret_cast<const char*>(this+1);}
144 /// Create - Create a StringMapEntry for the specified key and default
145 /// construct the value.
146 template<typename AllocatorTy, typename InitType>
147 static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd,
148 AllocatorTy &Allocator,
150 unsigned KeyLength = KeyEnd-KeyStart;
152 // Okay, the item doesn't already exist, and 'Bucket' is the bucket to fill
153 // in. Allocate a new item with space for the string at the end and a null
156 unsigned AllocSize = sizeof(StringMapEntry)+KeyLength+1;
157 unsigned Alignment = alignof<StringMapEntry>();
159 StringMapEntry *NewItem =
160 static_cast<StringMapEntry*>(Allocator.Allocate(AllocSize,Alignment));
162 // Default construct the value.
163 new (NewItem) StringMapEntry(KeyLength);
165 // Copy the string information.
166 char *StrBuffer = const_cast<char*>(NewItem->getKeyData());
167 memcpy(StrBuffer, KeyStart, KeyLength);
168 StrBuffer[KeyLength] = 0; // Null terminate for convenience of clients.
170 // Initialize the value if the client wants to.
171 StringMapEntryInitializer<ValueTy>::Initialize(NewItem->getValue(),InitVal);
175 template<typename AllocatorTy>
176 static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd,
177 AllocatorTy &Allocator) {
178 return Create(KeyStart, KeyEnd, Allocator, (void*)0);
182 /// Create - Create a StringMapEntry with normal malloc/free.
183 template<typename InitType>
184 static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd,
187 return Create(KeyStart, KeyEnd, A, InitVal);
190 static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd) {
191 return Create(KeyStart, KeyEnd, (void*)0);
194 /// GetStringMapEntryFromValue - Given a value that is known to be embedded
195 /// into a StringMapEntry, return the StringMapEntry itself.
196 static StringMapEntry &GetStringMapEntryFromValue(ValueTy &V) {
197 StringMapEntry *EPtr = 0;
198 char *Ptr = reinterpret_cast<char*>(&V) -
199 (reinterpret_cast<char*>(&EPtr->Val) -
200 reinterpret_cast<char*>(EPtr));
201 return *reinterpret_cast<StringMapEntry*>(Ptr);
203 static const StringMapEntry &GetStringMapEntryFromValue(const ValueTy &V) {
204 return GetStringMapEntryFromValue(const_cast<ValueTy&>(V));
207 /// Destroy - Destroy this StringMapEntry, releasing memory back to the
208 /// specified allocator.
209 template<typename AllocatorTy>
210 void Destroy(AllocatorTy &Allocator) {
211 // Free memory referenced by the item.
212 this->~StringMapEntry();
213 Allocator.Deallocate(this);
216 /// Destroy this object, releasing memory back to the malloc allocator.
224 /// StringMap - This is an unconventional map that is specialized for handling
225 /// keys that are "strings", which are basically ranges of bytes. This does some
226 /// funky memory allocation and hashing things to make it extremely efficient,
227 /// storing the string data *after* the value in the map.
228 template<typename ValueTy, typename AllocatorTy = MallocAllocator>
229 class StringMap : public StringMapImpl {
230 AllocatorTy Allocator;
231 typedef StringMapEntry<ValueTy> MapEntryTy;
233 StringMap() : StringMapImpl(sizeof(MapEntryTy)) {}
234 explicit StringMap(unsigned InitialSize)
235 : StringMapImpl(InitialSize, sizeof(MapEntryTy)) {}
237 AllocatorTy &getAllocator() { return Allocator; }
238 const AllocatorTy &getAllocator() const { return Allocator; }
240 typedef StringMapConstIterator<ValueTy> const_iterator;
241 typedef StringMapIterator<ValueTy> iterator;
244 return iterator(TheTable, NumBuckets == 0);
247 return iterator(TheTable+NumBuckets, true);
249 const_iterator begin() const {
250 return const_iterator(TheTable, NumBuckets == 0);
252 const_iterator end() const {
253 return const_iterator(TheTable+NumBuckets, true);
256 iterator find(const char *KeyStart, const char *KeyEnd) {
257 int Bucket = FindKey(KeyStart, KeyEnd);
258 if (Bucket == -1) return end();
259 return iterator(TheTable+Bucket);
262 const_iterator find(const char *KeyStart, const char *KeyEnd) const {
263 int Bucket = FindKey(KeyStart, KeyEnd);
264 if (Bucket == -1) return end();
265 return const_iterator(TheTable+Bucket);
268 /// insert - Insert the specified key/value pair into the map. If the key
269 /// already exists in the map, return false and ignore the request, otherwise
270 /// insert it and return true.
271 bool insert(MapEntryTy *KeyValue) {
273 LookupBucketFor(KeyValue->getKeyData(),
274 KeyValue->getKeyData()+KeyValue->getKeyLength());
275 ItemBucket &Bucket = TheTable[BucketNo];
276 if (Bucket.Item && Bucket.Item != getTombstoneVal())
277 return false; // Already exists in map.
279 if (Bucket.Item == getTombstoneVal())
281 Bucket.Item = KeyValue;
289 /// GetOrCreateValue - Look up the specified key in the table. If a value
290 /// exists, return it. Otherwise, default construct a value, insert it, and
292 template <typename InitTy>
293 StringMapEntry<ValueTy> &GetOrCreateValue(const char *KeyStart,
296 unsigned BucketNo = LookupBucketFor(KeyStart, KeyEnd);
297 ItemBucket &Bucket = TheTable[BucketNo];
298 if (Bucket.Item && Bucket.Item != getTombstoneVal())
299 return *static_cast<MapEntryTy*>(Bucket.Item);
301 MapEntryTy *NewItem = MapEntryTy::Create(KeyStart, KeyEnd, Allocator, Val);
303 if (Bucket.Item == getTombstoneVal())
307 // Fill in the bucket for the hash table. The FullHashValue was already
308 // filled in by LookupBucketFor.
309 Bucket.Item = NewItem;
316 StringMapEntry<ValueTy> &GetOrCreateValue(const char *KeyStart,
317 const char *KeyEnd) {
318 return GetOrCreateValue(KeyStart, KeyEnd, (void*)0);
321 /// remove - Remove the specified key/value pair from the map, but do not
322 /// erase it. This aborts if the key is not in the map.
323 void remove(MapEntryTy *KeyValue) {
327 void erase(iterator I) {
330 V.Destroy(Allocator);
334 for (ItemBucket *I = TheTable, *E = TheTable+NumBuckets; I != E; ++I) {
335 if (I->Item && I->Item != getTombstoneVal())
336 static_cast<MapEntryTy*>(I->Item)->Destroy(Allocator);
341 StringMap(const StringMap &); // FIXME: Implement.
342 void operator=(const StringMap &); // FIXME: Implement.
346 template<typename ValueTy>
347 class StringMapConstIterator {
349 StringMapImpl::ItemBucket *Ptr;
351 explicit StringMapConstIterator(StringMapImpl::ItemBucket *Bucket,
352 bool NoAdvance = false)
354 if (!NoAdvance) AdvancePastEmptyBuckets();
357 const StringMapEntry<ValueTy> &operator*() const {
358 return *static_cast<StringMapEntry<ValueTy>*>(Ptr->Item);
360 const StringMapEntry<ValueTy> *operator->() const {
361 return static_cast<StringMapEntry<ValueTy>*>(Ptr->Item);
364 bool operator==(const StringMapConstIterator &RHS) const {
365 return Ptr == RHS.Ptr;
367 bool operator!=(const StringMapConstIterator &RHS) const {
368 return Ptr != RHS.Ptr;
371 inline StringMapConstIterator& operator++() { // Preincrement
373 AdvancePastEmptyBuckets();
376 StringMapConstIterator operator++(int) { // Postincrement
377 StringMapConstIterator tmp = *this; ++*this; return tmp;
381 void AdvancePastEmptyBuckets() {
382 while (Ptr->Item == 0 || Ptr->Item == StringMapImpl::getTombstoneVal())
387 template<typename ValueTy>
388 class StringMapIterator : public StringMapConstIterator<ValueTy> {
390 StringMapIterator(StringMapImpl::ItemBucket *Bucket,
391 bool NoAdvance = false)
392 : StringMapConstIterator<ValueTy>(Bucket, NoAdvance) {
394 StringMapEntry<ValueTy> &operator*() const {
395 return *static_cast<StringMapEntry<ValueTy>*>(this->Ptr->Item);
397 StringMapEntry<ValueTy> *operator->() const {
398 return static_cast<StringMapEntry<ValueTy>*>(this->Ptr->Item);