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 namespace stactiverecord {
00029
00030 template <class T>
00031 class SarVector : public std::vector<T> {
00032 public:
00033 SarVector() : std::vector<T>() {};
00034 bool includes(T value) {
00035 for(unsigned int i=0; i < this->size(); i++)
00036 if(this->at(i) == value)
00037 return true;
00038 return false;
00039 };
00040 void operator<<(T value) {
00041 this->push_back(value);
00042 };
00043 void dump() {
00044 for(unsigned int i=0; i < this->size(); i++)
00045 std::cout << this->at(i) << ", ";
00046 std::cout << "\n";
00047 };
00048 void dumpeach() {
00049 for(unsigned int i=0; i < this->size(); i++)
00050 this->at(i).dump();
00051 };
00052 void remove(T value) {
00053
00054 for(unsigned int i=0; i < this->size(); i++) {
00055 if(this->at(i) == value) {
00056 this->erase(this->begin() + i);
00057 remove(value);
00058 }
00059 }
00060 };
00061 int count(T value) {
00062 int count = 0;
00063 for(unsigned int i=0; i < this->size(); i++)
00064 if(this->at(i) == value)
00065 count++;
00066 return count;
00067 };
00068
00069 SarVector<T> get_new(SarVector<T> others) {
00070 SarVector<T> newones;
00071 for(unsigned int i=0; i < others.size(); i++)
00072 if(!this->includes(others[i]))
00073 newones << others[i];
00074 return newones;
00075 };
00076 void unionize(SarVector<T> others) {
00077 for(unsigned int i=0; i < others.size(); i++)
00078 if(!this->includes(others[i]))
00079 this->push_back(others[i]);
00080 };
00081 SarVector<T> intersects(SarVector<T> others) {
00082 SarVector<T> joined;
00083 for(unsigned int i=0; i < others.size(); i++)
00084 if(this->includes(others[i]))
00085 joined << others[i];
00086 return joined;
00087 };
00088 void operator+(SarVector<T> others) {
00089 for(unsigned int i=0; i < others.size(); i++)
00090 this->push_back(others[i]);
00091 };
00092 void operator-(SarVector<T> others) {
00093 for(unsigned int i=0; i < others.size(); i++)
00094 this->remove(others[i]);
00095 };
00096
00097 bool operator==(SarVector<T>& others) {
00098 if(this->size() != others.size())
00099 return false;
00100 for(unsigned int i=0; i < others.size(); i++)
00101 if(others.count(this->at(i)) != 1)
00102 return false;
00103 return true;
00104 };
00105 bool operator!=(SarVector<T>& others) {
00106 return !(*this == others);
00107 };
00108 };
00109
00110 template <class T>
00111 class SarMap : public std::map<std::string,T> {
00112 public:
00113 SarMap() : std::map<std::string,T>() {};
00114 bool has_key(std::string key) {
00115 for(typename SarMap<T>::iterator i=this->begin(); i!=this->end(); ++i)
00116 if(std::string((*i).first) == key) {
00117 return true;
00118 }
00119 return false;
00120 };
00121 void dump() {
00122 for(typename SarMap<T>::iterator i=this->begin(); i!=this->end(); ++i)
00123 std::cout << (*i).first << ": " << (*i).second << "\n";
00124 };
00125 void dumpeach() {
00126 for(typename SarMap<T>::iterator i=this->begin(); i!=this->end(); ++i) {
00127 std::cout << (*i).first << ": ";
00128 ((*i).second).dump();
00129 std::cout << "\n";
00130 }
00131 };
00132 void submap(SarVector<std::string> keys, SarMap<T>& submap) {
00133
00134 submap.clear();
00135 for(unsigned int i=0; i < keys.size(); i++)
00136 submap[keys[i]] = this->operator[](keys[i]);
00137 };
00138 void remove(std::string key) {
00139 for(typename SarMap<T>::iterator i=this->begin(); i!=this->end(); ++i) {
00140 if(std::string((*i).first) == key)
00141 this->erase(i);
00142 }
00143 };
00144 SarVector<std::string> keys() {
00145 SarVector<std::string> keys;
00146 for(typename SarMap<T>::iterator i=this->begin(); i!=this->end(); ++i)
00147 keys << std::string((*i).first);
00148 return keys;
00149 };
00150 bool operator==(SarMap<T>& others) {
00151 if(this->size() != others.size() || this->keys() != others.keys())
00152 return false;
00153 std::string key;
00154 for(typename SarMap<T>::iterator i=this->begin(); i!=this->end(); ++i) {
00155 key = std::string((*i).first);
00156 if(this->operator[](key) != others[key])
00157 return false;
00158 }
00159 return true;
00160 };
00161 bool operator!=(SarMap<T>& others) const {
00162 return !(*this == others);
00163 };
00164 };
00165
00166 template <class T>
00167 class ObjGroup : public SarVector<T> {
00168 private:
00169 SarVector<int> ids;
00170 bool slow_inits;
00171 void ids_to_objs() {
00172 for(unsigned int i=0; i < ids.size(); i++)
00173 this->push_back(T(ids[i]));
00174 slow_inits = false;
00175 };
00176 public:
00177 ObjGroup() : SarVector<T>(), slow_inits(false) {};
00178 ObjGroup(SarVector<T>& sr) : SarVector<T>(), slow_inits(false) {
00179 for(unsigned int i=0; i<sr.size(); i++)
00180 push_back(sr[i]);
00181 };
00182 ObjGroup(SarVector<int> _ids) : SarVector<T>(), slow_inits(true) { ids = _ids; };
00183 int size() {
00184 if(slow_inits)
00185 return ids.size();
00186 return SarVector<T>::size();
00187 };
00188 SarVector<int> get_ids() {
00189 if(slow_inits)
00190 return ids;
00191 SarVector<int> sv;
00192 for(unsigned int i=0; i < this->size(); i++)
00193 sv.push_back(this->at(i).id);
00194 return sv;
00195 };
00198 template <class K> SarVector<K> get_property(std::string name) {
00199 SarVector<K> sv;
00200 K value;
00201 for(unsigned int i=0; i < this->size(); i++) {
00202 this->at(i).get(name, value);
00203 sv << value;
00204 }
00205 return sv;
00206 };
00207 bool has_id(int id) {
00208 return get_ids().includes(id);
00209 };
00210 bool has(T r) {
00211 for(unsigned int i=0; i < this->size(); i++)
00212 if(this->at(i) == r)
00213 return true;
00214 return false;
00215 };
00216 T& operator[](int i) {
00217 return this->at(i);
00218 };
00219 T& at(int i) {
00220 if(slow_inits) ids_to_objs();
00221 return SarVector<T>::at(i);
00222 };
00223 };
00224 };
00225
00226