From a377c7ced2b1f28402af29a7f3233a0ed0d9ef7d Mon Sep 17 00:00:00 2001 From: Michael Hemmer Date: Sun, 4 Jan 2009 16:05:03 +0000 Subject: [PATCH] don't inherit Exponent_vector from std::vector --- .../Polynomial_ref/Exponent_vector.tex | 12 +- Polynomial/include/CGAL/Exponent_vector.h | 197 +++++++++++------- .../test/Polynomial/Exponent_vector.cpp | 176 ++++++++-------- 3 files changed, 225 insertions(+), 160 deletions(-) diff --git a/Polynomial/doc_tex/Polynomial_ref/Exponent_vector.tex b/Polynomial/doc_tex/Polynomial_ref/Exponent_vector.tex index 2ebb8f8d7a0..0818a16ae85 100644 --- a/Polynomial/doc_tex/Polynomial_ref/Exponent_vector.tex +++ b/Polynomial/doc_tex/Polynomial_ref/Exponent_vector.tex @@ -13,19 +13,17 @@ since negative exponents may appear in intermediate results. The set of exponent vectors with elementwise addition forms an {\em Abelian Group}. -Though this class is derived from \ccc{std::vector} the comparison -is changes such that the lexicographic order starts with the last entry. -This reflects the fact that the last entry corresponds -to the last/outermost variable of a multivariate polynomial. +Beside the constructors \ccClassName\ has almost the same interface as an +\ccc{std::vector}. Moreover the comparison is changed such that +the lexicographic order starts the comparison at the last entry. +This reflects the fact that the last entry corresponds to the +outermost variable of a multivariate polynomial. %\footnote{Should we add a scalar multiplication? } \ccInclude{CGAL/Exponent_vector.h} -\ccInheritsFrom -\ccc{std::vector} - \ccIsModel \ccc{Random Access Container}\\ diff --git a/Polynomial/include/CGAL/Exponent_vector.h b/Polynomial/include/CGAL/Exponent_vector.h index 04627a2aa1f..f44ecb21804 100644 --- a/Polynomial/include/CGAL/Exponent_vector.h +++ b/Polynomial/include/CGAL/Exponent_vector.h @@ -16,8 +16,7 @@ // $Id$ // // -// Author(s) : Sebastian Limbach -// Michael Hemmer +// Author(s) : Michael Hemmer // // ============================================================================ @@ -33,93 +32,151 @@ CGAL_BEGIN_NAMESPACE -class Exponent_vector : - public std::vector, - public boost::less_than_comparable1< Exponent_vector > + +class Exponent_vector : + public boost::less_than_comparable1< Exponent_vector >, + public boost::equality_comparable1< Exponent_vector > { - typedef std::vector Base; + std::vector v; public: - Exponent_vector(): Base(){}; + typedef Exponent_vector Self; -// OLD CONSTRUCTORS - // Exponent_vector(Base::size_type i): Base(i){}; - // Exponent_vector(Base::size_type i, Base::value_type x): Base(i,x){}; - // Exponent_vector(int i, int x): Base(i,x){}; -// NEW CONSTRUCTORS + Exponent_vector(){}; - Exponent_vector(int e0): Base(1) { - (*this)[0]=e0; - }; - Exponent_vector(int e0, int e1): Base(2) { - (*this)[0]=e0; (*this)[1]=e1; - }; - Exponent_vector(int e0, int e1, int e2): Base(3) { - (*this)[0]=e0; (*this)[1]=e1; (*this)[2]=e2; - }; - Exponent_vector(int e0, int e1, int e2, int e3): Base(4) { - (*this)[0]=e0; (*this)[1]=e1; (*this)[2]=e2; (*this)[3]=e3; - }; + Exponent_vector(int e0): v(1) { + v[0]=e0; + }; + Exponent_vector(int e0, int e1): v(2) { + v[0]=e0; v[1]=e1; + }; + Exponent_vector(int e0, int e1, int e2): v(3) { + v[0]=e0; v[1]=e1; v[2]=e2; + }; + Exponent_vector(int e0, int e1, int e2, int e3): v(4) { + v[0]=e0; v[1]=e1; v[2]=e2; v[3]=e3; + }; - Exponent_vector(const Base& v): Base ( v ){}; - Exponent_vector(const Exponent_vector& v): Base ( v ){}; + Exponent_vector(const std::vector& v_): v(v_){}; + Exponent_vector(const Exponent_vector& ev): v(ev.v){}; - template - Exponent_vector(InputIterator begin , InputIterator end) - :Base(begin,end){ - typedef typename InputIterator::value_type value_type; - BOOST_STATIC_ASSERT(( ::boost::is_same::value)); - } - - bool operator<( const Exponent_vector& ev ) const { - CGAL_precondition(this->size()==ev.size()); - Base::const_reverse_iterator rit1(this->rbegin()); - Base::const_reverse_iterator rit2(ev.rbegin()); - while(rit1!=this->rend()){ - if(*rit1 < *rit2) return true; - if(*rit1 > *rit2) return false; - rit1++; rit2++; - } - CGAL_postcondition(rit1 == this->rend()); - CGAL_postcondition(rit2 == ev.rend()); - return false; + template + Exponent_vector(InputIterator begin , InputIterator end) + :v(begin,end){ + typedef typename std::iterator_traits::value_type value_type; + BOOST_STATIC_ASSERT(( ::boost::is_same::value)); + } + + + // mirror vector functions + typedef std::vector::value_type value_type; + typedef std::vector::pointer pointer; + typedef std::vector::const_pointer const_pointer; + typedef std::vector::reference reference; + typedef std::vector::const_reference const_reference; + typedef std::vector::size_type size_type; + typedef std::vector::difference_type difference_type; + typedef std::vector::iterator iterator; + typedef std::vector::const_iterator const_iterator; + typedef std::vector::reverse_iterator reverse_iterator; + typedef std::vector::const_reverse_iterator const_reverse_iterator; + + iterator begin(){return v.begin();} + iterator end(){return v.end();} + const_iterator begin() const {return v.begin();} + const_iterator end() const {return v.end();} + reverse_iterator rbegin() {return v.rbegin();} + reverse_iterator rend(){return v.rend();} + const_reverse_iterator rbegin() const {return v.rbegin();} + const_reverse_iterator rend() const {return v.rend();} + size_type size() const {return v.size();} + size_type max_size() const {return v.max_size();} + size_type capacity() const {return v.capacity();} + bool empty() const {return v.empty();} + reference operator[](size_type n) { return v[n]; } + const_reference operator[](size_type n) const {return v[n];} + // vector& operator=(const vector&) + + void reserve(size_t s){v.reserve(s);} + reference front(){return v.front();} + const_reference front() const {return v.front();} + reference back() {return v.back();} + const_reference back() const {return v.back();} + void push_back(const int& x) { v.push_back(x);} + void pop_back() {v.pop_back();} + void swap(Self& ev) {v.swap(ev.v);} + iterator insert(iterator pos, const int& x){return v.insert(pos,x);} + + template + void insert(iterator pos,InputIterator f, InputIterator l){ + v.insert(pos,f,l); + } + void insert(iterator pos, size_type n, const int& x){ + v.insert(pos,n,x); + } + iterator erase(iterator pos){return v.erase(pos);} + iterator erase(iterator first, iterator last){return v.erase(first,last);} + void clear(){v.clear();} + void resize(size_type n, int t = 0){v.resize(n,t);} + bool operator==(const Self& ev) const { return v == ev.v; } + + // this is the actual change + bool operator<( const Exponent_vector& ev ) const { + CGAL_precondition(this->size() == ev.size()); + const_reverse_iterator rit1(this->rbegin()); + const_reverse_iterator rit2(ev.rbegin()); + while(rit1!=this->rend()){ + if(*rit1 < *rit2) return true; + if(*rit1 > *rit2) return false; + rit1++; rit2++; } + CGAL_postcondition(rit1 == this->rend()); + CGAL_postcondition(rit2 == ev.rend()); + return false; + } - void output_benchmark( std::ostream& os ) const { - os << "( "; - for( unsigned i = 0; i < size(); ++i ) { - if( i != 0 ) - os << ", "; - os << at(i); - } - os << " )"; + void output_benchmark( std::ostream& os ) const { + os << "( "; + for( unsigned i = 0; i < size(); ++i ) { + if( i != 0 ) + os << ", "; + os << v.at(i); } + os << " )"; + } }; inline bool is_valid(const Exponent_vector& ev) { - Exponent_vector::const_iterator it; - for(it = ev.begin(); it != ev.end();it++){ - if (CGAL::is_negative(*it)) return false; - } - return true; + Exponent_vector::const_iterator it; + for(it = ev.begin(); it != ev.end();it++){ + if (CGAL::is_negative(*it)) return false; + } + return true; } inline std::ostream& operator << (std::ostream& os, const Exponent_vector& ev) { - Exponent_vector::const_iterator it; - os << "(" ; - for(it = ev.begin(); it != ev.end();it++){ - if (it == ev.begin()) { - os << *it ; - } - else{ - os <<"," << *it ; - } + Exponent_vector::const_iterator it; + os << "(" ; + for(it = ev.begin(); it != ev.end();it++){ + if (it == ev.begin()) { + os << *it ; } - os << ")" ; - return os; + else{ + os <<"," << *it ; + } + } + os << ")" ; + return os; } - CGAL_END_NAMESPACE +namespace std{ +template <> void swap(CGAL::Exponent_vector& ev1, CGAL::Exponent_vector& ev2){ + ev1.swap(ev2); +} + +} + + #endif // CGAL_EXPONENT_VECTOR_H diff --git a/Polynomial/test/Polynomial/Exponent_vector.cpp b/Polynomial/test/Polynomial/Exponent_vector.cpp index 47ca9745643..5fae9a87bf5 100644 --- a/Polynomial/test/Polynomial/Exponent_vector.cpp +++ b/Polynomial/test/Polynomial/Exponent_vector.cpp @@ -2,98 +2,108 @@ #include #include #include +#include int main() { - - CGAL::Exponent_vector ev1,ev2; - assert(ev1.size()==0); - assert(ev2.size()==0); - assert( (ev1==ev2)); - assert(!(ev1!=ev2)); - assert(!(ev1ev2)); - - ev1.push_back(1); - ev1.push_back(0); - ev1.push_back(0); - - ev2.push_back(0); - ev2.push_back(1); - ev2.push_back(0); - - assert(!(ev2 == ev1)) ; - assert( (ev2 != ev1)) ; - assert(!(ev2 < ev1)) ; - assert( (ev2 > ev1)) ; - - assert(CGAL::is_valid(ev1)); + typedef CGAL::Exponent_vector T; - std::vector vec; - vec.push_back(0); vec.push_back(1); vec.push_back(5); + boost::function_requires< boost::DefaultConstructibleConcept >(); + boost::function_requires< boost::AssignableConcept >(); + boost::function_requires< boost::EqualityComparableConcept >(); + boost::function_requires< boost::ComparableConcept >(); + boost::function_requires< boost::RandomAccessContainerConcept >(); + boost::function_requires< boost::BackInsertionSequenceConcept >(); + + CGAL::Exponent_vector ev1,ev2; + assert(ev1.size()==0); + assert(ev2.size()==0); + assert( (ev1==ev2)); + assert(!(ev1!=ev2)); + assert(!(ev1ev2)); - ev1 = CGAL::Exponent_vector(vec.begin(),vec.end()); - assert(ev1[0] == 0); - assert(ev1[1] == 1); - assert(ev1[2] == 5); + ev1.push_back(1); + ev1.push_back(0); + ev1.push_back(0); + + ev2.push_back(0); + ev2.push_back(1); + ev2.push_back(0); + + assert(!(ev2 == ev1)) ; + assert( (ev2 != ev1)) ; + assert(!(ev2 < ev1)) ; + assert( (ev2 > ev1)) ; + + assert(CGAL::is_valid(ev1)); + + std::vector vec; + vec.push_back(0); vec.push_back(1); vec.push_back(5); + + ev1 = CGAL::Exponent_vector(vec.begin(),vec.end()); + assert(ev1[0] == 0); + assert(ev1[1] == 1); + assert(ev1[2] == 5); - // new constructors - // univariate - { - CGAL::Exponent_vector ev(-1); - assert(!CGAL::is_valid(ev)); - } - { - CGAL::Exponent_vector ev(0); - assert(CGAL::is_valid(ev)); - assert(ev[0] == 0); - } - { - CGAL::Exponent_vector ev(1); - assert(CGAL::is_valid(ev)); - assert(ev[0] == 1); - } + // new constructors + // univariate + { + CGAL::Exponent_vector ev(-1); + assert(!CGAL::is_valid(ev)); + } + { + CGAL::Exponent_vector ev(0); + assert(CGAL::is_valid(ev)); + assert(ev[0] == 0); + } + { + CGAL::Exponent_vector ev(1); + assert(CGAL::is_valid(ev)); + assert(ev[0] == 1); + } - // bivariate - { - CGAL::Exponent_vector ev(0,-1); - assert(!CGAL::is_valid(ev)); - } - { - CGAL::Exponent_vector ev(2,1); - assert(CGAL::is_valid(ev)); - assert(ev[0] == 2); - assert(ev[1] == 1); + // bivariate + { + CGAL::Exponent_vector ev(0,-1); + assert(!CGAL::is_valid(ev)); + } + { + CGAL::Exponent_vector ev(2,1); + assert(CGAL::is_valid(ev)); + assert(ev[0] == 2); + assert(ev[1] == 1); - } + } - // trivariate - { - CGAL::Exponent_vector ev(0,0,-1); - assert(!CGAL::is_valid(ev)); - } - { - CGAL::Exponent_vector ev(3,2,1); - assert(CGAL::is_valid(ev)); - assert(ev[0] == 3); - assert(ev[1] == 2); - assert(ev[2] == 1); - } + // trivariate + { + CGAL::Exponent_vector ev(0,0,-1); + assert(!CGAL::is_valid(ev)); + } + { + CGAL::Exponent_vector ev(3,2,1); + assert(CGAL::is_valid(ev)); + assert(ev[0] == 3); + assert(ev[1] == 2); + assert(ev[2] == 1); + } - // four-variate - { - CGAL::Exponent_vector ev(0,0,0,-1); - assert(!CGAL::is_valid(ev)); - } - { - CGAL::Exponent_vector ev(4,3,2,1); - assert(CGAL::is_valid(ev)); - assert(ev[0] == 4); - assert(ev[1] == 3); - assert(ev[2] == 2); - assert(ev[3] == 1); - } - + // four-variate + { + CGAL::Exponent_vector ev(0,0,0,-1); + assert(!CGAL::is_valid(ev)); + } + { + CGAL::Exponent_vector ev(4,3,2,1); + assert(CGAL::is_valid(ev)); + assert(ev[0] == 4); + assert(ev[1] == 3); + assert(ev[2] == 2); + assert(ev[3] == 1); + } + + using std::swap; swap(ev1,ev2); - return 0; + return 0; }