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> class poly;
11 template<class T> poly<T> operator+ (const poly<T>&, const char* );
12 template<class T> poly<T> operator+ (const char*, const poly<T>& );
13 
14 template<class T>
15 class poly :
16  boost::incrementable< poly<T>,
17  boost::addable< poly<T>,
18  boost::multipliable< poly<T>,
19  boost::multipliable2< poly<T>, T,
20  boost::less_than_comparable< poly<T> > > > > > {
21 
22  std::list<std::set<T> > columns;
23 
24  public:
25 
27  typedef T value_type;
28  typedef typename std::list<std::set<T> >::iterator column_iterator;
29  typedef typename std::list<std::set<T> >::const_iterator const_column_iterator;
30  poly() {}
31  poly(const T& t) {operator+=(t);}
32 
33  bool operator<(const poly& R) const {
34  const_column_iterator column(columns.begin()), Rcolumn(R.columns.begin());
35  while( column!=columns.end() && Rcolumn!=R.columns.end() && *column==*Rcolumn) { ++column; ++Rcolumn; }
36  return column!=columns.end() && Rcolumn!=R.columns.end() && *column < *Rcolumn;
37  }
38  poly operator++() {columns.push_back(std::set<T>()); return *this;}
39  poly operator+=(const poly& R) { columns.insert(columns.end(),R.columns.begin(),R.columns.end()); return *this;}
40  poly operator+=(const T& r) { operator++(); return operator*=(r);}
41  poly operator*=(const T& r) { columns.back().insert(r); return *this;}
42  poly operator*=(const poly& R) { columns.back().insert(R.begin(),R.end()); return *this;}
43  friend poly<T> operator+ <> (const poly<T>&, const char*);
44  friend poly<T> operator+ <> (const char*, const poly<T>&);
45 
46  const_iterator begin() const { return const_iterator(*this);}
47  const_iterator end() const { return const_iterator::end_of(*this);}
48 
49  auto const& getColumns() const { return columns; }
50  auto& getColumns() { return columns; }
51 
52  size_t size() const {
53  if(columns.empty()) return 0;
54  size_t size=1;
55  for( const_column_iterator column = columns.begin(); column != columns.end(); ++column)
56  size *= column->size();
57  return size;
58  }
59 
61  : public boost::iterator_facade< const_iterator, T const, boost::bidirectional_traversal_tag, T > {
63 
64  std::list<typename std::set<T>::const_iterator> state;
65  typename std::list<std::set<T> >::const_iterator begin, end;
66 
67  typedef typename std::list<typename std::set<T>::const_iterator>::iterator state_iterator;
68  typedef typename std::list<typename std::set<T>::const_iterator>::const_iterator const_state_iterator;
69 
70  bool equal(const_iterator const& rhs) const { return std::equal( state.begin(), state.end(), rhs.state.begin() ); }
71  T dereference() const { T s; for(const_state_iterator istate=state.begin(); istate!=state.end(); ++istate) s+= **istate; return s; }
72  void increment() {
73  state_iterator istate = state.begin();
74  const_column_iterator column = begin;
75  while( column != end && ++*istate == column->end() ) { ++istate; ++column;}
76  if( column == end ) {--column; --istate;}
77  while( istate != state.begin() ) {--istate; *istate = (--column)->begin();}
78  }
79  void decrement() {
80  state_iterator istate = state.begin();
81  const_column_iterator column = begin;
82  while( column != end && *istate == column->begin()) { ++istate; ++column;}
83  if( column != end) --*istate;
84  while( istate != state.begin() ) {--istate; *istate = --((--column)->end());}
85  }
86 
87  public:
88 
90  const_iterator(const poly& p) : begin(p.getColumns().begin()), end(p.getColumns().end()) {
91  const_column_iterator column = begin;
92  while(column!=end) state.push_back((column++)->begin());
93  }
94  static const_iterator end_of(const poly& p) {
95  const_iterator it(p);
96  if(p.size()!=0) *--(it.state.end()) = (--p.getColumns().end())->end();
97  return it;
98  }
99 
100  };
101 
102 };
103 
104 template<class T> poly<T> operator+ (const poly<T>& lhs, const char* rhs ) { return lhs + poly<T>(rhs);}
105 template<class T> poly<T> operator+ (const char* lhs, const poly<T>& rhs ) { return poly<T>(lhs) + rhs;}
106 
107 template <class charT, class traits, class T>
108 inline
109 std::basic_ostream<charT,traits>& operator<<(std::basic_ostream<charT,traits>& strm, const poly<T>& f) {
110  for(auto const& column : f.getColumns())
111  { strm << "( "; for(auto const& entry : column) strm << entry << ", "; strm << " )" << std::endl; }
112  return strm;
113 }
114 
115 #endif
const_iterator(const poly &p)
Definition: poly.h:90
const_iterator end() const
Definition: poly.h:47
poly(const T &t)
Definition: poly.h:31
auto const & getColumns() const
Definition: poly.h:49
void decrement()
Definition: poly.h:79
auto & getColumns()
Definition: poly.h:50
poly operator*=(const T &r)
Definition: poly.h:41
std::list< std::set< T > >::const_iterator begin
Definition: poly.h:65
friend poly< T > operator+(const poly< T > &, const char *)
Definition: poly.h:104
std::list< typename std::set< T >::const_iterator > state
Definition: poly.h:64
poly operator+=(const poly &R)
Definition: poly.h:39
bool equal(const T &first, const T &second)
Definition: Equal.h:34
const_iterator begin() const
Definition: poly.h:46
bool equal(const_iterator const &rhs) const
Definition: poly.h:70
poly()
Definition: poly.h:30
std::list< std::set< T > > columns
Definition: poly.h:22
double f[11][100]
Definition: poly.h:10
void increment()
Definition: poly.h:72
std::list< typename std::set< T >::const_iterator >::const_iterator const_state_iterator
Definition: poly.h:68
std::list< std::set< T > >::const_iterator const_column_iterator
Definition: poly.h:29
std::list< typename std::set< T >::const_iterator >::iterator state_iterator
Definition: poly.h:67
static const_iterator end_of(const poly &p)
Definition: poly.h:94
poly operator++()
Definition: poly.h:38
bool operator<(const poly &R) const
Definition: poly.h:33
poly< T > operator+(const poly< T > &, const char *)
Definition: poly.h:104
friend class boost::iterator_core_access
Definition: poly.h:62
std::list< std::set< T > >::iterator column_iterator
Definition: poly.h:28
poly operator*=(const poly &R)
Definition: poly.h:42
size_t size() const
Definition: poly.h:52
T value_type
Definition: poly.h:26
T dereference() const
Definition: poly.h:71
long double T
poly operator+=(const T &r)
Definition: poly.h:40
std::list< std::set< T > >::const_iterator end
Definition: poly.h:65