00001 #ifndef __SET_H__
00002 #define __SET_H__
00003
00004 #include <mobject.h>
00005
00006 class AnySet : public Object {
00007 };
00008
00009
00010
00012
00013
00014
00015
00016
00017
00018
00020
00021 template <class ttype>
00022 class Variant : public AnySet {
00023 public:
00024 ttype i;
00025
00026 operator ttype () const {return i;}
00027
00028 };
00029
00030 template<class ttype>
00031 class IteratorInterface {
00032 public:
00033 virtual void start ()=0;
00034 virtual void next ()=0;
00035 virtual bool exhausted () const=0;
00036 virtual bool next_ends () const {MUST_OVERLOAD}
00037 };
00038
00039
00041
00042
00043
00044
00045
00046
00047
00049
00050
00051 template <class ttype>
00052 class Iterator : public Variant<ttype>, public IteratorInterface<ttype> {
00053 ttype m_start, m_end, m_step;
00054 public:
00055
00056 Iterator (ttype s, ttype e=-9999, ttype stp=1) {
00057 m_start = s;
00058 ASSERTWITH (s<=e, "Start of a variant must be less than end");
00059 m_end = (e==-9999)? s:e;
00060 ASSERTWITH (stp>0, "Finite variant violation");
00061 m_step = stp;
00062 start ();
00063 }
00064 void start () {i = m_start;}
00065 void next () {i += m_step;}
00066 bool exhausted () const {return i>m_end;}
00067 bool next_ends () const {return i+m_step>m_end;}
00068 ttype max () const {return m_end;}
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 };
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00105
00106
00107
00108
00109
00110
00111
00113
00114
00115 template <class ttype>
00116 class Invariant : public Variant<ttype> {
00117 public:
00118 Invariant (ttype s) {
00119 i = s;
00120 }
00121 DumpContext& operator>> (DumpContext& out) const {
00122 out.name ("i") << i;
00123 }
00124 };
00125
00126
00127
00128 #define forSet(name) for (name.start(); !name.exhausted(); name.next())
00129
00130 #endif