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
00044 # ifndef TPL_DYNSLIST_H
00045 # define TPL_DYNSLIST_H
00046
00047 # include <tpl_slist.H>
00048
00049 using namespace Aleph;
00050
00051 namespace Aleph {
00052
00062 template <typename T>
00063 class DynSlist : public Slist<T>
00064 {
00065
00066 private:
00067
00068 size_t num_items;
00069 int current_pos;
00070 Snode<T> * current_node;
00071 typename Slist<T>::Node * get_previous_to_pos(const int & pos)
00072 {
00073
00074 if (pos > num_items)
00075 throw std::out_of_range ("position out of range");
00076
00077 if (pos < current_pos)
00078 {
00079 current_pos = 0;
00080 current_node = this;
00081 }
00082
00083 while (current_pos < pos)
00084 {
00085 current_node = current_node->get_next();
00086 ++current_pos;
00087 }
00088
00089 return current_node;
00090 }
00091
00092 public:
00093
00095 DynSlist() : num_items(0), current_pos(0), current_node(this)
00096 {
00097
00098 }
00114 T & operator [] (const size_t & i)
00115
00116 throw(std::exception, std::out_of_range)
00117
00118 {
00119 return get_previous_to_pos(i)->get_next()->get_data();
00120 }
00121
00123 size_t size() const { return num_items; }
00124
00125
00137 void insert(const int & pos, const T & data)
00138
00139 throw(std::exception, std::bad_alloc, std::out_of_range)
00140
00141 {
00142
00143 typename Slist<T>::Node * node = new typename Slist<T>::Node (data);
00144
00145
00146 typename Slist<T>::Node * prev = get_previous_to_pos(pos);
00147
00148 prev->insert_next(node);
00149 ++num_items;
00150 }
00151
00157 void remove(const int & pos)
00158
00159 throw(std::exception, std::range_error)
00160
00161 {
00162
00163 typename Slist<T>::Node * prev = get_previous_to_pos(pos);
00164
00165 typename Slist<T>::Node * node_to_delete = prev->remove_next();
00166
00167 delete node_to_delete;
00168
00169 --num_items;
00170 }
00171
00173 ~DynSlist()
00174 {
00175
00176 while (not this->is_empty())
00177 delete this->remove_first();
00178 }
00184 struct Iterator : public Slist<T>::Iterator
00185 {
00187 Iterator(DynSlist & list) : Slist<T>::Iterator(list) { }
00188
00190 T & get_current() { return Slist<T>::Iterator::get_current()->get_data(); }
00191 };
00192
00193 };
00194
00195 }
00196
00197 # endif // TPL_DYNSLIST_H
00198