00001
00002 #ifndef __SET_H__
00003 #define __SET_H__
00004
00005 #include <mobject.h>
00006
00007 class AnySet : public Object {
00008 };
00009
00010
00011
00013
00014
00015
00016
00017
00018
00019
00021
00022 template <class ttype>
00023 class Variant : public AnySet {
00024 public:
00025 ttype i;
00026
00027 operator ttype () const {return i;}
00028
00029 };
00030
00031 template<class ttype>
00032 class IteratorInterface {
00033 public:
00034 virtual void start ()=0;
00035 virtual void next ()=0;
00036 virtual bool exhausted () const=0;
00037 virtual bool next_ends () const {MUST_OVERLOAD}
00038 };
00039
00040
00042
00043
00044
00045
00046
00047
00048
00050
00051
00052 template <class ttype>
00053 class Iterator : public Variant<ttype>, public IteratorInterface<ttype> {
00054 ttype m_start, m_end, m_step;
00055 public:
00056
00057 Iterator (ttype s, ttype e=-9999, ttype stp=1) {
00058 m_start = s;
00059 ASSERTWITH (s<=e, "Start of a variant must be less than end");
00060 m_end = (e==-9999)? s:e;
00061 ASSERTWITH (stp>0, "Finite variant violation");
00062 m_step = stp;
00063 start ();
00064 }
00065 void start () {i = m_start;}
00066 void next () {i += m_step;}
00067 bool exhausted () const {return i>m_end;}
00068 bool next_ends () const {return i+m_step>m_end;}
00069 ttype max () const {return m_end;}
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
00104
00106
00107
00108
00109
00110
00111
00112
00114
00115
00116 template <class ttype>
00117 class Invariant : public Variant<ttype> {
00118 public:
00119 Invariant (ttype s) {
00120 i = s;
00121 }
00122 FormatOStream& operator>> (FormatOStream& out) const {
00123 out.name ("i") << i;
00124 }
00125 };
00126
00127
00128
00129 #define forSet(name) for (name.start(); !name.exhausted(); name.next())
00130
00131 #endif
00132