Enumerator.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 /*****************************************************************************\
3  * Computer Algebra System SINGULAR
4 \*****************************************************************************/
5 /** @file Enumerator.h
6  *
7  * Abstract API for enumerators.
8  *
9  * ABSTRACT: Abstract interface for forward iteratable containers (enumerators)
10  * of standalone objects (e.g. polynomials as sets of numbers), without any
11  * knowledge of their internals.
12  *
13  * @author Oleksandr Motsak
14  *
15  *
16  **/
17 /*****************************************************************************/
18 
19 #ifndef ENUMERATOR_H
20 #define ENUMERATOR_H
21 
22 /** @class IBaseEnumerator
23  *
24  * Base enumerator interface for simple iteration over a generic collection.
25  *
26  * Abstract API of enumerators for enumerable collections of standalone objects.
27  * Just like IEnumerator from C#. Usage pattern can be as follows:
28  *
29  * @code
30  * IBaseEnumerator& itr = ...;
31  * itr.Reset(); // goes to the "-1" element
32  * // NOTE: itr is not useable here!
33  * while( itr.MoveNext() )
34  * {
35  * do something custom with itr...
36  * }
37  * @endcode
38  *
39  * Note that the Reset()
40  *
41  * @sa IEnumerator
42  */
43 class IBaseEnumerator // IDisposable
44 {
45  public:
46  /// Advances the enumerator to the next element of the collection.
47  /// returns true if the enumerator was successfully advanced to the
48  /// next element;
49  /// false if the enumerator has passed the end of the collection.
50  virtual bool MoveNext() = 0;
51 
52  /// Sets the enumerator to its initial position: -1,
53  /// which is before the first element in the collection.
54  virtual void Reset() = 0;
55 
56  /// Current position is inside the collection (not -1 or past the end)
57  virtual bool IsValid() const = 0;
58 
59  private:
60  /// disable copy constructor and assignment operator
62  void operator=(const IBaseEnumerator&);
63 
64  protected:
66  ~IBaseEnumerator() {} // TODO: needed?
67 
68 
69 };
70 
71 
72 /** @class IAccessor
73  *
74  * Templated accessor interface for accessing individual data (for instance, of an enumerator).
75  *
76  * T is the type of objects to access, available via the Current() method.
77  *
78  * @sa IBaseEnumerator
79  */
80 template <typename T>
81 class IAccessor // IDisposable
82 {
83  public:
84  typedef T value_type;
85  typedef value_type& reference;
86  typedef const value_type& const_reference;
87 
88  /// Gets the current element in the collection (read and write).
89  virtual reference Current() = 0;
90 
91  /// Gets the current element in the collection (read only).
92  virtual const_reference Current() const = 0;
93 
94  protected:
96  ~IAccessor() {} // TODO: needed?
97 
98 };
99 
100 /** @class IEnumerator
101  *
102  * Templated enumerator interface for simple iteration over a generic collection of T's.
103  *
104  * Abstract API of enumerators for generic enumerable collections of standalone
105  * objects of type T. Inspired by IEnumerator from C#. Usage parrten can be as
106  * follows:
107  *
108  * @code
109  * IEnumerator<T>& itr = ...;
110  *
111  * itr.Reset(); // goes before the first element, thus no itr.Current() is available here!
112  *
113  * while( itr.MoveNext() )
114  * {
115  * use/change itr.Current()...
116  * }
117  * @endcode
118  *
119  * T is the type of objects to enumerate, available via Current() method
120  *
121  * @sa IBaseEnumerator
122  */
123 template <typename T>
124 class IEnumerator: public virtual IBaseEnumerator, public virtual IAccessor<T>
125 {
126  public:
127 };
128 
129 #if 0
130 // the following is not used for now //
131 // the following is not used for now //
132 // the following is not used for now //
133 // the following is not used for now //
134 // the following is not used for now //
135 
136 // include basic definitions
137 //??// #include <iterator>
138 
139 /** @class IBaseIterator
140  *
141  * A base abstract iterator API with virtualized standard iterator operators
142  *
143  * Abstract API for iterators that should work with STL and BOOST.
144  *
145  * @sa STL iterators
146  */
147 template <class A>
148 class IBaseIterator //??// : public std::iterator<std::forward_iterator_tag, A>
149 {
150  public:
151  typedef IBaseIterator<A> self;
152  typedef self& self_reference;
153  typedef const self_reference const_self_reference;
154 
155  virtual bool operator==(const_self_reference rhs) = 0;
156 
157  /// ++itr, goes to the next state, returns the new state
158  virtual self_reference operator++() = 0;
159 
160  /// itr++, goes to the next state, returns the previous state
161  virtual self_reference operator++(int) = 0;
162 
163  virtual A& operator*() = 0;
164  virtual A* operator->() = 0;
165 
166  inline bool operator!=(const_self_reference rhs){ return !((*this) == rhs); }
167 };
168 
169 /** @class AIterator
170  *
171  * An abstract iterator with virtualized assignment operator and
172  * constructors.
173  *
174  * Abstract API for iterators that should work with STL and BOOST.
175  *
176  * @sa STL iterators
177  */
178 template <class A>
179 class IIterator: public IBaseIterator<A>
180 {
181  public:
182  typedef IIterator<A> self;
183  typedef self& self_reference;
184  typedef const self_reference const_self_reference;
185 
186  IIterator(){ void_constructor(); }
187 
188  IIterator(const_self_reference itr) { copy_constructor(itr); }
189 
190  virtual self_reference operator=(const_self_reference other) = 0;
191 
192  private:
193  virtual void void_constructor() = 0;
194  virtual void copy_constructor(const_self_reference itr) = 0;
195 };
196 
197 /** @class IContainer
198  *
199  * Container of standalone objects
200  *
201  * Abstract API for containers of objects and their iterators
202  *
203  * @sa STL containers and iterators
204  */
205 template <class T>
206 class IContainer
207 {
208  public:
209  typedef T value_type;
210  typedef value_type& reference;
211  typedef const value_type& const_reference;
212 //??// typedef std::size_t size_type;
213 //??// virtual size_type size() const = 0;
214  virtual bool empty() const = 0;
215 
216  typedef IIterator<reference> iterator;
217  virtual iterator begin() = 0;
218  virtual iterator end() = 0;
219 
220  typedef IIterator<const_reference> const_iterator;
221  virtual const_iterator begin() const = 0;
222  virtual const_iterator end() const = 0;
223 };
224 #endif
225 
226 
227 #endif
228 /* #ifndef ENUMERATOR_H */
229 
230 // Vi-modeline: vim: filetype=c:syntax:shiftwidth=2:tabstop=8:textwidth=0:expandtab
const value_type & const_reference
Definition: Enumerator.h:86
Templated accessor interface for accessing individual data (for instance, of an enumerator).
Definition: Enumerator.h:81
value_type & reference
Definition: Enumerator.h:85
virtual void Reset()=0
Sets the enumerator to its initial position: -1, which is before the first element in the collection...
fglmVector operator*(const fglmVector &v, const number n)
Definition: fglmvec.cc:427
Templated enumerator interface for simple iteration over a generic collection of T&#39;s.
Definition: Enumerator.h:124
#define A
Definition: sirandom.c:23
virtual bool IsValid() const =0
Current position is inside the collection (not -1 or past the end)
T value_type
Definition: Enumerator.h:84
virtual bool MoveNext()=0
Advances the enumerator to the next element of the collection. returns true if the enumerator was suc...
Base enumerator interface for simple iteration over a generic collection.
Definition: Enumerator.h:43
~IAccessor()
Definition: Enumerator.h:96
bool operator==(const Rational &a, const Rational &b)
Definition: GMPrat.cc:319
static jList * T
Definition: janet.cc:37
void operator=(const IBaseEnumerator &)
bool operator!=(const Rational &a, const Rational &b)
Definition: GMPrat.cc:325