C++ main module for gpm Package  1.0
CORE_Map.hpp
Go to the documentation of this file.
1 #ifndef CORE_Map_CPP
2 #define CORE_Map_CPP
3 
4 #include "CORE_Map.h"
5 #include "CORE_String.h"
6 
7 
8 
9 template<class Key,class Value>
11 }
12 
13 
14 
15 template<class Key,class Value>
17  this();
18  vector<Key> keys;
19  m.getKeys(keys);
20  int n=keys.size();
21  for (int i=0;i<n;i++)
22  put(keys[i],m.get(keys[i]));
23 }
24 
25 
26 
27 template<class Key,class Value>
29  clear();
30 }
31 
32 template<class Key,class Value>
33 template<class K2, class V2>
35  if (this==&copyMap) return;
36  vector<K2> keys;
37  copyMap.getKeys(keys);
38  clear();
39  int n=keys.size();
40  for (int i=0;i<n;i++) {
41  Key newKey=(Key) keys[i];
42  const Value *newValue=(const Value *) copyMap.get(keys[i]);
43  put(newKey,*newValue);
44  }
45 }
46 template<class Key,class Value>
48  typename map<Key,Value>::const_iterator iter=mMap.find(k);
49  if (iter==mMap.end()) return false;
50  return true;
51 }
52 
53 template<class Key,class Value>
54 const Value* CORE_Map<Key,Value>::get(const Key& k) const {
55  typename map<Key,Value>::const_iterator iter=mMap.find(k);
56  if (iter==mMap.end()) return null;
57  return &(iter->second);
58 }
59 template<class Key,class Value>
60 Value* CORE_Map<Key,Value>::get(const Key& k) {
61  typename map<Key,Value>::iterator iter=mMap.find(k);
62  if (iter==mMap.end()) return null;
63  return &(iter->second);
64 }
65 
66 template<class Key,class Value>
68  tInteger n=mMap.size();
69  vals.clear();
70  vals.setSize(n);
71  typename map<Key,Value>::const_iterator iter=mMap.begin();
72  typename map<Key,Value>::const_iterator end=mMap.end();
73  tInteger i=0;
74  while (iter!=end) {
75  vals.set(i++,iter->second);
76  iter++;
77  }
78 }
79 template<class Key,class Value>
81  tInteger n=mMap.size();
82  vals.clear();
83  vals.setSize(n);
84  typename map<Key,Value>::const_iterator iter=mMap.begin();
85  typename map<Key,Value>::const_iterator end=mMap.end();
86  tInteger i=0;
87  while (iter!=end) {
88  vals.set(i++,iter->second);
89  iter++;
90  }
91 }
92 template<class Key,class Value>
93 void CORE_Map<Key,Value>::getValues(vector<Value>& vals) const {
94  tInteger n=mMap.size();
95  vals.clear();
96  vals.resize(n);
97  typename map<Key,Value>::const_iterator iter=mMap.begin();
98  typename map<Key,Value>::const_iterator end=mMap.end();
99  tInteger i=0;
100  while (iter!=end) {
101  vals[i++]=iter->second;
102  iter++;
103  }
104 }
105 
106 
107 
108 template<class Key,class Value>
109 void CORE_Map<Key,Value>::getKeys(vector<Key>& keys) const {
110  int nbKeys=mMap.size();
111  keys.resize(nbKeys);
112  typename map<Key,Value>::const_iterator iter=mMap.begin();
113  typename map<Key,Value>::const_iterator end=mMap.end();
114  int i=0;
115  while (iter!=end) {
116  keys[i]=iter->first;
117  iter++;
118  i++;
119  }
120 }
121 
122 template<class Key,class Value>
124  keys.clear();
125  typename map<Key,Value>::const_iterator iter=mMap.begin();
126  typename map<Key,Value>::const_iterator end=mMap.end();
127  while (iter!=end) {
128  keys.add(iter->first);
129  iter++;
130  }
131 }
132 template<class Key,class Value>
134  keys.clear();
135  typename map<Key,Value>::const_iterator iter=mMap.begin();
136  typename map<Key,Value>::const_iterator end=mMap.end();
137  while (iter!=end) {
138  keys.add(iter->first);
139  iter++;
140  }
141 }
142 template<class Key,class Value>
144  vector<Key> ks;
145  m.getKeys(ks);
146  int nbKeys=ks.size();
147  for (int i=0;i<nbKeys;i++) {
148  put(ks[i],m.get(ks[i]));
149  }
150 }
151 
152 template<class Key,class Value>
154  typename map<Key,Value>::const_iterator exists=mMap.find(k);
155  if (exists!=mMap.end()) {
156  mMap.erase(k);
157  return true;
158  };
159  return false;
160 };
161 
162 template<class Key,class Value>
164  tBoolean ret=false;
165  vector<Key> ks;
166  getKeys(ks);
167  int n=ks.size();
168  for (int i=0;i<n;i++) {
169  if (get(ks[i])==v) ret=remove(ks[i]);
170  }
171  return ret;
172 };
173 
174 #endif
175 
tBoolean exists(const Key &k) const
exists return true if the key exists in map
Definition: CORE_Map.hpp:47
virtual void clear()
clear the array
Definition: CORE_Array.h:489
tBoolean remove(const Key &k)
remove the key
Definition: CORE_Map.hpp:153
void merge(const CORE_Map< Key, Value > &m)
merge the map
Definition: CORE_Map.hpp:143
this class describes an array
Definition: CORE_Vector.h:18
void set(const int &i, const T &obj)
set the object at the index i
Definition: CORE_Array.h:339
void keys(CORE_Vector< Key > &ks) const
return an array of keys
Definition: CORE_Map.h:153
tBoolean removeValue(const Value &k)
remove the value
Definition: CORE_Map.hpp:163
#define tBoolean
Definition: types.h:35
void add(const CORE_Vector< T > &v)
init the value to v
Definition: CORE_Vector.h:206
#define null
Definition: types.h:13
const Value * get(const Key &k) const
get the value at key k
Definition: CORE_Map.hpp:54
void add(const T &obj)
add an element at the end re-allocate the array if capacity too small
Definition: CORE_Array.hpp:157
void setSize(const int &n)
set the size of the array
Definition: CORE_Array.hpp:141
void put(const Key &k, const Value &v)
set the value at the index k
Definition: CORE_Map.h:178
this class describes a map
Definition: CORE_Map.h:18
this class describes an array
Definition: CORE_Array.h:18
virtual ~CORE_Map()
destroy a map
Definition: CORE_Map.hpp:28
abstract base class for most classes.
Definition: CORE_Object.h:30
tBoolean set(int i, const T &obj)
set the pointer at the index i
Definition: CORE_Vector.hpp:178
void setSize(const int &n)
set the size of the array
Definition: CORE_Vector.h:162
void copy(const CORE_Map< K2, V2 > &mapCpy)
copy a map
Definition: CORE_Map.hpp:34
CORE_Map()
build a map
Definition: CORE_Map.hpp:10
void clear()
clear the array
Definition: CORE_Vector.h:350
void getValues(CORE_Vector< Value > &vals) const
return an array of values
Definition: CORE_Map.hpp:67
#define tInteger
Definition: types.h:32
void getKeys(vector< Key > &ks) const
return a vector of keys
Definition: CORE_Map.hpp:109