00001 #ifndef OPTFRAME_NSSEQUNIONADAPTER_HPP_
00002 #define OPTFRAME_NSSEQUNIONADAPTER_HPP_
00003
00004
00005 #include "../Move.hpp"
00006 #include "../NSSeq.hpp"
00007
00008 using namespace std;
00009
00010 template<class R, class M = OPTFRAME_DEFAULT_MEMORY>
00011 class MoveNSSeqUnion: public Move<R, M>
00012 {
00013 protected:
00014 int id;
00015 Move<R, M>& m;
00016
00017 public:
00018
00019 MoveNSSeqUnion(int _id, Move<R, M>& _m) :
00020 id(_id), m(_m)
00021 {
00022 }
00023
00024 int get_id()
00025 {
00026 return id;
00027 }
00028
00029 Move<R, M>& get_m()
00030 {
00031 return m;
00032 }
00033
00034 virtual ~MoveNSSeqUnion()
00035 {
00036 delete &m;
00037 }
00038
00039 bool canBeApplied(const R& r)
00040 {
00041 return m.canBeApplied(r);
00042 }
00043
00044 Move<R, M>& apply(R& r)
00045 {
00046 return *new MoveNSSeqUnion<R, M> (id, m.apply(r));
00047 }
00048
00049 Move<R, M>& apply(M& mem, R& rep)
00050 {
00051 return *new MoveNSSeqUnion<R, M> (id, m.apply(mem, rep));
00052 }
00053
00054 virtual bool operator==(const Move<R, M>& _m) const
00055 {
00056 const MoveNSSeqUnion<R, M>& m1 = (const MoveNSSeqUnion<R, M>&) _m;
00057 if (id == m1.id)
00058 return m == m1.m;
00059 else
00060 return false;
00061 }
00062
00063 void print()
00064 {
00065 cout << "MoveNSSeqUnion: id=" << id << "; move = ";
00066 m.print();
00067 }
00068 };
00069
00070 class NSSeqUnionAdapterOutOfBound
00071 {
00072 public:
00073 NSSeqUnionAdapterOutOfBound()
00074 {
00075 }
00076 };
00077
00078 template<class R, class M = OPTFRAME_DEFAULT_MEMORY, class MOVE = MoveNSSeqUnion<R, M> >
00079 class IteratorNSSeqUnion: public NSIterator<R, M>
00080 {
00081 private:
00082 NSIterator<R, M>& i1;
00083 NSIterator<R, M>& i2;
00084
00085 public:
00086
00087 IteratorNSSeqUnion(NSIterator<R, M>& _i1, NSIterator<R, M>& _i2) :
00088 i1(_i1), i2(_i2)
00089 {
00090 }
00091
00092 virtual ~IteratorNSSeqUnion()
00093 {
00094 delete &i1;
00095 delete &i2;
00096 }
00097
00098 void first()
00099 {
00100 i1.first();
00101 i2.first();
00102 }
00103
00104 void next()
00105 {
00106 if (!i1.isDone())
00107 i1.next();
00108 else
00109 i2.next();
00110 }
00111
00112 bool isDone()
00113 {
00114 return i1.isDone() && i2.isDone();
00115 }
00116
00117 Move<R, M>& current()
00118 {
00119 if (!i1.isDone())
00120 return *new MOVE(0, i1.current());
00121 else if (!i2.isDone())
00122 return *new MOVE(1, i2.current());
00123 else
00124 throw NSSeqUnionAdapterOutOfBound();
00125 }
00126 };
00127
00128 template<class R, class M = OPTFRAME_DEFAULT_MEMORY, class MOVE = MoveNSSeqUnion<R, M> >
00129 class NSSeqUnionAdapter: public NSSeq<R, M>
00130 {
00131 private:
00132 NSSeq<R, M>& n1;
00133 NSSeq<R, M>& n2;
00134
00135 public:
00136
00137 NSSeqUnionAdapter(NSSeq<R, M>& _n1, NSSeq<R, M>& _n2) :
00138 n1(_n1), n2(_n2)
00139 {
00140 }
00141
00142 virtual ~NSSeqUnionAdapter()
00143 {
00144 }
00145
00146 Move<R, M>& move(const R& r)
00147 {
00148 int x = rand() % 2;
00149
00150 if (x == 0)
00151 return *new MOVE(0, n1.move(r));
00152 else
00153 return *new MOVE(1, n2.move(r));
00154 }
00155
00156 virtual NSIterator<R, M>& getIterator(const R& r)
00157 {
00158 return *new IteratorNSSeqUnion<R, M, MOVE> (n1.getIterator(r), n2.getIterator(r));
00159 }
00160
00161 virtual void print()
00162 {
00163 cout << "NSSeqUnionAdapter {" << endl;
00164 n1.print();
00165 n2.print();
00166 cout << "}" << endl;
00167 }
00168 };
00169
00170 #endif