KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StringMap.h
Go to the documentation of this file.
1 #ifndef STRING_MAP_H_
2 #define STRING_MAP_H_
3 
11 #include "spear/RCIPtr.h"
12 #include "spear/HashMap.h"
15 #include "spear/Wide.h"
16 
17 namespace spear {
18 
19  template <class T>
20  class StringMapEntry : public RCObject {
21 
22  public:
24  const T & value) {
25  _key = key;
26  _value = value;
27  };
28 
29  Char * getKey() const { return _key; };
30 
31  const T & getValue() const { return _value; };
32 
33  void setValue(const T & v) { _value = v; };
34 
36  delete [] _key;
37  };
38 
39  private:
40  Char * _key;
41  T _value;
42  };
43 
44  template <class T>
45  class StringMap {
46 
47  public:
48 
52 
56 
57  bool set(const Char * key, const T & value) {
58  if(contains(key)){
59  return false;
60  }
61 
62  int keyLen = 0;
63  for( ; key[keyLen] != 0; keyLen ++);
64 
65  Char * newKey = new Char[keyLen + 1];
66  for(int i = 0; i < keyLen; i ++) newKey[i] = key[i];
67  newKey[keyLen] = '\0';
68 
69  RCIPtr< StringMapEntry<T> > p(new StringMapEntry<T>(newKey, value));
70  _map[p->getKey()] = p;
71 
72  return true;
73  }
74 
75  bool overwrite(const Char * key, const T & value) {
76  if(contains(key)){
77  iterator it = _map.find(key);
78  (* it).second->setValue(value);
79  return true;
80  } else {
81  return set(key, value);
82  }
83  }
84 
85  bool get(const Char * key, T & value) const {
86  const_iterator it;
87 
88  if((it = _map.find(key)) != _map.end()){
89  value = (* it).second->getValue();
90  return true;
91  }
92 
93  return false;
94  }
95 
96  RCIPtr< StringMapEntry<T> > get(const Char * key) const {
97  const_iterator it;
98 
99  if((it = _map.find(key)) != _map.end()){
100  return (* it).second;
101  }
102 
103  return RCIPtr< StringMapEntry<T> >();
104  }
105 
106  bool contains(const Char * key) const {
107  if(_map.find(key) != _map.end()){
108  return true;
109  }
110 
111  return false;
112  }
113 
114  void getKeys(std::vector<std::string> & keys) const {
115  for(const_iterator it = begin(); it != end(); it ++){
116  keys.push_back((* it).second->getKey());
117  }
118  }
119 
120  size_t size() const { return _map.size(); };
121 
122  bool empty() const { return _map.empty(); };
123 
124  const_iterator begin() const { return _map.begin(); };
125 
126  const_iterator end() const { return _map.end(); };
127 
128  iterator mbegin() { return _map.begin(); };
129 
130  iterator mend() { return _map.end(); };
131 
132  protected:
133 
137 
138  };
139 
140 } // end namespace spear
141 
142 #endif
spear::HashMap< const Char *, RCIPtr< StringMapEntry< bool > >, spear::CharArrayHashFunc, spear::CharArrayEqualFunc >::const_iterator const_iterator
Definition: StringMap.h:55
const_iterator end() const
Definition: StringMap.h:126
spear::HashMap< const Char *, RCIPtr< StringMapEntry< T > >, spear::CharArrayHashFunc, spear::CharArrayEqualFunc > _map
Definition: StringMap.h:130
Reference counting pointer class This file contains the code for the classes and class templates maki...
Definition: HashMap.h:18
Char * getKey() const
Definition: StringMap.h:29
Definition: RCIPtr.h:67
Definition: StringMap.h:45
char Char
Definition: Wide.h:34
iterator mbegin()
Definition: StringMap.h:128
spear::HashMap< const Char *, RCIPtr< StringMapEntry< T > >, spear::CharArrayHashFunc, spear::CharArrayEqualFunc >::iterator iterator
Definition: StringMap.h:51
void setValue(const T &v)
Definition: StringMap.h:33
const T & getValue() const
Definition: StringMap.h:31
iterator mend()
Definition: StringMap.h:130
~StringMapEntry()
Definition: StringMap.h:35
struct spear::CharArrayHashFunc CharArrayHashFunc
bool contains(const Char *key) const
Definition: StringMap.h:106
size_t size() const
Definition: StringMap.h:120
bool overwrite(const Char *key, const T &value)
Definition: StringMap.h:75
bool empty() const
Definition: StringMap.h:122
Definition: RCIPtr.h:24
const_iterator begin() const
Definition: StringMap.h:124
bool set(const Char *key, const T &value)
Definition: StringMap.h:57
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
void getKeys(std::vector< std::string > &keys) const
Definition: StringMap.h:114
StringMapEntry(Char *key, const T &value)
Definition: StringMap.h:23
Definition: CharArrayEqualFunc.h:15
Definition: StringMap.h:20