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 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 
26 namespace faudes {
27 
28 /*
29 *********************************************************************************
30 *********************************************************************************
31 *********************************************************************************
32 
33  IndexSet
34 
35 *********************************************************************************
36 *********************************************************************************
37 *********************************************************************************
38 */
39 
40 // std faudes type
41 FAUDES_TYPE_IMPLEMENTATION(IndexSet,IndexSet,TBaseSet<Idx>)
42 
43 /*
44 FAUDES_TYPE_IMPLEMENTATION_NEW(IndexSet,IndexSet,TBaseSet<Idx>)
45 FAUDES_TYPE_IMPLEMENTATION_COPY(IndexSet,IndexSet,TBaseSet<Idx>)
46 FAUDES_TYPE_IMPLEMENTATION_CAST(IndexSet,IndexSet,TBaseSet<Idx>)
47 FAUDES_TYPE_IMPLEMENTATION_ASSIGN(IndexSet,IndexSet,TBaseSet<Idx>)
48 FAUDES_TYPE_IMPLEMENTATION_EQUAL(IndexSet,IndexSet,TBaseSet<Idx>)
49 FAUDES_TYPE_IMPLEMENTATION_REGISTER(IndexSet,IndexSet,TBaseSet<Idx>)
50 */
51 
52 
53 
54 // IndexSet()
56  FD_DC("IndexSet(" << this << ")::IndexSet()");
57  Name("IndexSet");
58 }
59 
60 // IndexSet(rOtherSet)
61 IndexSet::IndexSet(const IndexSet& rOtherSet) :
62  TBaseSet<Idx>(rOtherSet)
63 {
64  FD_DC("IndexSet(" << this << ")::IndexSet(rOtherSet " << &rOtherSet << ")");
65  // copy my members (none)
66 }
67 
68 // IndexSet(rOtherSet)
69 IndexSet::IndexSet(const TBaseSet<Idx>& rOtherSet) :
70  TBaseSet<Idx>(rOtherSet)
71 {
72  FD_DC("IndexSet(" << this << ")::IndexSet(rOtherSet " << &rOtherSet << ")");
73  // copy my members (none)
74 }
75 
76 // File constructor
77 IndexSet::IndexSet(const std::string& rFilename, const std::string& rLabel) :
78  TBaseSet<Idx>()
79 {
80  FD_DC("IndexSet(" << this << ")::IndexSet(" << rFilename << ")");
81  Read(rFilename, rLabel);
82 }
83 
84 // assignment (attributes to default)
85 void IndexSet::DoAssign(const IndexSet& rSourceSet) {
86  FD_DC("NameSet(" << this << ")::DoAssign(..)");
87  // call base
88  TBaseSet<Idx>::DoAssign(rSourceSet);
89 }
90 
91 // DoWrite(rTw&)
92 void IndexSet::DoWrite(TokenWriter& rTw, const std::string& rLabel,const Type* pContext) const {
93  // figure section
94  std::string label=rLabel;
95  if(label=="") label=Name();
96  if(label=="") label="IndexSet";
97  FD_DC("IndexSet(" << this << ")::DoWrite(..): section " << label);
98  /*
99  // figure context: default to non
100  const SymbolTable* symboltable=0;
101  const std::map<Idx,Idx>* indexmap=0;
102  // figure context: can use vgenerator
103  if(const Generator* vgen=dynamic_cast<const Generator*>(pContext)) {
104  FD_DC("TaIndexSet(" << this << ")::DoWrite(..): generator context");
105  symboltable=&vgen->StateSymbolTable();
106  indexmap=&vgen->MinStateIndexMap();
107  }
108  */
109  rTw.WriteBegin(label);
110  Iterator it, conit;
111  // iterate states to write
112  for (it = Begin(); it != End(); ++it) {
113  // identify consecutive block
114  Idx start = *it;
115  Idx anoncount = 0;
116  for(conit=it; conit != End(); ++conit) {
117  if(!Attribute(*conit).IsDefault()) break;
118  if(*conit != start+anoncount) break;
119  ++anoncount;
120  }
121  // write consecutive block
122  if (anoncount > FD_CONSECUTIVE) {
123  rTw.WriteBegin("Consecutive");
124  rTw << start;
125  rTw << start+anoncount-1;
126  rTw.WriteEnd("Consecutive");
127  it=conit;
128  }
129  // break loop
130  if(it == End() )
131  break;
132  // write individual state
133  rTw << *it;
134  // write state attribute
135  const AttributeVoid& attr=Attribute(*it);
136  attr.Write(rTw,"",pContext);
137  }
138  rTw.WriteEnd(label);
139 }
140 
141 // DoXWrite(rTw&)
142 void IndexSet::DoXWrite(TokenWriter& rTw, const std::string& rLabel,const Type* pContext) const {
143  // Set up outer tag
144  Token btag=XBeginTag(rLabel,"IndexSet");
145  rTw.Write(btag);
146  FD_DC("IndexSet(" << this << ")::DoXWrite(..): section " << btag.StringValue() << " #" << Size());
147 
148  /*
149  // figure context: default to non
150  const SymbolTable* symboltable=0;
151  const std::map<Idx,Idx>* indexmap=0;
152  // figure context: can use vgenerator
153  if(const Generator* vgen=dynamic_cast<const Generator*>(pContext)) {
154  FD_DC("TaIndexSet(" << this << ")::DoWrite(..): generator context");
155  symboltable=&vgen->StateSymbolTable();
156  indexmap=&vgen->MinStateIndexMap();
157  }
158  */
159  // Loop elements
160  Iterator it, conit;
161  std::string etstr=XElementTag();
162  for(it = Begin(); it != End(); ++it) {
163  // identify consecutive block
164  Idx start = *it;
165  Idx anoncount = 0;
166  for(conit=it; conit != End(); ++conit) {
167  if(!Attribute(*conit).IsDefault()) break;
168  if(*conit != start+anoncount) break;
169  ++anoncount;
170  }
171  // write anonymous block
172  if(anoncount > FD_CONSECUTIVE) {
173  Token contag;
174  contag.SetEmpty("Consecutive");
175  contag.InsAttributeInteger("from",start);
176  contag.InsAttributeInteger("to",start+anoncount-1);
177  rTw.Write(contag);
178  it=conit;
179  }
180  // break loop
181  if(it == End()) break;
182  // prepare token
183  Token sttag;
184  Idx index=*it;
185  sttag.SetBegin(etstr);
186  sttag.InsAttributeInteger("id",index);
187  rTw.Write(sttag);
188  // attribute
189  const AttributeVoid& attr=Attribute(index);
190  if(!attr.IsDefault()) attr.XWrite(rTw);
191  // done
192  rTw.WriteEnd(etstr);
193  }
194  rTw.WriteEnd(btag.StringValue());
195 }
196 
197 // DoRead(rTr, rLabel)
198 void IndexSet::DoRead(TokenReader& rTr, const std::string& rLabel,const Type* pContext) {
199  // set up defaults
200  std::string label=rLabel;
201  std::string ftype=TypeName();
202  std::string etstr=XElementTag();
203  // figure section from current token
204  Token token;
205  if(label=="") {
206  rTr.Peek(token);
207  if(token.IsBegin()) label=token.StringValue();
208  }
209  if(label=="") label="IndexSet";
210  Name(label);
211  // read begin
212  FD_DC("IndexSet(" << this << ")::DoRead(..): section " << label);
213  rTr.ReadBegin(label,token);
214  if(token.ExistsAttributeString("name"))
215  Name(token.AttributeStringValue("name"));
216  // prepare attribute
217  AttributeVoid* attrp = AttributeType()->New();
218  FD_DC("IndexSet(" << this << ")::DoRead(..): attribute type " << typeid(*attrp).name());
219  // loop tokens
220  while(!rTr.Eos(label)) {
221  rTr.Peek(token);
222  // read by index (faudes format)
223  if(token.IsInteger()) {
224  FD_DC("TaIndexSet(" << this << ")::DoRead(..): inserting element in faudes format \""
225  << token.IntegerValue() << "\"");
226  rTr.Get(token);
227  Idx index=token.IntegerValue();
228  // read attribute
229  attrp->Read(rTr,"",pContext);
230  // skip unknown attributes
231  AttributeVoid::Skip(rTr);
232  // insert element with attribute
233  Insert(index);
234  Attribute(index,*attrp);
235  continue;
236  }
237  // read consecutive block of anonymous states (both faudes or XML)
238  if(token.IsBegin("Consecutive")) {
239  Token ctag;
240  rTr.ReadBegin("Consecutive",ctag);
241  // Read up to two tokens for faudes format
242  Token token1,token2;
243  rTr.Peek(token1);
244  if(!token1.IsEnd()) rTr.Get(token1);
245  rTr.Peek(token2);
246  if(!token2.IsEnd()) rTr.Get(token2);
247  rTr.ReadEnd("Consecutive");
248  // Try to interpret
249  int from=-1;
250  int to=-1;
251  if(token1.IsInteger()) from=token1.IntegerValue();
252  if(token2.IsInteger()) to=token2.IntegerValue();
253  if(ctag.ExistsAttributeInteger("from"))
254  from= ctag.AttributeIntegerValue("from");
255  if(ctag.ExistsAttributeInteger("to"))
256  to= ctag.AttributeIntegerValue("to");
257  // Insist in valid range
258  if(from<0 || to <0 || to < from) {
259  delete attrp;
260  std::stringstream errstr;
261  errstr << "Invalid range of consecutive indices" << rTr.FileLine();
262  throw Exception("IndexSet::DoRead", errstr.str(), 50);
263  }
264  FD_DC("IndexSet(" << this << ")::DoRead(..): inserting range "
265  << from << " to " << to);
266  for(Idx index = (Idx) from; index <= (Idx) to; ++index)
267  Insert(index);
268  continue;
269  }
270  // read element section (XML format)
271  if(token.IsBegin(etstr)) {
272  FD_DC("TaIndexSet(" << this << ")::DoRead(..): inserting element in xml format \""
273  << token.StringValue() << "\"");
274  rTr.ReadBegin(etstr,token);
275  Idx index=token.AttributeIntegerValue("id");
276  // read faudes attribute
277  attrp->Read(rTr,"",pContext);
278  // skip unknown faudes attributes
279  rTr.ReadEnd(etstr);
280  // insert element with attribute
281  Insert(index);
282  Attribute(index,*attrp);
283  continue;
284  }
285  // cannot process token
286  delete attrp;
287  std::stringstream errstr;
288  errstr << "Invalid token type " << token.Type() << " at " << rTr.FileLine();
289  throw Exception("IndexSet::DoRead", errstr.str(), 50);
290  }
291  rTr.ReadEnd(label);
292  // dispose attribute
293  delete attrp;
294 }
295 
296 
297 // MaxIndex()
298 Idx IndexSet::MaxIndex(void) const {
299  if(Size()==0) return 0;
300  Idx res = *(--(End()));
301 #ifdef FAUDES_CHECKED
302  if(res==FAUDES_IDX_MAX) {
303  std::stringstream errstr;
304  errstr << "IndexSet(): MaxIndex exceeds platform limitations -- reimplement index allocation: \""
305  << Name() << "\"";
306  throw Exception("vGenerator::EventReame(name)", errstr.str(), 61);
307  }
308 #endif
309  return res;
310 }
311 
312 // Insert()
314  FD_DC("IndexSet(" << this << ")::Insert()");
315  Idx index=MaxIndex()+1;
316  TBaseSet<Idx>::Insert(index);
317  FD_DC("IndexSet(" << this << ")::Insert(): done");
318  return index;
319 }
320 
321 //Insert(idx)
322 bool IndexSet::Insert(const Idx& rIndex) {
323  FD_DC("IndexSet(" << this << ")::Insert(idx)");
324  return TBaseSet<Idx>::Insert(rIndex);
325 }
326 
327 
328 //Valid(idx)
329 bool IndexSet::Valid(const Idx& rIndex) const {
330  return rIndex!=0;
331 }
332 
333 
334 // Signature()
336  // prepare result
337  Idx sig = 0;
338  // helpers:
339  Idx i = 1;
340  Iterator it=Begin() ;
341  Iterator it_end=End() ;
342  // algorithm:
343  for(; it != it_end; ++it) {
344  sig += *it * i;
345  ++i;
346  }
347  return sig;
348 }
349 
350 
351 
352 
353 } // end name space
#define FD_DC(message)
Debug: optional report on container operations.
#define FAUDES_IDX_MAX
#define FD_CONSECUTIVE
Min number of states to use consecutive section in file output.
Classes IndexSet, TaIndexSet.
#define FAUDES_TYPE_IMPLEMENTATION(ftype, ctype, cbase)
faudes type implementation macros, overall
Definition: cfl_types.h:946
Minimal Attribute.
virtual bool IsDefault(void) const
Test for default value.
static void Skip(TokenReader &rTr)
Skip attribute tokens.
Faudes exception class.
Set of indices.
Definition: cfl_indexset.h:78
virtual void DoXWrite(TokenWriter &tw, const std::string &rLabel="", const Type *pContext=0) const
Write to TokenWriter, see Type::Write for public wrappers.
Idx Signature(void) const
Compute an Idx type signature for a Set.
IndexSet(void)
Constructor.
virtual void DoWrite(TokenWriter &tw, const std::string &rLabel="", const Type *pContext=0) const
Write to TokenWriter, see Type::Write for public wrappers.
void DoAssign(const IndexSet &rSource)
Assign my members.
bool Valid(const Idx &rIndex) const
Test whether index is not 0.
Idx Insert(void)
Insert new index to set.
Idx MaxIndex(void) const
Get maximum index used in this set (0 for emptyset)
virtual void DoRead(TokenReader &rTr, const std::string &rLabel="", const Type *pContext=0)
Read from TokenReader, see Type::Read for public wrappers.
STL style set template.
Definition: cfl_baseset.h:93
A TokenReader reads sequential tokens from a file or string.
std::string FileLine(void) const
Return "filename:line".
bool Eos(const std::string &rLabel)
Peek a token and check whether it ends the specified section.
void ReadEnd(const std::string &rLabel)
Close the current section by matching the previous ReadBegin().
void ReadBegin(const std::string &rLabel)
Open a section by specified label.
bool Get(Token &token)
Get next token.
bool Peek(Token &token)
Peek next token.
A TokenWriter writes sequential tokens to a file, a string or stdout.
void Write(Token &rToken)
Write next token.
void WriteEnd(const std::string &rLabel)
Write end label.
void WriteBegin(const std::string &rLabel)
Write begin label.
Tokens model atomic data for stream IO.
Definition: cfl_token.h:53
const std::string & StringValue(void) const
Get string value of a name token.
Definition: cfl_token.cpp:177
Int AttributeIntegerValue(const std::string &name)
Access attribute value.
Definition: cfl_token.cpp:396
Int IntegerValue(void) const
Get integer value of a numeric token.
Definition: cfl_token.cpp:166
bool IsInteger(void) const
Test token Type.
Definition: cfl_token.cpp:218
bool ExistsAttributeString(const std::string &name)
Test attibute existence.
Definition: cfl_token.cpp:355
bool IsBegin(void) const
Test token Type.
Definition: cfl_token.cpp:258
void SetEmpty(const std::string &rName)
Initialize as empty-tag token.
Definition: cfl_token.cpp:105
void SetBegin(const std::string &rName)
Initialize as Begin token.
Definition: cfl_token.cpp:91
bool ExistsAttributeInteger(const std::string &name)
Test attibute existence.
Definition: cfl_token.cpp:365
void InsAttributeInteger(const std::string &name, Int value)
Insert named attribute with integer value.
Definition: cfl_token.cpp:318
bool IsEnd(void) const
Test token Type.
Definition: cfl_token.cpp:269
const std::string & AttributeStringValue(const std::string &name)
Access attribute value.
Definition: cfl_token.cpp:385
TokenType Type(void) const
Get token Type.
Definition: cfl_token.cpp:198
Base class of all libFAUDES objects that participate in the run-time interface.
Definition: cfl_types.h:239
virtual Token XBeginTag(const std::string &rLabel="", const std::string &rFallbackLabel="") const
Definition: cfl_types.cpp:315
void Read(const std::string &rFileName, const std::string &rLabel="", const Type *pContext=0)
Read configuration data from file with label specified.
Definition: cfl_types.cpp:261
virtual void XWrite(const std::string &pFileName, const std::string &rLabel="", const Type *pContext=0) const
Write configuration data to an XML file.
Definition: cfl_types.cpp:200
virtual Type * New(void) const
Construct on heap.
Definition: cfl_types.cpp:54
void Write(const Type *pContext=0) const
Write configuration data to console.
Definition: cfl_types.cpp:139
virtual bool Insert(const T &rElem)
Insert specified element.
Definition: cfl_baseset.h:1960
virtual const AttributeVoid * AttributeType(void) const
Attribute typeinfo.
Definition: cfl_baseset.h:2171
virtual const std::string & XElementTag(void) const
Get name of elements (used for XML IO)
Definition: cfl_baseset.h:1777
void DoAssign(const TBaseSet &rSourceSet)
assign my members
Definition: cfl_baseset.h:1261
Iterator End(void) const
Iterator to the end of set.
Definition: cfl_baseset.h:1896
Iterator Begin(void) const
Iterator to the begin of set.
Definition: cfl_baseset.h:1891
virtual const std::string & TypeName(void) const
Get objects's type name.
Definition: cfl_baseset.h:1795
virtual const AttributeVoid & Attribute(const Idx &rElem) const
Attribute access.
Definition: cfl_baseset.h:2290
const std::string & Name(void) const
Return name of TBaseSet.
Definition: cfl_baseset.h:1755
Idx Size(void) const
Get Size of TBaseSet.
Definition: cfl_baseset.h:1819
libFAUDES resides within the namespace faudes.
uint32_t Idx
Type definition for index type (allways 32bit)

libFAUDES 2.32b --- 2024.03.01 --- c++ api documentaion by doxygen