00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #ifndef SCV_ASSOCIATIVE_ARRAY_H
00044 #define SCV_ASSOCIATIVE_ARRAY_H
00045
00046 #include "scv/_scv_data_structure.h"
00047 #include "scv/scv_config.h"
00048 #include "scv/scv_util.h"
00049 #include "scv/scv_random.h"
00050 #include <map>
00051
00052 #if defined(_USE_HASH_MAP)
00053 #include <hash_map.h>
00054 #endif
00055
00056 template <class Data>
00057 ostream& operator<<(ostream&os, const list<Data> & t) {
00058 typename list<Data>::const_iterator temp;
00059 for (temp=t.begin(); temp != t.end(); ++temp) {
00060 os << (*temp) << endl;
00061 }
00062 return os;
00063 }
00064
00065 template <class Key, class Data, typename container_type = map<Key, Data, less<Key> > >
00066 class _scv_associative_array : public _scv_data_structure {
00067 public:
00068 typedef typename container_type::iterator iteratorT;
00069 typedef typename container_type::const_iterator constIteratorT;
00070 typedef typename container_type::const_iterator const_iterator;
00071 private:
00072 Data _defaultValue;
00073 container_type _map;
00074
00075 public:
00076
00077 _scv_associative_array(const char *name,
00078 const Data& defaultValue)
00079 : _scv_data_structure(name), _defaultValue(defaultValue), _map() { }
00080
00081
00082
00083
00084 const Data& defaultValue() const { return _defaultValue; }
00085 const Data& default_value() const { return _defaultValue; }
00086
00087
00088
00089
00090 _scv_associative_array(const _scv_associative_array& other, const char *nameP="<anonymous>")
00091 : _scv_data_structure(nameP), _defaultValue(other._defaultValue), _map(other._map) { }
00092
00093
00094
00095
00096 virtual ~_scv_associative_array() { }
00097
00098 virtual const char *kind() const
00099 { static const char name[]="_scv_associative_array"; return name; }
00100
00101
00102
00103
00104
00105 virtual Data& operator[](const Key& i) {
00106 if (_map.count(i)) return _map[i];
00107 else {
00108 pair<const Key, Data> tmp(i, _defaultValue); _map.insert(tmp);
00109 return _map[i];
00110 }
00111 }
00112 virtual const Data& operator[](const Key& i) const {
00113 if (_map.count(i)) {
00114 container_type * myMap = (container_type *) &_map;
00115 return (*myMap)[i];
00116 }
00117 else {
00118 return _defaultValue;
00119 }
00120 }
00121
00122 void insert(const Key& k, const Data& data){
00123 _map[k] = data ;
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 const Data& getValue(const Key& i) const {
00137 return (*this)[i] ;
00138 }
00139
00140
00141
00142
00143
00144 void erase(const Key& i) { _map.erase((const Key&)i); }
00145
00146
00147
00148
00149 void clear() { _map.erase(_map.begin(), _map.end()); }
00150
00151
00152
00153
00154
00155 iteratorT begin() { return _map.begin(); }
00156 iteratorT end() { return _map.end(); }
00157 constIteratorT begin() const { return _map.begin(); }
00158 constIteratorT end() const { return _map.end(); }
00159
00160 bool empty() const { return _map.empty(); }
00161
00162 iteratorT find(const Key& i) { return _map.find(i); }
00163 constIteratorT find(const Key& i) const { return _map.find(i); }
00164
00165
00166
00167
00168 _scv_associative_array& operator=(const _scv_associative_array& other) {
00169 if (this != &other) {
00170 _map = other._map;
00171 }
00172 return *this;
00173 }
00174 bool is_equal(const _scv_associative_array<Key, Data, container_type> & other) const ;
00175
00176
00177
00178
00179
00180 int numExplicitElt() const { return _map.size(); }
00181 int num_explicit_elt() const { return _map.size(); }
00182
00183
00184
00185
00186
00187 bool hasExplicitElt() const { return numExplicitElt() > 0; }
00188 bool has_explicit_elt() const { return numExplicitElt() > 0; }
00189
00190
00191
00192
00193
00194
00195 int numNonDefaultElt() const ;
00196 int num_non_default_elt() const ;
00197
00198
00199
00200
00201
00202 int hasNonDefaultElt() const;
00203 int has_non_default_elt() const;
00204
00205 virtual void print(ostream& os=scv_out, int details=0, int indent=0) const {
00206 typename container_type::const_iterator temp;
00207 os << kind() << " Name: " << get_name() << endl;
00208 if ( ! details ) return;
00209 for (temp = _map.begin(); temp != _map.end(); ++temp) {
00210 os << "\t[ " << (*temp).first << " ] = ";
00211 os << (*temp).second << endl;
00212 }
00213 }
00214
00215 virtual void show(int details=0, int indent=0) const {
00216 print(scv_out, details, indent);
00217 }
00218
00219
00220
00221 };
00222
00223 template <class Key, class Data, typename container_type>
00224 ostream& operator<<(ostream&os, const _scv_associative_array<Key, Data, container_type>& t){
00225 t.print(os, 1);
00226 return(os);
00227 }
00228
00229 template <class Key, class Data, typename container_type>
00230 bool _scv_associative_array<Key, Data, container_type>::is_equal(const _scv_associative_array<Key, Data, container_type>& other) const {
00231 return this->_map == other._map;
00232 }
00233
00234 template <class Key, class Data, typename container_type>
00235 bool operator==(const _scv_associative_array<Key, Data, container_type>& a,
00236 const _scv_associative_array<Key, Data, container_type>& b){
00237 return a.is_equal(b);
00238 }
00239
00240 template <class Key, class Data, typename container_type>
00241 int _scv_associative_array<Key, Data, container_type>::numNonDefaultElt() const {
00242 int nelements = 0;
00243 constIteratorT temp;
00244 for(temp = _map.begin(); temp != _map.end(); ++temp) {
00245 if ((*temp).second != _defaultValue) nelements++;
00246 }
00247 return nelements;
00248 }
00249
00250 template <class Key, class Data, typename container_type>
00251 int _scv_associative_array<Key, Data, container_type>::num_non_default_elt() const {
00252 return _scv_associative_array<Key, Data, container_type>::numNonDefaultElt();
00253 }
00254
00255 template <class Key, class Data, typename container_type>
00256 int _scv_associative_array<Key, Data, container_type>::hasNonDefaultElt() const {
00257 constIteratorT temp;
00258 for(temp = _map.begin(); temp != _map.end(); ++temp) {
00259 if ((*temp).second != _defaultValue) return 1;
00260 }
00261 return 0;
00262 }
00263
00264 template <class Key, class Data, typename container_type>
00265 int _scv_associative_array<Key, Data, container_type>::has_non_default_elt() const {
00266 return _scv_associative_array<Key, Data, container_type>::hasNonDefaultElt();
00267 }
00268
00269 #if defined(_USE_HASH_MAP)
00270
00271
00272 template<class T>
00273 class hash<T*> : public hash<T> {
00274 public:
00275 size_t operator()(T * i) const { return hash<T>::operator()((T) i)
00276 ; }
00277 };
00278
00279 template<>
00280 class hash<string> : public hash<int> {
00281 public:
00282 size_t operator()(const string &i) const { return hash<int>::operator()((int) atoi(i.c_str())); }
00283 };
00284
00285 #endif
00286
00287 #endif // SCV_ASSOCIATIVE_ARRAY_H
00288
00289
00290