00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef MORE_GEN_ITERATOR_H
00031 #define MORE_GEN_ITERATOR_H
00032
00033 #include <more/bits/config.h>
00034 #include <iosfwd>
00035 #include <iterator>
00036
00037
00038 namespace more {
00039 namespace gen {
00040
00041
00042
00043
00044
00045
00046
00047 struct trivial_iterator_tag {};
00048
00049
00050
00051
00052 template<typename Iterator, typename Function>
00053 struct transforming_iterator
00054 {
00055 typedef typename std::iterator_traits<Iterator>::iterator_category
00056 iterator_category;
00057 typedef typename Function::result_type value_type;
00058 typedef typename std::iterator_traits<Iterator>::difference_type
00059 difference_type;
00060 typedef value_type& reference;
00061 typedef value_type* pointer;
00062
00063 transforming_iterator() {}
00064 transforming_iterator(transforming_iterator const& x) : it(x.it) {}
00065 explicit transforming_iterator(Iterator const& it0) : it(it0) {}
00066
00067 transforming_iterator& operator=(transforming_iterator const& rhs)
00068 { it = rhs.it; return *this; }
00069 bool operator==(transforming_iterator const& rhs) const
00070 { return it==rhs.it; }
00071 bool operator!=(transforming_iterator const& rhs) const
00072 { return !operator==(rhs); }
00073 bool operator<(transforming_iterator const& rhs) const
00074 { return it<rhs.it; }
00075
00076 transforming_iterator& operator++() { ++it; return *this; }
00077 transforming_iterator operator++(int)
00078 { transforming_iterator tmp = *this; ++it; return tmp; }
00079 transforming_iterator& operator--() { --it; return *this; }
00080 transforming_iterator& operator--(int)
00081 { transforming_iterator tmp = *this; --it; return tmp; }
00082
00083 transforming_iterator& operator+=(int i) { it += i; return *this; }
00084 transforming_iterator& operator-=(int i) { it -= i; return *this; }
00085 int operator-(transforming_iterator const& v) const
00086 { return it - v.it; }
00087
00088 value_type& operator*() const { return f(*it); }
00089 value_type& operator[](int i) const { return f(it[i]); }
00090 value_type* operator->() const { return &f(*it); }
00091 Iterator sub_iterator() { return it; }
00092
00093 private:
00094 Iterator it;
00095 Function f;
00096 };
00097
00098 template<typename T, typename U> inline transforming_iterator<T, U>
00099 operator+(transforming_iterator<T, U> u, int i)
00100 {
00101 u += i;
00102 return u;
00103 }
00104
00105 template<typename T, typename U> inline transforming_iterator<T, U>
00106 operator+(int i, transforming_iterator<T, U> u)
00107 {
00108 u += i;
00109 return u;
00110 }
00111
00112 template<typename T, typename U> inline transforming_iterator<T, U>
00113 operator-(transforming_iterator<T, U> u, int i)
00114 {
00115 u -= i;
00116 return u;
00117 }
00118
00119
00120
00121
00122
00123
00124
00125 class null_output_iterator
00126 {
00127 typedef std::ptrdiff_t difference_type;
00128 typedef void value_type;
00129 typedef void reference;
00130 typedef void pointer;
00131 typedef std::output_iterator_tag iterator_category;
00132
00133 struct proxy
00134 {
00135 template<typename T> proxy operator=(const T&x) { return *this; }
00136 };
00137
00138 public:
00139 proxy operator*() { return proxy(); }
00140 null_output_iterator operator++() { return *this; }
00141 null_output_iterator operator++(int) { return *this; }
00142 };
00143
00144
00145
00146
00147 template<typename T>
00148 struct assign_iterator
00149 {
00150 typedef std::ptrdiff_t difference_type;
00151 typedef void value_type;
00152 typedef void reference;
00153 typedef void pointer;
00154 typedef std::output_iterator_tag iterator_category;
00155 explicit assign_iterator(T& ref_) : ref(&ref_) {}
00156 T& operator*() { return *ref; }
00157 assign_iterator& operator++() { return *this; }
00158 assign_iterator& operator++(int) { return *this; }
00159 private:
00160 T* ref;
00161 };
00162
00163 template<typename T>
00164 assign_iterator<T>
00165 assigner(T& x)
00166 {
00167 return assign_iterator<T>(x);
00168 }
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180 template< typename Value,
00181 typename Char = char, typename Traits = std::char_traits<Char> >
00182 struct ostream_iterator
00183 {
00184 typedef std::ptrdiff_t difference_type;
00185 typedef void value_type;
00186 typedef void reference;
00187 typedef void pointer;
00188 typedef std::output_iterator_tag iterator_category;
00189 typedef Char char_type;
00190 typedef Traits traits_type;
00191 typedef std::basic_ostream<Char, Traits> ostream_type;
00192
00193
00194
00195
00196 ostream_iterator(ostream_type& os, char const* sep = "",
00197 int width = 0, int precision = 0)
00198 : m_os(&os), m_sep(sep),
00199 m_width(width), m_precision(precision),
00200 m_is_first(true) {}
00201
00202 ostream_iterator&
00203 operator=(Value const& value)
00204 {
00205 if (m_is_first)
00206 m_is_first = false;
00207 else
00208 *m_os << m_sep;
00209 if (m_width > 0)
00210 m_os->width(m_width);
00211 if (m_precision > 0)
00212 m_os->precision(m_precision);
00213 *m_os << value;
00214 return *this;
00215 }
00216
00217 ostream_iterator& operator*() { return *this; }
00218 ostream_iterator& operator++() { return *this; }
00219 ostream_iterator& operator++(int) { return *this; }
00220
00221 private:
00222 ostream_type* m_os;
00223 char const* m_sep;
00224 int m_width, m_precision;
00225 bool m_is_first;
00226 };
00227
00228 }}
00229
00230 #endif