llvm.org GIT mirror llvm / 794a014
Extend StringMap to support being initialized as completely empty. When initialized this way, they do not do a malloc to allocate their buckets. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@35642 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 13 years ago
2 changed file(s) with 46 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
5454 unsigned NumTombstones;
5555 unsigned ItemSize;
5656 protected:
57 StringMapImpl(unsigned itemSize) : ItemSize(itemSize) { init(16); }
5758 StringMapImpl(unsigned InitSize, unsigned ItemSize);
5859 void RehashTable();
5960
8687 /// RemoveKey - Remove the StringMapEntry for the specified key from the
8788 /// table, returning it. If the key is not in the table, this returns null.
8889 StringMapEntryBase *RemoveKey(const char *KeyStart, const char *KeyEnd);
89
90 private:
91 void init(unsigned Size);
9092 public:
9193 static StringMapEntryBase *getTombstoneVal() {
9294 return (StringMapEntryBase*)-1;
184186 AllocatorTy Allocator;
185187 typedef StringMapEntry MapEntryTy;
186188 public:
187 StringMap(unsigned InitialSize = 0)
189 StringMap() : StringMapImpl(sizeof(MapEntryTy)) {}
190 StringMap(unsigned InitialSize)
188191 : StringMapImpl(InitialSize, sizeof(MapEntryTy)) {}
189192
190193 AllocatorTy &getAllocator() { return Allocator; }
193196 typedef StringMapConstIterator const_iterator;
194197 typedef StringMapIterator iterator;
195198
196 iterator begin() { return iterator(TheTable); }
197 iterator end() { return iterator(TheTable+NumBuckets); }
198 const_iterator begin() const { return const_iterator(TheTable); }
199 const_iterator end() const { return const_iterator(TheTable+NumBuckets); }
200
199 iterator begin() {
200 return iterator(TheTable, NumBuckets == 0);
201 }
202 iterator end() {
203 return iterator(TheTable+NumBuckets, true);
204 }
205 const_iterator begin() const {
206 return const_iterator(TheTable, NumBuckets == 0);
207 }
208 const_iterator end() const {
209 return const_iterator(TheTable+NumBuckets, true);
210 }
201211
202212 iterator find(const char *KeyStart, const char *KeyEnd) {
203213 int Bucket = FindKey(KeyStart, KeyEnd);
278288 protected:
279289 StringMapImpl::ItemBucket *Ptr;
280290 public:
281 StringMapConstIterator(StringMapImpl::ItemBucket *Bucket) : Ptr(Bucket) {
282 AdvancePastEmptyBuckets();
291 StringMapConstIterator(StringMapImpl::ItemBucket *Bucket,
292 bool NoAdvance = false)
293 : Ptr(Bucket) {
294 if (!NoAdvance) AdvancePastEmptyBuckets();
283295 }
284296
285297 const StringMapEntry &operator*() const {
315327 template
316328 class StringMapIterator : public StringMapConstIterator {
317329 public:
318 StringMapIterator(StringMapImpl::ItemBucket *Bucket)
319 : StringMapConstIterator(Bucket) {
330 StringMapIterator(StringMapImpl::ItemBucket *Bucket,
331 bool NoAdvance = false)
332 : StringMapConstIterator(Bucket, NoAdvance) {
320333 }
321334 StringMapEntry &operator*() const {
322335 return *static_cast*>(this->Ptr->Item);
1515 using namespace llvm;
1616
1717 StringMapImpl::StringMapImpl(unsigned InitSize, unsigned itemSize) {
18 ItemSize = itemSize;
19
20 // If a size is specified, initialize the table with that many buckets.
21 if (InitSize) {
22 init(InitSize);
23 return;
24 }
25
26 // Otherwise, initialize it with zero buckets to avoid the allocation.
27 TheTable = 0;
28 NumBuckets = 0;
29 NumItems = 0;
30 NumTombstones = 0;
31 }
32
33 void StringMapImpl::init(unsigned InitSize) {
1834 assert((InitSize & (InitSize-1)) == 0 &&
1935 "Init Size must be a power of 2 or zero!");
2036 NumBuckets = InitSize ? InitSize : 16;
21 ItemSize = itemSize;
2237 NumItems = 0;
2338 NumTombstones = 0;
2439
5166 /// case, the FullHashValue field of the bucket will be set to the hash value
5267 /// of the string.
5368 unsigned StringMapImpl::LookupBucketFor(const char *NameStart,
54 const char *NameEnd) {
69 const char *NameEnd) {
5570 unsigned HTSize = NumBuckets;
71 if (HTSize == 0) { // Hash table unallocated so far?
72 init(16);
73 HTSize = NumBuckets;
74 }
5675 unsigned FullHashValue = HashString(NameStart, NameEnd);
5776 unsigned BucketNo = FullHashValue & (HTSize-1);
5877
109128 /// This does not modify the map.
110129 int StringMapImpl::FindKey(const char *KeyStart, const char *KeyEnd) const {
111130 unsigned HTSize = NumBuckets;
131 if (HTSize == 0) return -1; // Really empty table?
112132 unsigned FullHashValue = HashString(KeyStart, KeyEnd);
113133 unsigned BucketNo = FullHashValue & (HTSize-1);
114134