llvm.org GIT mirror llvm / aec7870
provide an optional API to allow datatypes in a stringmap to be *gasp* initialized with a value if they want, by specializing the StringMapEntryInitializer class. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44430 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 12 years ago
1 changed file(s) with 42 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
2222 template
2323 class StringMapIterator;
2424
25 /// StringMapEntryInitializer - This datatype can be partially specialized for
26 /// various datatypes in a stringmap to allow them to be initialized when an
27 /// entry is default constructed for the map.
28 template
29 class StringMapEntryInitializer {
30 public:
31 template
32 static void Initialize(ValueTy &T, InitTy InitVal) {
33 }
34 };
35
2536
2637 /// StringMapEntryBase - Shared base class of StringMapEntry instances.
2738 class StringMapEntryBase {
131142
132143 /// Create - Create a StringMapEntry for the specified key and default
133144 /// construct the value.
134 template>
145 template, typename InitType>
135146 static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd,
136 AllocatorTy &Allocator) {
147 AllocatorTy &Allocator,
148 InitType InitVal) {
137149 unsigned KeyLength = KeyEnd-KeyStart;
138150
139151 // Okay, the item doesn't already exist, and 'Bucket' is the bucket to fill
153165 char *StrBuffer = const_cast(NewItem->getKeyData());
154166 memcpy(StrBuffer, KeyStart, KeyLength);
155167 StrBuffer[KeyLength] = 0; // Null terminate for convenience of clients.
168
169 // Initialize the value if the client wants to.
170 StringMapEntryInitializer::Initialize(NewItem->getValue(),InitVal);
156171 return NewItem;
157172 }
158173
174 template
175 static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd,
176 AllocatorTy &Allocator) {
177 return Create(KeyStart, KeyEnd, Allocator, (void*)0);
178 }
179
180
159181 /// Create - Create a StringMapEntry with normal malloc/free.
182 template
183 static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd,
184 InitType InitVal) {
185 MallocAllocator A;
186 return Create(KeyStart, KeyEnd, A, InitVal);
187 }
188
160189 static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd) {
161 MallocAllocator A;
162 return Create(KeyStart, KeyEnd, A);
163 }
164
190 return Create(KeyStart, KeyEnd, (void*)0);
191 }
165192
166193 /// GetStringMapEntryFromValue - Given a value that is known to be embedded
167194 /// into a StringMapEntry, return the StringMapEntry itself.
261288 /// GetOrCreateValue - Look up the specified key in the table. If a value
262289 /// exists, return it. Otherwise, default construct a value, insert it, and
263290 /// return.
291 template
264292 StringMapEntry &GetOrCreateValue(const char *KeyStart,
265 const char *KeyEnd) {
293 const char *KeyEnd,
294 InitTy Val) {
266295 unsigned BucketNo = LookupBucketFor(KeyStart, KeyEnd);
267296 ItemBucket &Bucket = TheTable[BucketNo];
268297 if (Bucket.Item && Bucket.Item != getTombstoneVal())
269298 return *static_cast(Bucket.Item);
270299
271 MapEntryTy *NewItem = MapEntryTy::Create(KeyStart, KeyEnd, Allocator);
300 MapEntryTy *NewItem = MapEntryTy::Create(KeyStart, KeyEnd, Allocator, Val);
272301
273302 if (Bucket.Item == getTombstoneVal())
274303 --NumTombstones;
281310 if (ShouldRehash())
282311 RehashTable();
283312 return *NewItem;
313 }
314
315 StringMapEntry &GetOrCreateValue(const char *KeyStart,
316 const char *KeyEnd) {
317 return GetOrCreateValue(KeyStart, KeyEnd, (void*)0);
284318 }
285319
286320 /// remove - Remove the specified key/value pair from the map, but do not