Changeset 3b02b04 for src/dgp/sgraph.hh


Ignore:
Timestamp:
01/17/13 20:50:41 (11 years ago)
Author:
Tomasz Obrebski <to@…>
Branches:
master
Children:
d2f119e
Parents:
555c7f8
git-author:
Tomasz Obrebski <to@…> (01/17/13 20:50:41)
git-committer:
Tomasz Obrebski <to@…> (01/17/13 20:50:41)
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/dgp/sgraph.hh

    re7de6cc r3b02b04  
    77#include <vector> 
    88#include <bitset> 
     9#include <stack> 
    910 
    1011#include "const.hh" 
     
    7172  if(init_attached != p.init_attached) return false; 
    7273  if(fin_attached != p.fin_attached) return false; 
    73    
    74   list<Boubble*>::const_iterator b1 = p.boubbles.begin(); 
    75   for(list<Boubble*>::const_iterator b = boubbles.begin(); b != boubbles.end(); b++) 
    76     { 
    77       if(b1 == p.boubbles.end()) 
    78         return false; 
    79       if(!(**b == **b1)) 
    80         return false; 
    81     } 
    82   if(b1 != p.boubbles.end()) 
     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()) 
    8380    return false; 
    84    
     81 
    8582  return true; 
    8683} 
     
    172169{ 
    173170   
     171  SNode() { visible_as_neighbour = true; } 
     172 
    174173  int mnode; 
    175174 
    176175  NodeProp prop; 
     176 
     177  vector<int> edge; 
     178  bool        edge_contains_self; 
     179  bool visible_as_neighbour; 
    177180 
    178181  bitset<MAXNODES> LV; 
     
    231234  int size()              {return nodes.size(); } 
    232235 
     236  friend class LViterator; 
     237  friend class LHiterator; 
     238  friend class LDiterator; 
     239  friend class LNiterator; 
     240 
    233241private: 
    234242 
     
    259267 
    260268//---------------------------------------------------------------------------------------------------- 
     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  // if(!strict) 
     330  //   { 
     331  //     push_ld(n); 
     332  //              push_ln(n); 
     333  //   } 
     334} 
     335 
     336inline int LViterator::next() 
     337{ 
     338  if(wayup.empty()) 
     339    { 
     340      if(waydown.empty()) 
     341        return -1; //  
     342      else 
     343        { 
     344          int k = waydown.top(); 
     345          waydown.pop(); 
     346          push_ld(k); 
     347          push_ln(k); 
     348          if(wayup.empty()) 
     349            return -1; // k NIE MA POPRZEDNIKÓW, NIE MO¯E TE¯ ZATEM MIEÆ LEWOSTRONNYCH PODRZÊDNIKÓW 
     350          else 
     351            { 
     352              int i = wayup.top(); 
     353              wayup.pop(); 
     354              push_lh(i); 
     355              return i; 
     356            } 
     357        } 
     358       
     359    } 
     360  else 
     361    { 
     362      int i = wayup.top(); 
     363      wayup.pop(); 
     364      push_lh(i); 
     365      return i; 
     366    }; 
     367} 
     368 
     369inline void LViterator::push_ld(int i) 
     370{ 
     371  vector<Arc>& arcs = sgraph[i].deps; 
     372  for(vector<Arc>::iterator a = arcs.begin(); a != arcs.end(); ++a) 
     373    if(mgraph[sgraph[a->dst].mnode].pos < mgraph[sgraph[i].mnode].pos) 
     374      push(waydown,a->dst); 
     375} 
     376 
     377inline void LViterator::push_lh(int i) 
     378{ 
     379  vector<Arc>& arcs = sgraph[i].heads; 
     380  for(vector<Arc>::iterator a = arcs.begin(); a != arcs.end(); ++a) 
     381    if(mgraph[sgraph[a->dst].mnode].pos < mgraph[sgraph[i].mnode].pos) 
     382      push(wayup,a->dst); 
     383} 
     384 
     385inline void LViterator::push_ln(int i) 
     386{ 
     387  vector<int>& mpredecessors = mgraph[sgraph[i].mnode].pred; 
     388  for(vector<int>::iterator mp = mpredecessors.begin(); mp != mpredecessors.end(); ++mp) // poprzedniki w mgraph 
     389    { 
     390      vector<int>& spredecessors = mgraph[*mp].snodes; 
     391      for(vector<int>::iterator sp = spredecessors.begin(); sp != spredecessors.end(); ++sp ) 
     392        if(sgraph[*sp].visible_as_neighbour || !strict) 
     393          push(wayup,*sp); 
     394    } 
     395} 
     396 
     397 
     398//---------------------------------------------------------------------------------------------------- 
     399//---------------------------------------------------------------------------------------------------- 
     400 
     401class LNiterator 
     402{ 
     403public: 
     404  LNiterator(SGraph& sg, int n); 
     405 
     406  int next(); 
     407 
     408private: 
     409 
     410  SGraph& sgraph; 
     411  MGraph& mgraph; 
     412  int thenode; 
     413  stack<int> wayup; 
     414 
     415  void push_ln(int i); 
     416}; 
     417 
     418inline LNiterator::LNiterator(SGraph& sg, int n) : sgraph(sg), mgraph(sg.mgraph), thenode(n) 
     419{ 
     420  push_ln(n); 
     421} 
     422 
     423inline int LNiterator::next() 
     424{ 
     425  if(wayup.empty()) 
     426    return -1; 
     427  else 
     428    { 
     429      int i = wayup.top(); 
     430      wayup.pop(); 
     431      return i; 
     432    }; 
     433} 
     434 
     435inline void LNiterator::push_ln(int i) 
     436{ 
     437  vector<int>& mpredecessors = mgraph[sgraph[i].mnode].pred; 
     438  for(vector<int>::iterator mp = mpredecessors.begin(); mp != mpredecessors.end(); ++mp) // poprzedniki w mgraph 
     439    { 
     440      vector<int>& spredecessors = mgraph[*mp].snodes; 
     441      for(vector<int>::iterator sp = spredecessors.begin(); sp != spredecessors.end(); ++sp ) 
     442          wayup.push(*sp); 
     443    } 
     444} 
     445 
     446 
     447//---------------------------------------------------------------------------------------------------- 
     448//---------------------------------------------------------------------------------------------------- 
     449 
     450class LHiterator 
     451{ 
     452public: 
     453  LHiterator(SGraph& sg, int n); 
     454 
     455  int next(); 
     456 
     457private: 
     458 
     459  SGraph& sgraph; 
     460  MGraph& mgraph; 
     461  stack<int> wayup; 
     462 
     463  void push_lh(int i); 
     464}; 
     465 
     466inline LHiterator::LHiterator(SGraph& sg, int n) : sgraph(sg), mgraph(sg.mgraph) 
     467{ 
     468  push_lh(n); 
     469} 
     470 
     471inline int LHiterator::next() 
     472{ 
     473  if(wayup.empty()) 
     474        return -1;  
     475  else 
     476    { 
     477      int i = wayup.top(); 
     478      wayup.pop(); 
     479      push_lh(i); 
     480      return i; 
     481    }; 
     482} 
     483 
     484inline void LHiterator::push_lh(int i) 
     485{ 
     486  vector<Arc>& arcs = sgraph[i].heads; 
     487  for(vector<Arc>::iterator a = arcs.begin(); a != arcs.end(); ++a) 
     488    if(mgraph[sgraph[a->dst].mnode].pos < mgraph[sgraph[i].mnode].pos) 
     489      wayup.push(a->dst); 
     490} 
     491 
     492//---------------------------------------------------------------------------------------------------- 
     493//---------------------------------------------------------------------------------------------------- 
     494 
     495class LDiterator 
     496{ 
     497public: 
     498  LDiterator(SGraph& sg, int n); 
     499 
     500  int next(); 
     501 
     502private: 
     503 
     504  SGraph& sgraph; 
     505  MGraph& mgraph; 
     506  int thenode; 
     507  stack<int> waydown; 
     508 
     509  void push_ld(int i); 
     510}; 
     511 
     512inline LDiterator::LDiterator(SGraph& sg, int n) : sgraph(sg), mgraph(sg.mgraph), thenode(n) 
     513{ 
     514  push_ld(n); 
     515} 
     516 
     517inline int LDiterator::next() 
     518{ 
     519  if(waydown.empty()) 
     520    return -1; 
     521  else 
     522    { 
     523      int k = waydown.top(); 
     524      waydown.pop(); 
     525      push_ld(k); 
     526      return k; 
     527    } 
     528} 
     529 
     530inline void LDiterator::push_ld(int i) 
     531{ 
     532  vector<Arc>& arcs = sgraph[i].deps; 
     533  for(vector<Arc>::iterator a = arcs.begin(); a != arcs.end(); ++a) 
     534    if(mgraph[sgraph[a->dst].mnode].pos < mgraph[sgraph[thenode].mnode].pos) 
     535      waydown.push(a->dst); 
     536} 
     537 
     538 
    261539 
    262540#endif 
Note: See TracChangeset for help on using the changeset viewer.