1 //===--- CStringMap.h - CString 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 CStringMap class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_CSTRINGMAP_H
15 #define LLVM_ADT_CSTRINGMAP_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 /// CStringMapVisitor - Subclasses of this class may be implemented to walk all
32 /// of the items in a CStringMap.
33 class CStringMapVisitor {
35 virtual ~CStringMapVisitor();
36 virtual void Visit(const char *Key, StringMapEntryBase *Value) const = 0;
39 /// CStringMapImpl - This is the base class of CStringMap that is shared among
40 /// all of its instantiations.
41 class CStringMapImpl {
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 CStringMapImpl(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 CStringMapVisitor &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);}
100 /// CStringMap - This is an unconventional map that is specialized for handling
101 /// keys that are "strings", which are basically ranges of bytes. This does some
102 /// funky memory allocation and hashing things to make it extremely efficient,
103 /// storing the string data *after* the value in the map.
104 template<typename ValueTy, typename AllocatorTy = MallocAllocator>
105 class CStringMap : public CStringMapImpl {
106 AllocatorTy Allocator;
107 typedef StringMapEntry<ValueTy> MapEntryTy;
109 CStringMap(unsigned InitialSize = 0)
110 : CStringMapImpl(InitialSize, sizeof(MapEntryTy)) {}
112 AllocatorTy &getAllocator() { return Allocator; }
113 const AllocatorTy &getAllocator() const { return Allocator; }
115 /// FindValue - Look up the specified key in the map. If it exists, return a
116 /// pointer to the element, otherwise return null.
117 MapEntryTy *FindValue(const char *KeyStart, const char *KeyEnd) {
118 unsigned BucketNo = LookupBucketFor(KeyStart, KeyEnd);
119 return static_cast<MapEntryTy*>(TheTable[BucketNo].Item);
122 /// GetOrCreateValue - Look up the specified key in the table. If a value
123 /// exists, return it. Otherwise, default construct a value, insert it, and
125 StringMapEntry<ValueTy> &GetOrCreateValue(const char *KeyStart,
126 const char *KeyEnd) {
127 unsigned BucketNo = LookupBucketFor(KeyStart, KeyEnd);
128 ItemBucket &Bucket = TheTable[BucketNo];
130 return *static_cast<MapEntryTy*>(Bucket.Item);
132 unsigned KeyLength = KeyEnd-KeyStart;
134 // Okay, the item doesn't already exist, and 'Bucket' is the bucket to fill
135 // in. Allocate a new item with space for the string at the end and a null
137 unsigned AllocSize = sizeof(MapEntryTy)+KeyLength+1;
140 unsigned Alignment = __alignof__(MapEntryTy);
143 unsigned Alignment = 8;
145 MapEntryTy *NewItem =
146 static_cast<MapEntryTy*>(Allocator.Allocate(AllocSize, Alignment));
148 // Default construct the value.
149 new (NewItem) MapEntryTy(KeyLength);
152 // Copy the string information.
153 char *StrBuffer = const_cast<char*>(NewItem->getKeyData());
154 memcpy(StrBuffer, KeyStart, KeyLength);
155 StrBuffer[KeyLength] = 0; // Null terminate for convenience of clients.
157 // Fill in the bucket for the hash table. The FullHashValue was already
158 // filled in by LookupBucketFor.
159 Bucket.Item = NewItem;
161 // If the hash table is now more than 3/4 full, rehash into a larger table.
162 if (NumItems > NumBuckets*3/4)
168 for (ItemBucket *I = TheTable, *E = TheTable+NumBuckets; I != E; ++I) {
169 if (MapEntryTy *Id = static_cast<MapEntryTy*>(I->Item)) {
170 // Free memory referenced by the item.
172 Allocator.Deallocate(Id);