CMS 3D CMS Logo

poly.h
Go to the documentation of this file.
1 #ifndef PolyType_h
2 #define PolyType_h
3 
4 #include <boost/iterator/iterator_facade.hpp>
5 #include <boost/operators.hpp>
6 #include <iostream>
7 #include <list>
8 #include <set>
9 
10 template <class T>
11 class poly;
12 template <class T>
13 poly<T> operator+(const poly<T>&, const char*);
14 template <class T>
15 poly<T> operator+(const char*, const poly<T>&);
16 
17 template <class T>
18 class poly
19  : boost::incrementable<
20  poly<T>,
21  boost::addable<
22  poly<T>,
23  boost::multipliable<poly<T>, boost::multipliable2<poly<T>, T, boost::less_than_comparable<poly<T> > > > > > {
24  std::list<std::set<T> > columns;
25 
26 public:
28  typedef T value_type;
29  typedef typename std::list<std::set<T> >::iterator column_iterator;
30  typedef typename std::list<std::set<T> >::const_iterator const_column_iterator;
31  poly() {}
32  poly(const T& t) { operator+=(t); }
33 
34  bool operator<(const poly& R) const {
35  const_column_iterator column(columns.begin()), Rcolumn(R.columns.begin());
36  while (column != columns.end() && Rcolumn != R.columns.end() && *column == *Rcolumn) {
37  ++column;
38  ++Rcolumn;
39  }
40  return column != columns.end() && Rcolumn != R.columns.end() && *column < *Rcolumn;
41  }
43  columns.push_back(std::set<T>());
44  return *this;
45  }
46  poly operator+=(const poly& R) {
47  columns.insert(columns.end(), R.columns.begin(), R.columns.end());
48  return *this;
49  }
50  poly operator+=(const T& r) {
51  operator++();
52  return operator*=(r);
53  }
54  poly operator*=(const T& r) {
55  columns.back().insert(r);
56  return *this;
57  }
58  poly operator*=(const poly& R) {
59  columns.back().insert(R.begin(), R.end());
60  return *this;
61  }
62  friend poly<T> operator+<>(const poly<T>&, const char*);
63  friend poly<T> operator+<>(const char*, const poly<T>&);
64 
65  const_iterator begin() const { return const_iterator(*this); }
66  const_iterator end() const { return const_iterator::end_of(*this); }
67 
68  auto const& getColumns() const { return columns; }
69  auto& getColumns() { return columns; }
70 
71  size_t size() const {
72  if (columns.empty())
73  return 0;
74  size_t size = 1;
75  for (const_column_iterator column = columns.begin(); column != columns.end(); ++column)
76  size *= column->size();
77  return size;
78  }
79 
80  class const_iterator : public boost::iterator_facade<const_iterator, T const, boost::bidirectional_traversal_tag, T> {
82 
83  std::list<typename std::set<T>::const_iterator> state;
84  typename std::list<std::set<T> >::const_iterator begin, end;
85 
86  typedef typename std::list<typename std::set<T>::const_iterator>::iterator state_iterator;
87  typedef typename std::list<typename std::set<T>::const_iterator>::const_iterator const_state_iterator;
88 
89  bool equal(const_iterator const& rhs) const { return std::equal(state.begin(), state.end(), rhs.state.begin()); }
90  T dereference() const {
91  T s;
92  for (const_state_iterator istate = state.begin(); istate != state.end(); ++istate)
93  s += **istate;
94  return s;
95  }
96  void increment() {
97  state_iterator istate = state.begin();
99  while (column != end && ++*istate == column->end()) {
100  ++istate;
101  ++column;
102  }
103  if (column == end) {
104  --column;
105  --istate;
106  }
107  while (istate != state.begin()) {
108  --istate;
109  *istate = (--column)->begin();
110  }
111  }
112  void decrement() {
113  state_iterator istate = state.begin();
114  const_column_iterator column = begin;
115  while (column != end && *istate == column->begin()) {
116  ++istate;
117  ++column;
118  }
119  if (column != end)
120  --*istate;
121  while (istate != state.begin()) {
122  --istate;
123  *istate = --((--column)->end());
124  }
125  }
126 
127  public:
130  const_column_iterator column = begin;
131  while (column != end)
132  state.push_back((column++)->begin());
133  }
134  static const_iterator end_of(const poly& p) {
135  const_iterator it(p);
136  if (p.size() != 0)
137  *--(it.state.end()) = (--p.getColumns().end())->end();
138  return it;
139  }
140  };
141 };
142 
143 template <class T>
144 poly<T> operator+(const poly<T>& lhs, const char* rhs) {
145  return lhs + poly<T>(rhs);
146 }
147 template <class T>
148 poly<T> operator+(const char* lhs, const poly<T>& rhs) {
149  return poly<T>(lhs) + rhs;
150 }
151 
152 template <class charT, class traits, class T>
153 inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& strm, const poly<T>& f) {
154  for (auto const& column : f.getColumns()) {
155  strm << "( ";
156  for (auto const& entry : column)
157  strm << entry << ", ";
158  strm << " )" << std::endl;
159  }
160  return strm;
161 }
162 
163 #endif
bool operator<(const poly &R) const
Definition: poly.h:34
const_iterator(const poly &p)
Definition: poly.h:129
poly(const T &t)
Definition: poly.h:32
void decrement()
Definition: poly.h:112
auto & getColumns()
Definition: poly.h:69
poly operator*=(const T &r)
Definition: poly.h:54
std::list< std::set< T > >::const_iterator begin
Definition: poly.h:84
auto const & getColumns() const
Definition: poly.h:68
T dereference() const
Definition: poly.h:90
std::list< typename std::set< T >::const_iterator > state
Definition: poly.h:83
poly operator+=(const poly &R)
Definition: poly.h:46
bool equal(const T &first, const T &second)
Definition: Equal.h:32
poly()
Definition: poly.h:31
std::list< std::set< T > > columns
Definition: poly.h:24
double f[11][100]
Definition: poly.h:11
void increment()
Definition: poly.h:96
std::list< typename std::set< T >::const_iterator >::const_iterator const_state_iterator
Definition: poly.h:87
bool equal(const_iterator const &rhs) const
Definition: poly.h:89
std::list< std::set< T > >::const_iterator const_column_iterator
Definition: poly.h:30
std::list< typename std::set< T >::const_iterator >::iterator state_iterator
Definition: poly.h:86
static const_iterator end_of(const poly &p)
Definition: poly.h:134
poly operator++()
Definition: poly.h:42
poly< T > operator+(const poly< T > &, const char *)
Definition: poly.h:144
const_iterator end() const
Definition: poly.h:66
friend class boost::iterator_core_access
Definition: poly.h:81
std::list< std::set< T > >::iterator column_iterator
Definition: poly.h:29
const_iterator begin() const
Definition: poly.h:65
poly operator*=(const poly &R)
Definition: poly.h:58
T value_type
Definition: poly.h:27
long double T
poly operator+=(const T &r)
Definition: poly.h:50
std::list< std::set< T > >::const_iterator end
Definition: poly.h:84
size_t size() const
Definition: poly.h:71