00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __WEIBASE_H__
00021 #define __WEIBASE_H__
00022
00023 #include <boost/serialization/base_object.hpp>
00024 #include <boost/serialization/utility.hpp>
00025 #include <boost/serialization/list.hpp>
00026 #include <boost/serialization/map.hpp>
00027 #include <boost/serialization/assume_abstract.hpp>
00028 #include <boost/variant.hpp>
00029 #include <boost/serialization/variant.hpp>
00030 #include <string>
00031 #include <algorithm>
00032 #include <map>
00033 #include <vector>
00034 #include "weLogger.h"
00035
00036 using namespace std;
00037
00038 #define DECLARE_SERIALIZATOR \
00039 friend class boost::serialization::access; \
00040 template<class Archive> \
00041 void serialize(Archive & ar, const unsigned int version)
00042
00043
00044 #define DECLARE_SERIAL_SAVE \
00045 template<class Archive> \
00046 void save(Archive & ar, const unsigned int version) const
00047
00048 #define DECLARE_SERIAL_LOAD \
00049 template<class Archive> \
00050 void load(Archive & ar, const unsigned int version)
00051
00052 #define DECLARE_SERIAL_BASE friend class boost::serialization::access;
00053
00054
00063 template <typename _Key, typename _Val>
00064 class WeLinkedListElem
00065 {
00066 public:
00067 WeLinkedListElem() :
00068 first(), second()
00069 {
00070 next = NULL;
00071 };
00072
00073 WeLinkedListElem( WeLinkedListElem<_Key, _Val> &elem ) :
00074 first(), second()
00075 {
00076 first = elem.first;
00077 second = elem.second;
00078 next = NULL;
00079 };
00080
00081 ~WeLinkedListElem()
00082 {
00083 next = NULL;
00084 }
00085
00086 void Add(WeLinkedListElem<_Key, _Val> *elem)
00087 {
00088 WeLinkedListElem<_Key, _Val> *curr = next;
00089 next = elem;
00090 elem->next = curr;
00091 }
00092
00093
00094 void Link(WeLinkedListElem<_Key, _Val> *elem) { next = elem; };
00095 WeLinkedListElem<_Key, _Val>* Next() {return next;};
00096
00097
00098 const _Key &Key(void) const { return(first); };
00099 void Key(const _Key &elem) { first = elem; };
00100
00101
00102 const _Val &Value(void) const { return(second); };
00103 void Value(const _Val &elem) { second = elem; };
00104
00105 protected:
00106 _Key first;
00107 _Val second;
00108 WeLinkedListElem<_Key, _Val> *next;
00109
00110 private:
00111 DECLARE_SERIAL_BASE;
00112 DECLARE_SERIAL_SAVE
00113 {
00114 bool last = (next == NULL);
00115 ar & BOOST_SERIALIZATION_NVP(first);
00116 ar & BOOST_SERIALIZATION_NVP(second);
00117 ar & BOOST_SERIALIZATION_NVP(last);
00118 if (! last) {
00119 ar & BOOST_SERIALIZATION_NVP(next);
00120 };
00121 };
00122 DECLARE_SERIAL_LOAD
00123 {
00124 bool last;
00125 ar & BOOST_SERIALIZATION_NVP(first);
00126 ar & BOOST_SERIALIZATION_NVP(second);
00127 ar & BOOST_SERIALIZATION_NVP(last);
00128 if (!last) {
00129 next = new WeLinkedListElem<_Key, _Val>;
00130 ar & BOOST_SERIALIZATION_NVP(next);
00131 }
00132 };
00133 BOOST_SERIALIZATION_SPLIT_MEMBER();
00134 };
00135
00136 template <typename _Key, typename _Val>
00137
00146 class WeLinkedList
00147 {
00148 public:
00149 WeLinkedList()
00150 {
00151 data = NULL;
00152 curr = NULL;
00153 };
00154 WeLinkedList(WeLinkedList<_Key, _Val>& lst)
00155 {
00156 WeLinkedListElem<_Key, _Val> *obj;
00157
00158 data = NULL;
00159 curr = NULL;
00160 obj = lst.First();
00161 data = new WeLinkedListElem<_Key, _Val>(*obj);
00162 curr = data;
00163 obj = lst++;
00164 while (obj != NULL) {
00165 curr->Add(new WeLinkedListElem<_Key, _Val>(*obj));
00166 curr = curr->Next();
00167 obj = lst++;
00168 }
00169 };
00170 virtual ~WeLinkedList()
00171 {
00172 Clear();
00173 data = NULL;
00174 curr = NULL;
00175 };
00176
00177 virtual _Val& FindFirst(_Key name)
00178 {
00179 WeLinkedListElem<_Key, _Val>* elem = curr;
00180
00181 curr = data;
00182 while (curr != NULL) {
00183 if (curr->Key() == name) {
00184 break;
00185 }
00186 curr = curr->Next();
00187 }
00188 if (curr != NULL) {
00189 return *(new _Val(curr->Value()));
00190 }
00191 curr = elem;
00192 return *(new _Val());
00193 };
00194
00195 virtual _Val& FindNext(_Key name)
00196 {
00197 WeLinkedListElem<_Key, _Val>* elem = curr;
00198
00199 if (curr != NULL) {
00200 curr = curr->Next();
00201 }
00202 while (curr != NULL) {
00203 if (curr->Key() == name) {
00204 break;
00205 }
00206 curr = curr->Next();
00207 }
00208 if (curr != NULL) {
00209 return *(new _Val(curr->Value()));
00210 }
00211 curr = elem;
00212 return *(new _Val());
00213 };
00214
00215 void Append(_Key name, _Val value)
00216 {
00217 WeLinkedListElem<_Key, _Val>* elem;
00218 WeLinkedListElem<_Key, _Val>* obj;
00219
00220 obj = new WeLinkedListElem<_Key, _Val>();
00221 obj->Key(name);
00222 obj->Value(value);
00223 if (data == NULL) {
00224 data = obj;
00225 curr = data;
00226 }
00227 else {
00228 elem = curr;
00229 Last();
00230 curr->Add(obj);
00231 curr = elem;
00232 }
00233 };
00234 void Insert(_Key name, _Val value)
00235 {
00236 WeLinkedListElem<_Key, _Val>* obj;
00237
00238 obj = new WeLinkedListElem<_Key, _Val>();
00239 obj->Key(name);
00240 obj->Value(value);
00241 if (data == NULL) {
00242 data = obj;
00243 curr = data;
00244 }
00245 else {
00246 curr->Add(obj);
00247 }
00248 };
00249 void Clear()
00250 {
00251 curr = data;
00252 while (curr != NULL) {
00253 curr = curr->Next();
00254 delete data;
00255 data = curr;
00256 }
00257 };
00258
00259 WeLinkedListElem<_Key, _Val>* First() { return (data); };
00260 WeLinkedListElem<_Key, _Val>* Last()
00261 {
00262 if (curr == NULL) {
00263 return curr;
00264 }
00265 while (curr->Next() != NULL) {
00266 curr = curr->Next();
00267 }
00268 return curr;
00269 };
00270 WeLinkedListElem<_Key, _Val>* Current() { return curr; };
00271 void Erase()
00272 {
00273 WeLinkedListElem<_Key, _Val>* obj;
00274
00275 if (curr == NULL) {
00276 return;
00277 }
00278 obj = data;
00279 while (obj != NULL) {
00280 if (obj->Next() == curr) {
00281 break;
00282 }
00283 obj = obj->Next();
00284 }
00285 if (obj == NULL) {
00286 return;
00287 }
00288 obj->Link(curr->Next());
00289
00290 };
00291 WeLinkedListElem<_Key, _Val>* operator++(int) { if(curr != NULL) { curr = curr->Next(); } return curr; };
00292
00293 protected:
00294 #ifndef __DOXYGEN__
00295 WeLinkedListElem<_Key, _Val>* data;
00296 WeLinkedListElem<_Key, _Val>* curr;
00297 #endif //__DOXYGEN__
00298 private:
00299 DECLARE_SERIAL_BASE;
00300 DECLARE_SERIAL_SAVE
00301 {
00302 bool last = (data == NULL);
00303 ar & BOOST_SERIALIZATION_NVP(last);
00304 if (!last) {
00305 ar & BOOST_SERIALIZATION_NVP(data);
00306 };
00307 };
00308 DECLARE_SERIAL_LOAD
00309 {
00310 bool last;
00311 ar & BOOST_SERIALIZATION_NVP(last);
00312 if (! last) {
00313 data = new WeLinkedListElem<_Key, _Val>;
00314 ar & BOOST_SERIALIZATION_NVP(data);
00315 }
00316 else {
00317 data = NULL;
00318 }
00319 curr = data;
00320 };
00321 BOOST_SERIALIZATION_SPLIT_MEMBER();
00322 };
00323
00324 template<class _Key, class _Val>
00325 class orderedmap
00326 {
00327 public:
00328 typedef typename std::vector<_Val> valvector;
00329 typedef typename std::vector<_Val>::iterator iterator;
00330 typedef typename std::map<_Key, int> mapkeys;
00331 typedef typename mapkeys::value_type value_type;
00332
00333 public:
00334 orderedmap()
00335 : values(), keys()
00336 {
00337 };
00338
00339 int erase(const _Key& _Keyval)
00340 {
00341
00342
00343 return (keys.erase(_Keyval));
00344 }
00345
00346 void clear() { values.clear(); keys.clear(); };
00347
00348 iterator begin() {return values.begin(); };
00349 iterator end() {return values.end(); };
00350
00351 iterator find(const _Key& _Keyval)
00352 {
00353 typename mapkeys::iterator it = keys.find(_Keyval);
00354 if(it != keys.end()) {
00355 return (values.begin() + it->second);
00356 }
00357 else {
00358 return values.end();
00359 }
00360
00361 }
00362
00363 _Key& key(iterator& _Where)
00364 {
00365 typename mapkeys::iterator it;
00366 for(it = keys.begin(); it != keys.end(); it++) {
00367 if ((values.begin() + it->second) == _Where) {
00368 return (_Key&)it->first;
00369 }
00370 }
00371 return *(new _Key());
00372 };
00373
00374 _Val& val(iterator& _Where)
00375 {
00376 return *_Where;
00377 };
00378
00379 _Val& operator[](const _Key& _Keyval)
00380 {
00381 typename mapkeys::iterator _Where = keys.find(_Keyval);
00382 if (_Where == keys.end()) {
00383 values.push_back(_Val());
00384 int ins = values.size() - 1;
00385 _Where = keys.insert(_Where, value_type(_Keyval, ins));
00386 }
00387 return (_Val&)values[_Where->second];
00388 }
00389
00390 protected:
00391 valvector values;
00392 mapkeys keys;
00393 };
00394
00395 #endif //__WEIBASE_H__