Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 4110a7a

Browse files
committed
Add StringMap::insert(pair) consistent with the standard associative container concept.
Patch by Agustín Bergé. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211309 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 594f05c commit 4110a7a

File tree

3 files changed

+70
-21
lines changed

3 files changed

+70
-21
lines changed

include/llvm/ADT/StringMap.h

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class StringMapImpl {
6464
}
6565

6666
StringMapImpl(unsigned InitSize, unsigned ItemSize);
67-
void RehashTable();
67+
unsigned RehashTable(unsigned BucketNo = 0);
6868

6969
/// LookupBucketFor - Look up the bucket that the specified string should end
7070
/// up in. If it already exists as a key in the map, the Item pointer for the
@@ -323,6 +323,28 @@ class StringMap : public StringMapImpl {
323323
return true;
324324
}
325325

326+
/// insert - Inserts the specified key/value pair into the map if the key
327+
/// isn't already in the map. The bool component of the returned pair is true
328+
/// if and only if the insertion takes place, and the iterator component of
329+
/// the pair points to the element with key equivalent to the key of the pair.
330+
std::pair<iterator, bool> insert(std::pair<StringRef, ValueTy> KV) {
331+
unsigned BucketNo = LookupBucketFor(KV.first);
332+
StringMapEntryBase *&Bucket = TheTable[BucketNo];
333+
if (Bucket && Bucket != getTombstoneVal())
334+
return std::make_pair(iterator(TheTable + BucketNo, false),
335+
false); // Already exists in map.
336+
337+
if (Bucket == getTombstoneVal())
338+
--NumTombstones;
339+
Bucket =
340+
MapEntryTy::Create(KV.first, Allocator, std::move(KV.second));
341+
++NumItems;
342+
assert(NumItems + NumTombstones <= NumBuckets);
343+
344+
BucketNo = RehashTable(BucketNo);
345+
return std::make_pair(iterator(TheTable + BucketNo, false), true);
346+
}
347+
326348
// clear - Empties out the StringMap
327349
void clear() {
328350
if (empty()) return;
@@ -346,24 +368,7 @@ class StringMap : public StringMapImpl {
346368
/// return.
347369
template <typename InitTy>
348370
MapEntryTy &GetOrCreateValue(StringRef Key, InitTy Val) {
349-
unsigned BucketNo = LookupBucketFor(Key);
350-
StringMapEntryBase *&Bucket = TheTable[BucketNo];
351-
if (Bucket && Bucket != getTombstoneVal())
352-
return *static_cast<MapEntryTy*>(Bucket);
353-
354-
MapEntryTy *NewItem = MapEntryTy::Create(Key, Allocator, std::move(Val));
355-
356-
if (Bucket == getTombstoneVal())
357-
--NumTombstones;
358-
++NumItems;
359-
assert(NumItems + NumTombstones <= NumBuckets);
360-
361-
// Fill in the bucket for the hash table. The FullHashValue was already
362-
// filled in by LookupBucketFor.
363-
Bucket = NewItem;
364-
365-
RehashTable();
366-
return *NewItem;
371+
return *insert(std::make_pair(Key, std::move(Val))).first;
367372
}
368373

369374
MapEntryTy &GetOrCreateValue(StringRef Key) {

lib/Support/StringMap.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ StringMapEntryBase *StringMapImpl::RemoveKey(StringRef Key) {
181181

182182
/// RehashTable - Grow the table, redistributing values into the buckets with
183183
/// the appropriate mod-of-hashtable-size.
184-
void StringMapImpl::RehashTable() {
184+
unsigned StringMapImpl::RehashTable(unsigned BucketNo) {
185185
unsigned NewSize;
186186
unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
187187

@@ -193,9 +193,10 @@ void StringMapImpl::RehashTable() {
193193
} else if (NumBuckets-(NumItems+NumTombstones) <= NumBuckets/8) {
194194
NewSize = NumBuckets;
195195
} else {
196-
return;
196+
return BucketNo;
197197
}
198198

199+
unsigned NewBucketNo = BucketNo;
199200
// Allocate one extra bucket which will always be non-empty. This allows the
200201
// iterators to stop at end.
201202
StringMapEntryBase **NewTableArray =
@@ -215,6 +216,8 @@ void StringMapImpl::RehashTable() {
215216
if (!NewTableArray[NewBucket]) {
216217
NewTableArray[FullHash & (NewSize-1)] = Bucket;
217218
NewHashArray[FullHash & (NewSize-1)] = FullHash;
219+
if (I == BucketNo)
220+
NewBucketNo = NewBucket;
218221
continue;
219222
}
220223

@@ -227,6 +230,8 @@ void StringMapImpl::RehashTable() {
227230
// Finally found a slot. Fill it in.
228231
NewTableArray[NewBucket] = Bucket;
229232
NewHashArray[NewBucket] = FullHash;
233+
if (I == BucketNo)
234+
NewBucketNo = NewBucket;
230235
}
231236
}
232237

@@ -235,4 +240,5 @@ void StringMapImpl::RehashTable() {
235240
TheTable = NewTableArray;
236241
NumBuckets = NewSize;
237242
NumTombstones = 0;
243+
return NewBucketNo;
238244
}

unittests/ADT/StringMapTest.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "gtest/gtest.h"
1111
#include "llvm/ADT/StringMap.h"
1212
#include "llvm/Support/DataTypes.h"
13+
#include <tuple>
1314
using namespace llvm;
1415

1516
namespace {
@@ -203,6 +204,43 @@ TEST_F(StringMapTest, InsertTest) {
203204
assertSingleItemMap();
204205
}
205206

207+
// Test insert(pair<K, V>) method
208+
TEST_F(StringMapTest, InsertPairTest) {
209+
bool Inserted;
210+
StringMap<uint32_t>::iterator NewIt;
211+
std::tie(NewIt, Inserted) =
212+
testMap.insert(std::make_pair(testKeyFirst, testValue));
213+
EXPECT_EQ(1u, testMap.size());
214+
EXPECT_EQ(testValue, testMap[testKeyFirst]);
215+
EXPECT_EQ(testKeyFirst, NewIt->first());
216+
EXPECT_EQ(testValue, NewIt->second);
217+
EXPECT_TRUE(Inserted);
218+
219+
StringMap<uint32_t>::iterator ExistingIt;
220+
std::tie(ExistingIt, Inserted) =
221+
testMap.insert(std::make_pair(testKeyFirst, testValue + 1));
222+
EXPECT_EQ(1u, testMap.size());
223+
EXPECT_EQ(testValue, testMap[testKeyFirst]);
224+
EXPECT_FALSE(Inserted);
225+
EXPECT_EQ(NewIt, ExistingIt);
226+
}
227+
228+
// Test insert(pair<K, V>) method when rehashing occurs
229+
TEST_F(StringMapTest, InsertRehashingPairTest) {
230+
// Check that the correct iterator is returned when the inserted element is
231+
// moved to a different bucket during internal rehashing. This depends on
232+
// the particular key, and the implementation of StringMap and HashString.
233+
// Changes to those might result in this test not actually checking that.
234+
StringMap<uint32_t> t(1);
235+
EXPECT_EQ(1u, t.getNumBuckets());
236+
237+
StringMap<uint32_t>::iterator It =
238+
t.insert(std::make_pair("abcdef", 42)).first;
239+
EXPECT_EQ(2u, t.getNumBuckets());
240+
EXPECT_EQ("abcdef", It->first());
241+
EXPECT_EQ(42u, It->second);
242+
}
243+
206244
// Create a non-default constructable value
207245
struct StringMapTestStruct {
208246
StringMapTestStruct(int i) : i(i) {}

0 commit comments

Comments
 (0)