From 774b5ba6de99f7be34b02594c22611ace0730005 Mon Sep 17 00:00:00 2001 From: Markus Blatt <mblatt@dune-project.org> Date: Wed, 1 Dec 2004 18:09:07 +0000 Subject: [PATCH] Added classes describing sets of enumeration values with some tests. [[Imported from SVN: r1189]] --- common/enumset.hh | 137 +++++++++++++++++++++++++++++++++++++++++ common/sllist.hh | 8 +++ common/test/.gitignore | 3 +- common/test/settest.cc | 12 ++++ 4 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 common/enumset.hh create mode 100644 common/test/settest.cc diff --git a/common/enumset.hh b/common/enumset.hh new file mode 100644 index 000000000..da9e67529 --- /dev/null +++ b/common/enumset.hh @@ -0,0 +1,137 @@ +// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- +// vi: set et ts=4 sw=2 sts=2: +#ifndef DUNE_ENUMSET_HH +#define DUNE_ENUMSET_HH + +#include <iostream> + +namespace Dune +{ + /** + * @file + * @brief Classes for building sets out of enumeration values. + * @author Markus Blatt + */ + /** @addtogroup Common + * + * @{ + */ + + /** + * @brief An empty set. + */ + template<typename TA> + class EmptySet + { + public: + /** + * @brief The POD type the set holds. + */ + typedef TA Type; + /** + * @brief Always returns false. + */ + static bool contains(const Type& attribute); + }; + + /** + * @brief A set consisting only of one item. + */ + template<typename TA, int item> + class EnumItem + { + public: + /** + * @brief The type the set holds. + */ + typedef TA Type; + + /** + * @brief Tests whether an item is in the set. + * @return True if item==Type. + */ + static bool contains(const Type& attribute); + }; + + /** + * @brief A set representing a range including the borders. + */ + template<typename T,int from, int end> + class EnumRange //: public PODSet<EnumRange<T,from,end>,T> + { + public: + /** + * @brief The type the set holds. + */ + typedef T Type; + static bool contains(const T& item); + }; + + /** + * @brief A set combining two other sets. + */ + template<class TI1, class TI2, typename TA> + class Combine + { + public: + static bool contains(const TA& item); + }; + + template<class TI, typename TA> + inline bool PODSet<TI,TA>::contains(const Type& attribute) + { + return static_cast<Implementation*>(this)->contains(); + } + + template<typename TA> + inline bool EmptySet<TA>::contains(const TA& attribute) + { + return false; + } + + template<typename TA,int i> + inline bool EnumItem<TA,i>::contains(const TA& item) + { + return item==i; + } + + template<typename TA,int i> + inline std::ostream& operator<<(std::ostream& os, const EnumItem<TA,i>&) + { + return os<<i; + } + + template<typename TA, int from, int to> + inline bool EnumRange<TA,from,to>::contains(const TA& item) + { + return from<=item && item<=to; + } + + template<typename TA, int from, int to> + inline std::ostream& operator<<(std::ostream& os, const EnumRange<TA,from,to>&) + { + return os<<"["<<from<<" - "<<to<<"]"; + } + + template<class TI1, class TI2, typename TA> + inline bool Combine<TI1,TI2,TA>::contains(const TA& item) + { + return TI1::contains(item) || + TI2::contains(item); + } + + template<class TI1, class TI2> + inline Combine<TI1,TI2,typename TI1::Type> combine(const TI1& set1, const TI2& set2) + { + return Combine<TI1,TI2,typename TI1::Type>(); + } + + template<class TI1, class TI2, class T> + inline std::ostream& operator<<(std::ostream& os, const Combine<TI1,TI2,T>&) + { + return os << TI1()<<" "<<TI2(); + } + /** @} */ +} + +#endif diff --git a/common/sllist.hh b/common/sllist.hh index b028b19e2..016e28013 100644 --- a/common/sllist.hh +++ b/common/sllist.hh @@ -170,6 +170,10 @@ namespace Dune : current_(item) {} + inline SLListIterator() + : current_(0) + {} + /** * @brief Dereferencing function for the iterator facade. * @return A reference to the element at the current position. @@ -222,6 +226,10 @@ namespace Dune friend class SLListIterator<T,A>; public: + inline SLListConstIterator() + : current_(0) + {} + inline SLListConstIterator(typename SLList<T,A>::Element* item) : current_(item) {} diff --git a/common/test/.gitignore b/common/test/.gitignore index d1a2c7540..35db20b45 100644 --- a/common/test/.gitignore +++ b/common/test/.gitignore @@ -8,4 +8,5 @@ test-stack arraylisttest smartpointertest iteratorfacadetest -sllisttest \ No newline at end of file +sllisttest +settest \ No newline at end of file diff --git a/common/test/settest.cc b/common/test/settest.cc new file mode 100644 index 000000000..c73227db2 --- /dev/null +++ b/common/test/settest.cc @@ -0,0 +1,12 @@ +// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- +// vi: set et ts=4 sw=2 sts=2: +#include <dune/common/enumset.hh> +#include <iostream> +int main() +{ + using namespace Dune; + std::cout<<Combine<EnumItem<int,1>,EnumItem<int,2>,int>::contains(1)<< + " "<<Combine<EnumItem<int,1>,EnumItem<int,2>,int>::contains(2)<< + " "<<Combine<Combine<EnumItem<int,1>,EnumItem<int,2>,int>,EnumItem<int,0>,int>::contains(3)<< + " "<<EnumRange<int,1,3>::contains(3)<<std::endl; +} -- GitLab