00001
00004
00005 #ifndef FBwLSTH
00006 #define FBwLSTH
00007
00008
00010
00011 {
00012 using std::string;
00013
00015 template <class TNode > class SimListNode
00016 {
00017 SimListNode *Next;
00018 bool mmanage;
00019 string name;
00020 public:
00021 TNode *node;
00022 SimListNode(string pName,TNode *p,bool managed=true)
00023 {
00024 node=p;
00025 Next=NULL;
00026 mmanage=managed;
00027 name=pName;
00028 }
00029
00030
00031
00032
00033
00034
00035
00036 SimListNode(string pName, const TNode &p)
00037 {
00038 node=new TNode(p);
00039 Next=NULL;
00040 mmanage=true;
00041 name=pName;
00042 }
00043 SimListNode *GetNext() const
00044 {
00045 return Next;
00046 }
00047 void SetNext(SimListNode *pNext)
00048 {
00049 Next=pNext;
00050 }
00051 string Name()
00052 {
00053 return name;
00054 }
00055 ~SimListNode()
00056 {
00057 if( node && mmanage)
00058 {
00059 delete node;
00060 }
00061 }
00062 SimListNode *operator++()
00063 {
00064 return Next;
00065 }
00066 };
00067
00068 template <class TNode>
00069 class SimListIterator
00070 {
00071 SimListNode<TNode> *n;
00072 public:
00073 SimListIterator()
00074 {
00075 n=NULL;
00076 }
00077 SimListIterator(SimListNode<TNode> *p)
00078 {
00079 n=p;
00080 }
00081 SimListIterator(const SimListIterator &p)
00082 {
00083 n=p.n;
00084 }
00085 SimListIterator &operator=(const SimListIterator &p)
00086 {
00087 n=p.n;
00088 return *this;
00089 }
00090 TNode *operator++()
00091 {
00092 if( n )
00093 n=n->GetNext();
00094 return n->node;
00095 }
00096 TNode *operator++(int)
00097 {
00098 SimListNode<TNode> *p=n;
00099 if( n )
00100 n=n->GetNext();
00101 return p->node;
00102 }
00103 int operator==(const SimListIterator &p) const
00104 {
00105 return n==p.n;
00106 }
00107 int operator!=(const SimListIterator &p) const
00108 {
00109 return n!=p.n;
00110 }
00111 TNode &operator*()
00112 {
00113 return *(n->node);
00114 }
00115 string Name() const
00116 {
00117 return n->Name();
00118 }
00119 };
00120
00121 template <class TNode> class SimList
00122 {
00123 SimListNode<TNode> *Raiz;
00124 SimListNode<TNode> *Final;
00125 int count;
00126
00127 protected:
00128 int AppendNode(SimListNode<TNode> *x)
00129 {
00130 if( Raiz==NULL)
00131 {
00132 Raiz=Final=x;
00133 }
00134 else
00135 {
00136 Final->SetNext(x);
00137 Final=x;
00138 }
00139 return count++;
00140 }
00141
00142 public:
00143 typedef SimListIterator<TNode> Iterator;
00144 typedef SimListNode<TNode> node_type;
00145 SimList()
00146 {
00147 Raiz=Final=NULL;
00148 count=0;
00149 }
00150 int Append(string name,TNode *node)
00151 {
00152 if( Find(name) != NULL )
00153 return -1;
00154 node_type *x = new node_type(name,node);
00155 return AppendNode(x);
00156 }
00157 int Append(string name,node_type *node)
00158 {
00159 return AppendNode(node);
00160 }
00161
00162
00163
00164
00165
00167
00168 int Append(string name,const TNode &node)
00169 {
00170 if( Find(name) != NULL )
00171 return -1;
00172 node_type *x = new node_type(name,node);
00173 return AppendNode(x);
00174 }
00175 TNode *Find(int idx) const
00176 {
00177 SimListNode<TNode> *x;
00178 int i=0;
00179 for(x=Raiz;x!=NULL;x=x->GetNext(),i++)
00180 {
00181 if( i==idx )
00182 return x->node;
00183 }
00184 return NULL;
00185 }
00186 TNode *Find(const string pNam) const
00187 {
00188 node_type *x;
00189 for(x=Raiz; x!=NULL; x=x->GetNext())
00190 {
00191 if( x->Name()==pNam )
00192 return x->node;
00193 }
00194 return NULL;
00195 }
00196 TNode *operator[](const string pNam) const
00197 {
00198 return Find(pNam);
00199 }
00200 TNode *operator[](int idx) const
00201 {
00202 return Find(idx);
00203 }
00204 void Destroy()
00205 {
00206 while(Raiz != NULL)
00207 {
00208 Final=Raiz->GetNext();
00209 delete Raiz;
00210 Raiz=Final;
00211 }
00212 Raiz=Final=NULL;
00213 count=0;
00214 }
00215 int Count() const
00216 {
00217 return count;
00218 }
00219 ~SimList()
00220 {
00221 Destroy();
00222 }
00223 Iterator begin() const
00224 {
00225 return Iterator(Raiz);
00226 }
00227 Iterator end() const
00228 {
00229 return Iterator();
00230 }
00231 void Copy(const Iterator &b, const Iterator &e)
00232 {
00233 Destroy();
00234 for(Iterator i=b; i!=e; i++)
00235 {
00236 Append(i.Name(),*i);
00237 }
00238 }
00239 void Copy(const SimList &sl)
00240 {
00241 Destroy();
00242 for(Iterator i=sl.begin(); i!=sl.end(); i++)
00243 {
00244 Append(i.Name(),*i);
00245 }
00246 }
00247 };
00248
00249
00250
00251 };
00252 #endif