source: src/dgp/sgraph.hh @ 519eaf5

Last change on this file since 519eaf5 was 519eaf5, checked in by Tomasz Obrebski <obrebski@…>, 10 years ago

Bug fixes: bubbles,props

  • Property mode set to 100644
File size: 12.2 KB
Line 
1#ifndef _SGRAPH_HH
2#define _SGRAPH_HH
3
4#include <stdio.h>
5
6#include <list>
7#include <vector>
8#include <bitset>
9#include <stack>
10
11#include "const.hh"
12#include "mgraph.hh"
13#include "thesymbols.hh"
14#include "boubble.hh"
15
16
17using namespace std;
18
19//====================================================================================================
20// CLASS Arc
21//====================================================================================================
22struct Arc
23{
24  int dst;
25  Role role;
26  int headanc;
27  int depanc;
28 
29  Arc(int d, Role r, int ha, int da) : dst(d), role(r), headanc(ha), depanc(da) {};
30};
31
32//====================================================================================================
33// CLASS NodeProp
34//====================================================================================================
35
36struct NodeProp
37{
38  NodeProp();
39  NodeProp(const NodeProp& p);
40  ~NodeProp();
41
42  bool operator==(const NodeProp& p);
43  NodeProp& operator=(const NodeProp& p);
44
45  void clear_boubbles();
46  void merge_boubbles(list<Boubble*> new_boubbles);
47
48  void copy(const NodeProp& p);
49  void clear();
50
51  RoleSet required;
52  RoleSet forbidden;
53  RoleSet attached;
54
55  bool init_attached;
56  bool fin_attached;
57 
58  FlagSet flags;
59
60  list<Boubble*>   boubbles;
61};
62
63//----------------------------------------------------------------------------------------------------
64
65inline
66bool NodeProp::operator==(const NodeProp& p)
67{
68  if(required != p.required) return false;
69  if(forbidden != p.forbidden) return false;
70  if(attached != p.attached) return false;
71  if(flags != p.flags) return false;
72  if(init_attached != p.init_attached) return false;
73  if(fin_attached != p.fin_attached) return false;
74
75  list<Boubble*>::const_iterator b,b1;
76  for(b=boubbles.begin(), b1=p.boubbles.begin(); b != boubbles.end() && b1 != p.boubbles.end(); b++,b1++)
77    if(!(**b == **b1))
78      return false;
79  if(b != boubbles.end() || b1 != p.boubbles.end())
80    return false;
81
82  return true;
83}
84
85//----------------------------------------------------------------------------------------------------
86
87inline
88void NodeProp::clear_boubbles()
89{
90  for(list<Boubble*>::iterator b = boubbles.begin(); b!=boubbles.end(); b++)
91    delete *b;
92  boubbles.clear();
93}
94
95//----------------------------------------------------------------------------------------------------
96
97inline
98void NodeProp::merge_boubbles(list<Boubble*> new_boubbles)
99{
100  boubbles.merge(new_boubbles);
101}
102
103//----------------------------------------------------------------------------------------------------
104
105inline
106void NodeProp::copy(const NodeProp& p)
107{
108  required=p.required;
109  forbidden=p.forbidden;
110  attached=p.attached;
111  flags=p.flags;
112  init_attached=p.init_attached;
113  fin_attached=p.fin_attached;
114  for(list<Boubble*>::const_iterator b = p.boubbles.begin(); b!=p.boubbles.end(); b++)
115    boubbles.push_back(new Boubble(**b));
116}
117
118//----------------------------------------------------------------------------------------------------
119
120inline
121NodeProp::~NodeProp()
122{
123  clear_boubbles();
124}
125//----------------------------------------------------------------------------------------------------
126
127inline
128NodeProp::NodeProp()
129{
130  clear();
131}
132
133//----------------------------------------------------------------------------------------------------
134
135inline
136NodeProp::NodeProp(const NodeProp& p)
137{
138  copy(p);
139}
140
141//----------------------------------------------------------------------------------------------------
142
143inline
144NodeProp& NodeProp::operator=(const NodeProp& p)
145{
146  clear();
147  copy(p);
148  return *this;
149}
150
151//----------------------------------------------------------------------------------------------------
152
153inline
154void NodeProp::clear()
155{
156  required.reset();
157  forbidden.reset();
158  attached.reset();
159  init_attached=false;
160  fin_attached=false;
161  clear_boubbles();
162}
163
164//====================================================================================================
165// CLASS SNode
166//====================================================================================================
167
168struct SNode
169{
170 
171  SNode() { visible_as_neighbour = true; }
172
173  int mnode;
174
175  NodeProp prop;
176
177  vector<int> edge;
178  bool        edge_contains_self;
179  bool visible_as_neighbour;
180
181  bitset<MAXNODES> LV;
182  bitset<MAXNODES> LH;
183  bitset<MAXNODES> LD;
184  bool in_LH;
185
186  vector<Arc> heads;
187  vector<Arc> deps;
188
189  void clear();
190  bool saturated();
191};
192
193//----------------------------------------------------------------------------------------------------
194inline
195void SNode::clear()
196{ prop.clear(), LV.reset(), LD.reset(), LH.reset(), heads.clear(), deps.clear(); }
197//----------------------------------------------------------------------------------------------------
198inline
199bool SNode::saturated()
200{ return prop.required.none(); }
201
202//====================================================================================================
203// SGraph CLASS
204//====================================================================================================
205
206class SGraph
207{
208public:
209
210  enum Output { HEADS=1, DEPS=2, SETS=4, CONSTRAINTS=8, BOUBBLES=16 };
211
212  SGraph(MGraph& mg) : mgraph(mg)               { clear(); }
213
214  SNode& operator[](const int i)                { return nodes[i]; }
215
216  void clear()                                  { nodes.clear(); }
217  int  add_base_snode(int mnodeind);
218  int  clone(int ancind, NodeProp newprop);
219  void update_left(int headind, int depind);
220  void update_right(int headind, int depind);
221  bool visible(int left, int right);
222  bool saturated(int node);
223
224  Cat  cat(int i) const { return mgraph[nodes[i].mnode].cat; }   
225  char* form(int i) const { return mgraph[nodes[i].mnode].form; }
226
227  int print_node(FILE* f, int n, unsigned int info);
228  int print_node_debug(FILE* f, const char* pref, int n, int anc);
229
230  void print_arc(FILE* f, const char* msg, int left, int right, Role role, int dir); // 0 - left, 1 - right
231
232  //private:
233
234  int size()              {return nodes.size(); }
235
236  friend class LViterator;
237  friend class LHiterator;
238  friend class LDiterator;
239  friend class LNiterator;
240
241private:
242
243  MGraph& mgraph;
244 
245  vector<SNode> nodes;
246
247  int lastnodeind()       { return nodes.size()-1; }
248  SNode& makenewnode()    { nodes.push_back(SNode()); nodes.back().clear(); return nodes.back(); }
249
250  int sprint_node(char* buf, int n, int anc, unsigned int info);
251  int sprint_node_debug(char* buf, const char* pref, int n, int anc);
252};
253
254//----------------------------------------------------------------------------------------------------
255
256inline bool SGraph::visible(int left, int right)
257{
258  return nodes[right].LV[left];
259}
260
261//----------------------------------------------------------------------------------------------------
262
263inline bool SGraph::saturated(int node)
264{
265  return nodes[node].saturated();
266}
267
268//----------------------------------------------------------------------------------------------------
269//----------------------------------------------------------------------------------------------------
270
271class XXiterator
272{
273protected:
274
275  bool checked(int i) { return _checked[i]; }
276  void check(int i)   { _checked.set(i); }
277
278  void push(stack<int>& s, int i)
279  {
280    if(!checked(i))
281      {
282        s.push(i);
283        check(i);
284      }
285  }
286
287  bitset<MAXNODES> _checked;
288};
289
290//----------------------------------------------------------------------------------------------------
291//----------------------------------------------------------------------------------------------------
292
293class LViterator : public XXiterator
294{
295public:
296  LViterator(SGraph& sg, int n, bool s);
297  int next();
298
299private:
300
301  SGraph& sgraph;
302  MGraph& mgraph;
303  stack<int> waydown;
304  stack<int> wayup;
305  bool strict;
306
307  void push_ld(int i);
308  void push_lh(int i);
309  void push_ln(int i);
310};
311
312inline LViterator::LViterator(SGraph& sg, int n, bool s=true) : sgraph(sg), mgraph(sg.mgraph), strict(s)
313{
314  if(sg[n].edge_contains_self) // TO DODAÆ PO PRZEJŠCIU NA EDGE_CONTAINS_SELF
315    {
316      push_ld(n);
317      push_ln(n);
318    }
319
320  for(vector<int>::iterator i=sg[n].edge.begin(); i!=sg[n].edge.end(); ++i)
321    {
322      if(*i != n)
323        {
324          push_ld(*i);
325          push_ln(*i);
326        }
327     
328    }
329}
330
331inline int LViterator::next()
332{
333  if(wayup.empty())
334    {
335      if(waydown.empty())
336        return -1; //
337      else
338        {
339          int k = waydown.top();
340          waydown.pop();
341          push_ld(k);
342          push_ln(k);
343          if(wayup.empty())
344            return -1; // k NIE MA POPRZEDNIKÓW, NIE MO¯E TE¯ ZATEM MIEÆ LEWOSTRONNYCH PODRZÊDNIKÓW
345          else
346            {
347              int i = wayup.top();
348              wayup.pop();
349              push_lh(i);
350              return i;
351            }
352        }
353     
354    }
355  else
356    {
357      int i = wayup.top();
358      wayup.pop();
359      push_lh(i);
360      return i;
361    };
362}
363
364inline void LViterator::push_ld(int i)
365{
366  vector<Arc>& arcs = sgraph[i].deps;
367  for(vector<Arc>::iterator a = arcs.begin(); a != arcs.end(); ++a)
368    if(mgraph[sgraph[a->dst].mnode].pos < mgraph[sgraph[i].mnode].pos)
369      push(waydown,a->dst);
370}
371
372inline void LViterator::push_lh(int i)
373{
374  vector<Arc>& arcs = sgraph[i].heads;
375  for(vector<Arc>::iterator a = arcs.begin(); a != arcs.end(); ++a)
376    if(mgraph[sgraph[a->dst].mnode].pos < mgraph[sgraph[i].mnode].pos)
377      push(wayup,a->dst);
378}
379
380inline void LViterator::push_ln(int i)
381{
382  vector<int>& mpredecessors = mgraph[sgraph[i].mnode].pred;
383  for(vector<int>::iterator mp = mpredecessors.begin(); mp != mpredecessors.end(); ++mp) // poprzedniki w mgraph
384    {
385      vector<int>& spredecessors = mgraph[*mp].snodes;
386      for(vector<int>::iterator sp = spredecessors.begin(); sp != spredecessors.end(); ++sp )
387        if(sgraph[*sp].visible_as_neighbour || !strict)
388          push(wayup,*sp);
389    }
390}
391
392
393//----------------------------------------------------------------------------------------------------
394//----------------------------------------------------------------------------------------------------
395
396class LNiterator
397{
398public:
399  LNiterator(SGraph& sg, int n);
400
401  int next();
402
403private:
404
405  SGraph& sgraph;
406  MGraph& mgraph;
407  int thenode;
408  stack<int> wayup;
409
410  void push_ln(int i);
411};
412
413inline LNiterator::LNiterator(SGraph& sg, int n) : sgraph(sg), mgraph(sg.mgraph), thenode(n)
414{
415  push_ln(n);
416}
417
418inline int LNiterator::next()
419{
420  if(wayup.empty())
421    return -1;
422  else
423    {
424      int i = wayup.top();
425      wayup.pop();
426      return i;
427    };
428}
429
430inline void LNiterator::push_ln(int i)
431{
432  vector<int>& mpredecessors = mgraph[sgraph[i].mnode].pred;
433  for(vector<int>::iterator mp = mpredecessors.begin(); mp != mpredecessors.end(); ++mp) // poprzedniki w mgraph
434    {
435      vector<int>& spredecessors = mgraph[*mp].snodes;
436      for(vector<int>::iterator sp = spredecessors.begin(); sp != spredecessors.end(); ++sp )
437          wayup.push(*sp);
438    }
439}
440
441
442//----------------------------------------------------------------------------------------------------
443//----------------------------------------------------------------------------------------------------
444
445class LHiterator
446{
447public:
448  LHiterator(SGraph& sg, int n);
449
450  int next();
451
452private:
453
454  SGraph& sgraph;
455  MGraph& mgraph;
456  stack<int> wayup;
457
458  void push_lh(int i);
459};
460
461inline LHiterator::LHiterator(SGraph& sg, int n) : sgraph(sg), mgraph(sg.mgraph)
462{
463  push_lh(n);
464}
465
466inline int LHiterator::next()
467{
468  if(wayup.empty())
469        return -1; 
470  else
471    {
472      int i = wayup.top();
473      wayup.pop();
474      push_lh(i);
475      return i;
476    };
477}
478
479inline void LHiterator::push_lh(int i)
480{
481  vector<Arc>& arcs = sgraph[i].heads;
482  for(vector<Arc>::iterator a = arcs.begin(); a != arcs.end(); ++a)
483    if(mgraph[sgraph[a->dst].mnode].pos < mgraph[sgraph[i].mnode].pos)
484      wayup.push(a->dst);
485}
486
487//----------------------------------------------------------------------------------------------------
488//----------------------------------------------------------------------------------------------------
489
490class LDiterator
491{
492public:
493  LDiterator(SGraph& sg, int n);
494
495  int next();
496
497private:
498
499  SGraph& sgraph;
500  MGraph& mgraph;
501  int thenode;
502  stack<int> waydown;
503
504  void push_ld(int i);
505};
506
507inline LDiterator::LDiterator(SGraph& sg, int n) : sgraph(sg), mgraph(sg.mgraph), thenode(n)
508{
509  push_ld(n);
510}
511
512inline int LDiterator::next()
513{
514  if(waydown.empty())
515    return -1;
516  else
517    {
518      int k = waydown.top();
519      waydown.pop();
520      push_ld(k);
521      return k;
522    }
523}
524
525inline void LDiterator::push_ld(int i)
526{
527  vector<Arc>& arcs = sgraph[i].deps;
528  for(vector<Arc>::iterator a = arcs.begin(); a != arcs.end(); ++a)
529    if(mgraph[sgraph[a->dst].mnode].pos < mgraph[sgraph[thenode].mnode].pos)
530      waydown.push(a->dst);
531}
532
533
534
535#endif
Note: See TracBrowser for help on using the repository browser.