source: src/dgp/sgraph.hh @ d484a32

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

some unnecessary variables/functions deleted

  • Property mode set to 100644
File size: 12.4 KB
RevLine 
[5f4d9c3]1#ifndef _SGRAPH_HH
2#define _SGRAPH_HH
3
4#include <stdio.h>
5
6#include <list>
7#include <vector>
8#include <bitset>
[3b02b04]9#include <stack>
[5f4d9c3]10
11#include "const.hh"
[e7de6cc]12#include "mgraph.hh"
[5f4d9c3]13#include "thesymbols.hh"
[e7de6cc]14#include "boubble.hh"
[5f4d9c3]15
16
[e7de6cc]17using namespace std;
[5f4d9c3]18
[e7de6cc]19//====================================================================================================
20// CLASS Arc
21//====================================================================================================
[5f4d9c3]22struct Arc
23{
24  int dst;
25  Role role;
[e7de6cc]26  int headanc;
27  int depanc;
[5f4d9c3]28 
[e7de6cc]29  Arc(int d, Role r, int ha, int da) : dst(d), role(r), headanc(ha), depanc(da) {};
30};
[5f4d9c3]31
[e7de6cc]32//====================================================================================================
33// CLASS NodeProp
34//====================================================================================================
[5f4d9c3]35
36struct NodeProp
37{
[e7de6cc]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);
[5f4d9c3]47
[e7de6cc]48  void copy(const NodeProp& p);
49  void clear();
[5f4d9c3]50
[e7de6cc]51  RoleSet required;
52  RoleSet forbidden;
53  RoleSet attached;
[5f4d9c3]54
[e7de6cc]55  bool init_attached;
56  bool fin_attached;
57 
58  FlagSet flags;
59
60  list<Boubble*>   boubbles;
[5f4d9c3]61};
62
[e7de6cc]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;
[3b02b04]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())
[e7de6cc]80    return false;
[3b02b04]81
[e7de6cc]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//====================================================================================================
[5f4d9c3]167
168struct SNode
169{
170 
[3b02b04]171  SNode() { visible_as_neighbour = true; }
172
[e7de6cc]173  int mnode;
[5f4d9c3]174
175  NodeProp prop;
176
[3b02b04]177  vector<int> edge;
178  bool        edge_contains_self;
179  bool visible_as_neighbour;
180
[5f4d9c3]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
[e7de6cc]189  void clear();
190  bool saturated();
[5f4d9c3]191};
192
[e7de6cc]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(); }
[5f4d9c3]201
[e7de6cc]202//====================================================================================================
203// SGraph CLASS
204//====================================================================================================
[5f4d9c3]205
206class SGraph
207{
208public:
209
[e7de6cc]210  enum Output { HEADS=1, DEPS=2, SETS=4, CONSTRAINTS=8, BOUBBLES=16 };
[5f4d9c3]211
[e7de6cc]212  SGraph(MGraph& mg) : mgraph(mg)               { clear(); }
[5f4d9c3]213
[e7de6cc]214  SNode& operator[](const int i)                { return nodes[i]; }
[5f4d9c3]215
[e7de6cc]216  void clear()                                  { nodes.clear(); }
217  int  add_base_snode(int mnodeind);
218  int  clone(int ancind, NodeProp newprop);
[5f4d9c3]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
[e7de6cc]224  Cat  cat(int i) const { return mgraph[nodes[i].mnode].cat; }   
225  char* form(int i) const { return mgraph[nodes[i].mnode].form; }
[5f4d9c3]226
227  int print_node(FILE* f, int n, unsigned int info);
[e7de6cc]228  int print_node_debug(FILE* f, const char* pref, int n, int anc);
[5f4d9c3]229
[519eaf5]230  void print_arc(FILE* f, const char* msg, int left, int right, Role role, int dir); // 0 - left, 1 - right
[5f4d9c3]231
[e7de6cc]232  //private:
233
234  int size()              {return nodes.size(); }
235
[3b02b04]236  friend class LViterator;
237  friend class LHiterator;
238  friend class LDiterator;
239  friend class LNiterator;
240
[e7de6cc]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);
[5f4d9c3]252};
253
[e7de6cc]254//----------------------------------------------------------------------------------------------------
[5f4d9c3]255
256inline bool SGraph::visible(int left, int right)
257{
258  return nodes[right].LV[left];
259}
260
[e7de6cc]261//----------------------------------------------------------------------------------------------------
262
[5f4d9c3]263inline bool SGraph::saturated(int node)
264{
265  return nodes[node].saturated();
266}
267
[e7de6cc]268//----------------------------------------------------------------------------------------------------
[3b02b04]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();
[d484a32]298  void update_edge(SGraph& sg, int e);
[3b02b04]299
300private:
301
302  SGraph& sgraph;
303  MGraph& mgraph;
304  stack<int> waydown;
305  stack<int> wayup;
306  bool strict;
307
308  void push_ld(int i);
309  void push_lh(int i);
310  void push_ln(int i);
[d484a32]311
[3b02b04]312};
313
314inline LViterator::LViterator(SGraph& sg, int n, bool s=true) : sgraph(sg), mgraph(sg.mgraph), strict(s)
315{
316  if(sg[n].edge_contains_self) // TO DODAÆ PO PRZEJŠCIU NA EDGE_CONTAINS_SELF
317    {
318      push_ld(n);
319      push_ln(n);
320    }
321
322  for(vector<int>::iterator i=sg[n].edge.begin(); i!=sg[n].edge.end(); ++i)
323    {
324      if(*i != n)
325        {
326          push_ld(*i);
327          push_ln(*i);
328        }
329     
330    }
331}
332
[d484a32]333inline void LViterator::update_edge(SGraph& sg, int n)
334{
335  for(vector<int>::iterator i=sg[n].edge.begin(); i!=sg[n].edge.end(); ++i)
336    {
337      push_ld(*i);
338      push_ln(*i);
339    }
340}
341
[3b02b04]342inline int LViterator::next()
343{
344  if(wayup.empty())
345    {
346      if(waydown.empty())
347        return -1; //
348      else
349        {
350          int k = waydown.top();
351          waydown.pop();
352          push_ld(k);
353          push_ln(k);
354          if(wayup.empty())
355            return -1; // k NIE MA POPRZEDNIKÓW, NIE MO¯E TE¯ ZATEM MIEÆ LEWOSTRONNYCH PODRZÊDNIKÓW
356          else
357            {
358              int i = wayup.top();
359              wayup.pop();
360              push_lh(i);
361              return i;
362            }
363        }
364     
365    }
366  else
367    {
368      int i = wayup.top();
369      wayup.pop();
370      push_lh(i);
371      return i;
372    };
373}
374
375inline void LViterator::push_ld(int i)
376{
377  vector<Arc>& arcs = sgraph[i].deps;
378  for(vector<Arc>::iterator a = arcs.begin(); a != arcs.end(); ++a)
379    if(mgraph[sgraph[a->dst].mnode].pos < mgraph[sgraph[i].mnode].pos)
380      push(waydown,a->dst);
381}
382
383inline void LViterator::push_lh(int i)
384{
385  vector<Arc>& arcs = sgraph[i].heads;
386  for(vector<Arc>::iterator a = arcs.begin(); a != arcs.end(); ++a)
387    if(mgraph[sgraph[a->dst].mnode].pos < mgraph[sgraph[i].mnode].pos)
388      push(wayup,a->dst);
389}
390
391inline void LViterator::push_ln(int i)
392{
393  vector<int>& mpredecessors = mgraph[sgraph[i].mnode].pred;
394  for(vector<int>::iterator mp = mpredecessors.begin(); mp != mpredecessors.end(); ++mp) // poprzedniki w mgraph
395    {
396      vector<int>& spredecessors = mgraph[*mp].snodes;
397      for(vector<int>::iterator sp = spredecessors.begin(); sp != spredecessors.end(); ++sp )
398        if(sgraph[*sp].visible_as_neighbour || !strict)
399          push(wayup,*sp);
400    }
401}
402
403
404//----------------------------------------------------------------------------------------------------
405//----------------------------------------------------------------------------------------------------
406
407class LNiterator
408{
409public:
410  LNiterator(SGraph& sg, int n);
411
412  int next();
413
414private:
415
416  SGraph& sgraph;
417  MGraph& mgraph;
418  int thenode;
419  stack<int> wayup;
420
421  void push_ln(int i);
422};
423
424inline LNiterator::LNiterator(SGraph& sg, int n) : sgraph(sg), mgraph(sg.mgraph), thenode(n)
425{
426  push_ln(n);
427}
428
429inline int LNiterator::next()
430{
431  if(wayup.empty())
432    return -1;
433  else
434    {
435      int i = wayup.top();
436      wayup.pop();
437      return i;
438    };
439}
440
441inline void LNiterator::push_ln(int i)
442{
443  vector<int>& mpredecessors = mgraph[sgraph[i].mnode].pred;
444  for(vector<int>::iterator mp = mpredecessors.begin(); mp != mpredecessors.end(); ++mp) // poprzedniki w mgraph
445    {
446      vector<int>& spredecessors = mgraph[*mp].snodes;
447      for(vector<int>::iterator sp = spredecessors.begin(); sp != spredecessors.end(); ++sp )
448          wayup.push(*sp);
449    }
450}
451
452
453//----------------------------------------------------------------------------------------------------
454//----------------------------------------------------------------------------------------------------
455
456class LHiterator
457{
458public:
459  LHiterator(SGraph& sg, int n);
460
461  int next();
462
463private:
464
465  SGraph& sgraph;
466  MGraph& mgraph;
467  stack<int> wayup;
468
469  void push_lh(int i);
470};
471
472inline LHiterator::LHiterator(SGraph& sg, int n) : sgraph(sg), mgraph(sg.mgraph)
473{
474  push_lh(n);
475}
476
477inline int LHiterator::next()
478{
479  if(wayup.empty())
480        return -1; 
481  else
482    {
483      int i = wayup.top();
484      wayup.pop();
485      push_lh(i);
486      return i;
487    };
488}
489
490inline void LHiterator::push_lh(int i)
491{
492  vector<Arc>& arcs = sgraph[i].heads;
493  for(vector<Arc>::iterator a = arcs.begin(); a != arcs.end(); ++a)
494    if(mgraph[sgraph[a->dst].mnode].pos < mgraph[sgraph[i].mnode].pos)
495      wayup.push(a->dst);
496}
497
498//----------------------------------------------------------------------------------------------------
499//----------------------------------------------------------------------------------------------------
500
501class LDiterator
502{
503public:
504  LDiterator(SGraph& sg, int n);
505
506  int next();
507
508private:
509
510  SGraph& sgraph;
511  MGraph& mgraph;
512  int thenode;
513  stack<int> waydown;
514
515  void push_ld(int i);
516};
517
518inline LDiterator::LDiterator(SGraph& sg, int n) : sgraph(sg), mgraph(sg.mgraph), thenode(n)
519{
520  push_ld(n);
521}
522
523inline int LDiterator::next()
524{
525  if(waydown.empty())
526    return -1;
527  else
528    {
529      int k = waydown.top();
530      waydown.pop();
531      push_ld(k);
532      return k;
533    }
534}
535
536inline void LDiterator::push_ld(int i)
537{
538  vector<Arc>& arcs = sgraph[i].deps;
539  for(vector<Arc>::iterator a = arcs.begin(); a != arcs.end(); ++a)
540    if(mgraph[sgraph[a->dst].mnode].pos < mgraph[sgraph[thenode].mnode].pos)
541      waydown.push(a->dst);
542}
543
544
[e7de6cc]545
[5f4d9c3]546#endif
Note: See TracBrowser for help on using the repository browser.