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// std faudes type
45FAUDES_TYPE_IMPLEMENTATION(IndexSet,IndexSet,TBaseSet<Idx>)
46
47/*
48FAUDES_TYPE_IMPLEMENTATION_NEW(IndexSet,IndexSet,TBaseSet<Idx>)
49FAUDES_TYPE_IMPLEMENTATION_COPY(IndexSet,IndexSet,TBaseSet<Idx>)
50FAUDES_TYPE_IMPLEMENTATION_CAST(IndexSet,IndexSet,TBaseSet<Idx>)
51FAUDES_TYPE_IMPLEMENTATION_ASSIGN(IndexSet,IndexSet,TBaseSet<Idx>)
52FAUDES_TYPE_IMPLEMENTATION_EQUAL(IndexSet,IndexSet,TBaseSet<Idx>)
53FAUDES_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)
65IndexSet::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)
74 TBaseSet<Idx>(rOtherSet)
75{
76 FD_DC("IndexSet(" << this << ")::IndexSet(rOtherSet " << &rOtherSet << ")");
77 // copy my members (none)
78}
79
80// File constructor
81IndexSet::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)
89void IndexSet::DoAssign(const IndexSet& rSourceSet) {
90 FD_DC("NameSet(" << this << ")::DoAssign(..)");
91 // call base
92 TBaseSet<Idx>::DoAssign(rSourceSet);
93}
94
95// DoWrite(rTw&)
96void 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&)
146void 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// debug variant
202void IndexSet::DoDWriteElement(TokenWriter& rTw, const Idx& rElem, const std::string &rLabel, const Type* pContext) const {
203 (void) pContext;
204 (void) rLabel;
205 rTw << rElem;
206}
207
208// DoRead(rTr, rLabel)
209void IndexSet::DoRead(TokenReader& rTr, const std::string& rLabel,const Type* pContext) {
210 // set up defaults
211 std::string label=rLabel;
212 std::string ftype=TypeName();
213 std::string etstr=ElementTag();
214 // figure section from current token
215 Token token;
216 if(label=="") {
217 rTr.Peek(token);
218 if(token.IsBegin()) label=token.StringValue();
219 }
220 if(label=="") label="IndexSet";
221 Name(label);
222 // read begin
223 FD_DC("IndexSet(" << this << ")::DoRead(..): section " << label);
224 rTr.ReadBegin(label,token);
225 if(token.ExistsAttributeString("name"))
226 Name(token.AttributeStringValue("name"));
227 // prepare attribute
228 AttributeVoid* attrp = AttributeType()->New();
229 FD_DC("IndexSet(" << this << ")::DoRead(..): attribute type " << typeid(*attrp).name());
230 // loop tokens
231 while(!rTr.Eos(label)) {
232 rTr.Peek(token);
233 // read by index (faudes format)
234 if(token.IsInteger()) {
235 FD_DC("TaIndexSet(" << this << ")::DoRead(..): inserting element in faudes format \""
236 << token.IntegerValue() << "\"");
237 rTr.Get(token);
238 Idx index=token.IntegerValue();
239 // read attribute
240 attrp->Read(rTr,"",pContext);
241 // skip unknown attributes
243 // insert element with attribute
244 Insert(index);
245 Attribute(index,*attrp);
246 continue;
247 }
248 // read consecutive block of anonymous states (both faudes or XML)
249 if(token.IsBegin("Consecutive")) {
250 Token ctag;
251 rTr.ReadBegin("Consecutive",ctag);
252 // Read up to two tokens for faudes format
253 Token token1,token2;
254 rTr.Peek(token1);
255 if(!token1.IsEnd()) rTr.Get(token1);
256 rTr.Peek(token2);
257 if(!token2.IsEnd()) rTr.Get(token2);
258 rTr.ReadEnd("Consecutive");
259 // Try to interpret
260 int from=-1;
261 int to=-1;
262 if(token1.IsInteger()) from=token1.IntegerValue();
263 if(token2.IsInteger()) to=token2.IntegerValue();
264 if(ctag.ExistsAttributeInteger("from"))
265 from= ctag.AttributeIntegerValue("from");
266 if(ctag.ExistsAttributeInteger("to"))
267 to= ctag.AttributeIntegerValue("to");
268 // Insist in valid range
269 if(from<0 || to <0 || to < from) {
270 delete attrp;
271 std::stringstream errstr;
272 errstr << "Invalid range of consecutive indices" << rTr.FileLine();
273 throw Exception("IndexSet::DoRead", errstr.str(), 50);
274 }
275 FD_DC("IndexSet(" << this << ")::DoRead(..): inserting range "
276 << from << " to " << to);
277 for(Idx index = (Idx) from; index <= (Idx) to; ++index)
278 Insert(index);
279 continue;
280 }
281 // read element section (XML format)
282 if(token.IsBegin(etstr)) {
283 FD_DC("TaIndexSet(" << this << ")::DoRead(..): inserting element in xml format \""
284 << token.StringValue() << "\"");
285 rTr.ReadBegin(etstr,token);
286 Idx index=token.AttributeIntegerValue("id");
287 // read faudes attribute
288 attrp->Read(rTr,"",pContext);
289 // skip unknown faudes attributes
290 rTr.ReadEnd(etstr);
291 // insert element with attribute
292 Insert(index);
293 Attribute(index,*attrp);
294 continue;
295 }
296 // cannot process token
297 delete attrp;
298 std::stringstream errstr;
299 errstr << "Invalid token type " << token.Type() << " at " << rTr.FileLine();
300 throw Exception("IndexSet::DoRead", errstr.str(), 50);
301 }
302 rTr.ReadEnd(label);
303 // dispose attribute
304 delete attrp;
305}
306
307
308// MaxIndex()
310 if(Size()==0) return 0;
311 Idx res = *(--(End()));
312#ifdef FAUDES_CHECKED
313 if(res==FAUDES_IDX_MAX) {
314 std::stringstream errstr;
315 errstr << "IndexSet(): MaxIndex exceeds platform limitations -- reimplement index allocation: \""
316 << Name() << "\"";
317 throw Exception("vGenerator::EventReame(name)", errstr.str(), 61);
318 }
319#endif
320 return res;
321}
322
323// Insert()
325 FD_DC("IndexSet(" << this << ")::Insert()");
326 Idx index=MaxIndex()+1;
328 FD_DC("IndexSet(" << this << ")::Insert(): done");
329 return index;
330}
331
332//Insert(idx)
333bool IndexSet::Insert(const Idx& rIndex) {
334 FD_DC("IndexSet(" << this << ")::Insert(idx)");
335 return TBaseSet<Idx>::Insert(rIndex);
336}
337
338
339//Valid(idx)
340bool IndexSet::Valid(const Idx& rIndex) const {
341 return rIndex!=0;
342}
343
344
345// Signature()
347 // prepare result
348 Idx sig = 0;
349 // helpers:
350 Idx i = 1;
351 Iterator it=Begin() ;
352 Iterator it_end=End() ;
353 // algorithm:
354 for(; it != it_end; ++it) {
355 sig += *it * i;
356 ++i;
357 }
358 return sig;
359}
360
361
362
363
364} // 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:958
static void Skip(TokenReader &rTr)
virtual bool IsDefault(void) const
Definition cfl_types.h:1075
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
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)
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 DoAssign(const TBaseSet &rSourceSet)
Iterator End(void) const
Iterator Begin(void) const
virtual const AttributeVoid & Attribute(const Idx &rElem) const
uint32_t Idx

libFAUDES 2.33k --- 2025.09.16 --- c++ api documentaion by doxygen