1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 02:33:06 +01:00

A few cosmetic cleanups to StringMap/StringSet.h, including fixing

the indentation of the StringSet.h file and its file comment header,
and significantly reduce redundant #includes that are already pulled
in transitively.  NFC.

This is in preparation for a more interesting patch I'll post to phab.
This commit is contained in:
Chris Lattner 2020-04-11 22:14:35 -07:00
parent 768e5a5b49
commit 3652a30d15
3 changed files with 78 additions and 93 deletions

View File

@ -14,24 +14,16 @@
#define LLVM_ADT_STRINGMAP_H
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/iterator.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/Support/AllocatorBase.h"
#include "llvm/Support/PointerLikeTypeTraits.h"
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <initializer_list>
#include <iterator>
#include <utility>
namespace llvm {
template<typename ValueTy> class StringMapConstIterator;
template<typename ValueTy> class StringMapIterator;
template<typename ValueTy> class StringMapKeyIterator;
template <typename ValueTy> class StringMapConstIterator;
template <typename ValueTy> class StringMapIterator;
template <typename ValueTy> class StringMapKeyIterator;
/// StringMapEntryBase - Shared base class of StringMapEntry instances.
class StringMapEntryBase {
@ -57,8 +49,7 @@ protected:
unsigned ItemSize;
protected:
explicit StringMapImpl(unsigned itemSize)
: ItemSize(itemSize) {}
explicit StringMapImpl(unsigned itemSize) : ItemSize(itemSize) {}
StringMapImpl(StringMapImpl &&RHS)
: TheTable(RHS.TheTable), NumBuckets(RHS.NumBuckets),
NumItems(RHS.NumItems), NumTombstones(RHS.NumTombstones),
@ -122,13 +113,13 @@ public:
/// Factored out into a separate base class to make it easier to specialize.
/// This is primarily intended to support StringSet, which doesn't need a value
/// stored at all.
template<typename ValueTy>
template <typename ValueTy>
class StringMapEntryStorage : public StringMapEntryBase {
public:
ValueTy second;
explicit StringMapEntryStorage(size_t strLen)
: StringMapEntryBase(strLen), second() {}
: StringMapEntryBase(strLen), second() {}
template <typename... InitTy>
StringMapEntryStorage(size_t strLen, InitTy &&... InitVals)
: StringMapEntryBase(strLen), second(std::forward<InitTy>(InitVals)...) {}
@ -140,11 +131,10 @@ public:
void setValue(const ValueTy &V) { second = V; }
};
template<>
class StringMapEntryStorage<NoneType> : public StringMapEntryBase {
template <> class StringMapEntryStorage<NoneType> : public StringMapEntryBase {
public:
explicit StringMapEntryStorage(size_t strLen, NoneType none = None)
: StringMapEntryBase(strLen) {}
: StringMapEntryBase(strLen) {}
StringMapEntryStorage(StringMapEntryStorage &E) = delete;
NoneType getValue() const { return None; }
@ -153,7 +143,7 @@ public:
/// StringMapEntry - This is used to represent one value that is inserted into
/// a StringMap. It contains the Value itself and the key: the string length
/// and data.
template<typename ValueTy>
template <typename ValueTy>
class StringMapEntry final : public StringMapEntryStorage<ValueTy> {
public:
using StringMapEntryStorage<ValueTy>::StringMapEntryStorage;
@ -165,7 +155,9 @@ public:
/// getKeyData - Return the start of the string data that is the key for this
/// value. The string data is always stored immediately after the
/// StringMapEntry object.
const char *getKeyData() const {return reinterpret_cast<const char*>(this+1);}
const char *getKeyData() const {
return reinterpret_cast<const char *>(this + 1);
}
StringRef first() const {
return StringRef(getKeyData(), this->getKeyLength());
@ -184,17 +176,17 @@ public:
size_t Alignment = alignof(StringMapEntry);
StringMapEntry *NewItem =
static_cast<StringMapEntry*>(Allocator.Allocate(AllocSize,Alignment));
static_cast<StringMapEntry *>(Allocator.Allocate(AllocSize, Alignment));
assert(NewItem && "Unhandled out-of-memory");
// Construct the value.
new (NewItem) StringMapEntry(KeyLength, std::forward<InitTy>(InitVals)...);
// Copy the string information.
char *StrBuffer = const_cast<char*>(NewItem->getKeyData());
char *StrBuffer = const_cast<char *>(NewItem->getKeyData());
if (KeyLength > 0)
memcpy(StrBuffer, Key.data(), KeyLength);
StrBuffer[KeyLength] = 0; // Null terminate for convenience of clients.
StrBuffer[KeyLength] = 0; // Null terminate for convenience of clients.
return NewItem;
}
@ -212,14 +204,13 @@ public:
/// GetStringMapEntryFromKeyData - Given key data that is known to be embedded
/// into a StringMapEntry, return the StringMapEntry itself.
static StringMapEntry &GetStringMapEntryFromKeyData(const char *KeyData) {
char *Ptr = const_cast<char*>(KeyData) - sizeof(StringMapEntry<ValueTy>);
return *reinterpret_cast<StringMapEntry*>(Ptr);
char *Ptr = const_cast<char *>(KeyData) - sizeof(StringMapEntry<ValueTy>);
return *reinterpret_cast<StringMapEntry *>(Ptr);
}
/// Destroy - Destroy this StringMapEntry, releasing memory back to the
/// specified allocator.
template<typename AllocatorTy>
void Destroy(AllocatorTy &Allocator) {
template <typename AllocatorTy> void Destroy(AllocatorTy &Allocator) {
// Free memory referenced by the item.
size_t AllocSize = sizeof(StringMapEntry) + this->getKeyLength() + 1;
this->~StringMapEntry();
@ -237,7 +228,7 @@ public:
/// keys that are "strings", which are basically ranges of bytes. This does some
/// funky memory allocation and hashing things to make it extremely efficient,
/// storing the string data *after* the value in the map.
template<typename ValueTy, typename AllocatorTy = MallocAllocator>
template <typename ValueTy, typename AllocatorTy = MallocAllocator>
class StringMap : public StringMapImpl {
AllocatorTy Allocator;
@ -247,14 +238,15 @@ public:
StringMap() : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))) {}
explicit StringMap(unsigned InitialSize)
: StringMapImpl(InitialSize, static_cast<unsigned>(sizeof(MapEntryTy))) {}
: StringMapImpl(InitialSize, static_cast<unsigned>(sizeof(MapEntryTy))) {}
explicit StringMap(AllocatorTy A)
: StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))), Allocator(A) {}
: StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))), Allocator(A) {
}
StringMap(unsigned InitialSize, AllocatorTy A)
: StringMapImpl(InitialSize, static_cast<unsigned>(sizeof(MapEntryTy))),
Allocator(A) {}
: StringMapImpl(InitialSize, static_cast<unsigned>(sizeof(MapEntryTy))),
Allocator(A) {}
StringMap(std::initializer_list<std::pair<StringRef, ValueTy>> List)
: StringMapImpl(List.size(), static_cast<unsigned>(sizeof(MapEntryTy))) {
@ -266,9 +258,9 @@ public:
StringMap(StringMap &&RHS)
: StringMapImpl(std::move(RHS)), Allocator(std::move(RHS.Allocator)) {}
StringMap(const StringMap &RHS) :
StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))),
Allocator(RHS.Allocator) {
StringMap(const StringMap &RHS)
: StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))),
Allocator(RHS.Allocator) {
if (RHS.empty())
return;
@ -315,7 +307,7 @@ public:
for (unsigned I = 0, E = NumBuckets; I != E; ++I) {
StringMapEntryBase *Bucket = TheTable[I];
if (Bucket && Bucket != getTombstoneVal()) {
static_cast<MapEntryTy*>(Bucket)->Destroy(Allocator);
static_cast<MapEntryTy *>(Bucket)->Destroy(Allocator);
}
}
}
@ -325,7 +317,7 @@ public:
AllocatorTy &getAllocator() { return Allocator; }
const AllocatorTy &getAllocator() const { return Allocator; }
using key_type = const char*;
using key_type = const char *;
using mapped_type = ValueTy;
using value_type = StringMapEntry<ValueTy>;
using size_type = size_t;
@ -333,17 +325,13 @@ public:
using const_iterator = StringMapConstIterator<ValueTy>;
using iterator = StringMapIterator<ValueTy>;
iterator begin() {
return iterator(TheTable, NumBuckets == 0);
}
iterator end() {
return iterator(TheTable+NumBuckets, true);
}
iterator begin() { return iterator(TheTable, NumBuckets == 0); }
iterator end() { return iterator(TheTable + NumBuckets, true); }
const_iterator begin() const {
return const_iterator(TheTable, NumBuckets == 0);
}
const_iterator end() const {
return const_iterator(TheTable+NumBuckets, true);
return const_iterator(TheTable + NumBuckets, true);
}
iterator_range<StringMapKeyIterator<ValueTy>> keys() const {
@ -353,14 +341,16 @@ public:
iterator find(StringRef Key) {
int Bucket = FindKey(Key);
if (Bucket == -1) return end();
return iterator(TheTable+Bucket, true);
if (Bucket == -1)
return end();
return iterator(TheTable + Bucket, true);
}
const_iterator find(StringRef Key) const {
int Bucket = FindKey(Key);
if (Bucket == -1) return end();
return const_iterator(TheTable+Bucket, true);
if (Bucket == -1)
return end();
return const_iterator(TheTable + Bucket, true);
}
/// lookup - Return the entry for the specified key, or a default
@ -377,9 +367,7 @@ public:
ValueTy &operator[](StringRef Key) { return try_emplace(Key).first->second; }
/// count - Return 1 if the element is in the map, 0 otherwise.
size_type count(StringRef Key) const {
return find(Key) == end() ? 0 : 1;
}
size_type count(StringRef Key) const { return find(Key) == end() ? 0 : 1; }
template <typename InputTy>
size_type count(const StringMapEntry<InputTy> &MapEntry) const {
@ -393,7 +381,7 @@ public:
unsigned BucketNo = LookupBucketFor(KeyValue->getKey());
StringMapEntryBase *&Bucket = TheTable[BucketNo];
if (Bucket && Bucket != getTombstoneVal())
return false; // Already exists in map.
return false; // Already exists in map.
if (Bucket == getTombstoneVal())
--NumTombstones;
@ -447,14 +435,15 @@ public:
// clear - Empties out the StringMap
void clear() {
if (empty()) return;
if (empty())
return;
// Zap all values, resetting the keys back to non-present (not tombstone),
// which is safe because we're removing all elements.
for (unsigned I = 0, E = NumBuckets; I != E; ++I) {
StringMapEntryBase *&Bucket = TheTable[I];
if (Bucket && Bucket != getTombstoneVal()) {
static_cast<MapEntryTy*>(Bucket)->Destroy(Allocator);
static_cast<MapEntryTy *>(Bucket)->Destroy(Allocator);
}
Bucket = nullptr;
}
@ -465,9 +454,7 @@ public:
/// 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.
void remove(MapEntryTy *KeyValue) {
RemoveKey(KeyValue);
}
void remove(MapEntryTy *KeyValue) { RemoveKey(KeyValue); }
void erase(iterator I) {
MapEntryTy &V = *I;
@ -477,7 +464,8 @@ public:
bool erase(StringRef Key) {
iterator I = find(Key);
if (I == end()) return false;
if (I == end())
return false;
erase(I);
return true;
}
@ -496,7 +484,8 @@ public:
explicit StringMapIterBase(StringMapEntryBase **Bucket,
bool NoAdvance = false)
: Ptr(Bucket) {
if (!NoAdvance) AdvancePastEmptyBuckets();
if (!NoAdvance)
AdvancePastEmptyBuckets();
}
DerivedTy &operator=(const DerivedTy &Other) {

View File

@ -1,4 +1,4 @@
//===- StringSet.h - The LLVM Compiler Driver -------------------*- C++ -*-===//
//===- StringSet.h - An efficient set built on StringMap --------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
@ -14,43 +14,38 @@
#define LLVM_ADT_STRINGSET_H
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/AllocatorBase.h"
#include <cassert>
#include <initializer_list>
#include <utility>
namespace llvm {
/// StringSet - A wrapper for StringMap that provides set-like functionality.
template <class AllocatorTy = MallocAllocator>
class StringSet : public StringMap<NoneType, AllocatorTy> {
using base = StringMap<NoneType, AllocatorTy>;
/// StringSet - A wrapper for StringMap that provides set-like functionality.
template <class AllocatorTy = MallocAllocator>
class StringSet : public StringMap<NoneType, AllocatorTy> {
using Base = StringMap<NoneType, AllocatorTy>;
public:
StringSet() = default;
StringSet(std::initializer_list<StringRef> S) {
for (StringRef X : S)
insert(X);
}
explicit StringSet(AllocatorTy A) : base(A) {}
public:
StringSet() = default;
StringSet(std::initializer_list<StringRef> initializer) {
for (StringRef str : initializer)
insert(str);
}
explicit StringSet(AllocatorTy a) : Base(a) {}
std::pair<typename base::iterator, bool> insert(StringRef Key) {
return base::insert(std::make_pair(Key, None));
}
std::pair<typename Base::iterator, bool> insert(StringRef key) {
return Base::insert(std::make_pair(key, None));
}
template <typename InputIt>
void insert(const InputIt &Begin, const InputIt &End) {
for (auto It = Begin; It != End; ++It)
base::insert(std::make_pair(*It, None));
}
template <typename InputIt>
void insert(const InputIt &begin, const InputIt &end) {
for (auto it = begin; it != end; ++it)
Base::insert(std::make_pair(*it, None));
}
template <typename ValueTy>
std::pair<typename base::iterator, bool>
insert(const StringMapEntry<ValueTy> &MapEntry) {
return insert(MapEntry.getKey());
}
};
template <typename ValueTy>
std::pair<typename Base::iterator, bool>
insert(const StringMapEntry<ValueTy> &mapEntry) {
return insert(mapEntry.getKey());
}
};
} // end namespace llvm

View File

@ -15,7 +15,7 @@
#define LLVM_SUPPORT_POINTERLIKETYPETRAITS_H
#include "llvm/Support/DataTypes.h"
#include <assert.h>
#include <cassert>
#include <type_traits>
namespace llvm {
@ -37,8 +37,9 @@ template <typename T, typename U = void> struct HasPointerLikeTypeTraits {
};
// sizeof(T) is valid only for a complete T.
template <typename T> struct HasPointerLikeTypeTraits<
T, decltype((sizeof(PointerLikeTypeTraits<T>) + sizeof(T)), void())> {
template <typename T>
struct HasPointerLikeTypeTraits<
T, decltype((sizeof(PointerLikeTypeTraits<T>) + sizeof(T)), void())> {
static const bool value = true;
};