| |
libFAUDES
Sections
Index
|
2_containers.cppGo to the documentation of this file.00001 /** @file 2_containers.cpp 00002 00003 Tutorial, container classes. This tutorial demonstrates 00004 how to use the faudes::EventSet and faudes::StateSet containers. 00005 00006 The EventSet class consists of an internal sorted set of unique elements 00007 of integer type faudes::Idx. Events are required to have 00008 globally unique names. For event name resolution, the EventSet holds a 00009 pointer to a (static) SymbolTable object. File IO is via event names 00010 as opposed to event indices. EventSets are seen as selfcontained. 00011 00012 The StateSet class consists of an internal sorted set of unique elements 00013 of integer type faudes::Idx. StateSets do *NOT* provide state names 00014 and file IO is via indices only. (Note: generators provide states names, 00015 so you may prefer generator methods for file IO) 00016 00017 @ingroup Tutorials 00018 00019 @include 2_containers.cpp 00020 */ 00021 00022 00023 00024 #include "libfaudes.h" 00025 00026 00027 using namespace faudes; 00028 00029 00030 00031 00032 int main() { 00033 00034 //////////////////////////////////////////////////// 00035 // EventSets 00036 //////////////////////////////////////////////////// 00037 00038 // Create EventSet objects 00039 EventSet alphabet1; 00040 EventSet alphabet2; 00041 EventSet alphabet3; 00042 00043 // Set names 00044 alphabet1.Name("A1"); 00045 alphabet2.Name("A2"); 00046 alphabet3.Name("A3"); 00047 00048 // New events can be inserted by calling the Insert method 00049 // with a symbolic name. If the symbolic name is not kown, it is assigned 00050 // the next free index in the static EventSymbolTable. It any case, 00051 // Insert returns the index inserted to the set. 00052 Idx ev1 = alphabet1.Insert("a"); 00053 Idx ev2 = alphabet1.Insert("b"); 00054 Idx ev3 = alphabet1.Insert("c"); 00055 Idx ev4 = alphabet1.Insert("d"); 00056 Idx ev5 = alphabet2.Insert("c"); // ev3 == ev5 00057 Idx ev6 = alphabet2.Insert("d"); // ev4 == ev6 00058 Idx ev7 = alphabet2.Insert("e"); 00059 Idx ev8 = alphabet2.Insert("f"); 00060 Idx ev9 = alphabet2.Insert("g"); 00061 00062 // The event index can be used to refer to existing events. This avoids 00063 // name lookup. 00064 alphabet3.Insert(ev1); // "a" 00065 alphabet3.Insert(ev7); // "e" 00066 alphabet3.Insert(ev8); // "f" 00067 00068 // Report to console 00069 std::cout << "################################\n"; 00070 std::cout << "# tutorial, alphabets A1,A2 and A3 \n"; 00071 alphabet1.DWrite(); 00072 alphabet2.DWrite(); 00073 alphabet3.DWrite(); 00074 std::cout << "################################\n"; 00075 00076 00077 // Iterator usage 00078 EventSet::Iterator eit; 00079 std::cout << "################################\n"; 00080 std::cout << "# tutorial, iterators \n"; 00081 for (eit = alphabet1.Begin(); eit != alphabet1.End(); eit++) { 00082 std::cout << alphabet1.SymbolicName(*eit) << ": " << *eit << std::endl; 00083 } 00084 std::cout << "################################\n"; 00085 00086 00087 // Read an alphabet from generator file 00088 alphabet3.Read("data/simplemachine.gen", "Alphabet"); 00089 00090 // Read an alphabet from file at object construction 00091 EventSet alphabet4("data/simplemachine.gen", "Alphabet"); 00092 00093 // Write a alphabet to file 00094 alphabet2.Write("tmp_alphabet.txt"); 00095 00096 // Report 00097 std::cout << "################################\n"; 00098 std::cout << "# tutorial, alphabets of simple machine \n"; 00099 alphabet4.DWrite(); 00100 std::cout << "################################\n"; 00101 00102 00103 // Set inclusion by overloaded <= operator 00104 if(alphabet1 <= alphabet2) { 00105 std::cout << "################################\n"; 00106 std::cout << "alphabet1 includes alphabet2" << std::endl; 00107 std::cout << "################################\n"; 00108 } 00109 else { 00110 std::cout << "################################\n"; 00111 std::cout << "alphabet1 does not include alphabet2" << std::endl; 00112 std::cout << "################################\n"; 00113 } 00114 00115 // Delete an event by name 00116 alphabet2.Erase("e"); 00117 00118 // Delete an event by index 00119 alphabet2.Erase(alphabet2.Index("f")); 00120 00121 // Clear an eventset 00122 alphabet4.Clear(); 00123 00124 // Test existence of event 00125 if (alphabet2.Exists("d")) { 00126 std::cout << "alphabet2: event d exists" << std::endl; 00127 } 00128 00129 00130 // Report 00131 std::cout << "################################\n"; 00132 std::cout << "# tutorial, updated alphabets 1 and 2 \n"; 00133 alphabet1.DWrite(); 00134 alphabet2.DWrite(); 00135 std::cout << "################################\n"; 00136 00137 00138 // Set difference 00139 EventSet adifference = alphabet1 - alphabet2; 00140 std::cout << "################################\n"; 00141 std::cout << "set difference: " << adifference.ToString() << std::endl; 00142 std::cout << "################################\n"; 00143 00144 00145 // Set union 00146 EventSet aunion = alphabet1 + alphabet2; 00147 std::cout << "################################\n"; 00148 std::cout << "set union: " << aunion.ToString() << std::endl; 00149 std::cout << "################################\n"; 00150 00151 // Set intersection 00152 EventSet aintersection = alphabet1 * alphabet2; 00153 std::cout << "################################\n"; 00154 std::cout << "set intersection: " << aintersection.ToString() << std::endl; 00155 std::cout << "################################\n"; 00156 00157 // Test protocol 00158 FAUDES_TEST_DUMP("set difference",adifference); 00159 FAUDES_TEST_DUMP("set union",aunion); 00160 FAUDES_TEST_DUMP("set intersection",aintersection); 00161 00162 //////////////////////////////////////////////////// 00163 // StateSets 00164 //////////////////////////////////////////////////// 00165 00166 00167 std::cout << "################################\n"; 00168 std::cout << "# tutorial, state sets \n"; 00169 00170 // Create a StateSet 00171 StateSet stateset1; 00172 00173 // Introduce states 00174 Idx state1 = stateset1.Insert(47); 00175 Idx state2 = stateset1.Insert(11); 00176 Idx state3 = stateset1.Insert(); // becomes 48 00177 00178 // Introduce more states 00179 for(int i=0; i<25; i++) stateset1.Insert(); // becomes 49 ... 73 00180 Idx state4 = stateset1.Insert(100); 00181 00182 // Iterator usage 00183 StateSet::Iterator sit; 00184 std::cout << "stateset1: "; 00185 for (sit = stateset1.Begin(); sit != stateset1.End(); ++sit) { 00186 std::cout << stateset1.Str(*sit) << " "; 00187 } 00188 std::cout << std::endl; 00189 00190 // Print as string (using file format) 00191 std::cout << "stateset1: " << stateset1.ToString() << std::endl; 00192 00193 // Write a stateset to file (section defaults to "IndexSet") 00194 stateset1.Write("tmp_stateset.txt"); 00195 00196 // Read back from file (section defaults to "current begin token") 00197 StateSet stateset2; 00198 stateset2.Read("tmp_stateset.txt"); 00199 00200 // Debug output to console 00201 stateset2.Write(); 00202 00203 // Delete a state by index 00204 stateset2.Erase(state2); 00205 00206 // Copy a StateSet 00207 StateSet stateset3 = stateset1; 00208 00209 // Clear a StateSet 00210 stateset1.Clear(); 00211 00212 // Test existence of state 00213 if (stateset3.Exists(state1)) { 00214 std::cout << "stateset3: state " << state1 << " exists" << std::endl; 00215 } 00216 00217 std::cout << "################################\n\n"; 00218 00219 00220 //////////////////////////////////////////////////// 00221 // advanced topic: attributed sets 00222 //////////////////////////////////////////////////// 00223 00224 00225 std::cout << "################################\n"; 00226 std::cout << "# tutorial, attributes \n"; 00227 00228 // Convenience type definition for states with flag attribute (see attributes.h) 00229 typedef TaStateSet<AttributeFlags> FStateSet; 00230 00231 // Construct from a file (read attributes if specified) 00232 FStateSet fstateset1("tmp_stateset.txt"); 00233 00234 // Construct from stateset with no attributes 00235 FStateSet fstateset3(stateset3); 00236 00237 // Report 00238 std::cout << "fstateset3: " << fstateset3.ToString() << std::endl; 00239 00240 // Manipulate attribute by state index (this requires the state to exist) 00241 fstateset3.Attributep(60)->Set(0xff); 00242 00243 // Insert new state with attribute 00244 AttributeFlags fattr; 00245 fattr.Set(0x55); 00246 Idx fstate = fstateset3.Insert(fattr); 00247 00248 // Report 00249 std::cout << "fstateset3: attribute of state 60: " 00250 << fstateset3.Attribute(60).ToString() << std::endl; 00251 std::cout << "fstateset3: attribute of state " << fstate 00252 << ": " << fstateset3.Attribute(fstate).ToString() << std::endl; 00253 std::cout << "fstateset3: " << fstateset3.ToString() << std::endl; 00254 00255 // Write to file 00256 fstateset3.Write("tmp_fstateset.txt"); 00257 00258 // Convert to set without attributes (drop attributes) 00259 stateset3 = fstateset3; 00260 00261 // Report 00262 std::cout << "stateset3: " << stateset3.ToString() << std::endl; 00263 00264 // Set comparision ignores attributes 00265 if(stateset3==fstateset3) 00266 std::cout << "stateset3 indeed equals fstateset3 " << std::endl; 00267 00268 // Explicit equality test shows 00269 if(!stateset3.EqualAttributes(fstateset3)) 00270 std::cout << "stateset3 indeed has different attributes as fstateset3 " << std::endl; 00271 00272 // Provided that actual types match, attributes are copied even when accesssing 00273 // via non attributed StateSet methods 00274 FStateSet fstateset4; 00275 StateSet& rfstateset3 = fstateset3; 00276 StateSet& rfstateset4 = fstateset4; 00277 rfstateset4 = rfstateset3; 00278 00279 // Remove a state with no attribute (provoce deep copy) 00280 rfstateset3.Erase(50); 00281 00282 // Test attribute equality 00283 if(fstateset4.EqualAttributes(fstateset3)) 00284 std::cout << "fstateset4 indeed has equal attributes with fstateset3 " << std::endl; 00285 if(rfstateset4.EqualAttributes(rfstateset3)) 00286 std::cout << "rfstateset4 indeed has equal attributes with rfstateset3 " << std::endl; 00287 00288 // Report 00289 std::cout << "fstateset4: " << fstateset4.ToString() << std::endl; 00290 std::cout << "################################\n\n"; 00291 00292 // Test protocol 00293 FAUDES_TEST_DUMP("attrbibutes eq1", stateset3.EqualAttributes(fstateset3)); 00294 FAUDES_TEST_DUMP("attrbibutes eq2", fstateset4.EqualAttributes(fstateset3)); 00295 FAUDES_TEST_DUMP("attrbibutes eq3", rfstateset4.EqualAttributes(rfstateset3)); 00296 00297 00298 //////////////////////////////////////////////////// 00299 // Vectors 00300 //////////////////////////////////////////////////// 00301 00302 std::cout << "################################\n"; 00303 std::cout << "# tutorial, vectors \n"; 00304 00305 // Have a vector of event sets 00306 EventSetVector alphvect; 00307 00308 // Populate the vector (take copies) 00309 alphvect.PushBack(alphabet1); 00310 alphvect.PushBack(alphabet2); 00311 alphvect.PushBack(alphabet3); 00312 00313 // Access entries 00314 std::cout << "# event set no 1 (counting from 0):\n"; 00315 alphvect.At(1).Write(); 00316 00317 // Manipulate entries 00318 alphvect.At(1).Insert("delta_1"); 00319 00320 // Report 00321 std::cout << "# all three event sets:\n"; 00322 alphvect.Write(); 00323 00324 // Set filenames for convenient token io 00325 alphvect.FilenameAt(0,"tmp_alph0.txt"); 00326 alphvect.FilenameAt(1,"tmp_alph1.txt"); 00327 alphvect.FilenameAt(2,"tmp_alph2.txt"); 00328 alphvect.Write("tmp_alphvect.txt"); 00329 00330 00331 // Done 00332 std::cout << "################################\n"; 00333 00334 //////////////////////////////////////////////////// 00335 // Developper internal: deferred copy stress test 00336 //////////////////////////////////////////////////// 00337 00338 00339 std::cout << "################################\n"; 00340 std::cout << "# tutorial, deferred copy stress test \n"; 00341 00342 // Create state set {1,2,...} 00343 StateSet setA; 00344 for(Idx state=1; state<45; state++) { 00345 setA.Insert(state); 00346 } 00347 setA.Write(); 00348 00349 // Have a deferred copy 00350 StateSet setB=setA; 00351 00352 // Collect iterators 00353 std::vector<StateSet::Iterator> edIts; 00354 00355 // Investigate deferred copy setB 00356 StateSet::Iterator cit=setB.Begin(); 00357 for(;cit!=setB.End(); cit++) { 00358 if(*cit % 5 ==0) { 00359 edIts.push_back(cit); 00360 } 00361 } 00362 00363 // setB should share with setA and have quite some iterators 00364 setB.DWrite(); 00365 00366 // Trigger detach and lock set B 00367 setB.Lock(); 00368 00369 // Further investigate true copy of setB 00370 cit=setB.Begin(); 00371 for(;cit!=setB.End(); cit++) { 00372 if(*cit % 2 ==0) { 00373 edIts.push_back(cit); 00374 } 00375 } 00376 00377 // setB neither shares nor track iterators 00378 setB.DWrite(); 00379 00380 // Have other deferred copy 00381 StateSet setC=setB; 00382 00383 // Write on the deferred copy to trigger actual copy 00384 setC.Insert(77); 00385 00386 // Perform edit on deferred copy 00387 std::vector<StateSet::Iterator>::iterator iit=edIts.begin(); 00388 for(;iit!=edIts.end(); iit++) { 00389 Idx oldstate = **iit; 00390 setB.Erase(*iit); 00391 setB.Insert(100+oldstate); 00392 } 00393 00394 // setB should not share and still dont track any iterators 00395 setB.Write(); 00396 setB.DWrite(); 00397 std::cout << "################################\n"; 00398 00399 00400 return 0; 00401 } |
libFAUDES 2.14g --- 2009-12-3 --- c++ source docu by doxygen 1.5.6