source: src/dgp/sgraph.hh @ a15e59b

Last change on this file since a15e59b was e7de6cc, checked in by Tomasz Obrebski <to@…>, 12 years ago

new version of dgp
added dgc, tre and compdic components
compiledic renamed to compdic_utf8
./configure updated

  • Property mode set to 100644
File size: 6.5 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
10#include "const.hh"
11#include "mgraph.hh"
12#include "thesymbols.hh"
13#include "boubble.hh"
14
15
16using namespace std;
17
18//====================================================================================================
19// CLASS Arc
20//====================================================================================================
21struct Arc
22{
23  int dst;
24  Role role;
25  int headanc;
26  int depanc;
27 
28  Arc(int d, Role r, int ha, int da) : dst(d), role(r), headanc(ha), depanc(da) {};
29};
30
31//====================================================================================================
32// CLASS NodeProp
33//====================================================================================================
34
35struct NodeProp
36{
37  NodeProp();
38  NodeProp(const NodeProp& p);
39  ~NodeProp();
40
41  bool operator==(const NodeProp& p);
42  NodeProp& operator=(const NodeProp& p);
43
44  void clear_boubbles();
45  void merge_boubbles(list<Boubble*> new_boubbles);
46
47  void copy(const NodeProp& p);
48  void clear();
49
50  RoleSet required;
51  RoleSet forbidden;
52  RoleSet attached;
53
54  bool init_attached;
55  bool fin_attached;
56 
57  FlagSet flags;
58
59  list<Boubble*>   boubbles;
60};
61
62//----------------------------------------------------------------------------------------------------
63
64inline
65bool NodeProp::operator==(const NodeProp& p)
66{
67  if(required != p.required) return false;
68  if(forbidden != p.forbidden) return false;
69  if(attached != p.attached) return false;
70  if(flags != p.flags) return false;
71  if(init_attached != p.init_attached) return false;
72  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())
83    return false;
84 
85  return true;
86}
87
88//----------------------------------------------------------------------------------------------------
89
90inline
91void NodeProp::clear_boubbles()
92{
93  for(list<Boubble*>::iterator b = boubbles.begin(); b!=boubbles.end(); b++)
94    delete *b;
95  boubbles.clear();
96}
97
98//----------------------------------------------------------------------------------------------------
99
100inline
101void NodeProp::merge_boubbles(list<Boubble*> new_boubbles)
102{
103  boubbles.merge(new_boubbles);
104}
105
106//----------------------------------------------------------------------------------------------------
107
108inline
109void NodeProp::copy(const NodeProp& p)
110{
111  required=p.required;
112  forbidden=p.forbidden;
113  attached=p.attached;
114  flags=p.flags;
115  init_attached=p.init_attached;
116  fin_attached=p.fin_attached;
117  for(list<Boubble*>::const_iterator b = p.boubbles.begin(); b!=p.boubbles.end(); b++)
118    boubbles.push_back(new Boubble(**b));
119}
120
121//----------------------------------------------------------------------------------------------------
122
123inline
124NodeProp::~NodeProp()
125{
126  clear_boubbles();
127}
128//----------------------------------------------------------------------------------------------------
129
130inline
131NodeProp::NodeProp()
132{
133  clear();
134}
135
136//----------------------------------------------------------------------------------------------------
137
138inline
139NodeProp::NodeProp(const NodeProp& p)
140{
141  copy(p);
142}
143
144//----------------------------------------------------------------------------------------------------
145
146inline
147NodeProp& NodeProp::operator=(const NodeProp& p)
148{
149  clear();
150  copy(p);
151  return *this;
152}
153
154//----------------------------------------------------------------------------------------------------
155
156inline
157void NodeProp::clear()
158{
159  required.reset();
160  forbidden.reset();
161  attached.reset();
162  init_attached=false;
163  fin_attached=false;
164  clear_boubbles();
165}
166
167//====================================================================================================
168// CLASS SNode
169//====================================================================================================
170
171struct SNode
172{
173 
174  int mnode;
175
176  NodeProp prop;
177
178  bitset<MAXNODES> LV;
179  bitset<MAXNODES> LH;
180  bitset<MAXNODES> LD;
181  bool in_LH;
182
183  vector<Arc> heads;
184  vector<Arc> deps;
185
186  void clear();
187  bool saturated();
188};
189
190//----------------------------------------------------------------------------------------------------
191inline
192void SNode::clear()
193{ prop.clear(), LV.reset(), LD.reset(), LH.reset(), heads.clear(), deps.clear(); }
194//----------------------------------------------------------------------------------------------------
195inline
196bool SNode::saturated()
197{ return prop.required.none(); }
198
199//====================================================================================================
200// SGraph CLASS
201//====================================================================================================
202
203class SGraph
204{
205public:
206
207  enum Output { HEADS=1, DEPS=2, SETS=4, CONSTRAINTS=8, BOUBBLES=16 };
208
209  SGraph(MGraph& mg) : mgraph(mg)               { clear(); }
210
211  SNode& operator[](const int i)                { return nodes[i]; }
212
213  void clear()                                  { nodes.clear(); }
214  int  add_base_snode(int mnodeind);
215  int  clone(int ancind, NodeProp newprop);
216  void update_left(int headind, int depind);
217  void update_right(int headind, int depind);
218  bool visible(int left, int right);
219  bool saturated(int node);
220
221  Cat  cat(int i) const { return mgraph[nodes[i].mnode].cat; }   
222  char* form(int i) const { return mgraph[nodes[i].mnode].form; }
223
224  int print_node(FILE* f, int n, unsigned int info);
225  int print_node_debug(FILE* f, const char* pref, int n, int anc);
226
227  void print_arc(FILE* f, int left, int right, Role role, int dir); // 0 - left, 1 - right
228
229  //private:
230
231  int size()              {return nodes.size(); }
232
233private:
234
235  MGraph& mgraph;
236 
237  vector<SNode> nodes;
238
239  int lastnodeind()       { return nodes.size()-1; }
240  SNode& makenewnode()    { nodes.push_back(SNode()); nodes.back().clear(); return nodes.back(); }
241
242  int sprint_node(char* buf, int n, int anc, unsigned int info);
243  int sprint_node_debug(char* buf, const char* pref, int n, int anc);
244};
245
246//----------------------------------------------------------------------------------------------------
247
248inline bool SGraph::visible(int left, int right)
249{
250  return nodes[right].LV[left];
251}
252
253//----------------------------------------------------------------------------------------------------
254
255inline bool SGraph::saturated(int node)
256{
257  return nodes[node].saturated();
258}
259
260//----------------------------------------------------------------------------------------------------
261
262#endif
Note: See TracBrowser for help on using the repository browser.