2_sets.cpp File Reference


Detailed Description

Tutorial, container classes.

This tutorial demonstrates how to use the faudes::EventSet and faudes::StateSet containers.

The EventSet class consists of an internal sorted set of unique elements of integer type faudes::Idx. Events are required to have globally unique names. For event name resolution, the EventSet holds a pointer to a (static) SymbolTable object. File IO is via event names as opposed to event indices. EventSets are seen as selfcontained.

The StateSet class consists of an internal sorted set of unique elements of integer type faudes::Idx. StateSets do *NOT* provide state names and file IO is via indices only. (Note: generators provide states names, so you may prefer generator methods for file IO)

#include "libfaudes.h"


using namespace faudes;




int main() {

  // EventSets

  // Create EventSet objects
  
  EventSet alphabet1;
  EventSet alphabet2;
  EventSet alphabet3;

  // set names
  alphabet1.Name("alph1");
  alphabet2.Name("alph2");
  alphabet3.Name("alph2");

  // New events always have to be inserted by calling the Insert method
  // with a symbolic name. This assigns the next free index to the
  // symbolic name in the static EventSymbolTable. 

  Idx ev1 = alphabet1.Insert("a");
  Idx ev2 = alphabet1.Insert("b");
  Idx ev3 = alphabet1.Insert("c");
  Idx ev4 = alphabet1.Insert("d");

  // If the symbolic name of an event is already known in the EventSymbolTable, 
  // Insert returns the existing index. Thus, there is a global one-to-one
  // correspondence between event names and event indices

  Idx ev5 = alphabet2.Insert("c"); // ev3 == ev5
  Idx ev6 = alphabet2.Insert("d"); // ev4 == ev6
  Idx ev7 = alphabet2.Insert("e");
  Idx ev8 = alphabet2.Insert("f");
  Idx ev9 = alphabet2.Insert("g");

  // The event index can be used to refer to existing events. This avoids
  // name lookup.

  alphabet3.Insert(ev1);
  alphabet3.Insert(ev7);
  alphabet3.Insert(ev8);
 
  // report to console

  std::cout << "################################\n";
  std::cout << "# tutorial, alphabets 1,2 and 3 \n";
  alphabet1.DWrite();
  alphabet2.DWrite();
  alphabet3.DWrite();
  std::cout << "################################\n";


  // iterator usage

  EventSet::Iterator eit;
  std::cout << "################################\n";
  std::cout << "# tutorial, iterators \n";
  for (eit = alphabet1.Begin(); eit != alphabet1.End(); eit++) {
    std::cout << alphabet1.SymbolicName(*eit) << ": " << *eit << std::endl;
  }
  std::cout << "################################\n";

  // read an alphabet from generator file

  alphabet3.Read("data/simplemachine.gen", "Alphabet");

  // read an alphabet from file at object construction

  EventSet alphabet4("data/simplemachine.gen", "Alphabet");

  // write a alphabet to file

  alphabet2.Write("tmp_alphabet.txt");

  // report

  std::cout << "################################\n";
  std::cout << "# tutorial, alphabets of s1mple machine  \n";
  alphabet4.DWrite();
  std::cout << "################################\n";


  // set inclusion by overloaded <= operator

  if (alphabet1 <= alphabet2) {
    std::cout << "################################\n";
    std::cout << "alphabet1 includes alphabet2" << std::endl;
    std::cout << "################################\n";
  }
  else {
    std::cout << "################################\n";
    std::cout << "alphabet1 does not include alphabet2" << std::endl;
    std::cout << "################################\n";

  }
  
  // We can do the same with the stl algorithm "includes". This is just an
  // example for using stl algorithms with libfaudes.

  if (std::includes(alphabet1.Begin(), alphabet1.End(), alphabet2.Begin(), alphabet2.End())) {
    std::cout << "################################\n";
    std::cout << "alphabet1 includes alphabet2" << std::endl;
    std::cout << "################################\n";
  }
  else {
    std::cout << "################################\n";
    std::cout << "alphabet1 does not include alphabet2" << std::endl;
    std::cout << "################################\n";
  }

  // delete an event by name

  alphabet2.Erase("e");

  // delete an event by index

  alphabet2.Erase(alphabet2.Index("f"));

  // clear a eventset

  alphabet4.Clear();

  // test existence of event

  if (alphabet2.Exists("d")) {
    std::cout << "alphabet2: event d exists" << std::endl;
  }


  // report

  std::cout << "################################\n";
  std::cout << "# tutorial, updated alphabets 1 and 2  \n";
  alphabet1.DWrite();
  alphabet2.DWrite();
  std::cout << "################################\n";



  // The EventSet class is capable of set operations:

  // set difference

  alphabet4 = alphabet1 - alphabet2;
  std::cout << "################################\n";
  std::cout << "set difference: " << alphabet4.ToString() << std::endl;
  std::cout << "################################\n";

  // set union

  alphabet4 = alphabet1 + alphabet2;
  std::cout << "################################\n";
  std::cout << "set union: " << alphabet4.ToString() << std::endl;
  std::cout << "################################\n";

  // set intersection

  alphabet4 = alphabet1 * alphabet2;
  std::cout << "################################\n";
  std::cout << "set intersection: " << alphabet4.ToString() << std::endl;
  std::cout << "################################\n";


  // StateSets


  std::cout << "################################\n";
  std::cout << "# tutorial, state sets \n";

  // Create a StateSet

  StateSet stateset1;

  // Introduce states

  Idx state1 = stateset1.Insert(47);
  Idx state2 = stateset1.Insert(11);
  Idx state3 = stateset1.Insert();    // becomes 48

  // Introduce more states

  for(int i=0; i<25; i++) stateset1.Insert(); // becomes 49 ... 73
  Idx state4 = stateset1.Insert(100);

  // iterator usage 

  StateSet::Iterator sit;
  std::cout << "stateset1: ";
  for (sit = stateset1.Begin(); sit != stateset1.End(); ++sit) {
    std::cout << stateset1.Str(*sit) << " ";
  }
  std::cout << std::endl;

  // print as string (using file format)

  std::cout << "stateset1: " << stateset1.ToString() << std::endl;
  
  // write a stateset to file (section defaults to "IndexSet")

  stateset1.Write("tmp_stateset.txt");

  // read back from file

  StateSet stateset2;
  stateset2.Read("tmp_stateset.txt","IndexSet");

  // debug output to console

  stateset2.Write();

  // delete a state by index

  stateset2.Erase(state2);

  // copy a StateSet

  StateSet stateset3 = stateset1;

  // clear a StateSet

  stateset1.Clear();

  // test existence of state

  if (stateset3.Exists(state1)) {
    std::cout << "stateset3: state " << state1 << " exists" << std::endl;
  }

  std::cout << "################################\n\n";

  // advanced topic: attributes

       
  std::cout << "################################\n";
  std::cout << "# tutorial, attributes \n";

  // convenience type def for states with flag attribute (see attributes.h)

  typedef TaStateSet<AttributeFlags> FStateSet;

  // construct from a file (with or without attributes)

  FStateSet fstateset1("tmp_stateset.txt");

  // construct from stateset with no attributes

  FStateSet fstateset3=stateset3;

  // report

  std::cout << "fstateset3: " << fstateset3.ToString() << std::endl;
  
  // manipulate attribute by state index (this requires the state to exist)

  fstateset3.Attributep(60)->Set(0xff);

  // insert new state with attribute

  AttributeFlags fattr;
  fattr.Set(0x55);
  Idx fstate = fstateset3.Insert(fattr);

  // report
    
  std::cout << "fstateset3: attribute of state 60: " 
            << fstateset3.Attribute(60).ToString() << std::endl;
  std::cout << "fstateset3: attribute of state " << fstate 
      << ": " << fstateset3.Attribute(fstate).ToString() << std::endl;
  std::cout << "fstateset3: " << fstateset3.ToString() << std::endl;

  // write to file
       
  fstateset3.Write("tmp_fstateset.txt");  

  // convert to set without attributes (drop attributes)

  stateset3 = fstateset3;

  // report

  std::cout << "stateset3: " << stateset3.ToString() << std::endl;

       
  // set comparision ignores attributes

  if(stateset3==fstateset3)
    std::cout << "stateset3 indeed equals fstateset3 " << std::endl;

  std::cout << "################################\n";

  // developper internal: deferred copy stress test

 
  std::cout << "################################\n";
  std::cout << "# tutorial, deferred copy stress test \n";

  // create state set {1,2,...}
  StateSet setA;
  for(Idx state=1; state<45; state++) {
    setA.Insert(state);
  }
  setA.Write();

  // have a deferred copy
  StateSet setB=setA;

  // collect iterators
  std::vector<StateSet::Iterator> edIts;

  // investigate deferred copy setB
  StateSet::Iterator cit=setB.Begin(); 
  for(;cit!=setB.End(); cit++) {
    if(*cit % 5 ==0) {
      edIts.push_back(cit);
    }
  } 
  
  // setB should share with setA and have quite some iterators
  setB.DWrite();

  // trigger detach and lock set B
  setB.Lock();

  // further investigate true copy of setB
  cit=setB.Begin(); 
  for(;cit!=setB.End(); cit++) {
    if(*cit % 2 ==0) {
      edIts.push_back(cit);
    }
  } 
  
  // setB neither share nor track iterators
  setB.DWrite();

  // have other deferred copy
  StateSet setC=setB;

  // write on the deferred copy to trigger actual copy 
  setC.Insert(77);

  // perform edit on deferred copy
  std::vector<StateSet::Iterator>::iterator iit=edIts.begin();
  for(;iit!=edIts.end(); iit++) {
    Idx oldstate = **iit;
    setB.Erase(*iit);
    setB.Insert(100+oldstate); // trigger actual copy
  }
     
  // setB should not share and have quite some iterators
  // report
  setB.Write();
  setB.DWrite();
  std::cout << "################################\n";
     
     
  return 0;
}

Definition in file 2_sets.cpp.

#include "libfaudes.h"

Go to the source code of this file.

Functions

int main ()


Function Documentation

int main  ) 
 

Definition at line 32 of file 2_sets.cpp.


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