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
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #if !defined(ALIZE_SegCluster_cpp)
00056 #define ALIZE_SegCluster_cpp
00057
00058 #include "SegCluster.h"
00059 #include "SegServer.h"
00060 #include "Exception.h"
00061 #include <new>
00062 #include "limits.h"
00063 #include <iostream>
00064
00065 using namespace alize;
00066 using namespace std;
00067 typedef SegCluster C;
00068
00069 C::SegCluster(SegServer& ss, unsigned long lc, const String& s,
00070 const String& sn)
00071 :SegAbstract(ss, lc, s, sn), _pCurrentSeg(NULL) { rewind(); }
00072
00073 SegCluster& C::create(const K&, SegServer& ss, unsigned long lc,
00074 const String& s, const String& sn)
00075 {
00076 SegCluster* p = new (std::nothrow) SegCluster(ss, lc, s, sn);
00077 assertMemoryIsAllocated(p, __FILE__, __LINE__);
00078 return *p;
00079 }
00080
00081
00082
00083
00084
00085 SegCluster& C::duplicate(const K&, SegServer& ss) const
00086 {
00087 SegCluster& cl = create(K::k, ss, labelCode(), string(), sourceName());
00088 cl.list() = _list;
00089 return cl;
00090 }
00091
00092 unsigned long C::getId() const { return _id; }
00093
00094 void C::setId(const K&, unsigned long id) { _id = id; }
00095
00096 void C::rewind() const
00097 {
00098 _current = 0;
00099 _pCurrentSubSeg = NULL;
00100 }
00101
00102 Seg* C::getSeg() const
00103 {
00104 do
00105 {
00106 if (_pCurrentSubSeg == NULL)
00107 {
00108 if (_current >= _vect.size())
00109 return NULL;
00110 _pCurrentSubSeg = &get(_current);
00111 _pCurrentSubSeg->rewind();
00112 }
00113 Seg* p = _pCurrentSubSeg->getSeg();
00114 if (p != NULL)
00115 return p;
00116 _pCurrentSubSeg = NULL;
00117 _current++;
00118 }
00119 while (true);
00120 }
00121
00122 unsigned long C::begin() const
00123 {
00124 bool isDefined = false;
00125 unsigned long b, e;
00126 getExtremeBoundaries(K::k, b, e, isDefined);
00127 if (!isDefined)
00128 throw Exception("Undefined begin value", __FILE__, __LINE__);
00129 return b;
00130 }
00131
00132 unsigned long C::length() const
00133 {
00134 bool isDefined = false;
00135 unsigned long b, e;
00136 getExtremeBoundaries(K::k, b, e, isDefined);
00137 if (!isDefined)
00138 throw Exception("Undefined length value", __FILE__, __LINE__);
00139 return e-b;
00140 }
00141
00142 void C::add(SegAbstract& s)
00143 {
00144 if (!getServer().isSameObject(s.getServer()))
00145 throw Exception("Cannot add a extern segment", __FILE__, __LINE__);
00146 _vect.addObject(s);
00147 s.addOwner(K::k, *this);
00148 }
00149
00150 void C::add(SegAbstract& s, unsigned long pos)
00151 {
00152 if (!getServer().isSameObject(s.getServer()))
00153 throw Exception("Cannot add a extern segment", __FILE__, __LINE__);
00154 _vect.addObject(s, pos);
00155 s.addOwner(K::k, *this);
00156 }
00157
00158 Seg& C::addCopy(const Seg& s)
00159 {
00160 Seg& seg = getServer().duplicateSeg(s);
00161 _vect.addObject(seg);
00162 seg.addOwner(K::k, *this);
00163 return seg;
00164 }
00165
00166 Seg& C::addCopy(const Seg& s, unsigned long pos)
00167 {
00168 Seg& seg = getServer().duplicateSeg(s);
00169 _vect.addObject(seg, pos);
00170 seg.addOwner(K::k, *this);
00171 return seg;
00172 }
00173
00174 Seg& C::addNewSeg(unsigned long b, unsigned long l, unsigned long lc,
00175 const String& s, const String& sn)
00176 {
00177 Seg& seg = getServer().createSeg(b, l, lc, s, sn);
00178 _vect.addObject(seg);
00179 seg.addOwner(K::k, *this);
00180 return seg;
00181 }
00182
00183 void C::remove(SegAbstract& s)
00184 {
00185 s.removeOwner(K::k, *this);
00186 _vect.removeObject(s);
00187 }
00188
00189 SegAbstract& C::remove(unsigned long i)
00190 {
00191 SegAbstract& s = get(i);
00192 s.removeOwner(K::k, *this);
00193 _vect.removeObject(i);
00194 return s;
00195 }
00196
00197 void C::removeAll()
00198 {
00199 for (unsigned long i=0; i<_vect.size(); i++)
00200 get(i).removeOwner(K::k, *this);
00201 _vect.clear();
00202 }
00203
00204 unsigned long C::getCount() const { return _vect.size(); }
00205
00206 SegAbstract& C::get(unsigned long i) const { return _vect.getObject(i); }
00207
00208 bool C::getFeatureLabelCode(unsigned long n, unsigned long& lc) const
00209 {
00210 if (_pCurrentSeg != NULL && n >= _b && n < _e)
00211 {
00212 lc = _lc;
00213 return true;
00214 }
00215 bool f, l;
00216 return getFeatureLabelCode(n, lc, f, l);
00217 }
00218
00219 bool C::getFeatureLabelCode(unsigned long n, unsigned long& lc,
00220 bool& isFirst, bool& isLast) const
00221 {
00222 if (_pCurrentSeg != NULL && n >= _b && n < _e)
00223 {
00224 isFirst = (n == _b);
00225 isLast = (n == _e-1);
00226 lc = _lc;
00227 return true;
00228 }
00229 _pStartSeg = NULL;
00230
00231 do
00232 {
00233 _pCurrentSeg = getSeg();
00234
00235 if (_pCurrentSeg != NULL)
00236 {
00237 if (_pCurrentSeg == _pStartSeg)
00238 break;
00239 _b = _pCurrentSeg->begin();
00240 _e = _b + _pCurrentSeg->length();
00241 if (n >= _b && n < _e)
00242 {
00243 isFirst = (n == _b);
00244 isLast = (n == _e-1);
00245 lc = _lc = _pCurrentSeg->labelCode();
00246 return true;
00247 }
00248 if (_pStartSeg == NULL)
00249 _pStartSeg = _pCurrentSeg;
00250 }
00251 else
00252 rewind();
00253 }
00254 while (true);
00255
00256 _pCurrentSeg = NULL;
00257 return false;
00258 }
00259
00260 void C::getExtremeBoundaries(const K&, unsigned long& b,
00261 unsigned long& e, bool& isDefined) const
00262 {
00263 for (unsigned long i=0; i<getCount(); i++)
00264 { get(i).getExtremeBoundaries(K::k, b, e, isDefined); }
00265 }
00266
00267 String C::getClassName() const { return "SegCluster"; }
00268
00269 String C::toString() const
00270 {
00271 String b("undefined");
00272 String l("undefined");
00273 try { b = String::valueOf(begin()); } catch (Exception&) {}
00274 try { l = String::valueOf(length()); } catch (Exception&) {}
00275 return Object::toString()
00276 + "\n server name = '" + getServer().getServerName() + "'"
00277 + "\n begin = " + b
00278 + "\n length = " + l
00279 + "\n labelCode = " + String::valueOf(_labelCode)
00280 + "\n string = '" + _string + "'"
00281 + "\n sourceName = '" + _srcName + "'";
00282
00283 }
00284
00285 C::~SegCluster() { removeAll(); }
00286
00287
00288 #endif // !defined(ALIZE_SegCluster_cpp)