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