00001 #ifndef __SUIF_INDEXED_LIST_H__ 00002 #define __SUIF_INDEXED_LIST_H__ 00003 00004 #include "suif_list.h" 00005 00021 template <class Domain,class Range> class indexed_list 00022 { 00023 public: 00024 class pair 00025 { 00026 friend class indexed_list<Domain,Range>; 00027 public: 00028 Domain first; 00029 Range second; 00030 pair(const Domain &key,const Range &value) : first(key),second(value) {} 00031 static void* constructorFunction(void* address) { 00032 return new (address) pair; 00033 } 00034 00035 protected: 00036 pair() {} 00037 }; 00038 00039 typedef list<pair> pair_list; 00040 typedef typename pair_list::iterator iterator; 00041 typedef typename pair_list::const_iterator const_iterator; 00042 //typedef const iterator const_iterator; 00043 typedef pair value_type; 00044 00046 void push_back(const Domain &key,const Range &value) 00047 { 00048 the_list.push_back(pair(key,value)); 00049 } 00050 00054 pair &back() {return the_list.back();} 00055 00057 iterator insert(const iterator& pos, const pair& x) 00058 { 00059 return the_list.insert(pos,x); 00060 } 00061 00063 void clear_list() { 00064 the_list.clear_list(); 00065 } 00066 00069 iterator find(const Domain &key) { 00070 iterator iter = the_list.begin(); 00071 while (iter != the_list.end()) 00072 { 00073 if ((*iter).first == key) 00074 break; 00075 iter ++; 00076 } 00077 return iter;; 00078 } 00079 const_iterator find(const Domain &key) const { 00080 const_iterator iter = the_list.begin(); 00081 while (iter != the_list.end()) 00082 { 00083 if ((*iter).first == key) 00084 break; 00085 iter ++; 00086 } 00087 return iter;; 00088 } 00089 00091 int num_with_key(const Domain &key) { 00092 int count = 0; 00093 iterator iter = the_list.begin(); 00094 while (iter != the_list.end()) 00095 { 00096 if ((*iter).first == key) 00097 count ++; 00098 iter ++; 00099 } 00100 return count; 00101 } 00102 00106 iterator find(const Domain &key,int no) { 00107 iterator iter = the_list.begin(); 00108 while (iter != the_list.end()) 00109 { 00110 if ((*iter).first == key) 00111 { 00112 no --; 00113 if (no <= 0) 00114 break; 00115 } 00116 iter ++; 00117 } 00118 return iter;; 00119 } 00120 const_iterator find(const Domain &key,int no) const { 00121 const_iterator iter = the_list.begin(); 00122 while (iter != the_list.end()) 00123 { 00124 if ((*iter).first == key) 00125 { 00126 no --; 00127 if (no <= 0) 00128 break; 00129 } 00130 iter ++; 00131 } 00132 return iter;; 00133 } 00134 00136 bool is_member(const Domain &key) const { 00137 return find(key) != the_list.end(); 00138 } 00139 00141 Range remove(iterator iter) { 00142 Range x = (*iter).second; 00143 the_list.erase(iter); 00144 return x; 00145 } 00146 00149 bool remove(Domain &key) { 00150 iterator iter = find(key); 00151 if (iter == the_list.end()) 00152 return false; 00153 the_list.erase(iter); 00154 return true; 00155 } 00156 00157 iterator begin() { return the_list.begin();} 00158 const_iterator begin() const { return the_list.begin();} 00159 iterator end() { return the_list.end();} 00160 const_iterator end() const { return the_list.end();} 00161 00163 unsigned length() const { return the_list.length(); } 00165 unsigned size() const { return the_list.size(); } 00166 00170 pair & operator [](int i) const { 00171 return the_list[i]; 00172 } 00173 00174 00175 00176 private: 00177 pair_list the_list; 00178 }; 00179 00184 template <class Domain> class searchable_list : public list<Domain> 00185 { 00186 public: 00187 typedef typename list<Domain>::iterator iterator; 00188 /* Find an entry in the list */ 00189 iterator find(const Domain &key) { 00190 iterator iter = begin(); 00191 while (iter != end()) 00192 { 00193 if ((*iter) == key) 00194 break; 00195 iter ++; 00196 } 00197 return iter;; 00198 } 00199 00200 const_iterator find(const Domain &key) const { 00201 const_iterator iter = begin(); 00202 while (iter != end()) 00203 { 00204 if ((*iter) == key) 00205 break; 00206 iter ++; 00207 } 00208 return iter;; 00209 } 00210 00212 bool is_member(const Domain &key) const { 00213 return find(key) != end(); 00214 } 00215 00217 bool remove(const Domain &key) { 00218 iterator iter = find(key); 00219 if (iter == end()) 00220 return false; 00221 erase(iter); 00222 return true; 00223 } 00224 00225 }; 00226 #endif