1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
/* This file is part of The New Aspell
* Copyright (C) 2001 by Kevin Atkinson under the GNU LGPL
* license version 2.0 or 2.1. You should have received a copy of the
* LGPL license along with this library if you did not you can find it
* at http://www.gnu.org/. */
#ifndef ASPELL_STRING_MAP__HPP
#define ASPELL_STRING_MAP__HPP
#include "mutable_container.hpp"
#include "parm_string.hpp"
#include "posib_err.hpp"
#include "string_pair.hpp"
#include "hash-t.hpp"
#include "objstack.hpp"
namespace acommon {
class StringPairEnumeration;
using std::pair;
class StringMap : public MutableContainer {
public: // but don't use
struct Parms {
typedef StringPair Value;
typedef const char * Key;
const char * key(const Value & v) {return v.first;}
static const bool is_multi = false;
acommon::hash<const char *> hash;
bool equal(const char * x, const char * y) {return strcmp(x,y) == 0;}
};
typedef StringPair Value_;
typedef HashTable<Parms> Lookup;
typedef Lookup::iterator Iter_;
typedef Lookup::const_iterator CIter_;
private:
HashTable<Parms> lookup_;
ObjStack buffer_;
/*const */ char empty_str[1];
void copy(const StringMap & other);
// copy and destructor provided
public:
PosibErr<void> clear() {lookup_.clear(); buffer_.reset(); return no_err;}
StringMap() {empty_str[0] = '\0';}
StringMap(const StringMap & other) {empty_str[0] = '\0'; copy(other);}
StringMap & operator= (const StringMap & o) {clear(); copy(o); return *this;}
~StringMap() {}
StringMap * clone() const {
return new StringMap(*this);
}
void assign(const StringMap * other) {
*this = *(const StringMap *)(other);
}
StringPairEnumeration * elements() const;
// insert a new element. Will NOT overwrite an existing entry.
// returns false if the element already exists.
bool insert(ParmStr key, ParmStr value) {
pair<Iter_,bool> res = lookup_.insert(Value_(key,0));
if (res.second) {
res.first->first = buffer_.dup(key);
res.first->second = buffer_.dup(value);
return true;
} else {
return false;
}
}
PosibErr<bool> add(ParmStr key) {
pair<Iter_,bool> res = lookup_.insert(Value_(key,0));
if (res.second) {
res.first->first = buffer_.dup(key);
res.first->second = empty_str;
return true;
} else {
return false;
}
}
// insert a new element. WILL overwrite an exitsing entry
// always returns true
bool replace(ParmStr key, ParmStr value) {
pair<Iter_,bool> res = lookup_.insert(Value_(key,0));
if (res.second) {
res.first->first = buffer_.dup(key);
res.first->second = buffer_.dup(value);
} else {
res.first->second = buffer_.dup(value);
}
return true;
}
// removes an element. Returns true if the element existed.
PosibErr<bool> remove(ParmStr key) {return lookup_.erase(key);}
// looks up an element. Returns null if the element did not exist.
// returns an empty string if the element exists but has a null value
// otherwise returns the value
const char * lookup(ParmStr key) const
{
CIter_ i = lookup_.find(key);
if (i == lookup_.end())
return 0;
else
return i->second;
}
bool have(ParmStr key) const {return lookup(key) != 0;}
unsigned int size() const {return lookup_.size();}
bool empty() const {return lookup_.empty();}
};
StringMap * new_string_map();
}
#endif /* ASPELL_STRING_MAP__HPP */
|