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
30namespace faudes {
31
32/*
33*********************************************************************************
34*********************************************************************************
35*********************************************************************************
36
37 IndexSet
38
39*********************************************************************************
40*********************************************************************************
41*********************************************************************************
42*/
43
44// register
46
47// std faudes type
49
50
51
52
53// IndexSet()
55 FD_DC("IndexSet(" << this << ")::IndexSet()");
56 Name("IndexSet");
57}
58
59// IndexSet(rOtherSet)
60IndexSet::IndexSet(const IndexSet& rOtherSet) :
61 TBaseSet<Idx>(rOtherSet)
62{
63 FD_DC("IndexSet(" << this << ")::IndexSet(rOtherSet " << &rOtherSet << ")");
64 // copy my members (none)
65}
66
67// IndexSet(rOtherSet)
69 TBaseSet<Idx>(rOtherSet)
70{
71 FD_DC("IndexSet(" << this << ")::IndexSet(rOtherSet " << &rOtherSet << ")");
72 // copy my members (none)
73}
74
75// File constructor
76IndexSet::IndexSet(const std::string& rFilename, const std::string& rLabel) :
77 TBaseSet<Idx>()
78{
79 FD_DC("IndexSet(" << this << ")::IndexSet(" << rFilename << ")");
80 Read(rFilename, rLabel);
81}
82
83// assignment (attributes to default)
84void IndexSet::DoCopy(const IndexSet& rSourceSet) {
85 FD_DC("IndexSet(" << this << ")::DoCopy(..)");
86 // call base
87 TBaseSet<Idx>::DoCopy(rSourceSet);
88}
89
90// assignment (attributes to default)
91void IndexSet::DoMove(IndexSet& rSourceSet) {
92 FD_DF("IndexSet(" << this << ")::DoMove(..)");
93 // call base
94 TBaseSet<Idx>::DoMove(rSourceSet);
95}
96
97// DoWrite(rTw&)
98void IndexSet::DoWrite(TokenWriter& rTw, const std::string& rLabel,const Type* pContext) const {
99 // figure section
100 std::string label=rLabel;
101 if(label=="") label=Name();
102 if(label=="") label="IndexSet";
103 FD_DC("IndexSet(" << this << ")::DoWrite(..): section " << label);
104 /*
105 // figure context: default to non
106 const SymbolTable* symboltable=0;
107 const std::map<Idx,Idx>* indexmap=0;
108 // figure context: can use vgenerator
109 if(const Generator* vgen=dynamic_cast<const Generator*>(pContext)) {
110 FD_DC("TaIndexSet(" << this << ")::DoWrite(..): generator context");
111 symboltable=&vgen->StateSymbolTable();
112 indexmap=&vgen->MinStateIndexMap();
113 }
114 */
115 rTw.WriteBegin(label);
116 Iterator it, conit;
117 // iterate states to write
118 for (it = Begin(); it != End(); ++it) {
119 // identify consecutive block
120 Idx start = *it;
121 Idx anoncount = 0;
122 for(conit=it; conit != End(); ++conit) {
123 if(!Attribute(*conit).IsDefault()) break;
124 if(*conit != start+anoncount) break;
125 ++anoncount;
126 }
127 // write consecutive block
128 if (anoncount > FD_CONSECUTIVE) {
129 rTw.WriteBegin("Consecutive");
130 rTw << start;
131 rTw << start+anoncount-1;
132 rTw.WriteEnd("Consecutive");
133 it=conit;
134 }
135 // break loop
136 if(it == End() )
137 break;
138 // write individual state
139 rTw << *it;
140 // write state attribute
141 const AttributeVoid& attr=Attribute(*it);
142 attr.Write(rTw,"",pContext);
143 }
144 rTw.WriteEnd(label);
145}
146
147// DoXWrite(rTw&)
148void IndexSet::DoXWrite(TokenWriter& rTw, const std::string& rLabel,const Type* pContext) const {
149 // Set up outer tag
150 Token btag=XBeginTag(rLabel,"IndexSet");
151 rTw.Write(btag);
152 FD_DC("IndexSet(" << this << ")::DoXWrite(..): section " << btag.StringValue() << " #" << Size());
153
154 /*
155 // figure context: default to non
156 const SymbolTable* symboltable=0;
157 const std::map<Idx,Idx>* indexmap=0;
158 // figure context: can use vgenerator
159 if(const Generator* vgen=dynamic_cast<const Generator*>(pContext)) {
160 FD_DC("TaIndexSet(" << this << ")::DoWrite(..): generator context");
161 symboltable=&vgen->StateSymbolTable();
162 indexmap=&vgen->MinStateIndexMap();
163 }
164 */
165 // Loop elements
166 Iterator it, conit;
167 std::string etstr=ElementTag();
168 for(it = Begin(); it != End(); ++it) {
169 // identify consecutive block
170 Idx start = *it;
171 Idx anoncount = 0;
172 for(conit=it; conit != End(); ++conit) {
173 if(!Attribute(*conit).IsDefault()) break;
174 if(*conit != start+anoncount) break;
175 ++anoncount;
176 }
177 // write anonymous block
178 if(anoncount > FD_CONSECUTIVE) {
179 Token contag;
180 contag.SetEmpty("Consecutive");
181 contag.InsAttributeInteger("from",start);
182 contag.InsAttributeInteger("to",start+anoncount-1);
183 rTw.Write(contag);
184 it=conit;
185 }
186 // break loop
187 if(it == End()) break;
188 // prepare token
189 Token sttag;
190 Idx index=*it;
191 sttag.SetBegin(etstr);
192 sttag.InsAttributeInteger("id",index);
193 rTw.Write(sttag);
194 // attribute
195 const AttributeVoid& attr=Attribute(index);
196 if(!attr.IsDefault()) attr.XWrite(rTw);
197 // done
198 rTw.WriteEnd(etstr);
199 }
200 rTw.WriteEnd(btag.StringValue());
201}
202
203// debug variant
204void IndexSet::DoDWriteElement(TokenWriter& rTw, const Idx& rElem, const std::string &rLabel, const Type* pContext) const {
205 (void) pContext;
206 (void) rLabel;
207 rTw << rElem;
208}
209
210// DoRead(rTr, rLabel)
211void IndexSet::DoRead(TokenReader& rTr, const std::string& rLabel,const Type* pContext) {
212 // set up defaults
213 std::string label=rLabel;
214 std::string ftype=TypeName();
215 std::string etstr=ElementTag();
216 // figure section from current token
217 Token token;
218 if(label=="") {
219 rTr.Peek(token);
220 if(token.IsBegin()) label=token.StringValue();
221 }
222 if(label=="") label="IndexSet";
223 Name(label);
224 // read begin
225 FD_DC("IndexSet(" << this << ")::DoRead(..): section " << label);
226 rTr.ReadBegin(label,token);
227 if(token.ExistsAttributeString("name"))
228 Name(token.AttributeStringValue("name"));
229 // prepare attribute
230 AttributeVoid* attrp = AttributeType()->New();
231 FD_DC("IndexSet(" << this << ")::DoRead(..): attribute type " << typeid(*attrp).name());
232 // loop tokens
233 while(!rTr.Eos(label)) {
234 rTr.Peek(token);
235 // read by index (faudes format)
236 if(token.IsInteger()) {
237 FD_DC("TaIndexSet(" << this << ")::DoRead(..): inserting element in faudes format \""
238 << token.IntegerValue() << "\"");
239 rTr.Get(token);
240 Idx index=token.IntegerValue();
241 // read attribute
242 attrp->Read(rTr,"",pContext);
243 // skip unknown attributes
245 // insert element with attribute
246 Insert(index);
247 Attribute(index,*attrp);
248 continue;
249 }
250 // read consecutive block of anonymous states (both faudes or XML)
251 if(token.IsBegin("Consecutive")) {
252 Token ctag;
253 rTr.ReadBegin("Consecutive",ctag);
254 // Read up to two tokens for faudes format
255 Token token1,token2;
256 rTr.Peek(token1);
257 if(!token1.IsEnd()) rTr.Get(token1);
258 rTr.Peek(token2);
259 if(!token2.IsEnd()) rTr.Get(token2);
260 rTr.ReadEnd("Consecutive");
261 // Try to interpret
262 int from=-1;
263 int to=-1;
264 if(token1.IsInteger()) from=token1.IntegerValue();
265 if(token2.IsInteger()) to=token2.IntegerValue();
266 if(ctag.ExistsAttributeInteger("from"))
267 from= ctag.AttributeIntegerValue("from");
268 if(ctag.ExistsAttributeInteger("to"))
269 to= ctag.AttributeIntegerValue("to");
270 // Insist in valid range
271 if(from<0 || to <0 || to < from) {
272 delete attrp;
273 std::stringstream errstr;
274 errstr << "Invalid range of consecutive indices" << rTr.FileLine();
275 throw Exception("IndexSet::DoRead", errstr.str(), 50);
276 }
277 FD_DC("IndexSet(" << this << ")::DoRead(..): inserting range "
278 << from << " to " << to);
279 for(Idx index = (Idx) from; index <= (Idx) to; ++index)
280 Insert(index);
281 continue;
282 }
283 // read element section (XML format)
284 if(token.IsBegin(etstr)) {
285 FD_DC("TaIndexSet(" << this << ")::DoRead(..): inserting element in xml format \""
286 << token.StringValue() << "\"");
287 rTr.ReadBegin(etstr,token);
288 Idx index=token.AttributeIntegerValue("id");
289 // read faudes attribute
290 attrp->Read(rTr,"",pContext);
291 // skip unknown faudes attributes
292 rTr.ReadEnd(etstr);
293 // insert element with attribute
294 Insert(index);
295 Attribute(index,*attrp);
296 continue;
297 }
298 // cannot process token
299 delete attrp;
300 std::stringstream errstr;
301 errstr << "Invalid token type " << token.Type() << " at " << rTr.FileLine();
302 throw Exception("IndexSet::DoRead", errstr.str(), 50);
303 }
304 rTr.ReadEnd(label);
305 // dispose attribute
306 delete attrp;
307}
308
309
310// MaxIndex()
312 if(Size()==0) return 0;
313 Idx res = *(--(End()));
314#ifdef FAUDES_CHECKED
315 if(res==FAUDES_IDX_MAX) {
316 std::stringstream errstr;
317 errstr << "IndexSet(): MaxIndex exceeds platform limitations -- reimplement index allocation: \""
318 << Name() << "\"";
319 throw Exception("vGenerator::EventReame(name)", errstr.str(), 61);
320 }
321#endif
322 return res;
323}
324
325// Insert()
327 FD_DC("IndexSet(" << this << ")::Insert()");
328 Idx index=MaxIndex()+1;
330 FD_DC("IndexSet(" << this << ")::Insert(): done");
331 return index;
332}
333
334//Insert(idx)
335bool IndexSet::Insert(const Idx& rIndex) {
336 FD_DC("IndexSet(" << this << ")::Insert(idx)");
337 return TBaseSet<Idx>::Insert(rIndex);
338}
339
340
341//Valid(idx)
342bool IndexSet::Valid(const Idx& rIndex) const {
343 return rIndex!=0;
344}
345
346
347// Signature()
349 // prepare result
350 Idx sig = 0;
351 // helpers:
352 Idx i = 1;
353 Iterator it=Begin() ;
354 Iterator it_end=End() ;
355 // algorithm:
356 for(; it != it_end; ++it) {
357 sig += *it * i;
358 ++i;
359 }
360 return sig;
361}
362
363
364
365
366} // end name space
#define FD_DC(message)
#define FD_DF(message)
#define FAUDES_IDX_MAX
#define FD_CONSECUTIVE
Classes IndexSet, TaIndexSet.
#define FAUDES_TYPE_IMPLEMENTATION(ftype, ctype, cbase)
Definition cfl_types.h:1017
static void Skip(TokenReader &rTr)
virtual bool IsDefault(void) const
Definition cfl_types.h:1148
virtual const std::string & ElementTag(void) const
const std::string & Name(void) const
virtual const std::string & TypeName(void) const
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
bool Valid(const Idx &rIndex) const
void DoCopy(const IndexSet &rSource)
void DoMove(IndexSet &rSource)
Idx MaxIndex(void) const
virtual void DoRead(TokenReader &rTr, const std::string &rLabel="", const Type *pContext=0)
virtual void DoDWriteElement(TokenWriter &rTw, const Idx &rElem, const std::string &rLabel="", const Type *pContext=0) const
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
Int AttributeIntegerValue(const std::string &name)
Int IntegerValue(void) const
bool IsInteger(void) const
bool ExistsAttributeString(const std::string &name)
bool IsBegin(void) const
void SetEmpty(const std::string &rName)
void SetBegin(const std::string &rName)
Definition cfl_token.cpp:92
bool ExistsAttributeInteger(const std::string &name)
void InsAttributeInteger(const std::string &name, Int value)
bool IsEnd(void) const
const std::string & AttributeStringValue(const std::string &name)
TokenType Type(void) const
virtual Token XBeginTag(const std::string &rLabel="", const std::string &rFallbackLabel="") const
void Read(const std::string &rFileName, const std::string &rLabel="", const Type *pContext=0)
virtual void XWrite(const std::string &pFileName, const std::string &rLabel="", const Type *pContext=0) const
virtual Type * New(void) const
Definition cfl_types.cpp:54
void Write(const Type *pContext=0) const
virtual bool Insert(const T &rElem)
virtual const AttributeVoid * AttributeType(void) const
void DoMove(TBaseSet &rSourceSet)
Iterator End(void) const
Iterator Begin(void) const
virtual const AttributeVoid & Attribute(const Idx &rElem) const
void DoCopy(const TBaseSet &rSourceSet)
uint32_t Idx
AutoRegisterType< StateSet > gRtiStateSet("StateSet")

libFAUDES 2.34e --- 2026.03.16 --- c++ api documentaion by doxygen