source: src/dgp/sgraph.hh @ acbabee

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

many changes, mainly dgp1 algorithm

  • Property mode set to 100644
File size: 14.4 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#include "global.hh"
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
118inline NodeProp::~NodeProp() { clear_boubbles(); }
119inline NodeProp::NodeProp() { clear(); }
120inline NodeProp::NodeProp(const NodeProp& p) { copy(p); }
121inline NodeProp& NodeProp::operator=(const NodeProp& p) { clear(); copy(p); return *this; }
122inline void NodeProp::clear()
123{
124  required.reset();
125  forbidden.reset();
126  attached.reset();
127  init_attached=false;
128  fin_attached=false;
129  clear_boubbles();
130}
131
132//====================================================================================================
133// CLASS Edge
134//====================================================================================================
135
136class Edge
137{
138public:
139  Edge() : _self(false) { }
140  Edge(const Edge& e, int map_self) { assign(e,map_self); }
141
142  bool self() const { return _self; }
143  list<int>& others() { return _others; }
144
145  void insert_self(bool b=true) { _self=b; }
146  void insert(int n) { list<int>::iterator i=others().begin(); while(i!=others().end() && *i<n) ++i; others().insert(i,n);}
147  void insert(list<int> l) { for(list<int>::const_iterator i=l.begin(); i!=l.end(); i++) insert(*i); }
148
149  void assign(const Edge& e, int map_self=-1) { _others = e._others; if(e.self()) { _self = false; insert(map_self); } }
150  const bool operator==(const Edge& e) const { return _self == e._self && _others == e._others; }
151 
152private:
153  bool _self;
154  list<int> _others;
155};
156
157//====================================================================================================
158// CLASS SNode
159//====================================================================================================
160
161struct SNode
162{
163 
164  SNode() { visible_as_neighbour = true; }
165
166  int mnode;
167
168  NodeProp prop;
169
170  Edge edge;
171  bool visible_as_neighbour;
172
173  bitset<MAXNODES> LV;
174  bitset<MAXNODES> LH;
175  bitset<MAXNODES> LD;
176  bool in_LH;
177
178  vector<Arc> heads;
179  vector<Arc> deps;
180
181  void clear();
182  bool saturated();
183
184  // void edge_clear()               { edge.clear(); edge_contains_self=false;}
185  // void edge_set(int i)            { edge.clear(); edge_contains_self=false; edge.push_back(i); }
186  // void edge_set(vector<int>& v)   { edge.assign(v.begin(),v.end()); edge_contains_self=false; }
187  // void edge_set_self(bool b=true) { edge.clear(); edge_contains_self=b; }
188  // void edge_add(int i)            { edge.push_back(i); }
189  // void edge_add(vector<int>& v)   { edge.insert(edge.end(),v.begin(),v.end()); }
190  // void edge_add_self(bool b=true) { edge_contains_self=b; }
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, Edge edge);
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  void update_edge(SGraph& sg, int e);
299
300private:
301  int snode;
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);
311
312};
313
314inline LViterator::LViterator(SGraph& sg, int n, bool s=true) : snode(n), sgraph(sg), mgraph(sg.mgraph), strict(s)
315{
316  if(sg[n].edge.self())
317    {
318      push_ld(n);
319      push_ln(n);
320    }
321
322  for(list<int>::iterator i=sg[n].edge.others().begin(); i!=sg[n].edge.others().end(); ++i)
323    {
324      push_ld(*i);
325      push_ln(*i);
326     
327    }
328}
329
330inline void LViterator::update_edge(SGraph& sg, int n)
331{
332  if(sg[n].edge.self()) 
333    {
334      push_ld(n);
335      push_ln(n);
336    }
337
338  for(list<int>::iterator i=sg[n].edge.others().begin(); i!=sg[n].edge.others().end(); ++i)
339    {
340      push_ld(*i);
341      push_ln(*i);
342    }
343}
344
345inline int LViterator::next()
346{
347  if(wayup.empty())
348    {
349      if(waydown.empty())
350        {
351          if(debug) fprintf(stderr,"\t\tLViterator(%d)\treturn %d\n",snode,-1);
352          return -1; //
353        }
354      else
355        {
356          int k = waydown.top();
357          waydown.pop();
358          push_ld(k);
359          push_ln(k);
360          if(wayup.empty())
361            {
362              if(debug) fprintf(stderr,"\t\tLViterator(%d)\treturn %d\n",snode,-1);
363              return -1; // k NIE MA POPRZEDNIKÓW, NIE MO¯E TE¯ ZATEM MIEÆ LEWOSTRONNYCH PODRZÊDNIKÓW
364            }
365          else
366            {
367              int i = wayup.top();
368              wayup.pop();
369              push_lh(i);
370              if(debug) fprintf(stderr,"\t\tLViterator(%d)\treturn %d\n",snode,i);
371              return i;
372            }
373        }
374     
375    }
376  else
377    {
378      int i = wayup.top();
379      wayup.pop();
380      push_lh(i);
381      if(debug) fprintf(stderr,"\t\tLViterator(%d)\treturn %d\n",snode,i);
382      return i;
383    };
384}
385
386inline void LViterator::push_ld(int i)
387{
388  vector<Arc>& arcs = sgraph[i].deps;
389  for(vector<Arc>::iterator a = arcs.begin(); a != arcs.end(); ++a)
390    if(mgraph[sgraph[a->dst].mnode].pos < mgraph[sgraph[i].mnode].pos)
391      {
392        push(waydown,a->dst);
393        if(debug) fprintf(stderr,"\t\tLViterator(%d)\tPUSH_LD waydown %d\n",snode,a->dst);
394      }
395}
396
397inline void LViterator::push_lh(int i)
398{
399  vector<Arc>& arcs = sgraph[i].heads;
400  for(vector<Arc>::iterator a = arcs.begin(); a != arcs.end(); ++a)
401    if(mgraph[sgraph[a->dst].mnode].pos < mgraph[sgraph[i].mnode].pos)
402      {
403        push(wayup,a->dst);
404        if(debug) fprintf(stderr,"\t\tLViterator(%d)\tPUSH_LH wayup %d\n",snode,a->dst);
405      }
406}
407
408inline void LViterator::push_ln(int i)
409{
410  vector<int>& mpredecessors = mgraph[sgraph[i].mnode].pred;
411  for(vector<int>::iterator mp = mpredecessors.begin(); mp != mpredecessors.end(); ++mp) // poprzedniki w mgraph
412    {
413      vector<int>& spredecessors = mgraph[*mp].snodes;
414      for(vector<int>::iterator sp = spredecessors.begin(); sp != spredecessors.end(); ++sp )
415        if(sgraph[*sp].visible_as_neighbour || !strict)
416          {
417            push(wayup,*sp);
418            if(debug) fprintf(stderr,"\t\tLViterator(%d)\tPUSH_LN wayup %d\n",snode, *sp);
419          }
420    }
421}
422
423
424//----------------------------------------------------------------------------------------------------
425//----------------------------------------------------------------------------------------------------
426
427class LNiterator
428{
429public:
430  LNiterator(SGraph& sg, int n);
431
432  int next();
433
434private:
435  int snode;
436  SGraph& sgraph;
437  MGraph& mgraph;
438  stack<int> wayup;
439
440  void push_ln(int i);
441};
442
443inline LNiterator::LNiterator(SGraph& sg, int n) : sgraph(sg), mgraph(sg.mgraph), snode(n)
444{
445  push_ln(n);
446}
447
448inline int LNiterator::next()
449{
450  if(wayup.empty())
451    {
452      if(debug) fprintf(stderr,"\t\tLNiterator(%d)\treturn %d\n",snode,-1);
453      return -1;
454    }
455  else
456    {
457      int i = wayup.top();
458      wayup.pop();
459      if(debug) fprintf(stderr,"\t\tLNiterator(%d)\treturn %d\n",snode,i);
460      return i;
461    };
462}
463
464inline void LNiterator::push_ln(int i)
465{
466  vector<int>& mpredecessors = mgraph[sgraph[i].mnode].pred;
467  for(vector<int>::iterator mp = mpredecessors.begin(); mp != mpredecessors.end(); ++mp) // poprzedniki w mgraph
468    {
469      vector<int>& spredecessors = mgraph[*mp].snodes;
470      for(vector<int>::iterator sp = spredecessors.begin(); sp != spredecessors.end(); ++sp )
471        {
472          wayup.push(*sp);
473          if(debug) fprintf(stderr,"\t\tLNiterator(%d)\tPUSH %d\n",snode,-1);
474        }
475    }
476}
477
478
479//----------------------------------------------------------------------------------------------------
480//----------------------------------------------------------------------------------------------------
481
482class LHiterator
483{
484public:
485  LHiterator(SGraph& sg, int n);
486
487  int next();
488
489private:
490  int snode;
491  SGraph& sgraph;
492  MGraph& mgraph;
493  stack<int> wayup;
494
495  void push_lh(int i);
496};
497
498inline LHiterator::LHiterator(SGraph& sg, int n) : snode(n), sgraph(sg), mgraph(sg.mgraph)
499{
500  push_lh(n);
501}
502
503inline int LHiterator::next()
504{
505  if(wayup.empty())
506        return -1; 
507  else
508    {
509      int i = wayup.top();
510      wayup.pop();
511      push_lh(i);
512      return i;
513    };
514}
515
516inline void LHiterator::push_lh(int i)
517{
518  vector<Arc>& arcs = sgraph[i].heads;
519  for(vector<Arc>::iterator a = arcs.begin(); a != arcs.end(); ++a)
520    if(mgraph[sgraph[a->dst].mnode].pos < mgraph[sgraph[i].mnode].pos)
521      {
522        wayup.push(a->dst);
523        if(debug) fprintf(stderr,"\t\tLHiterator(%d)\tPUSH %d\n",snode,-1);
524      }
525}
526
527//----------------------------------------------------------------------------------------------------
528//----------------------------------------------------------------------------------------------------
529
530class LDiterator
531{
532public:
533  LDiterator(SGraph& sg, int n);
534
535  int next();
536
537private:
538  int snode;
539  SGraph& sgraph;
540  MGraph& mgraph;
541  stack<int> waydown;
542
543  void push_ld(int i);
544};
545
546inline LDiterator::LDiterator(SGraph& sg, int n) : sgraph(sg), mgraph(sg.mgraph), snode(n)
547{
548  push_ld(n);
549}
550
551inline int LDiterator::next()
552{
553  if(waydown.empty())
554    return -1;
555  else
556    {
557      int k = waydown.top();
558      waydown.pop();
559      push_ld(k);
560      return k;
561    }
562}
563
564inline void LDiterator::push_ld(int i)
565{
566  vector<Arc>& arcs = sgraph[i].deps;
567  for(vector<Arc>::iterator a = arcs.begin(); a != arcs.end(); ++a)
568    if(mgraph[sgraph[a->dst].mnode].pos < mgraph[sgraph[snode].mnode].pos)
569      {
570        waydown.push(a->dst);
571        if(debug) fprintf(stderr,"\t\tLDiterator(%d)\tPUSH %d\n",snode,-1);
572      }
573}
574
575#endif
Note: See TracBrowser for help on using the repository browser.