2017-05-17 01:10:25 +02:00
|
|
|
//===- StringMap.h - String Hash table map interface ------------*- C++ -*-===//
|
2006-10-30 00:42:03 +01:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2006-10-30 00:42:03 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2007-02-08 20:20:57 +01:00
|
|
|
// This file defines the StringMap class.
|
2006-10-30 00:42:03 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-02-08 20:20:57 +01:00
|
|
|
#ifndef LLVM_ADT_STRINGMAP_H
|
|
|
|
#define LLVM_ADT_STRINGMAP_H
|
2006-10-30 00:42:03 +01:00
|
|
|
|
2020-04-12 07:33:06 +02:00
|
|
|
#include "llvm/ADT/StringMapEntry.h"
|
2016-04-01 17:51:51 +02:00
|
|
|
#include "llvm/Support/PointerLikeTypeTraits.h"
|
2016-11-23 01:30:24 +01:00
|
|
|
#include <initializer_list>
|
2017-05-17 01:10:25 +02:00
|
|
|
#include <iterator>
|
2006-10-30 00:42:03 +01:00
|
|
|
|
|
|
|
namespace llvm {
|
2016-11-23 01:30:24 +01:00
|
|
|
|
2020-04-12 07:14:35 +02:00
|
|
|
template <typename ValueTy> class StringMapConstIterator;
|
|
|
|
template <typename ValueTy> class StringMapIterator;
|
|
|
|
template <typename ValueTy> class StringMapKeyIterator;
|
2007-02-11 09:12:13 +01:00
|
|
|
|
2007-02-08 20:20:57 +01:00
|
|
|
/// StringMapImpl - This is the base class of StringMap that is shared among
|
2006-10-30 00:42:03 +01:00
|
|
|
/// all of its instantiations.
|
2007-02-08 20:20:57 +01:00
|
|
|
class StringMapImpl {
|
2007-02-11 09:12:13 +01:00
|
|
|
protected:
|
2011-12-27 21:35:07 +01:00
|
|
|
// Array of NumBuckets pointers to entries, null pointers are holes.
|
2012-12-18 01:53:36 +01:00
|
|
|
// TheTable[NumBuckets] contains a sentinel value for easy iteration. Followed
|
2011-12-27 21:35:07 +01:00
|
|
|
// by an array of the actual hash values as unsigned integers.
|
2017-05-17 01:10:25 +02:00
|
|
|
StringMapEntryBase **TheTable = nullptr;
|
|
|
|
unsigned NumBuckets = 0;
|
|
|
|
unsigned NumItems = 0;
|
|
|
|
unsigned NumTombstones = 0;
|
2006-10-30 00:42:03 +01:00
|
|
|
unsigned ItemSize;
|
2015-08-10 06:21:43 +02:00
|
|
|
|
2006-10-30 00:42:03 +01:00
|
|
|
protected:
|
2020-04-12 07:14:35 +02:00
|
|
|
explicit StringMapImpl(unsigned itemSize) : ItemSize(itemSize) {}
|
2014-05-08 23:53:33 +02:00
|
|
|
StringMapImpl(StringMapImpl &&RHS)
|
|
|
|
: TheTable(RHS.TheTable), NumBuckets(RHS.NumBuckets),
|
|
|
|
NumItems(RHS.NumItems), NumTombstones(RHS.NumTombstones),
|
|
|
|
ItemSize(RHS.ItemSize) {
|
2014-05-08 23:52:29 +02:00
|
|
|
RHS.TheTable = nullptr;
|
|
|
|
RHS.NumBuckets = 0;
|
|
|
|
RHS.NumItems = 0;
|
|
|
|
RHS.NumTombstones = 0;
|
|
|
|
}
|
2014-05-08 23:52:26 +02:00
|
|
|
|
2007-02-08 20:20:57 +01:00
|
|
|
StringMapImpl(unsigned InitSize, unsigned ItemSize);
|
2014-06-23 20:28:53 +02:00
|
|
|
unsigned RehashTable(unsigned BucketNo = 0);
|
2008-01-31 13:10:41 +01:00
|
|
|
|
2006-10-30 00:42:03 +01:00
|
|
|
/// LookupBucketFor - Look up the bucket that the specified string should end
|
|
|
|
/// up in. If it already exists as a key in the map, the Item pointer for the
|
|
|
|
/// specified bucket will be non-null. Otherwise, it will be null. In either
|
|
|
|
/// case, the FullHashValue field of the bucket will be set to the hash value
|
|
|
|
/// of the string.
|
2009-11-06 11:58:06 +01:00
|
|
|
unsigned LookupBucketFor(StringRef Key);
|
2008-01-31 13:10:41 +01:00
|
|
|
|
2007-02-11 20:49:41 +01:00
|
|
|
/// FindKey - Look up the bucket that contains the specified key. If it exists
|
|
|
|
/// in the map, return the bucket number of the key. Otherwise return -1.
|
|
|
|
/// This does not modify the map.
|
2009-11-06 11:58:06 +01:00
|
|
|
int FindKey(StringRef Key) const;
|
2007-02-11 21:58:00 +01:00
|
|
|
|
|
|
|
/// RemoveKey - Remove the specified StringMapEntry from the table, but do not
|
|
|
|
/// delete it. This aborts if the value isn't in the table.
|
|
|
|
void RemoveKey(StringMapEntryBase *V);
|
|
|
|
|
|
|
|
/// RemoveKey - Remove the StringMapEntry for the specified key from the
|
|
|
|
/// table, returning it. If the key is not in the table, this returns null.
|
2009-11-06 11:58:06 +01:00
|
|
|
StringMapEntryBase *RemoveKey(StringRef Key);
|
2015-08-10 06:21:43 +02:00
|
|
|
|
2016-03-30 21:54:56 +02:00
|
|
|
/// Allocate the table with the specified number of buckets and otherwise
|
|
|
|
/// setup the map as empty.
|
2007-04-04 02:29:37 +02:00
|
|
|
void init(unsigned Size);
|
2015-08-10 06:21:43 +02:00
|
|
|
|
2006-10-30 00:42:03 +01:00
|
|
|
public:
|
2007-02-11 09:12:13 +01:00
|
|
|
static StringMapEntryBase *getTombstoneVal() {
|
2016-04-01 17:51:51 +02:00
|
|
|
uintptr_t Val = static_cast<uintptr_t>(-1);
|
|
|
|
Val <<= PointerLikeTypeTraits<StringMapEntryBase *>::NumLowBitsAvailable;
|
|
|
|
return reinterpret_cast<StringMapEntryBase *>(Val);
|
2007-02-11 09:12:13 +01:00
|
|
|
}
|
2008-01-31 13:10:41 +01:00
|
|
|
|
2006-10-30 00:42:03 +01:00
|
|
|
unsigned getNumBuckets() const { return NumBuckets; }
|
|
|
|
unsigned getNumItems() const { return NumItems; }
|
|
|
|
|
2007-02-11 09:12:13 +01:00
|
|
|
bool empty() const { return NumItems == 0; }
|
|
|
|
unsigned size() const { return NumItems; }
|
2013-05-22 19:10:11 +02:00
|
|
|
|
|
|
|
void swap(StringMapImpl &Other) {
|
|
|
|
std::swap(TheTable, Other.TheTable);
|
|
|
|
std::swap(NumBuckets, Other.NumBuckets);
|
|
|
|
std::swap(NumItems, Other.NumItems);
|
|
|
|
std::swap(NumTombstones, Other.NumTombstones);
|
|
|
|
}
|
2006-10-30 00:42:03 +01:00
|
|
|
};
|
|
|
|
|
2007-02-08 20:20:57 +01:00
|
|
|
/// StringMap - This is an unconventional map that is specialized for handling
|
2007-02-08 20:08:37 +01:00
|
|
|
/// keys that are "strings", which are basically ranges of bytes. This does some
|
2006-10-30 00:42:03 +01:00
|
|
|
/// funky memory allocation and hashing things to make it extremely efficient,
|
|
|
|
/// storing the string data *after* the value in the map.
|
2020-04-12 07:14:35 +02:00
|
|
|
template <typename ValueTy, typename AllocatorTy = MallocAllocator>
|
2007-02-08 20:20:57 +01:00
|
|
|
class StringMap : public StringMapImpl {
|
2006-10-30 00:42:03 +01:00
|
|
|
AllocatorTy Allocator;
|
2015-08-10 06:21:43 +02:00
|
|
|
|
2006-10-30 00:42:03 +01:00
|
|
|
public:
|
2017-05-17 01:10:25 +02:00
|
|
|
using MapEntryTy = StringMapEntry<ValueTy>;
|
2015-08-10 06:22:09 +02:00
|
|
|
|
2008-05-05 20:30:58 +02:00
|
|
|
StringMap() : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))) {}
|
2017-05-17 01:10:25 +02:00
|
|
|
|
2007-11-19 16:30:20 +01:00
|
|
|
explicit StringMap(unsigned InitialSize)
|
2020-04-12 07:14:35 +02:00
|
|
|
: StringMapImpl(InitialSize, static_cast<unsigned>(sizeof(MapEntryTy))) {}
|
2010-09-21 16:59:34 +02:00
|
|
|
|
2010-07-23 05:29:59 +02:00
|
|
|
explicit StringMap(AllocatorTy A)
|
2020-04-12 07:14:35 +02:00
|
|
|
: StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))), Allocator(A) {
|
|
|
|
}
|
2010-07-23 05:29:59 +02:00
|
|
|
|
2013-01-14 20:41:09 +01:00
|
|
|
StringMap(unsigned InitialSize, AllocatorTy A)
|
2020-04-12 07:14:35 +02:00
|
|
|
: StringMapImpl(InitialSize, static_cast<unsigned>(sizeof(MapEntryTy))),
|
|
|
|
Allocator(A) {}
|
2013-01-14 20:41:09 +01:00
|
|
|
|
2015-12-01 01:48:39 +01:00
|
|
|
StringMap(std::initializer_list<std::pair<StringRef, ValueTy>> List)
|
2016-03-25 06:57:47 +01:00
|
|
|
: StringMapImpl(List.size(), static_cast<unsigned>(sizeof(MapEntryTy))) {
|
2015-12-01 01:48:39 +01:00
|
|
|
for (const auto &P : List) {
|
|
|
|
insert(P);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-08 23:52:26 +02:00
|
|
|
StringMap(StringMap &&RHS)
|
2014-05-08 23:52:29 +02:00
|
|
|
: StringMapImpl(std::move(RHS)), Allocator(std::move(RHS.Allocator)) {}
|
|
|
|
|
2020-04-12 07:14:35 +02:00
|
|
|
StringMap(const StringMap &RHS)
|
|
|
|
: StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))),
|
|
|
|
Allocator(RHS.Allocator) {
|
2016-03-30 21:54:56 +02:00
|
|
|
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) {
|
2016-04-01 17:51:51 +02:00
|
|
|
StringMapEntryBase *Bucket = RHS.TheTable[I];
|
2016-03-30 21:54:56 +02:00
|
|
|
if (!Bucket || Bucket == getTombstoneVal()) {
|
|
|
|
TheTable[I] = Bucket;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-04-01 17:51:51 +02:00
|
|
|
TheTable[I] = MapEntryTy::Create(
|
|
|
|
static_cast<MapEntryTy *>(Bucket)->getKey(), Allocator,
|
|
|
|
static_cast<MapEntryTy *>(Bucket)->getValue());
|
2016-03-30 21:54:56 +02:00
|
|
|
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.
|
|
|
|
}
|
2009-02-04 02:54:10 +01:00
|
|
|
|
2017-05-17 01:10:25 +02:00
|
|
|
StringMap &operator=(StringMap RHS) {
|
|
|
|
StringMapImpl::swap(RHS);
|
|
|
|
std::swap(Allocator, RHS.Allocator);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~StringMap() {
|
|
|
|
// Delete all the elements in the map, but don't reset the elements
|
|
|
|
// to default values. This is a copy of clear(), but avoids unnecessary
|
|
|
|
// work not required in the destructor.
|
|
|
|
if (!empty()) {
|
|
|
|
for (unsigned I = 0, E = NumBuckets; I != E; ++I) {
|
|
|
|
StringMapEntryBase *Bucket = TheTable[I];
|
|
|
|
if (Bucket && Bucket != getTombstoneVal()) {
|
2020-04-12 07:14:35 +02:00
|
|
|
static_cast<MapEntryTy *>(Bucket)->Destroy(Allocator);
|
2017-05-17 01:10:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(TheTable);
|
|
|
|
}
|
|
|
|
|
2014-04-18 16:54:51 +02:00
|
|
|
AllocatorTy &getAllocator() { return Allocator; }
|
|
|
|
const AllocatorTy &getAllocator() const { return Allocator; }
|
2006-10-30 00:42:03 +01:00
|
|
|
|
2020-04-12 07:14:35 +02:00
|
|
|
using key_type = const char *;
|
2017-05-17 01:10:25 +02:00
|
|
|
using mapped_type = ValueTy;
|
|
|
|
using value_type = StringMapEntry<ValueTy>;
|
|
|
|
using size_type = size_t;
|
2008-01-31 18:43:25 +01:00
|
|
|
|
2017-05-17 01:10:25 +02:00
|
|
|
using const_iterator = StringMapConstIterator<ValueTy>;
|
|
|
|
using iterator = StringMapIterator<ValueTy>;
|
2008-01-31 13:10:41 +01:00
|
|
|
|
2020-04-12 07:14:35 +02:00
|
|
|
iterator begin() { return iterator(TheTable, NumBuckets == 0); }
|
|
|
|
iterator end() { return iterator(TheTable + NumBuckets, true); }
|
2007-04-04 02:29:37 +02:00
|
|
|
const_iterator begin() const {
|
|
|
|
return const_iterator(TheTable, NumBuckets == 0);
|
|
|
|
}
|
|
|
|
const_iterator end() const {
|
2020-04-12 07:14:35 +02:00
|
|
|
return const_iterator(TheTable + NumBuckets, true);
|
2007-04-04 02:29:37 +02:00
|
|
|
}
|
2008-01-31 13:10:41 +01:00
|
|
|
|
2017-05-17 01:10:25 +02:00
|
|
|
iterator_range<StringMapKeyIterator<ValueTy>> keys() const {
|
2017-03-22 00:45:03 +01:00
|
|
|
return make_range(StringMapKeyIterator<ValueTy>(begin()),
|
|
|
|
StringMapKeyIterator<ValueTy>(end()));
|
|
|
|
}
|
|
|
|
|
2009-11-06 11:58:06 +01:00
|
|
|
iterator find(StringRef Key) {
|
2009-07-23 20:17:34 +02:00
|
|
|
int Bucket = FindKey(Key);
|
2020-04-12 07:14:35 +02:00
|
|
|
if (Bucket == -1)
|
|
|
|
return end();
|
|
|
|
return iterator(TheTable + Bucket, true);
|
2007-02-11 20:49:41 +01:00
|
|
|
}
|
|
|
|
|
2009-11-06 11:58:06 +01:00
|
|
|
const_iterator find(StringRef Key) const {
|
2009-07-23 20:17:34 +02:00
|
|
|
int Bucket = FindKey(Key);
|
2020-04-12 07:14:35 +02:00
|
|
|
if (Bucket == -1)
|
|
|
|
return end();
|
|
|
|
return const_iterator(TheTable + Bucket, true);
|
2006-10-30 04:14:15 +01:00
|
|
|
}
|
2008-01-31 18:43:25 +01:00
|
|
|
|
2012-12-18 01:53:36 +01:00
|
|
|
/// lookup - Return the entry for the specified key, or a default
|
2009-03-12 02:16:06 +01:00
|
|
|
/// constructed value if no such entry exists.
|
2009-11-06 11:58:06 +01:00
|
|
|
ValueTy lookup(StringRef Key) const {
|
2009-03-12 02:16:06 +01:00
|
|
|
const_iterator it = find(Key);
|
|
|
|
if (it != end())
|
|
|
|
return it->second;
|
|
|
|
return ValueTy();
|
|
|
|
}
|
|
|
|
|
2016-03-25 06:58:04 +01:00
|
|
|
/// Lookup the ValueTy for the \p Key, or create a default constructed value
|
|
|
|
/// if the key is not in the map.
|
2016-07-21 15:37:48 +02:00
|
|
|
ValueTy &operator[](StringRef Key) { return try_emplace(Key).first->second; }
|
2008-01-31 18:43:25 +01:00
|
|
|
|
2013-12-22 13:04:23 +01:00
|
|
|
/// count - Return 1 if the element is in the map, 0 otherwise.
|
2020-04-12 07:14:35 +02:00
|
|
|
size_type count(StringRef Key) const { return find(Key) == end() ? 0 : 1; }
|
2008-01-31 18:43:25 +01:00
|
|
|
|
[ADT] Enable set_difference() to be used on StringSet
Summary: Re-land r362766 after it was reverted in r362823.
Reviewers: jhenderson, dsanders, aaron.ballman, MatzeB, lhames, dblaikie
Reviewed By: dblaikie
Subscribers: smeenai, mgrang, mgorny, dexonsmith, kristina, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62369
llvm-svn: 362835
2019-06-07 22:23:03 +02:00
|
|
|
template <typename InputTy>
|
|
|
|
size_type count(const StringMapEntry<InputTy> &MapEntry) const {
|
|
|
|
return count(MapEntry.getKey());
|
|
|
|
}
|
|
|
|
|
2007-02-11 21:58:00 +01:00
|
|
|
/// insert - Insert the specified key/value pair into the map. If the key
|
|
|
|
/// already exists in the map, return false and ignore the request, otherwise
|
|
|
|
/// insert it and return true.
|
|
|
|
bool insert(MapEntryTy *KeyValue) {
|
2009-07-23 20:17:34 +02:00
|
|
|
unsigned BucketNo = LookupBucketFor(KeyValue->getKey());
|
2011-12-27 21:35:07 +01:00
|
|
|
StringMapEntryBase *&Bucket = TheTable[BucketNo];
|
|
|
|
if (Bucket && Bucket != getTombstoneVal())
|
2020-04-12 07:14:35 +02:00
|
|
|
return false; // Already exists in map.
|
2008-01-31 13:10:41 +01:00
|
|
|
|
2011-12-27 21:35:07 +01:00
|
|
|
if (Bucket == getTombstoneVal())
|
2007-02-11 21:58:00 +01:00
|
|
|
--NumTombstones;
|
2011-12-27 21:35:07 +01:00
|
|
|
Bucket = KeyValue;
|
2007-02-11 21:58:00 +01:00
|
|
|
++NumItems;
|
2011-03-30 20:32:51 +02:00
|
|
|
assert(NumItems + NumTombstones <= NumBuckets);
|
2008-01-31 13:10:41 +01:00
|
|
|
|
2011-03-30 20:32:44 +02:00
|
|
|
RehashTable();
|
2007-02-11 21:58:00 +01:00
|
|
|
return true;
|
|
|
|
}
|
2008-01-31 13:10:41 +01:00
|
|
|
|
2014-06-23 20:28:53 +02:00
|
|
|
/// insert - Inserts the specified key/value pair into the map if the key
|
|
|
|
/// isn't already in the map. The bool component of the returned pair is true
|
|
|
|
/// if and only if the insertion takes place, and the iterator component of
|
|
|
|
/// the pair points to the element with key equivalent to the key of the pair.
|
|
|
|
std::pair<iterator, bool> insert(std::pair<StringRef, ValueTy> KV) {
|
2016-07-21 15:37:48 +02:00
|
|
|
return try_emplace(KV.first, std::move(KV.second));
|
2016-03-25 06:58:04 +01:00
|
|
|
}
|
|
|
|
|
2019-09-25 06:58:02 +02:00
|
|
|
/// Inserts an element or assigns to the current element if the key already
|
|
|
|
/// exists. The return type is the same as try_emplace.
|
|
|
|
template <typename V>
|
|
|
|
std::pair<iterator, bool> insert_or_assign(StringRef Key, V &&Val) {
|
|
|
|
auto Ret = try_emplace(Key, std::forward<V>(Val));
|
|
|
|
if (!Ret.second)
|
|
|
|
Ret.first->second = std::forward<V>(Val);
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2016-03-25 06:58:04 +01:00
|
|
|
/// Emplace a new element for the specified key into the map if the key isn't
|
|
|
|
/// already in the map. The bool component of the returned pair is true
|
|
|
|
/// if and only if the insertion takes place, and the iterator component of
|
|
|
|
/// the pair points to the element with key equivalent to the key of the pair.
|
|
|
|
template <typename... ArgsTy>
|
2016-07-21 15:37:48 +02:00
|
|
|
std::pair<iterator, bool> try_emplace(StringRef Key, ArgsTy &&... Args) {
|
2016-03-25 06:58:04 +01:00
|
|
|
unsigned BucketNo = LookupBucketFor(Key);
|
2014-06-23 20:28:53 +02:00
|
|
|
StringMapEntryBase *&Bucket = TheTable[BucketNo];
|
|
|
|
if (Bucket && Bucket != getTombstoneVal())
|
|
|
|
return std::make_pair(iterator(TheTable + BucketNo, false),
|
|
|
|
false); // Already exists in map.
|
|
|
|
|
|
|
|
if (Bucket == getTombstoneVal())
|
|
|
|
--NumTombstones;
|
2016-03-25 06:58:04 +01:00
|
|
|
Bucket = MapEntryTy::Create(Key, Allocator, std::forward<ArgsTy>(Args)...);
|
2014-06-23 20:28:53 +02:00
|
|
|
++NumItems;
|
|
|
|
assert(NumItems + NumTombstones <= NumBuckets);
|
|
|
|
|
|
|
|
BucketNo = RehashTable(BucketNo);
|
|
|
|
return std::make_pair(iterator(TheTable + BucketNo, false), true);
|
|
|
|
}
|
|
|
|
|
2008-07-02 07:26:32 +02:00
|
|
|
// clear - Empties out the StringMap
|
|
|
|
void clear() {
|
2020-04-12 07:14:35 +02:00
|
|
|
if (empty())
|
|
|
|
return;
|
2009-01-08 05:48:20 +01:00
|
|
|
|
2008-07-02 07:30:45 +02:00
|
|
|
// Zap all values, resetting the keys back to non-present (not tombstone),
|
|
|
|
// which is safe because we're removing all elements.
|
2011-12-27 21:35:07 +01:00
|
|
|
for (unsigned I = 0, E = NumBuckets; I != E; ++I) {
|
|
|
|
StringMapEntryBase *&Bucket = TheTable[I];
|
|
|
|
if (Bucket && Bucket != getTombstoneVal()) {
|
2020-04-12 07:14:35 +02:00
|
|
|
static_cast<MapEntryTy *>(Bucket)->Destroy(Allocator);
|
2008-07-02 07:30:45 +02:00
|
|
|
}
|
2014-04-07 06:17:22 +02:00
|
|
|
Bucket = nullptr;
|
2008-07-02 07:30:45 +02:00
|
|
|
}
|
2009-01-08 05:48:20 +01:00
|
|
|
|
|
|
|
NumItems = 0;
|
2011-03-30 20:32:51 +02:00
|
|
|
NumTombstones = 0;
|
2008-07-02 07:26:32 +02:00
|
|
|
}
|
|
|
|
|
2007-02-11 21:58:00 +01:00
|
|
|
/// remove - Remove the specified key/value pair from the map, but do not
|
|
|
|
/// erase it. This aborts if the key is not in the map.
|
2020-04-12 07:14:35 +02:00
|
|
|
void remove(MapEntryTy *KeyValue) { RemoveKey(KeyValue); }
|
2008-01-31 13:10:41 +01:00
|
|
|
|
2007-07-22 22:08:01 +02:00
|
|
|
void erase(iterator I) {
|
|
|
|
MapEntryTy &V = *I;
|
|
|
|
remove(&V);
|
|
|
|
V.Destroy(Allocator);
|
2008-06-23 23:07:03 +02:00
|
|
|
}
|
|
|
|
|
2009-11-06 11:58:06 +01:00
|
|
|
bool erase(StringRef Key) {
|
2008-06-23 23:07:03 +02:00
|
|
|
iterator I = find(Key);
|
2020-04-12 07:14:35 +02:00
|
|
|
if (I == end())
|
|
|
|
return false;
|
2008-06-23 23:07:03 +02:00
|
|
|
erase(I);
|
|
|
|
return true;
|
2007-07-22 22:08:01 +02:00
|
|
|
}
|
2006-10-30 00:42:03 +01:00
|
|
|
};
|
2008-01-31 13:10:41 +01:00
|
|
|
|
2017-03-22 00:45:03 +01:00
|
|
|
template <typename DerivedTy, typename ValueTy>
|
|
|
|
class StringMapIterBase
|
|
|
|
: public iterator_facade_base<DerivedTy, std::forward_iterator_tag,
|
|
|
|
ValueTy> {
|
2007-02-11 21:58:00 +01:00
|
|
|
protected:
|
2016-11-23 01:30:24 +01:00
|
|
|
StringMapEntryBase **Ptr = nullptr;
|
2015-08-10 06:21:43 +02:00
|
|
|
|
2007-02-11 09:12:13 +01:00
|
|
|
public:
|
2017-03-22 00:45:03 +01:00
|
|
|
StringMapIterBase() = default;
|
2017-03-21 22:23:57 +01:00
|
|
|
|
2017-03-22 00:45:03 +01:00
|
|
|
explicit StringMapIterBase(StringMapEntryBase **Bucket,
|
|
|
|
bool NoAdvance = false)
|
|
|
|
: Ptr(Bucket) {
|
2020-04-12 07:14:35 +02:00
|
|
|
if (!NoAdvance)
|
|
|
|
AdvancePastEmptyBuckets();
|
2007-02-11 09:12:13 +01:00
|
|
|
}
|
2008-01-31 13:10:41 +01:00
|
|
|
|
2017-03-22 00:45:03 +01:00
|
|
|
DerivedTy &operator=(const DerivedTy &Other) {
|
|
|
|
Ptr = Other.Ptr;
|
|
|
|
return static_cast<DerivedTy &>(*this);
|
2007-02-11 09:12:13 +01:00
|
|
|
}
|
2008-01-31 13:10:41 +01:00
|
|
|
|
2017-03-22 00:45:03 +01:00
|
|
|
bool operator==(const DerivedTy &RHS) const { return Ptr == RHS.Ptr; }
|
2008-01-31 13:10:41 +01:00
|
|
|
|
2017-03-22 00:45:03 +01:00
|
|
|
DerivedTy &operator++() { // Preincrement
|
2007-02-11 09:12:13 +01:00
|
|
|
++Ptr;
|
|
|
|
AdvancePastEmptyBuckets();
|
2017-03-22 00:45:03 +01:00
|
|
|
return static_cast<DerivedTy &>(*this);
|
2007-02-11 09:12:13 +01:00
|
|
|
}
|
2017-03-22 00:45:03 +01:00
|
|
|
|
|
|
|
DerivedTy operator++(int) { // Post-increment
|
|
|
|
DerivedTy Tmp(Ptr);
|
|
|
|
++*this;
|
|
|
|
return Tmp;
|
2007-02-11 09:12:13 +01:00
|
|
|
}
|
2008-01-31 13:10:41 +01:00
|
|
|
|
2007-02-11 09:12:13 +01:00
|
|
|
private:
|
|
|
|
void AdvancePastEmptyBuckets() {
|
2014-04-07 06:17:22 +02:00
|
|
|
while (*Ptr == nullptr || *Ptr == StringMapImpl::getTombstoneVal())
|
2007-02-11 09:12:13 +01:00
|
|
|
++Ptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-22 00:45:03 +01:00
|
|
|
template <typename ValueTy>
|
|
|
|
class StringMapConstIterator
|
|
|
|
: public StringMapIterBase<StringMapConstIterator<ValueTy>,
|
|
|
|
const StringMapEntry<ValueTy>> {
|
|
|
|
using base = StringMapIterBase<StringMapConstIterator<ValueTy>,
|
|
|
|
const StringMapEntry<ValueTy>>;
|
|
|
|
|
2017-03-21 21:27:36 +01:00
|
|
|
public:
|
2017-03-22 00:45:03 +01:00
|
|
|
StringMapConstIterator() = default;
|
|
|
|
explicit StringMapConstIterator(StringMapEntryBase **Bucket,
|
|
|
|
bool NoAdvance = false)
|
|
|
|
: base(Bucket, NoAdvance) {}
|
|
|
|
|
|
|
|
const StringMapEntry<ValueTy> &operator*() const {
|
|
|
|
return *static_cast<const StringMapEntry<ValueTy> *>(*this->Ptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename ValueTy>
|
|
|
|
class StringMapIterator : public StringMapIterBase<StringMapIterator<ValueTy>,
|
|
|
|
StringMapEntry<ValueTy>> {
|
|
|
|
using base =
|
|
|
|
StringMapIterBase<StringMapIterator<ValueTy>, StringMapEntry<ValueTy>>;
|
2017-03-21 22:23:57 +01:00
|
|
|
|
2017-03-22 00:45:03 +01:00
|
|
|
public:
|
|
|
|
StringMapIterator() = default;
|
2011-12-27 21:35:07 +01:00
|
|
|
explicit StringMapIterator(StringMapEntryBase **Bucket,
|
2007-12-14 16:41:34 +01:00
|
|
|
bool NoAdvance = false)
|
2017-03-22 00:45:03 +01:00
|
|
|
: base(Bucket, NoAdvance) {}
|
2017-03-21 21:27:36 +01:00
|
|
|
|
2017-03-21 22:23:57 +01:00
|
|
|
StringMapEntry<ValueTy> &operator*() const {
|
2017-03-22 00:45:03 +01:00
|
|
|
return *static_cast<StringMapEntry<ValueTy> *>(*this->Ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
operator StringMapConstIterator<ValueTy>() const {
|
|
|
|
return StringMapConstIterator<ValueTy>(this->Ptr, true);
|
2007-02-11 09:12:13 +01:00
|
|
|
}
|
2017-03-22 00:45:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename ValueTy>
|
|
|
|
class StringMapKeyIterator
|
|
|
|
: public iterator_adaptor_base<StringMapKeyIterator<ValueTy>,
|
|
|
|
StringMapConstIterator<ValueTy>,
|
|
|
|
std::forward_iterator_tag, StringRef> {
|
|
|
|
using base = iterator_adaptor_base<StringMapKeyIterator<ValueTy>,
|
|
|
|
StringMapConstIterator<ValueTy>,
|
|
|
|
std::forward_iterator_tag, StringRef>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
StringMapKeyIterator() = default;
|
|
|
|
explicit StringMapKeyIterator(StringMapConstIterator<ValueTy> Iter)
|
|
|
|
: base(std::move(Iter)) {}
|
|
|
|
|
|
|
|
StringRef &operator*() {
|
|
|
|
Key = this->wrapped()->getKey();
|
|
|
|
return Key;
|
2017-03-21 21:27:36 +01:00
|
|
|
}
|
2017-03-22 00:45:03 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
StringRef Key;
|
2017-03-21 21:27:36 +01:00
|
|
|
};
|
|
|
|
|
2016-11-23 01:30:24 +01:00
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif // LLVM_ADT_STRINGMAP_H
|