diff options
author | Hal Finkel <hfinkel@anl.gov> | 2016-03-30 19:54:56 +0000 |
---|---|---|
committer | Hal Finkel <hfinkel@anl.gov> | 2016-03-30 19:54:56 +0000 |
commit | 38bf13d02ce7dda0a9fc77c94457da0be8cf5c1b (patch) | |
tree | fe83c137628fd918bdeac3b777dbc9c1d61af3ed /llvm/include | |
parent | 4709190376d2d29f5b26a69f540feac31444080a (diff) | |
download | bcm5719-llvm-38bf13d02ce7dda0a9fc77c94457da0be8cf5c1b.tar.gz bcm5719-llvm-38bf13d02ce7dda0a9fc77c94457da0be8cf5c1b.zip |
Add a copy constructor to StringMap
There is code under review that requires StringMap to have a copy constructor,
and this makes StringMap more consistent with our other containers (like
DenseMap) that have copy constructors.
Differential Revision: http://reviews.llvm.org/D18506
llvm-svn: 264906
Diffstat (limited to 'llvm/include')
-rw-r--r-- | llvm/include/llvm/ADT/StringMap.h | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/llvm/include/llvm/ADT/StringMap.h b/llvm/include/llvm/ADT/StringMap.h index 3a3ffc2aa32..ce80484738f 100644 --- a/llvm/include/llvm/ADT/StringMap.h +++ b/llvm/include/llvm/ADT/StringMap.h @@ -89,7 +89,8 @@ protected: /// table, returning it. If the key is not in the table, this returns null. StringMapEntryBase *RemoveKey(StringRef Key); -private: + /// Allocate the table with the specified number of buckets and otherwise + /// setup the map as empty. void init(unsigned Size); public: @@ -244,7 +245,39 @@ public: return *this; } - // FIXME: Implement copy operations if/when they're needed. + StringMap(const StringMap &RHS) : + StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))), + Allocator(RHS.Allocator) { + if (RHS.empty()) + return; + + // Allocate TheTable of the same size as RHS's TheTable, and set the + // sentinel appropriately (and NumBuckets). + init(RHS.NumBuckets); + unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1), + *RHSHashTable = (unsigned *)(RHS.TheTable + NumBuckets + 1); + + NumItems = RHS.NumItems; + NumTombstones = RHS.NumTombstones; + for (unsigned I = 0, E = NumBuckets; I != E; ++I) { + MapEntryTy *Bucket = ((MapEntryTy**) RHS.TheTable)[I]; + if (!Bucket || Bucket == getTombstoneVal()) { + TheTable[I] = Bucket; + continue; + } + + TheTable[I] = MapEntryTy::Create(Bucket->getKey(), Allocator, + Bucket->getValue()); + HashTable[I] = RHSHashTable[I]; + } + + // Note that here we've copied everything from the RHS into this object, + // tombstones included. We could, instead, have re-probed for each key to + // instantiate this new object without any tombstone buckets. The + // assumption here is that items are rarely deleted from most StringMaps, + // and so tombstones are rare, so the cost of re-probing for all inputs is + // not worthwhile. + } AllocatorTy &getAllocator() { return Allocator; } const AllocatorTy &getAllocator() const { return Allocator; } |