| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef TAXVERTEX_H |
| 21 |
#define TAXVERTEX_H |
| 22 |
|
| 23 |
#include <set> |
| 24 |
#include <vector> |
| 25 |
#include <iosfwd> |
| 26 |
#include <cstring> |
| 27 |
|
| 28 |
#include "taxNamEntry.h" |
| 29 |
#include "tLabeller.h" |
| 30 |
|
| 31 |
class SaveLoadManager; |
| 32 |
|
| 33 |
class TaxonomyVertex |
| 34 |
{ |
| 35 |
protected: |
| 36 |
|
| 37 |
typedef std::vector<TaxonomyVertex*> TaxVertexLink; |
| 38 |
|
| 39 |
typedef std::vector<const ClassifiableEntry*> EqualNames; |
| 40 |
|
| 41 |
public: |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
typedef TaxVertexLink::iterator iterator; |
| 46 |
|
| 47 |
typedef TaxVertexLink::const_iterator const_iterator; |
| 48 |
|
| 49 |
private: |
| 50 |
|
| 51 |
TaxVertexLink Links[2]; |
| 52 |
|
| 53 |
protected: |
| 54 |
|
| 55 |
const ClassifiableEntry* sample = nullptr; |
| 56 |
|
| 57 |
EqualNames Synonyms; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
TLabeller::LabelType theChecked; |
| 63 |
|
| 64 |
TLabeller::LabelType theValued; |
| 65 |
|
| 66 |
unsigned int common = 0; |
| 67 |
|
| 68 |
bool checkValue = false; |
| 69 |
|
| 70 |
bool inUse = true; |
| 71 |
|
| 72 |
protected: |
| 73 |
|
| 74 |
TaxVertexLink& neigh ( bool upDirection ) { return Links[!upDirection]; } |
| 75 |
|
| 76 |
const TaxVertexLink& neigh ( bool upDirection ) const { return Links[!upDirection]; } |
| 77 |
|
| 78 |
|
| 79 |
void printSynonyms ( std::ostream& o ) const; |
| 80 |
|
| 81 |
void printNeighbours ( std::ostream& o, bool upDirection ) const; |
| 82 |
|
| 83 |
public: |
| 84 |
|
| 85 |
|
| 86 |
bool isChecked ( const TLabeller& checkLab ) const { return checkLab.isLabelled(theChecked); } |
| 87 |
void setChecked ( const TLabeller& checkLab ) { checkLab.set(theChecked); } |
| 88 |
|
| 89 |
|
| 90 |
bool isValued ( const TLabeller& valueLab ) const { return valueLab.isLabelled(theValued); } |
| 91 |
bool getValue ( void ) const { return checkValue; } |
| 92 |
bool setValued ( bool val, const TLabeller& valueLab ) |
| 93 |
{ |
| 94 |
valueLab.set(theValued); |
| 95 |
checkValue = val; |
| 96 |
return val; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
bool isCommon ( void ) const { return common != 0; } |
| 101 |
void setCommon ( void ) { ++common; } |
| 102 |
void clearCommon ( void ) { common = 0; } |
| 103 |
|
| 104 |
bool correctCommon ( unsigned int n ) |
| 105 |
{ |
| 106 |
if ( common == n ) |
| 107 |
return true; |
| 108 |
clearCommon(); |
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
void initFlags ( void ) |
| 114 |
{ |
| 115 |
TLabeller::clear(theChecked); |
| 116 |
TLabeller::clear(theValued); |
| 117 |
clearCommon(); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
const EqualNames& synonyms ( void ) const { return Synonyms; } |
| 123 |
|
| 124 |
|
| 125 |
void setVertexAsHost ( const ClassifiableEntry* entry ) { const_cast<ClassifiableEntry*>(entry)->setTaxVertex(this); } |
| 126 |
|
| 127 |
void setSample ( const ClassifiableEntry* entry, bool linkBack = true ) |
| 128 |
{ |
| 129 |
sample = entry; |
| 130 |
if ( likely(linkBack) ) |
| 131 |
setVertexAsHost(entry); |
| 132 |
} |
| 133 |
|
| 134 |
public: |
| 135 |
|
| 136 |
TaxonomyVertex() { initFlags(); } |
| 137 |
|
| 138 |
explicit TaxonomyVertex ( const ClassifiableEntry* p ) : TaxonomyVertex() |
| 139 |
{ setSample(p); } |
| 140 |
|
| 141 |
TaxonomyVertex ( const TaxonomyVertex& ) = delete; |
| 142 |
|
| 143 |
TaxonomyVertex& operator = ( const TaxonomyVertex& ) = delete; |
| 144 |
|
| 145 |
|
| 146 |
void addSynonym ( const ClassifiableEntry* p ) |
| 147 |
{ |
| 148 |
Synonyms.push_back(p); |
| 149 |
setVertexAsHost(p); |
| 150 |
} |
| 151 |
|
| 152 |
void clear ( void ) |
| 153 |
{ |
| 154 |
Links[0].clear(); |
| 155 |
Links[1].clear(); |
| 156 |
sample = nullptr; |
| 157 |
initFlags(); |
| 158 |
} |
| 159 |
|
| 160 |
const ClassifiableEntry* getPrimer ( void ) const { return sample; } |
| 161 |
|
| 162 |
|
| 163 |
void addNeighbour ( bool upDirection, TaxonomyVertex* p ) { neigh(upDirection).push_back(p); } |
| 164 |
|
| 165 |
bool noNeighbours ( bool upDirection ) const { return neigh(upDirection).empty(); } |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
iterator begin ( bool upDirection ) { return neigh(upDirection).begin(); } |
| 170 |
iterator end ( bool upDirection ) { return neigh(upDirection).end(); } |
| 171 |
|
| 172 |
const_iterator begin ( bool upDirection ) const { return neigh(upDirection).begin(); } |
| 173 |
const_iterator end ( bool upDirection ) const { return neigh(upDirection).end(); } |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
void incorporate ( void ); |
| 179 |
|
| 180 |
TaxonomyVertex* getSynonymNode ( void ) |
| 181 |
{ |
| 182 |
|
| 183 |
for ( auto& upV: neigh(true) ) |
| 184 |
for ( const auto& downV: neigh(false) ) |
| 185 |
if ( upV == downV ) |
| 186 |
return upV; |
| 187 |
return nullptr; |
| 188 |
} |
| 189 |
|
| 190 |
bool removeLink ( bool upDirection, TaxonomyVertex* p ); |
| 191 |
|
| 192 |
void clearLinks ( bool upDirection ) { neigh(upDirection).clear(); } |
| 193 |
|
| 194 |
void removeLinks ( bool upDirection ); |
| 195 |
|
| 196 |
void remove ( void ) { removeLinks(true); removeLinks(false); setInUse(false); } |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
bool isInUse ( void ) const { return inUse; } |
| 202 |
|
| 203 |
void setInUse ( bool value ) { inUse = value; } |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
void print ( std::ostream& o ) const; |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
void SaveLabel ( SaveLoadManager& m ) const; |
| 214 |
|
| 215 |
void LoadLabel ( SaveLoadManager& m ); |
| 216 |
|
| 217 |
void SaveNeighbours ( SaveLoadManager& m ) const; |
| 218 |
|
| 219 |
void LoadNeighbours ( SaveLoadManager& m ); |
| 220 |
}; |
| 221 |
|
| 222 |
|
| 223 |
struct TaxVertexLess |
| 224 |
{ |
| 225 |
bool operator()(const TaxonomyVertex* s1, const TaxonomyVertex* s2) const |
| 226 |
{ return strcmp(s1->getPrimer()->getName(), s2->getPrimer()->getName()) < 0; } |
| 227 |
}; |
| 228 |
|
| 229 |
|
| 230 |
typedef std::set<const TaxonomyVertex*, TaxVertexLess> TVSet; |
| 231 |
|
| 232 |
#endif |