source: src/dgp/boubble.hh @ 3b02b04

Last change on this file since 3b02b04 was 3b02b04, checked in by Tomasz Obrebski <to@…>, 11 years ago

prawie ca�kiem nowe dgc, du�e zmiany w dgp, pomniejsze poprawki

  • Property mode set to 100644
File size: 5.0 KB
Line 
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
11enum 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
23class Boubble
24{
25public:
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
48private:
49
50  int                  _src;
51  list<Role>           _upath;
52  list<Role>           _dpath;
53  LongRel              _rel;
54
55};
56
57//----------------------------------------------------------------------------------------------------
58
59inline
60Boubble::Boubble(list<Role> u, list<Role> d, LongRel l, int s) : _upath(u), _dpath(d), _rel(l), _src(s) {}
61
62//----------------------------------------------------------------------------------------------------
63
64inline
65Boubble::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
89inline
90Dir 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
101inline
102LongRel Boubble::rel()
103{ return _rel; }
104
105//----------------------------------------------------------------------------------------------------
106
107inline
108int Boubble::src()
109{ return _src; }
110
111//----------------------------------------------------------------------------------------------------
112
113inline
114void Boubble::src(int s)
115{ _src=s; }
116
117//----------------------------------------------------------------------------------------------------
118
119inline
120Role 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
131inline
132Boubble* 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
152inline
153bool Boubble::is_at_target()
154{ return _upath.empty() && _dpath.empty(); }
155
156//----------------------------------------------------------------------------------------------------
157
158inline
159bool 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
166inline
167bool Boubble::operator!=(Boubble const& b) const
168{
169  return !(*this==b);
170}
171
172//----------------------------------------------------------------------------------------------------
173
174inline
175bool 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
185inline
186std::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
214inline
215void 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
Note: See TracBrowser for help on using the repository browser.