10 #define COMTREETABLE_H
19 using namespace grafalgo;
43 string toString()
const;
44 friend ostream& operator<<(ostream& out,
const ClnkInfo& cli) {
60 string toString()
const;
61 friend ostream& operator<<(ostream& out,
const Entry& e) {
69 HashMap<int,ClnkInfo,Hash::s32> *
clMap;
79 bool validComtree(
comt_t)
const;
80 bool validCtx(
int)
const;
81 bool validClnk(
int,
int)
const;
82 bool checkEntry(
int)
const;
83 bool inCore(
int)
const;
84 bool isLink(
int,
int)
const;
85 bool isRtrLink(
int,
int)
const;
86 bool isCoreLink(
int,
int)
const;
89 int firstComt()
const;
90 int nextComt(
int)
const;
91 int firstComtLink(
int)
const;
92 int nextComtLink(
int,
int)
const;
93 int firstRtrLink(
int)
const;
94 int nextRtrLink(
int,
int)
const;
95 int firstCoreLink(
int)
const;
96 int nextCoreLink(
int,
int)
const;
99 int getComtIndex(
comt_t)
const;
100 Entry& getEntry(
int)
const;
101 comt_t getComtree(
int)
const;
102 int getLink(
int,
int)
const;
103 int getPlink(
int)
const;
104 int getPClink(
int)
const;
105 int getLinkCount(
int)
const;
106 int getClnkNum(
int,
int)
const;
107 ClnkInfo& getClnkInfo(
int,
int)
const;
108 int getLinkQ(
int,
int)
const;
109 fAdr_t getDest(
int,
int)
const;
111 const Dlist& getComtList(
int)
const;
115 bool removeEntry(
int);
116 bool addLink(
int,
int,
bool,
bool);
117 bool removeLink(
int,
int);
118 void setCoreFlag(
int,
bool);
119 void setPlink(
int,
int);
120 void setLinkQ(
int,
int,
int);
125 string toString()
const;
126 string entry2string(
int)
const;
136 bool readEntry(istream&);
137 void readLinks(istream&, set<int>&);
138 string links2string(
int)
const;
142 inline ComtreeTable::ClnkInfo::ClnkInfo() {
147 inline ComtreeTable::ClnkInfo::~ClnkInfo() { }
153 inline string ComtreeTable::ClnkInfo::toString()
const {
154 string s = Forest::fAdr2string(dest) +
" "
155 + to_string(qnum) +
" " + rates.toString();
160 inline ComtreeTable::Entry::Entry() {
161 pLnk = 0; pClnk = 0; coreFlag =
false;
162 clMap =
new HashMap<int,ClnkInfo,Hash::s32>;
163 rtrLinks =
new Dlist(); coreLinks =
new Dlist();
167 inline ComtreeTable::Entry::~Entry() {
168 delete clMap;
delete rtrLinks;
delete coreLinks;
177 *clMap = *(src.
clMap);
188 pLnk = src.
pLnk; pClnk = src.pClnk; coreFlag = src.coreFlag;
189 delete clMap; clMap = src.clMap; src.clMap =
nullptr;
190 delete rtrLinks; rtrLinks = src.rtrLinks; src.rtrLinks =
nullptr;
191 delete coreLinks; coreLinks = src.coreLinks; src.coreLinks =
nullptr;
200 inline string ComtreeTable::Entry::toString()
const {
201 string s = (coreFlag ?
"* " :
" ") + to_string(pLnk) +
" {";
202 for (
int cLnk = clMap->first(); cLnk != 0; cLnk = clMap->next(cLnk)) {
203 if (cLnk != clMap->first()) s +=
" ";
204 s += to_string(clMap->getKey(cLnk));
205 if (coreLinks->member(cLnk)) s +=
"*";
206 else if (rtrLinks->member(cLnk)) s +=
"+";
216 inline bool ComtreeTable::validComtree(
comt_t comt)
const {
217 return comtMap->contains(comt);
224 inline bool ComtreeTable::validCtx(
int ctx)
const {
225 return comtMap->valid(ctx);
234 inline bool ComtreeTable::validClnk(
int ctx,
int cLnk)
const {
235 Entry& e = getEntry(ctx);
236 return e.
clMap->valid(cLnk);
243 inline bool ComtreeTable::inCore(
int ctx)
const {
252 inline bool ComtreeTable::isLink(
int ctx,
int lnk)
const {
253 if (!validCtx(ctx))
return false;
254 Entry& e = getEntry(ctx);
255 return e.
clMap->contains(lnk);
263 inline bool ComtreeTable::isRtrLink(
int ctx,
int cLnk)
const {
264 if (cLnk == 0 || !validCtx(ctx))
return false;
265 Entry& e = getEntry(ctx);
274 inline bool ComtreeTable::isCoreLink(
int ctx,
int cLnk)
const {
275 if (!validCtx(ctx))
return false;
276 Entry& e = getEntry(ctx);
286 inline int ComtreeTable::firstComt()
const {
287 return comtMap->first();
297 inline int ComtreeTable::nextComt(
int ctx)
const {
298 return comtMap->next(ctx);
305 inline comt_t ComtreeTable::getComtree(
int ctx)
const {
306 return comtMap->getKey(ctx);
314 return comtMap->getValue(ctx);
321 inline int ComtreeTable::getComtIndex(
comt_t comt)
const {
322 return comtMap->find(comt);
329 inline int ComtreeTable::getLinkCount(
int ctx)
const {
330 Entry& e = getEntry(ctx);
331 return e.
clMap->size();
340 inline int ComtreeTable::getClnkNum(
int comt,
int lnk)
const {
341 int ctx = getComtIndex(comt);
342 if (ctx == 0)
return 0;
343 Entry& e = getEntry(ctx);
344 return e.
clMap->find(lnk);
353 ComtreeTable::getClnkInfo(
int ctx,
int cLnk)
const {
354 Entry& e = getEntry(ctx);
355 return e.
clMap->getValue(cLnk);
363 inline int ComtreeTable::getPlink(
int ctx)
const {
return getEntry(ctx).pLnk; }
371 inline int ComtreeTable::getPClink(
int ctx)
const {
372 return getEntry(ctx).pClnk;
380 inline int ComtreeTable::getLink(
int ctx,
int cLnk)
const {
381 Entry& e = getEntry(ctx);
382 return e.
clMap->getKey(cLnk);
390 inline int ComtreeTable::getLinkQ(
int ctx,
int cLnk)
const {
391 return getEntry(ctx).clMap->getValue(cLnk).qnum;
400 inline fAdr_t ComtreeTable::getDest(
int ctx,
int cLnk)
const {
401 return getEntry(ctx).clMap->getValue(cLnk).dest;
409 inline RateSpec& ComtreeTable::getRates(
int ctx,
int cLnk)
const {
410 return getEntry(ctx).clMap->getValue(cLnk).rates;
425 inline int ComtreeTable::firstComtLink(
int ctx)
const {
426 return getEntry(ctx).clMap->first();
434 inline int ComtreeTable::nextComtLink(
int ctx,
int cLnk)
const {
435 return getEntry(ctx).clMap->next(cLnk);
442 inline int ComtreeTable::firstRtrLink(
int ctx)
const {
443 return getEntry(ctx).rtrLinks->first();
451 inline int ComtreeTable::nextRtrLink(
int ctx,
int cLnk)
const {
452 return getEntry(ctx).rtrLinks->next(cLnk);
459 inline int ComtreeTable::firstCoreLink(
int ctx)
const {
460 return getEntry(ctx).coreLinks->first();
468 inline int ComtreeTable::nextCoreLink(
int ctx,
int cLnk)
const {
469 return getEntry(ctx).coreLinks->next(cLnk);
477 inline void ComtreeTable::setPlink(
int ctx,
int plink) {
478 if (!validCtx(ctx))
return;
479 Entry& e = getEntry(ctx);
480 if (plink == 0) { e.
pLnk = 0; e.
pClnk = 0;
return; }
481 int cLnk = e.
clMap->find(plink);
482 if (cLnk == 0 || !e.
rtrLinks->member(cLnk))
return;
490 inline void ComtreeTable::setCoreFlag(
int ctx,
bool f) {
491 if (validCtx(ctx)) getEntry(ctx).coreFlag = f;
499 inline void ComtreeTable::setLinkQ(
int ctx,
int cLnk,
int q) {
500 if (!validCtx(ctx))
return;
501 Entry& e = getEntry(ctx);
502 if (validClnk(ctx,cLnk)) e.
clMap->getValue(cLnk).qnum = q;