00001 #ifndef _MY_FaceBC_
00002 #define _MY_FaceBC_
00003
00004 #include <vector>
00005 #include <ostream>
00006
00007
00008 template<class _Data>
00009 class VecIncAccess;
00010
00011 template<class _Data>
00012 std::ostream& operator<< (std::ostream &out, const VecIncAccess<_Data> &v);
00013
00014
00015
00021 template <class _Data>
00022 class VecIncAccess
00023 {
00024 protected:
00025 class Node {
00026 public:
00027 Node(Index index,_Data &data)
00028 :index(index),data(data){}
00029 Node(){}
00030 Index index;
00031 _Data data;
00032 };
00033 std::vector<Node> m_list;
00034 protected:
00035
00036 public:
00037
00038 typedef typename std::vector<Node>::const_iterator data_iterator;
00039
00040
00041
00042 class iterator
00043 {
00044 friend class VecIncAccess<_Data>;
00045 private:
00046
00047 protected:
00048 VecIncAccess<_Data> *p;
00049 typename std::vector<Node>::iterator it;
00050
00051
00052 iterator(VecIncAccess<_Data> *p,
00053 typename std::vector<Node>::iterator it)
00054 :p(p),it(it){}
00055
00056
00057 bool checkIncSearch(const typename std::vector<Node>::const_iterator &it,unsigned index) const
00058 {
00059 if (index == INDEX_MAX)
00060 return false;
00061
00062 if (it == p->m_list.end())
00063 return false;
00064
00065 if (it != p->m_list.begin())
00066 {
00067 typename std::vector<Node>::const_iterator itprev=it;
00068 itprev--;
00069 if (index < itprev->index)
00070 return false;
00071 }
00072 return true;
00073 }
00074
00075
00076 public:
00077
00078
00079
00080 _Data* inc_search(Index index)
00081 {
00082 assert(checkIncSearch(it,index));
00083 if (index < it->index)
00084 return NULL;
00085 else if (index == it->index)
00086 return &(it->data);
00087 else
00088 {
00089 it++;
00090 if (index < it->index)
00091 return NULL;
00092 else
00093 {
00094 assert(index == it->index);
00095 return &(it->data);
00096 }
00097 }
00098
00099 }
00100 };
00101
00102
00103 class cyclic_iterator
00104 {
00105 friend class VecIncAccess<_Data>;
00106
00107 VecIncAccess<_Data> *p;
00108 typename std::vector<Node>::const_iterator it;
00109 bool wentToFirst;
00110 protected:
00111 cyclic_iterator(VecIncAccess<_Data> *p,
00112 typename std::vector<Node>::const_iterator it)
00113 :p(p),it(it),wentToFirst(false){}
00114
00115 public:
00116 const _Data* search(Index index)
00117 {
00118 if (wentToFirst)
00119 {
00120 if (index > it->index)
00121 return NULL;
00122 else if (index < it->index)
00123 {
00124 wentToFirst=false;
00125 return NULL;
00126 }
00127 else
00128 {
00129 wentToFirst=false;
00130 return &(it->data);
00131 }
00132 }
00133 if (index < it->index)
00134 return NULL;
00135 else if (index == it->index)
00136 return &(it->data);
00137 else
00138 {
00139 it++;
00140 if (it->index == INDEX_MAX)
00141 {
00142 it =p->begin();
00143 wentToFirst=1;
00144 }
00145 else
00146 {
00147 if (index < it->index)
00148 return NULL;
00149 else
00150 {
00151 assert(index == it->index);
00152 return &(it->data);
00153 }
00154 }
00155 }
00156
00157
00158 }
00159
00160
00161
00162
00163
00164
00165 };
00166
00167
00168 friend std::ostream& operator<< <>(std::ostream &out, const VecIncAccess<_Data> &v);
00169
00170 VecIncAccess(){
00171 Node node;
00172 node.index=INDEX_MAX;
00173 m_list.push_back(node);
00174 }
00175
00176 void push_back(Index index,const _Data &data)
00177 {
00178 Node node;
00179 node.index=index;
00180 node.data = data;
00181 m_list.push_back(node);
00182 std::swap(m_list.back(),m_list[m_list.size()-2]);
00183 }
00184 iterator begin(){return iterator(this,m_list.begin());}
00185
00186 cyclic_iterator begin_cyclic(){return cyclic_iterator(this,m_list.begin());}
00187
00188 data_iterator begin_data(){return m_list.begin();}
00189 data_iterator end_data(){assert(m_list.size() != 0);
00190 return m_list.begin() + (m_list.size() -1);}
00191
00192 void reserve(unsigned size)
00193 {
00194 m_list.reserve(size+2);
00195 }
00196
00197 Index size(){return m_list.size()-1;}
00198
00199 };
00200
00201
00202 template<class _Data>
00203 std::ostream& operator<< (std::ostream &out, const VecIncAccess<_Data> &v)
00204 {
00205 typename std::vector< typename VecIncAccess<_Data>::Node >::const_iterator it=v.m_list.begin();
00206 while(it!=v.m_list.end())
00207 {
00208 out << it->index << " " << it->data << "\n";
00209 }
00210
00211
00212 return out;
00213 }
00214
00215
00216 #endif