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"
22 /// StringMapEntryBase - Shared base class of StringMapEntry instances.
23 class StringMapEntryBase {
26 StringMapEntryBase(unsigned Len) : StrLen(Len) {}
28 unsigned getKeyLength() const { return StrLen; }
31 /// StringMapVisitor - Subclasses of this class may be implemented to walk all
32 /// of the items in a StringMap.
33 class StringMapVisitor {
35 virtual ~StringMapVisitor();
36 virtual void Visit(const char *Key, StringMapEntryBase *Value) const = 0;
39 /// StringMapImpl - This is the base class of StringMap that is shared among
40 /// all of its instantiations.
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.
46 /// FullHashValue - This remembers the full hash value of the key for
48 unsigned FullHashValue;
50 /// Item - This is a pointer to the actual item object.
51 StringMapEntryBase *Item;
59 StringMapImpl(unsigned InitSize, unsigned ItemSize);
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
67 unsigned LookupBucketFor(const char *KeyStart, const char *KeyEnd);
70 unsigned getNumBuckets() const { return NumBuckets; }
71 unsigned getNumItems() const { return NumItems; }
73 void VisitEntries(const StringMapVisitor &Visitor) const;
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
79 template<typename ValueTy>
80 class StringMapEntry : public StringMapEntryBase {
83 StringMapEntry(unsigned StrLen)
84 : StringMapEntryBase(StrLen), Val() {}
85 StringMapEntry(unsigned StrLen, const ValueTy &V)
86 : StringMapEntryBase(StrLen), Val(V) {}
88 const ValueTy &getValue() const { return Val; }
89 ValueTy &getValue() { return Val; }
91 void setValue(const ValueTy &V) { Val = V; }
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);}
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;
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
108 unsigned AllocSize = sizeof(StringMapEntry)+KeyLength+1;
111 unsigned Alignment = __alignof__(StringMapEntry);
114 unsigned Alignment = 8;
116 StringMapEntry *NewItem =
117 static_cast<StringMapEntry*>(Allocator.Allocate(AllocSize, Alignment));
119 // Default construct the value.
120 new (NewItem) StringMapEntry(KeyLength);
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.
129 /// Create - Create a StringMapEntry with normal malloc/free.
130 static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd) {
132 return Create(KeyStart, KeyEnd, A);
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);
144 /// Destroy this object, releasing memory back to the malloc allocator.
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;
161 StringMap(unsigned InitialSize = 0)
162 : StringMapImpl(InitialSize, sizeof(MapEntryTy)) {}
164 AllocatorTy &getAllocator() { return Allocator; }
165 const AllocatorTy &getAllocator() const { return Allocator; }
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);
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
177 StringMapEntry<ValueTy> &GetOrCreateValue(const char *KeyStart,
178 const char *KeyEnd) {
179 unsigned BucketNo = LookupBucketFor(KeyStart, KeyEnd);
180 ItemBucket &Bucket = TheTable[BucketNo];
182 return *static_cast<MapEntryTy*>(Bucket.Item);
184 MapEntryTy *NewItem = MapEntryTy::Create(KeyStart, KeyEnd, Allocator);
187 // Fill in the bucket for the hash table. The FullHashValue was already
188 // filled in by LookupBucketFor.
189 Bucket.Item = NewItem;
191 // If the hash table is now more than 3/4 full, rehash into a larger table.
192 if (NumItems > NumBuckets*3/4)
198 for (ItemBucket *I = TheTable, *E = TheTable+NumBuckets; I != E; ++I) {
199 if (MapEntryTy *Id = static_cast<MapEntryTy*>(I->Item))
200 Id->Destroy(Allocator);