cfl_indexset.cpp
Go to the documentation of this file.
1 /** @file cfl_indexset.cpp @brief Classes IndexSet */
2 
3 /* FAU Discrete Event Systems Library (libfaudes)
4 
5  Copyright (C) 2006 Bernd Opitz
6  Copyright (C) 2007, 2024 Thomas Moor
7  Exclusive copyright is granted to Klaus Schmidt
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #include "cfl_indexset.h"
24 
25 //local debug
26 //#undef FD_DC
27 //#define FD_DC(m) FD_WARN(m)
28 
29 
30 namespace faudes {
31 
32 /*
33 *********************************************************************************
34 *********************************************************************************
35 *********************************************************************************
36 
37  IndexSet
38 
39 *********************************************************************************
40 *********************************************************************************
41 *********************************************************************************
42 */
43 
44 // std faudes type
45 FAUDES_TYPE_IMPLEMENTATION(IndexSet,IndexSet,TBaseSet<Idx>)
46 
47 /*
48 FAUDES_TYPE_IMPLEMENTATION_NEW(IndexSet,IndexSet,TBaseSet<Idx>)
49 FAUDES_TYPE_IMPLEMENTATION_COPY(IndexSet,IndexSet,TBaseSet<Idx>)
50 FAUDES_TYPE_IMPLEMENTATION_CAST(IndexSet,IndexSet,TBaseSet<Idx>)
51 FAUDES_TYPE_IMPLEMENTATION_ASSIGN(IndexSet,IndexSet,TBaseSet<Idx>)
52 FAUDES_TYPE_IMPLEMENTATION_EQUAL(IndexSet,IndexSet,TBaseSet<Idx>)
53 FAUDES_TYPE_IMPLEMENTATION_REGISTER(IndexSet,IndexSet,TBaseSet<Idx>)
54 */
55 
56 
57 
58 // IndexSet()
60  FD_DC("IndexSet(" << this << ")::IndexSet()");
61  Name("IndexSet");
62 }
63 
64 // IndexSet(rOtherSet)
65 IndexSet::IndexSet(const IndexSet& rOtherSet) :
66  TBaseSet<Idx>(rOtherSet)
67 {
68  FD_DC("IndexSet(" << this << ")::IndexSet(rOtherSet " << &rOtherSet << ")");
69  // copy my members (none)
70 }
71 
72 // IndexSet(rOtherSet)
73 IndexSet::IndexSet(const TBaseSet<Idx>& rOtherSet) :
74  TBaseSet<Idx>(rOtherSet)
75 {
76  FD_DC("IndexSet(" << this << ")::IndexSet(rOtherSet " << &rOtherSet << ")");
77  // copy my members (none)
78 }
79 
80 // File constructor
81 IndexSet::IndexSet(const std::string& rFilename, const std::string& rLabel) :
82  TBaseSet<Idx>()
83 {
84  FD_DC("IndexSet(" << this << ")::IndexSet(" << rFilename << ")");
85  Read(rFilename, rLabel);
86 }
87 
88 // assignment (attributes to default)
89 void IndexSet::DoAssign(const IndexSet& rSourceSet) {
90  FD_DC("NameSet(" << this << ")::DoAssign(..)");
91  // call base
92  TBaseSet<Idx>::DoAssign(rSourceSet);
93 }
94 
95 // DoWrite(rTw&)
96 void IndexSet::DoWrite(TokenWriter& rTw, const std::string& rLabel,const Type* pContext) const {
97  // figure section
98  std::string label=rLabel;
99  if(label=="") label=Name();
100  if(label=="") label="IndexSet";
101  FD_DC("IndexSet(" << this << ")::DoWrite(..): section " << label);
102  /*
103  // figure context: default to non
104  const SymbolTable* symboltable=0;
105  const std::map<Idx,Idx>* indexmap=0;
106  // figure context: can use vgenerator
107  if(const Generator* vgen=dynamic_cast<const Generator*>(pContext)) {
108  FD_DC("TaIndexSet(" << this << ")::DoWrite(..): generator context");
109  symboltable=&vgen->StateSymbolTable();
110  indexmap=&vgen->MinStateIndexMap();
111  }
112  */
113  rTw.WriteBegin(label);
114  Iterator it, conit;
115  // iterate states to write
116  for (it = Begin(); it != End(); ++it) {
117  // identify consecutive block
118  Idx start = *it;
119  Idx anoncount = 0;
120  for(conit=it; conit != End(); ++conit) {
121  if(!Attribute(*conit).IsDefault()) break;
122  if(*conit != start+anoncount) break;
123  ++anoncount;
124  }
125  // write consecutive block
126  if (anoncount > FD_CONSECUTIVE) {
127  rTw.WriteBegin("Consecutive");
128  rTw << start;
129  rTw << start+anoncount-1;
130  rTw.WriteEnd("Consecutive");
131  it=conit;
132  }
133  // break loop
134  if(it == End() )
135  break;
136  // write individual state
137  rTw << *it;
138  // write state attribute
139  const AttributeVoid& attr=Attribute(*it);
140  attr.Write(rTw,"",pContext);
141  }
142  rTw.WriteEnd(label);
143 }
144 
145 // DoXWrite(rTw&)
146 void IndexSet::DoXWrite(TokenWriter& rTw, const std::string& rLabel,const Type* pContext) const {
147  // Set up outer tag
148  Token btag=XBeginTag(rLabel,"IndexSet");
149  rTw.Write(btag);
150  FD_DC("IndexSet(" << this << ")::DoXWrite(..): section " << btag.StringValue() << " #" << Size());
151 
152  /*
153  // figure context: default to non
154  const SymbolTable* symboltable=0;
155  const std::map<Idx,Idx>* indexmap=0;
156  // figure context: can use vgenerator
157  if(const Generator* vgen=dynamic_cast<const Generator*>(pContext)) {
158  FD_DC("TaIndexSet(" << this << ")::DoWrite(..): generator context");
159  symboltable=&vgen->StateSymbolTable();
160  indexmap=&vgen->MinStateIndexMap();
161  }
162  */
163  // Loop elements
164  Iterator it, conit;
165  std::string etstr=ElementTag();
166  for(it = Begin(); it != End(); ++it) {
167  // identify consecutive block
168  Idx start = *it;
169  Idx anoncount = 0;
170  for(conit=it; conit != End(); ++conit) {
171  if(!Attribute(*conit).IsDefault()) break;
172  if(*conit != start+anoncount) break;
173  ++anoncount;
174  }
175  // write anonymous block
176  if(anoncount > FD_CONSECUTIVE) {
177  Token contag;
178  contag.SetEmpty("Consecutive");
179  contag.InsAttributeInteger("from",start);
180  contag.InsAttributeInteger("to",start+anoncount-1);
181  rTw.Write(contag);
182  it=conit;
183  }
184  // break loop
185  if(it == End()) break;
186  // prepare token
187  Token sttag;
188  Idx index=*it;
189  sttag.SetBegin(etstr);
190  sttag.InsAttributeInteger("id",index);
191  rTw.Write(sttag);
192  // attribute
193  const AttributeVoid& attr=Attribute(index);
194  if(!attr.IsDefault()) attr.XWrite(rTw);
195  // done
196  rTw.WriteEnd(etstr);
197  }
198  rTw.WriteEnd(btag.StringValue());
199 }
200 
201 // DoRead(rTr, rLabel)
202 void IndexSet::DoRead(TokenReader& rTr, const std::string& rLabel,const Type* pContext) {
203  // set up defaults
204  std::string label=rLabel;
205  std::string ftype=TypeName();
206  std::string etstr=ElementTag();
207  // figure section from current token
208  Token token;
209  if(label=="") {
210  rTr.Peek(token);
211  if(token.IsBegin()) label=token.StringValue();
212  }
213  if(label=="") label="IndexSet";
214  Name(label);
215  // read begin
216  FD_DC("IndexSet(" << this << ")::DoRead(..): section " << label);
217  rTr.ReadBegin(label,token);
218  if(token.ExistsAttributeString("name"))
219  Name(token.AttributeStringValue("name"));
220  // prepare attribute
221  AttributeVoid* attrp = AttributeType()->New();
222  FD_DC("IndexSet(" << this << ")::DoRead(..): attribute type " << typeid(*attrp).name());
223  // loop tokens
224  while(!rTr.Eos(label)) {
225  rTr.Peek(token);
226  // read by index (faudes format)
227  if(token.IsInteger()) {
228  FD_DC("TaIndexSet(" << this << ")::DoRead(..): inserting element in faudes format \""
229  << token.IntegerValue() << "\"");
230  rTr.Get(token);
231  Idx index=token.IntegerValue();
232  // read attribute
233  attrp->Read(rTr,"",pContext);
234  // skip unknown attributes
235  AttributeVoid::Skip(rTr);
236  // insert element with attribute
237  Insert(index);
238  Attribute(index,*attrp);
239  continue;
240  }
241  // read consecutive block of anonymous states (both faudes or XML)
242  if(token.IsBegin("Consecutive")) {
243  Token ctag;
244  rTr.ReadBegin("Consecutive",ctag);
245  // Read up to two tokens for faudes format
246  Token token1,token2;
247  rTr.Peek(token1);
248  if(!token1.IsEnd()) rTr.Get(token1);
249  rTr.Peek(token2);
250  if(!token2.IsEnd()) rTr.Get(token2);
251  rTr.ReadEnd("Consecutive");
252  // Try to interpret
253  int from=-1;
254  int to=-1;
255  if(token1.IsInteger()) from=token1.IntegerValue();
256  if(token2.IsInteger()) to=token2.IntegerValue();
257  if(ctag.ExistsAttributeInteger("from"))
258  from= ctag.AttributeIntegerValue("from");
259  if(ctag.ExistsAttributeInteger("to"))
260  to= ctag.AttributeIntegerValue("to");
261  // Insist in valid range
262  if(from<0 || to <0 || to < from) {
263  delete attrp;
264  std::stringstream errstr;
265  errstr << "Invalid range of consecutive indices" << rTr.FileLine();
266  throw Exception("IndexSet::DoRead", errstr.str(), 50);
267  }
268  FD_DC("IndexSet(" << this << ")::DoRead(..): inserting range "
269  << from << " to " << to);
270  for(Idx index = (Idx) from; index <= (Idx) to; ++index)
271  Insert(index);
272  continue;
273  }
274  // read element section (XML format)
275  if(token.IsBegin(etstr)) {
276  FD_DC("TaIndexSet(" << this << ")::DoRead(..): inserting element in xml format \""
277  << token.StringValue() << "\"");
278  rTr.ReadBegin(etstr,token);
279  Idx index=token.AttributeIntegerValue("id");
280  // read faudes attribute
281  attrp->Read(rTr,"",pContext);
282  // skip unknown faudes attributes
283  rTr.ReadEnd(etstr);
284  // insert element with attribute
285  Insert(index);
286  Attribute(index,*attrp);
287  continue;
288  }
289  // cannot process token
290  delete attrp;
291  std::stringstream errstr;
292  errstr << "Invalid token type " << token.Type() << " at " << rTr.FileLine();
293  throw Exception("IndexSet::DoRead", errstr.str(), 50);
294  }
295  rTr.ReadEnd(label);
296  // dispose attribute
297  delete attrp;
298 }
299 
300 
301 // MaxIndex()
302 Idx IndexSet::MaxIndex(void) const {
303  if(Size()==0) return 0;
304  Idx res = *(--(End()));
305 #ifdef FAUDES_CHECKED
306  if(res==FAUDES_IDX_MAX) {
307  std::stringstream errstr;
308  errstr << "IndexSet(): MaxIndex exceeds platform limitations -- reimplement index allocation: \""
309  << Name() << "\"";
310  throw Exception("vGenerator::EventReame(name)", errstr.str(), 61);
311  }
312 #endif
313  return res;
314 }
315 
316 // Insert()
318  FD_DC("IndexSet(" << this << ")::Insert()");
319  Idx index=MaxIndex()+1;
320  TBaseSet<Idx>::Insert(index);
321  FD_DC("IndexSet(" << this << ")::Insert(): done");
322  return index;
323 }
324 
325 //Insert(idx)
326 bool IndexSet::Insert(const Idx& rIndex) {
327  FD_DC("IndexSet(" << this << ")::Insert(idx)");
328  return TBaseSet<Idx>::Insert(rIndex);
329 }
330 
331 
332 //Valid(idx)
333 bool IndexSet::Valid(const Idx& rIndex) const {
334  return rIndex!=0;
335 }
336 
337 
338 // Signature()
340  // prepare result
341  Idx sig = 0;
342  // helpers:
343  Idx i = 1;
344  Iterator it=Begin() ;
345  Iterator it_end=End() ;
346  // algorithm:
347  for(; it != it_end; ++it) {
348  sig += *it * i;
349  ++i;
350  }
351  return sig;
352 }
353 
354 
355 
356 
357 } // end name space
#define FD_DC(message)
#define FAUDES_IDX_MAX
#define FD_CONSECUTIVE
Classes IndexSet, TaIndexSet.
#define FAUDES_TYPE_IMPLEMENTATION(ftype, ctype, cbase)
Definition: cfl_types.h:959
static void Skip(TokenReader &rTr)
Definition: cfl_types.cpp:369
virtual bool IsDefault(void) const
Definition: cfl_types.h:1076
virtual const std::string & ElementTag(void) const
Definition: cfl_types.cpp:459
const std::string & Name(void) const
Definition: cfl_types.cpp:422
virtual const std::string & TypeName(void) const
Definition: cfl_types.cpp:443
virtual void DoXWrite(TokenWriter &tw, const std::string &rLabel="", const Type *pContext=0) const
Idx Signature(void) const
virtual void DoWrite(TokenWriter &tw, const std::string &rLabel="", const Type *pContext=0) const
void DoAssign(const IndexSet &rSource)
bool Valid(const Idx &rIndex) const
Idx MaxIndex(void) const
virtual void DoRead(TokenReader &rTr, const std::string &rLabel="", const Type *pContext=0)
std::string FileLine(void) const
bool Eos(const std::string &rLabel)
void ReadEnd(const std::string &rLabel)
void ReadBegin(const std::string &rLabel)
bool Get(Token &token)
bool Peek(Token &token)
void Write(Token &rToken)
void WriteEnd(const std::string &rLabel)
void WriteBegin(const std::string &rLabel)
const std::string & StringValue(void) const
Definition: cfl_token.cpp:178
Int AttributeIntegerValue(const std::string &name)
Definition: cfl_token.cpp:397
Int IntegerValue(void) const
Definition: cfl_token.cpp:167
bool IsInteger(void) const
Definition: cfl_token.cpp:219
bool ExistsAttributeString(const std::string &name)
Definition: cfl_token.cpp:356
bool IsBegin(void) const
Definition: cfl_token.cpp:259
void SetEmpty(const std::string &rName)
Definition: cfl_token.cpp:106
void SetBegin(const std::string &rName)
Definition: cfl_token.cpp:92
bool ExistsAttributeInteger(const std::string &name)
Definition: cfl_token.cpp:366
void InsAttributeInteger(const std::string &name, Int value)
Definition: cfl_token.cpp:319
bool IsEnd(void) const
Definition: cfl_token.cpp:270
const std::string & AttributeStringValue(const std::string &name)
Definition: cfl_token.cpp:386
TokenType Type(void) const
Definition: cfl_token.cpp:199
virtual Token XBeginTag(const std::string &rLabel="", const std::string &rFallbackLabel="") const
Definition: cfl_types.cpp:321
void Read(const std::string &rFileName, const std::string &rLabel="", const Type *pContext=0)
Definition: cfl_types.cpp:267
virtual void XWrite(const std::string &pFileName, const std::string &rLabel="", const Type *pContext=0) const
Definition: cfl_types.cpp:206
virtual Type * New(void) const
Definition: cfl_types.cpp:54
void Write(const Type *pContext=0) const
Definition: cfl_types.cpp:145
virtual bool Insert(const T &rElem)
Definition: cfl_baseset.h:2025
virtual const AttributeVoid * AttributeType(void) const
Definition: cfl_baseset.h:2236
void DoAssign(const TBaseSet &rSourceSet)
Definition: cfl_baseset.h:1281
Iterator End(void) const
Definition: cfl_baseset.h:1956
Iterator Begin(void) const
Definition: cfl_baseset.h:1951
virtual const AttributeVoid & Attribute(const Idx &rElem) const
Definition: cfl_baseset.h:2355
Idx Size(void) const
Definition: cfl_baseset.h:1782
uint32_t Idx

libFAUDES 2.33h --- 2025.06.18 --- c++ api documentaion by doxygen