CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 <boost/foreach.hpp>
7 #include <iostream>
8 #include <list>
9 #include <set>
10 
11 template<class T> class poly;
12 template<class T> poly<T> operator+ (const poly<T>&, const char* );
13 template<class T> poly<T> operator+ (const char*, const poly<T>& );
14 
15 template<class T>
16 class poly :
17  boost::incrementable< poly<T>,
18  boost::addable< poly<T>,
19  boost::multipliable< poly<T>,
20  boost::multipliable2< poly<T>, T,
21  boost::less_than_comparable< poly<T> > > > > > {
22 
23  std::list<std::set<T> > columns;
24 
25  public:
26 
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) { ++column; ++Rcolumn; }
37  return column!=columns.end() && Rcolumn!=R.columns.end() && *column < *Rcolumn;
38  }
39  poly operator++() {columns.push_back(std::set<T>()); return *this;}
40  poly operator+=(const poly& R) { columns.insert(columns.end(),R.columns.begin(),R.columns.end()); return *this;}
41  poly operator+=(const T& r) { operator++(); return operator*=(r);}
42  poly operator*=(const T& r) { columns.back().insert(r); return *this;}
43  poly operator*=(const poly& R) { columns.back().insert(R.begin(),R.end()); return *this;}
44  friend poly<T> operator+ <> (const poly<T>&, const char*);
45  friend poly<T> operator+ <> (const char*, const poly<T>&);
46 
47  const_iterator begin() const { return const_iterator(*this);}
48  const_iterator end() const { return const_iterator::end_of(*this);}
49 
50  column_iterator begin_columns() { return columns.begin();}
51  column_iterator end_columns() { return columns.end();}
52 
53  const_column_iterator begin_columns() const { return columns.begin();}
54  const_column_iterator end_columns() const { return columns.end();}
55 
56  size_t size() const {
57  if(columns.empty()) return 0;
58  size_t size=1;
59  for( const_column_iterator column = columns.begin(); column != columns.end(); ++column)
60  size *= column->size();
61  return size;
62  }
63 
65  : public boost::iterator_facade< const_iterator, T const, boost::bidirectional_traversal_tag, T > {
67 
68  std::list<typename std::set<T>::const_iterator> state;
69  typename std::list<std::set<T> >::const_iterator begin, end;
70 
71  typedef typename std::list<typename std::set<T>::const_iterator>::iterator state_iterator;
72  typedef typename std::list<typename std::set<T>::const_iterator>::const_iterator const_state_iterator;
73 
74  bool equal(const_iterator const& rhs) const { return std::equal( state.begin(), state.end(), rhs.state.begin() ); }
75  T dereference() const { T s; for(const_state_iterator istate=state.begin(); istate!=state.end(); ++istate) s+= **istate; return s; }
76  void increment() {
77  state_iterator istate = state.begin();
79  while( column != end && ++*istate == column->end() ) { ++istate; ++column;}
80  if( column == end ) {--column; --istate;}
81  while( istate != state.begin() ) {--istate; *istate = (--column)->begin();}
82  }
83  void decrement() {
84  state_iterator istate = state.begin();
86  while( column != end && *istate == column->begin()) { ++istate; ++column;}
87  if( column != end) --*istate;
88  while( istate != state.begin() ) {--istate; *istate = --((--column)->end());}
89  }
90 
91  public:
92 
95  const_column_iterator column = begin;
96  while(column!=end) state.push_back((column++)->begin());
97  }
98  static const_iterator end_of(const poly& p) {
99  const_iterator it(p);
100  if(p.size()!=0) *--(it.state.end()) = (--p.end_columns())->end();
101  return it;
102  }
103 
104  };
105 
106 };
107 
108 template<class T> poly<T> operator+ (const poly<T>& lhs, const char* rhs ) { return lhs + poly<T>(rhs);}
109 template<class T> poly<T> operator+ (const char* lhs, const poly<T>& rhs ) { return poly<T>(lhs) + rhs;}
110 
111 template <class charT, class traits, class T>
112 inline
113 std::basic_ostream<charT,traits>& operator<<(std::basic_ostream<charT,traits>& strm, const poly<T>& f) {
114  BOOST_FOREACH(std::set<T> column, std::make_pair(f.begin_columns(),f.end_columns()))
115  { strm << "( "; BOOST_FOREACH(T entry, column) strm << entry << ", "; strm << " )" << std::endl; }
116  return strm;
117 }
118 
119 #endif
const_iterator(const poly &p)
Definition: poly.h:94
const_iterator end() const
Definition: poly.h:48
poly(const T &t)
Definition: poly.h:32
column_iterator end_columns()
Definition: poly.h:51
void decrement()
Definition: poly.h:83
MatrixMeschach operator+(const MatrixMeschach &mat1, const MatrixMeschach &mat2)
poly operator*=(const T &r)
Definition: poly.h:42
std::list< std::set< T > >::const_iterator begin
Definition: poly.h:69
std::list< typename std::set< T >::const_iterator > state
Definition: poly.h:68
const_column_iterator begin_columns() const
Definition: poly.h:53
poly operator+=(const poly &R)
Definition: poly.h:40
const_iterator begin() const
Definition: poly.h:47
bool equal(const_iterator const &rhs) const
Definition: poly.h:74
poly()
Definition: poly.h:31
std::list< std::set< T > > columns
Definition: poly.h:23
std::pair< std::string, MonitorElement * > entry
Definition: ME_MAP.h:8
double f[11][100]
Definition: poly.h:11
void increment()
Definition: poly.h:76
std::list< typename std::set< T >::const_iterator >::const_iterator const_state_iterator
Definition: poly.h:72
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:71
static const_iterator end_of(const poly &p)
Definition: poly.h:98
poly operator++()
Definition: poly.h:39
const_column_iterator end_columns() const
Definition: poly.h:54
bool operator<(const poly &R) const
Definition: poly.h:34
friend class boost::iterator_core_access
Definition: poly.h:66
std::list< std::set< T > >::iterator column_iterator
Definition: poly.h:29
poly operator*=(const poly &R)
Definition: poly.h:43
size_t size() const
Definition: poly.h:56
T value_type
Definition: poly.h:27
string s
Definition: asciidump.py:422
T dereference() const
Definition: poly.h:75
long double T
poly operator+=(const T &r)
Definition: poly.h:41
std::list< std::set< T > >::const_iterator end
Definition: poly.h:69
column_iterator begin_columns()
Definition: poly.h:50