| 1 | #ifndef _BOUBBLE_HH_ |
|---|
| 2 | #define _BOUBBLE_HH_ |
|---|
| 3 | |
|---|
| 4 | #include <list> |
|---|
| 5 | #include <iostream> |
|---|
| 6 | #include <sstream> |
|---|
| 7 | |
|---|
| 8 | #include "thesymbols.hh" |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | enum Dir {UP=0,DOWN=1,AT_TARGET=2}; |
|---|
| 12 | |
|---|
| 13 | // class BoubbleAction |
|---|
| 14 | // { |
|---|
| 15 | // public: |
|---|
| 16 | // void apply() {}; |
|---|
| 17 | // private: |
|---|
| 18 | |
|---|
| 19 | // }; |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | class Boubble |
|---|
| 24 | { |
|---|
| 25 | public: |
|---|
| 26 | Boubble(list<Role> u, list<Role> d, LongRel l, int s=-1); |
|---|
| 27 | Boubble(const char* pathstr, const char* l, int s=-1); |
|---|
| 28 | // Boubble(const Boubble& b) {_src=b._src; _upath=b._upath; _dpath=b._dpath; _rel=b._rel; }; |
|---|
| 29 | |
|---|
| 30 | Dir dir(); |
|---|
| 31 | LongRel rel(); |
|---|
| 32 | int src(); |
|---|
| 33 | void src(int s); |
|---|
| 34 | |
|---|
| 35 | Role next(); |
|---|
| 36 | |
|---|
| 37 | Boubble* step(Role r, Dir d); |
|---|
| 38 | |
|---|
| 39 | bool is_at_target(); |
|---|
| 40 | |
|---|
| 41 | bool operator==(Boubble const& b) const; |
|---|
| 42 | bool operator!=(Boubble const& b) const; |
|---|
| 43 | bool operator<(Boubble const& b) const; |
|---|
| 44 | |
|---|
| 45 | void as_cstr(char* s); |
|---|
| 46 | friend std::ostream& operator<<(std::ostream&, const Boubble&); |
|---|
| 47 | |
|---|
| 48 | private: |
|---|
| 49 | |
|---|
| 50 | int _src; |
|---|
| 51 | list<Role> _upath; |
|---|
| 52 | list<Role> _dpath; |
|---|
| 53 | LongRel _rel; |
|---|
| 54 | |
|---|
| 55 | }; |
|---|
| 56 | |
|---|
| 57 | //---------------------------------------------------------------------------------------------------- |
|---|
| 58 | |
|---|
| 59 | inline |
|---|
| 60 | Boubble::Boubble(list<Role> u, list<Role> d, LongRel l, int s) : _upath(u), _dpath(d), _rel(l), _src(s) {} |
|---|
| 61 | |
|---|
| 62 | //---------------------------------------------------------------------------------------------------- |
|---|
| 63 | |
|---|
| 64 | inline |
|---|
| 65 | Boubble::Boubble(const char* pathstr, const char* l, int s) |
|---|
| 66 | { |
|---|
| 67 | Dir dir = UP; |
|---|
| 68 | const char* p = pathstr; |
|---|
| 69 | while(*p) |
|---|
| 70 | { |
|---|
| 71 | if(*p=='^') { dir = DOWN; p++; } |
|---|
| 72 | else if(isalpha(*p)) |
|---|
| 73 | { |
|---|
| 74 | char buf[80]; |
|---|
| 75 | sscanf(p,"%[a-zA-Z0-9_]",buf); |
|---|
| 76 | dir == UP ? _upath.push_back(Role(buf)) : _dpath.push_back(Role(buf)); |
|---|
| 77 | p += strlen(buf); |
|---|
| 78 | } |
|---|
| 79 | else |
|---|
| 80 | p++; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | _rel = LongRel(l); |
|---|
| 84 | _src = s; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | //---------------------------------------------------------------------------------------------------- |
|---|
| 88 | |
|---|
| 89 | inline |
|---|
| 90 | Dir Boubble::dir() |
|---|
| 91 | { |
|---|
| 92 | if(!_upath.empty()) |
|---|
| 93 | return UP; |
|---|
| 94 | else if(!_dpath.empty()) |
|---|
| 95 | return DOWN; |
|---|
| 96 | else return AT_TARGET; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | //---------------------------------------------------------------------------------------------------- |
|---|
| 100 | |
|---|
| 101 | inline |
|---|
| 102 | LongRel Boubble::rel() |
|---|
| 103 | { return _rel; } |
|---|
| 104 | |
|---|
| 105 | //---------------------------------------------------------------------------------------------------- |
|---|
| 106 | |
|---|
| 107 | inline |
|---|
| 108 | int Boubble::src() |
|---|
| 109 | { return _src; } |
|---|
| 110 | |
|---|
| 111 | //---------------------------------------------------------------------------------------------------- |
|---|
| 112 | |
|---|
| 113 | inline |
|---|
| 114 | void Boubble::src(int s) |
|---|
| 115 | { _src=s; } |
|---|
| 116 | |
|---|
| 117 | //---------------------------------------------------------------------------------------------------- |
|---|
| 118 | |
|---|
| 119 | inline |
|---|
| 120 | Role Boubble::next() |
|---|
| 121 | { |
|---|
| 122 | if(!_upath.empty()) |
|---|
| 123 | return _upath.front(); |
|---|
| 124 | else if(!_dpath.empty()) |
|---|
| 125 | return _dpath.front(); |
|---|
| 126 | else return Role("NULL"); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | //---------------------------------------------------------------------------------------------------- |
|---|
| 130 | |
|---|
| 131 | inline |
|---|
| 132 | Boubble* Boubble::step(Role r, Dir d) |
|---|
| 133 | { |
|---|
| 134 | if(d==UP && !_upath.empty() && _upath.front() == r) |
|---|
| 135 | { |
|---|
| 136 | Boubble* newboubble = new Boubble(_upath,_dpath,_rel,_src); |
|---|
| 137 | newboubble->_upath.pop_front(); |
|---|
| 138 | return newboubble; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | if(d==DOWN && _upath.empty() && !_dpath.empty() && _dpath.front() == r) |
|---|
| 142 | { |
|---|
| 143 | Boubble* newboubble = new Boubble(_upath,_dpath,_rel,_src); |
|---|
| 144 | newboubble->_dpath.pop_front(); |
|---|
| 145 | return newboubble; |
|---|
| 146 | } |
|---|
| 147 | return NULL; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | //---------------------------------------------------------------------------------------------------- |
|---|
| 151 | |
|---|
| 152 | inline |
|---|
| 153 | bool Boubble::is_at_target() |
|---|
| 154 | { return _upath.empty() && _dpath.empty(); } |
|---|
| 155 | |
|---|
| 156 | //---------------------------------------------------------------------------------------------------- |
|---|
| 157 | |
|---|
| 158 | inline |
|---|
| 159 | bool Boubble::operator==(Boubble const& b) const |
|---|
| 160 | { |
|---|
| 161 | return _src==b._src && _upath==b._upath && _dpath==b._dpath && _rel==b._rel; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | //---------------------------------------------------------------------------------------------------- |
|---|
| 165 | |
|---|
| 166 | inline |
|---|
| 167 | bool Boubble::operator!=(Boubble const& b) const |
|---|
| 168 | { |
|---|
| 169 | return !(*this==b); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | //---------------------------------------------------------------------------------------------------- |
|---|
| 173 | |
|---|
| 174 | inline |
|---|
| 175 | bool Boubble::operator<(Boubble const& b) const |
|---|
| 176 | { |
|---|
| 177 | if(_src < b._src) return true; |
|---|
| 178 | if(_rel < b._rel) return true; |
|---|
| 179 | if(this < &b) return true; |
|---|
| 180 | return false; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | //---------------------------------------------------------------------------------------------------- |
|---|
| 184 | |
|---|
| 185 | inline |
|---|
| 186 | std::ostream& operator<<(std::ostream& o, const Boubble& b) |
|---|
| 187 | { |
|---|
| 188 | o << "["; |
|---|
| 189 | |
|---|
| 190 | o << b._src << "|"; |
|---|
| 191 | |
|---|
| 192 | bool cont=false; |
|---|
| 193 | for(list<Role>::const_iterator i = b._upath.begin(); i != b._upath.end(); ++i) |
|---|
| 194 | { |
|---|
| 195 | if(cont) o << ','; |
|---|
| 196 | o << i->str(); |
|---|
| 197 | cont = true; |
|---|
| 198 | } |
|---|
| 199 | o << '^'; |
|---|
| 200 | cont=false; |
|---|
| 201 | for(list<Role>::const_iterator i = b._dpath.begin(); i != b._dpath.end(); ++i) |
|---|
| 202 | { |
|---|
| 203 | if(cont) o << ','; |
|---|
| 204 | o << i->str(); |
|---|
| 205 | cont = true; |
|---|
| 206 | } |
|---|
| 207 | o << ':'; |
|---|
| 208 | o << b._rel.str(); |
|---|
| 209 | o << "]"; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | //---------------------------------------------------------------------------------------------------- |
|---|
| 213 | |
|---|
| 214 | inline |
|---|
| 215 | void Boubble::as_cstr(char* s) |
|---|
| 216 | { |
|---|
| 217 | stringstream oss; |
|---|
| 218 | oss << *this; |
|---|
| 219 | strcpy(s,oss.str().c_str()); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | //==================================================================================================== |
|---|
| 223 | |
|---|
| 224 | #endif |
|---|