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