00001 #include "delindexlst.h"
00002 #include "vecdouble.h"
00003 #include "exception.h"
00004 #include <algorithm>
00005 #include <limits.h>
00006 #include <assert.h>
00007 #include "orthomesh.h"
00008
00009
00010 DelIndexLst::DelIndexLst()
00011 {
00012 m_bClosed = false;
00013
00014 m_vec.push_back(Info(0,0));
00015 m_vec.push_back(Info(UINT_MAX,0));
00016 }
00017
00018 DelIndexLst::~DelIndexLst()
00019 {
00020
00021 }
00022
00023 void DelIndexLst::addIndex(Index i,bool isInnerCell)
00024 {
00025 if (m_bClosed)
00026 throw new Exception("DelIndexLst:: List already closed\nCant insert another index.");
00027 struct Info info(i,isInnerCell);
00028 m_vec.push_back(info);
00029 }
00030
00031
00032
00033
00034 void DelIndexLst::close()
00035 {
00036
00037 std::sort(m_vec.begin(),m_vec.end());
00038
00039 std::vector<Info>::iterator it = m_vec.begin();
00040 it++;
00041 unsigned off=0;
00042 m_nNonGhosts=0;
00043 for(;it!=m_vec.end();it++)
00044 {
00045 if (it->m_off)
00046 m_nNonGhosts++;
00047 it->m_off = off++;
00048 it->m_ghostOff=it->m_off-m_nNonGhosts;
00049 }
00050 assert(m_vec.back().m_off == --off);
00051 m_bClosed=true;
00052
00053 }
00054
00055
00056 DelIndexLst::Index DelIndexLst::adjust(Index i,Iterator &it) const
00057 {
00058 if (it->index() > i)
00059 {
00060 do
00061 {
00062 it--;
00063 } while (it->index() > i);
00064
00065
00066 assert(it->index() != i || it == m_vec.begin());
00067 return i-(++it)->off();
00068 }
00069 else
00070 {
00071 while (it->index() <= i)
00072 {
00073 it++;
00074 }
00075 assert((it-1)->index() != i || (it-1) == m_vec.begin());
00076 return i-it->off();
00077
00078 }
00079
00080 }
00081
00082 Index DelIndexLst::convert(Index i,Iterator &it) const
00083 {
00084 if (it->index() > i)
00085 {
00086 do
00087 {
00088 it--;
00089 } while (it->index() > i);
00090
00091
00092 if (it->index() == i && it != m_vec.begin())
00093 {
00094 it++;
00095 return OrthoMesh::invalidIndex();
00096 }
00097 else
00098 return i-(++it)->off();
00099 }
00100 else
00101 {
00102 while (it->index() < i)
00103 {
00104 it++;
00105 }
00106 if (it->index() == i && it != m_vec.begin())
00107 {
00108 it++;
00109 return OrthoMesh::invalidIndex();
00110 }
00111 else
00112 return i-it->off();
00113 }
00114 }
00115
00116
00117
00118
00119 Index DelIndexLst::convertGhost(const Index &i,Iterator &it,const Index &Off) const
00120 {
00121 if (it->index() > i)
00122 {
00123 do
00124 {
00125 it--;
00126 } while (it->index() > i);
00127
00128
00129 if (it->index() == i && it != m_vec.begin())
00130 {
00131 return Off+(it++)->off();
00132 }
00133 else
00134 return i-(++it)->ghostOff();
00135 }
00136 else
00137 {
00138 while (it->index() < i)
00139 {
00140 it++;
00141 }
00142 if (it->index() == i && it != m_vec.begin())
00143 {
00144 return Off + (it++)->ghostOff();
00145 }
00146 else
00147 return i-it->off();
00148 }
00149 }
00150
00151
00152 DelIndexLst::Index DelIndexLst::adjustI(Index i,Iterator it) const
00153 {
00154 if (it->index() > i)
00155 {
00156 do
00157 {
00158 it--;
00159 } while (it->index() > i);
00160
00161
00162 if (it->index() == i && it != m_vec.begin())
00163 return OrthoMesh::invalidIndex();
00164 else
00165 return i-(++it)->off();
00166 }
00167 else
00168 {
00169 while (it->index() < i)
00170 {
00171 it++;
00172 }
00173 if (it->index() == i && it != m_vec.begin())
00174 return OrthoMesh::invalidIndex();
00175 else
00176 return i-it->off();
00177 }
00178 }
00179
00180
00187 DelIndexLst::Iterator DelIndexLst::findLUEntry(const Index i) const
00188 {
00189 assert(m_bClosed);
00190 Iterator it = begin();
00191 while (it->index() < i)
00192 it++;
00193 return it;
00194
00195
00196 }
00197
00198
00199
00200
00201
00202
00203
00204
00205
00210 void DelIndexLst::addUniqueIndex(Index i)
00211 {
00212 if (this->exist(i))
00213 return;
00214 else
00215 addIndex(i);
00216 }
00217
00218 bool DelIndexLst::exist(Index i)
00219 {
00220 assert(i!=OrthoMesh::invalidIndex());
00221 for (Iterator it=begin();it != end();it++)
00222 {
00223 if (it->index() == i)
00224 return true;
00225 }
00226 return false;
00227 }
00228
00229
00230
00231
00232
00233
00234 Index DelIndexLst::index_to_raw_index(Index i) const
00235 {
00236 assert(m_bClosed);
00237 Iterator it = findLUEntry(i);
00238 i+=it->off();
00239 while (i >= it->index())
00240 {
00241 i++;
00242 it++;
00243 }
00244 return i;
00245
00246 }
00247
00248
00249
00250
00251
00252
00253
00254 unsigned DelIndexLst::getNumInnerGhostCells()
00255 {
00256 assert(m_bClosed);
00257 return m_nNonGhosts;
00258 }