#include "llvm/Support/Allocator.h"
#include <cstring>
+#include <string>
namespace llvm {
template<typename ValueT>
/// and data.
template<typename ValueTy>
class StringMapEntry : public StringMapEntryBase {
- ValueTy Val;
public:
- explicit StringMapEntry(unsigned StrLen)
- : StringMapEntryBase(StrLen), Val() {}
- StringMapEntry(unsigned StrLen, const ValueTy &V)
- : StringMapEntryBase(StrLen), Val(V) {}
+ ValueTy second;
- const ValueTy &getValue() const { return Val; }
- ValueTy &getValue() { return Val; }
+ explicit StringMapEntry(unsigned strLen)
+ : StringMapEntryBase(strLen), second() {}
+ StringMapEntry(unsigned strLen, const ValueTy &V)
+ : StringMapEntryBase(strLen), second(V) {}
- void setValue(const ValueTy &V) { Val = V; }
+ const ValueTy &getValue() const { return second; }
+ ValueTy &getValue() { return second; }
+
+ void setValue(const ValueTy &V) { second = V; }
/// 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 *first() const { return getKeyData(); }
+
/// Create - Create a StringMapEntry for the specified key and default
/// construct the value.
template<typename AllocatorTy, typename InitType>
static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd,
AllocatorTy &Allocator,
InitType InitVal) {
- unsigned KeyLength = KeyEnd-KeyStart;
+ unsigned KeyLength = static_cast<unsigned>(KeyEnd-KeyStart);
// Okay, the item doesn't already exist, and 'Bucket' is the bucket to fill
// in. Allocate a new item with space for the string at the end and a null
// terminator.
- unsigned AllocSize = sizeof(StringMapEntry)+KeyLength+1;
+ unsigned AllocSize = static_cast<unsigned>(sizeof(StringMapEntry))+
+ KeyLength+1;
unsigned Alignment = alignof<StringMapEntry>();
StringMapEntry *NewItem =
static StringMapEntry &GetStringMapEntryFromValue(ValueTy &V) {
StringMapEntry *EPtr = 0;
char *Ptr = reinterpret_cast<char*>(&V) -
- (reinterpret_cast<char*>(&EPtr->Val) -
+ (reinterpret_cast<char*>(&EPtr->second) -
reinterpret_cast<char*>(EPtr));
return *reinterpret_cast<StringMapEntry*>(Ptr);
}
AllocatorTy Allocator;
typedef StringMapEntry<ValueTy> MapEntryTy;
public:
- StringMap() : StringMapImpl(sizeof(MapEntryTy)) {}
+ StringMap() : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))) {}
explicit StringMap(unsigned InitialSize)
- : StringMapImpl(InitialSize, sizeof(MapEntryTy)) {}
+ : StringMapImpl(InitialSize, static_cast<unsigned>(sizeof(MapEntryTy))) {}
AllocatorTy &getAllocator() { return Allocator; }
const AllocatorTy &getAllocator() const { return Allocator; }
+ typedef const char* key_type;
+ typedef ValueTy mapped_type;
+ typedef StringMapEntry<ValueTy> value_type;
+ typedef size_t size_type;
+
typedef StringMapConstIterator<ValueTy> const_iterator;
typedef StringMapIterator<ValueTy> iterator;
if (Bucket == -1) return end();
return iterator(TheTable+Bucket);
}
+ iterator find(const char *Key) {
+ return find(Key, Key + strlen(Key));
+ }
+ iterator find(const std::string &Key) {
+ const char* key_start = (Key.empty() ? NULL : &Key[0]);
+ return find(key_start, key_start + Key.size());
+ }
const_iterator find(const char *KeyStart, const char *KeyEnd) const {
int Bucket = FindKey(KeyStart, KeyEnd);
if (Bucket == -1) return end();
return const_iterator(TheTable+Bucket);
}
+ const_iterator find(const char *Key) const {
+ return find(Key, Key + strlen(Key));
+ }
+ const_iterator find(const std::string &Key) const {
+ const char* key_start = (Key.empty() ? NULL : &Key[0]);
+ return find(key_start, key_start + Key.size());
+ }
+
+ ValueTy& operator[](const char *Key) {
+ value_type& entry = GetOrCreateValue(Key, Key + strlen(Key));
+ return entry.getValue();
+ }
+ ValueTy& operator[](const std::string &Key) {
+ const char* key_start = (Key.empty() ? NULL : &Key[0]);
+ value_type& entry = GetOrCreateValue(key_start, key_start + Key.size());
+ return entry.getValue();
+ }
+
+ size_type count(const char *KeyStart, const char *KeyEnd) const {
+ return find(KeyStart, KeyEnd) == end() ? 0 : 1;
+ }
+ size_type count(const char *Key) const {
+ return count(Key, Key + strlen(Key));
+ }
+ size_type count(const std::string &Key) const {
+ const char* key_start = (Key.empty() ? NULL : &Key[0]);
+ return count(key_start, key_start + Key.size());
+ }
/// 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
V.Destroy(Allocator);
}
+ bool erase(const char *Key) {
+ iterator I = find(Key);
+ if (I == end()) return false;
+ erase(I);
+ return true;
+ }
+
+ bool erase(const std::string &Key) {
+ iterator I = find(Key);
+ if (I == end()) return false;
+ erase(I);
+ return true;
+ }
+
~StringMap() {
for (ItemBucket *I = TheTable, *E = TheTable+NumBuckets; I != E; ++I) {
if (I->Item && I->Item != getTombstoneVal())