2_sets.cpp

Go to the documentation of this file.
00001 
00024 #include "libfaudes.h"
00025 
00026 
00027 using namespace faudes;
00028 
00029 
00030 
00031 
00032 int main() {
00033 
00035   // EventSets
00037 
00038   // Create EventSet objects
00039   
00040   EventSet alphabet1;
00041   EventSet alphabet2;
00042   EventSet alphabet3;
00043 
00044   // set names
00045   alphabet1.Name("alph1");
00046   alphabet2.Name("alph2");
00047   alphabet3.Name("alph2");
00048 
00049   // New events always have to be inserted by calling the Insert method
00050   // with a symbolic name. This assigns the next free index to the
00051   // symbolic name in the static EventSymbolTable. 
00052 
00053   Idx ev1 = alphabet1.Insert("a");
00054   Idx ev2 = alphabet1.Insert("b");
00055   Idx ev3 = alphabet1.Insert("c");
00056   Idx ev4 = alphabet1.Insert("d");
00057 
00058   // If the symbolic name of an event is already known in the EventSymbolTable, 
00059   // Insert returns the existing index. Thus, there is a global one-to-one
00060   // correspondence between event names and event indices
00061 
00062   Idx ev5 = alphabet2.Insert("c"); // ev3 == ev5
00063   Idx ev6 = alphabet2.Insert("d"); // ev4 == ev6
00064   Idx ev7 = alphabet2.Insert("e");
00065   Idx ev8 = alphabet2.Insert("f");
00066   Idx ev9 = alphabet2.Insert("g");
00067 
00068   // The event index can be used to refer to existing events. This avoids
00069   // name lookup.
00070 
00071   alphabet3.Insert(ev1);
00072   alphabet3.Insert(ev7);
00073   alphabet3.Insert(ev8);
00074  
00075   // report to console
00076 
00077   std::cout << "################################\n";
00078   std::cout << "# tutorial, alphabets 1,2 and 3 \n";
00079   alphabet1.DWrite();
00080   alphabet2.DWrite();
00081   alphabet3.DWrite();
00082   std::cout << "################################\n";
00083 
00084 
00085   // iterator usage
00086 
00087   EventSet::Iterator eit;
00088   std::cout << "################################\n";
00089   std::cout << "# tutorial, iterators \n";
00090   for (eit = alphabet1.Begin(); eit != alphabet1.End(); eit++) {
00091     std::cout << alphabet1.SymbolicName(*eit) << ": " << *eit << std::endl;
00092   }
00093   std::cout << "################################\n";
00094 
00095   // read an alphabet from generator file
00096 
00097   alphabet3.Read("data/simplemachine.gen", "Alphabet");
00098 
00099   // read an alphabet from file at object construction
00100 
00101   EventSet alphabet4("data/simplemachine.gen", "Alphabet");
00102 
00103   // write a alphabet to file
00104 
00105   alphabet2.Write("tmp_alphabet.txt");
00106 
00107   // report
00108 
00109   std::cout << "################################\n";
00110   std::cout << "# tutorial, alphabets of s1mple machine  \n";
00111   alphabet4.DWrite();
00112   std::cout << "################################\n";
00113 
00114 
00115   // set inclusion by overloaded <= operator
00116 
00117   if (alphabet1 <= alphabet2) {
00118     std::cout << "################################\n";
00119     std::cout << "alphabet1 includes alphabet2" << std::endl;
00120     std::cout << "################################\n";
00121   }
00122   else {
00123     std::cout << "################################\n";
00124     std::cout << "alphabet1 does not include alphabet2" << std::endl;
00125     std::cout << "################################\n";
00126 
00127   }
00128   
00129   // We can do the same with the stl algorithm "includes". This is just an
00130   // example for using stl algorithms with libfaudes.
00131 
00132   if (std::includes(alphabet1.Begin(), alphabet1.End(), alphabet2.Begin(), alphabet2.End())) {
00133     std::cout << "################################\n";
00134     std::cout << "alphabet1 includes alphabet2" << std::endl;
00135     std::cout << "################################\n";
00136   }
00137   else {
00138     std::cout << "################################\n";
00139     std::cout << "alphabet1 does not include alphabet2" << std::endl;
00140     std::cout << "################################\n";
00141   }
00142 
00143   // delete an event by name
00144 
00145   alphabet2.Erase("e");
00146 
00147   // delete an event by index
00148 
00149   alphabet2.Erase(alphabet2.Index("f"));
00150 
00151   // clear a eventset
00152 
00153   alphabet4.Clear();
00154 
00155   // test existence of event
00156 
00157   if (alphabet2.Exists("d")) {
00158     std::cout << "alphabet2: event d exists" << std::endl;
00159   }
00160 
00161 
00162   // report
00163 
00164   std::cout << "################################\n";
00165   std::cout << "# tutorial, updated alphabets 1 and 2  \n";
00166   alphabet1.DWrite();
00167   alphabet2.DWrite();
00168   std::cout << "################################\n";
00169 
00170 
00171 
00172   // The EventSet class is capable of set operations:
00173 
00174   // set difference
00175 
00176   alphabet4 = alphabet1 - alphabet2;
00177   std::cout << "################################\n";
00178   std::cout << "set difference: " << alphabet4.ToString() << std::endl;
00179   std::cout << "################################\n";
00180 
00181   // set union
00182 
00183   alphabet4 = alphabet1 + alphabet2;
00184   std::cout << "################################\n";
00185   std::cout << "set union: " << alphabet4.ToString() << std::endl;
00186   std::cout << "################################\n";
00187 
00188   // set intersection
00189 
00190   alphabet4 = alphabet1 * alphabet2;
00191   std::cout << "################################\n";
00192   std::cout << "set intersection: " << alphabet4.ToString() << std::endl;
00193   std::cout << "################################\n";
00194 
00195 
00197   // StateSets
00199 
00200 
00201   std::cout << "################################\n";
00202   std::cout << "# tutorial, state sets \n";
00203 
00204   // Create a StateSet
00205 
00206   StateSet stateset1;
00207 
00208   // Introduce states
00209 
00210   Idx state1 = stateset1.Insert(47);
00211   Idx state2 = stateset1.Insert(11);
00212   Idx state3 = stateset1.Insert();    // becomes 48
00213 
00214   // Introduce more states
00215 
00216   for(int i=0; i<25; i++) stateset1.Insert(); // becomes 49 ... 73
00217   Idx state4 = stateset1.Insert(100);
00218 
00219   // iterator usage 
00220 
00221   StateSet::Iterator sit;
00222   std::cout << "stateset1: ";
00223   for (sit = stateset1.Begin(); sit != stateset1.End(); ++sit) {
00224     std::cout << stateset1.Str(*sit) << " ";
00225   }
00226   std::cout << std::endl;
00227 
00228   // print as string (using file format)
00229 
00230   std::cout << "stateset1: " << stateset1.ToString() << std::endl;
00231   
00232   // write a stateset to file (section defaults to "IndexSet")
00233 
00234   stateset1.Write("tmp_stateset.txt");
00235 
00236   // read back from file
00237 
00238   StateSet stateset2;
00239   stateset2.Read("tmp_stateset.txt","IndexSet");
00240 
00241   // debug output to console
00242 
00243   stateset2.Write();
00244 
00245   // delete a state by index
00246 
00247   stateset2.Erase(state2);
00248 
00249   // copy a StateSet
00250 
00251   StateSet stateset3 = stateset1;
00252 
00253   // clear a StateSet
00254 
00255   stateset1.Clear();
00256 
00257   // test existence of state
00258 
00259   if (stateset3.Exists(state1)) {
00260     std::cout << "stateset3: state " << state1 << " exists" << std::endl;
00261   }
00262 
00263   std::cout << "################################\n\n";
00264 
00266   // advanced topic: attributes
00268 
00269        
00270   std::cout << "################################\n";
00271   std::cout << "# tutorial, attributes \n";
00272 
00273   // convenience type def for states with flag attribute (see attributes.h)
00274 
00275   typedef TaStateSet<AttributeFlags> FStateSet;
00276 
00277   // construct from a file (with or without attributes)
00278 
00279   FStateSet fstateset1("tmp_stateset.txt");
00280 
00281   // construct from stateset with no attributes
00282 
00283   FStateSet fstateset3=stateset3;
00284 
00285   // report
00286 
00287   std::cout << "fstateset3: " << fstateset3.ToString() << std::endl;
00288   
00289   // manipulate attribute by state index (this requires the state to exist)
00290 
00291   fstateset3.Attributep(60)->Set(0xff);
00292 
00293   // insert new state with attribute
00294 
00295   AttributeFlags fattr;
00296   fattr.Set(0x55);
00297   Idx fstate = fstateset3.Insert(fattr);
00298 
00299   // report
00300     
00301   std::cout << "fstateset3: attribute of state 60: " 
00302             << fstateset3.Attribute(60).ToString() << std::endl;
00303   std::cout << "fstateset3: attribute of state " << fstate 
00304       << ": " << fstateset3.Attribute(fstate).ToString() << std::endl;
00305   std::cout << "fstateset3: " << fstateset3.ToString() << std::endl;
00306 
00307   // write to file
00308        
00309   fstateset3.Write("tmp_fstateset.txt");  
00310 
00311   // convert to set without attributes (drop attributes)
00312 
00313   stateset3 = fstateset3;
00314 
00315   // report
00316 
00317   std::cout << "stateset3: " << stateset3.ToString() << std::endl;
00318 
00319        
00320   // set comparision ignores attributes
00321 
00322   if(stateset3==fstateset3)
00323     std::cout << "stateset3 indeed equals fstateset3 " << std::endl;
00324 
00325   std::cout << "################################\n";
00326 
00328   // developper internal: deferred copy stress test
00330 
00331  
00332   std::cout << "################################\n";
00333   std::cout << "# tutorial, deferred copy stress test \n";
00334 
00335   // create state set {1,2,...}
00336   StateSet setA;
00337   for(Idx state=1; state<45; state++) {
00338     setA.Insert(state);
00339   }
00340   setA.Write();
00341 
00342   // have a deferred copy
00343   StateSet setB=setA;
00344 
00345   // collect iterators
00346   std::vector<StateSet::Iterator> edIts;
00347 
00348   // investigate deferred copy setB
00349   StateSet::Iterator cit=setB.Begin(); 
00350   for(;cit!=setB.End(); cit++) {
00351     if(*cit % 5 ==0) {
00352       edIts.push_back(cit);
00353     }
00354   } 
00355   
00356   // setB should share with setA and have quite some iterators
00357   setB.DWrite();
00358 
00359   // trigger detach and lock set B
00360   setB.Lock();
00361 
00362   // further investigate true copy of setB
00363   cit=setB.Begin(); 
00364   for(;cit!=setB.End(); cit++) {
00365     if(*cit % 2 ==0) {
00366       edIts.push_back(cit);
00367     }
00368   } 
00369   
00370   // setB neither share nor track iterators
00371   setB.DWrite();
00372 
00373   // have other deferred copy
00374   StateSet setC=setB;
00375 
00376   // write on the deferred copy to trigger actual copy 
00377   setC.Insert(77);
00378 
00379   // perform edit on deferred copy
00380   std::vector<StateSet::Iterator>::iterator iit=edIts.begin();
00381   for(;iit!=edIts.end(); iit++) {
00382     Idx oldstate = **iit;
00383     setB.Erase(*iit);
00384     setB.Insert(100+oldstate); // trigger actual copy
00385   }
00386      
00387   // setB should not share and have quite some iterators
00388   // report
00389   setB.Write();
00390   setB.DWrite();
00391   std::cout << "################################\n";
00392      
00393      
00394   return 0;
00395 }

Generated on Fri May 9 11:26:47 2008 for libFAUDES 2.09b by  doxygen 1.4.4