| 1 | 
tim | 
1232 | 
#ifndef _CONSTRAINT_ITERATOR_H_
 | 
| 2 | 
  | 
  | 
#define _CONSTRAINT_ITERATOR_H_ | 
| 3 | 
  | 
  | 
#include <list> | 
| 4 | 
  | 
  | 
#include <vector> | 
| 5 | 
  | 
  | 
 | 
| 6 | 
  | 
  | 
using namespace std;
 | 
| 7 | 
  | 
  | 
//abstract iterator class
 | 
| 8 | 
  | 
  | 
template<class Item> class IteratorBase{ | 
| 9 | 
  | 
  | 
  public: | 
| 10 | 
  | 
  | 
    virtual ~IteratorBase(){} | 
| 11 | 
  | 
  | 
    virtual void first() = 0; | 
| 12 | 
  | 
  | 
    virtual void next() = 0; | 
| 13 | 
  | 
  | 
    virtual bool isEnd() = 0;
 | 
| 14 | 
  | 
  | 
 | 
| 15 | 
  | 
  | 
    virtual Item* currentItem() =0;
 | 
| 16 | 
  | 
  | 
 | 
| 17 | 
  | 
  | 
  protected: | 
| 18 | 
  | 
  | 
    IteratorBase() {}; | 
| 19 | 
  | 
  | 
}; | 
| 20 | 
  | 
  | 
 | 
| 21 | 
  | 
  | 
template <class Item> class ConstraintIterator{ | 
| 22 | 
  | 
  | 
  public: | 
| 23 | 
  | 
  | 
    ConstraintIterator(list<vector<Item*> >& aContainer) : container(aContainer){} | 
| 24 | 
  | 
  | 
     | 
| 25 | 
  | 
  | 
    virtual void first(){
 | 
| 26 | 
  | 
  | 
      listIter = container.begin();
 | 
| 27 | 
  | 
  | 
      //the construction of the container will guarantee every vector in list will contain at least one element
 | 
| 28 | 
  | 
  | 
      //so we don't need to worry about the empty vector problem
 | 
| 29 | 
  | 
  | 
      //while (listIter->empty())
 | 
| 30 | 
  | 
  | 
      //  ++listIter;
 | 
| 31 | 
  | 
  | 
 | 
| 32 | 
  | 
  | 
      vectorIter = listIter->begin();
 | 
| 33 | 
  | 
  | 
    }
 | 
| 34 | 
  | 
  | 
    
 | 
| 35 | 
  | 
  | 
    virtual void next(){
 | 
| 36 | 
  | 
  | 
      ++vectorIter;
 | 
| 37 | 
  | 
  | 
      if (vectorIter == listIter->end()){
 | 
| 38 | 
  | 
  | 
 | 
| 39 | 
  | 
  | 
        //do{
 | 
| 40 | 
  | 
  | 
        //++listIter;
 | 
| 41 | 
  | 
  | 
        //}while(listIter->empty())
 | 
| 42 | 
  | 
  | 
        ++listIter;
 | 
| 43 | 
  | 
  | 
        if (listIter != container.end())
 | 
| 44 | 
  | 
  | 
          vectorIter = listIter->begin();
 | 
| 45 | 
  | 
  | 
      }
 | 
| 46 | 
  | 
  | 
    }
 | 
| 47 | 
  | 
  | 
    
 | 
| 48 | 
  | 
  | 
    virtual bool isEnd(){
 | 
| 49 | 
  | 
  | 
      return listIter == container.end();
 | 
| 50 | 
  | 
  | 
    }
 | 
| 51 | 
  | 
  | 
 | 
| 52 | 
  | 
  | 
    Item* currentItem(){
 | 
| 53 | 
  | 
  | 
      return listIter ==container.end() ? NULL : *vectorIter;
 | 
| 54 | 
  | 
  | 
    }
 | 
| 55 | 
  | 
  | 
 | 
| 56 | 
  | 
  | 
  private: | 
| 57 | 
  | 
  | 
    list<vector<Item*> >& container;
 | 
| 58 | 
  | 
  | 
 | 
| 59 | 
  | 
  | 
    list<vector<Item*> >::iterator listIter;
 | 
| 60 | 
  | 
  | 
    vector<Item*>::iterator vectorIter;
 | 
| 61 | 
  | 
  | 
 | 
| 62 | 
  | 
  | 
}; | 
| 63 | 
  | 
  | 
 | 
| 64 | 
  | 
  | 
typedef ConstraintIterator<ConstraintPair> ConstraintPairIterator; | 
| 65 | 
  | 
  | 
typedef ConstraintIterator<ConstraintElement> ConstraintElementIterator; | 
| 66 | 
  | 
  | 
 | 
| 67 | 
  | 
  | 
 | 
| 68 | 
  | 
  | 
#endif //end ifndef _CONSTRAINT_ITERATOR_H_ |