14 #ifndef LLVM_ADT_STRINGMAP_H
15 #define LLVM_ADT_STRINGMAP_H
23 template<
typename ValueT>
25 template<
typename ValueT>
27 template<
typename ValueTy>
60 RHS.TheTable =
nullptr;
63 RHS.NumTombstones = 0;
89 void init(
unsigned Size);
112 template<
typename ValueTy>
113 class StringMapEntry :
public StringMapEntryBase {
114 StringMapEntry(StringMapEntry &E) =
delete;
120 template <
class InitTy>
136 const char *
getKeyData()
const {
return reinterpret_cast<const char*
>(
this+1);}
142 template <
typename AllocatorTy,
typename InitType>
144 InitType &&InitVal) {
145 unsigned KeyLength = Key.
size();
149 unsigned AllocSize =
static_cast<unsigned>(
sizeof(
StringMapEntry))+
151 unsigned Alignment = alignOf<StringMapEntry>();
154 static_cast<StringMapEntry*
>(Allocator.Allocate(AllocSize,Alignment));
157 new (NewItem)
StringMapEntry(KeyLength, std::forward<InitType>(InitVal));
160 char *StrBuffer =
const_cast<char*
>(NewItem->getKeyData());
162 memcpy(StrBuffer, Key.
data(), KeyLength);
163 StrBuffer[KeyLength] = 0;
167 template<
typename AllocatorTy>
169 return Create(Key, Allocator, ValueTy());
173 template<
typename InitType>
176 return Create(Key, A, std::forward<InitType>(InitVal));
180 return Create(Key, ValueTy());
192 template<
typename AllocatorTy>
198 Allocator.Deallocate(static_cast<void *>(
this), AllocSize);
213 template<
typename ValueTy,
typename AllocatorTy = MallocAllocator>
215 AllocatorTy Allocator;
231 :
StringMapImpl(std::move(RHS)), Allocator(std::move(RHS.Allocator)) {}
267 if (Bucket == -1)
return end();
273 if (Bucket == -1)
return end();
287 return insert(std::make_pair(Key, ValueTy())).first->second;
292 return find(Key) ==
end() ? 0 : 1;
318 std::pair<iterator, bool>
insert(std::pair<StringRef, ValueTy> KV) {
345 static_cast<MapEntryTy*
>(Bucket)->Destroy(Allocator);
368 if (I ==
end())
return false;
381 static_cast<MapEntryTy*
>(Bucket)->Destroy(Allocator);
390 template<
typename ValueTy>
391 class StringMapConstIterator {
400 bool NoAdvance =
false)
402 if (!NoAdvance) AdvancePastEmptyBuckets();
413 return Ptr == RHS.Ptr;
416 return Ptr != RHS.Ptr;
421 AdvancePastEmptyBuckets();
429 void AdvancePastEmptyBuckets() {
435 template<
typename ValueTy>
436 class StringMapIterator :
public StringMapConstIterator<ValueTy> {
440 bool NoAdvance =
false)
StringMapEntryBase ** Ptr
const ValueTy & getValue() const
unsigned getNumItems() const
StringMapImpl(unsigned itemSize)
size_t size() const
size - Get the string size.
bool operator!=(const StringMapConstIterator &RHS) const
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
const_iterator find(StringRef Key) const
void setValue(const ValueTy &V)
StringMapEntryBase(unsigned Len)
unsigned RehashTable(unsigned BucketNo=0)
RehashTable - Grow the table, redistributing values into the buckets with the appropriate mod-of-hash...
static StringMapEntry * Create(StringRef Key, AllocatorTy &Allocator, InitType &&InitVal)
Create - Create a StringMapEntry for the specified key and default construct the value.
static StringMapEntry * Create(StringRef Key, InitType &&InitVal)
Create - Create a StringMapEntry with normal malloc/free.
StringMapEntry< ValueTy > value_type
int FindKey(StringRef Key) const
FindKey - Look up the bucket that contains the specified key.
static StringMapEntry * Create(StringRef Key, AllocatorTy &Allocator)
iterator find(StringRef Key)
unsigned LookupBucketFor(StringRef Key)
LookupBucketFor - Look up the bucket that the specified string should end up in.
StringMapIterator(StringMapEntryBase **Bucket, bool NoAdvance=false)
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
unsigned getNumBuckets() const
StringMap & operator=(StringMap RHS)
StringMapEntry< ValueTy > * operator->() const
static StringMapEntry & GetStringMapEntryFromKeyData(const char *KeyData)
GetStringMapEntryFromKeyData - Given key data that is known to be embedded into a StringMapEntry...
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
const AllocatorTy & getAllocator() const
StringMapImpl - This is the base class of StringMap that is shared among all of its instantiations...
void Destroy(AllocatorTy &Allocator)
Destroy - Destroy this StringMapEntry, releasing memory back to the specified allocator.
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
void swap(StringMapImpl &Other)
StringMapConstIterator< ValueTy > const_iterator
StringMap(StringMap &&RHS)
StringMapIterator< ValueTy > iterator
StringMapConstIterator operator++(int)
bool operator==(const StringMapConstIterator &RHS) const
StringMapEntryBase ** TheTable
StringMapEntry(unsigned strLen)
StringMapEntry< ValueTy > MapEntryTy
const_iterator end() const
void Destroy()
Destroy this object, releasing memory back to the malloc allocator.
StringMap(unsigned InitialSize)
const_iterator begin() const
StringMapConstIterator(StringMapEntryBase **Bucket, bool NoAdvance=false)
bool erase(StringRef Key)
ValueTy lookup(StringRef Key) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
AllocatorTy & getAllocator()
void RemoveKey(StringMapEntryBase *V)
RemoveKey - Remove the specified StringMapEntry from the table, but do not delete it...
std::pair< iterator, bool > insert(std::pair< StringRef, ValueTy > KV)
insert - Inserts the specified key/value pair into the map if the key isn't already in the map...
StringMapEntry< ValueTy > value_type
const char * getKeyData() const
getKeyData - Return the start of the string data that is the key for this value.
static StringMapEntryBase * getTombstoneVal()
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
StringMapConstIterator & operator++()
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
StringMap(unsigned InitialSize, AllocatorTy A)
StringMapEntry(unsigned strLen, InitTy &&V)
StringMapEntry< ValueTy > & operator*() const
const value_type * operator->() const
unsigned getKeyLength() const
static StringMapEntry * Create(StringRef Key)
StringMapImpl(StringMapImpl &&RHS)
const value_type & operator*() const
StringMapEntryBase - Shared base class of StringMapEntry instances.
StringRef - Represent a constant reference to a string, i.e.
ValueTy & operator[](StringRef Key)