container.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*-----------------------------------------------------------------------------+
  2. Copyright (c) 2010-2010: Joachim Faulhaber
  3. +------------------------------------------------------------------------------+
  4. Distributed under the Boost Software License, Version 1.0.
  5. (See accompanying file LICENCE.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. +-----------------------------------------------------------------------------*/
  8. #ifndef BOOST_ICL_CONCEPT_CONTAINER_HPP_JOFA_100923
  9. #define BOOST_ICL_CONCEPT_CONTAINER_HPP_JOFA_100923
  10. #include <boost/utility/enable_if.hpp>
  11. #include <boost/mpl/and.hpp>
  12. #include <boost/mpl/not.hpp>
  13. #include <boost/icl/type_traits/is_container.hpp>
  14. #include <boost/icl/type_traits/is_icl_container.hpp>
  15. namespace boost{ namespace icl
  16. {
  17. //==============================================================================
  18. //= Emptieness
  19. //==============================================================================
  20. /** Tests if the container is empty.
  21. Complexity: constant. */
  22. template<class Type>
  23. typename enable_if<is_container<Type>, bool>::type
  24. is_empty(const Type& object)
  25. {
  26. return object.begin()==object.end();
  27. }
  28. /** All content of the container is dropped.
  29. Complexity: linear. */
  30. template<class Type>
  31. typename enable_if<is_container<Type>, void>::type
  32. clear(Type& object)
  33. {
  34. object.erase(object.begin(), object.end());
  35. }
  36. //==============================================================================
  37. //= Size
  38. //==============================================================================
  39. template<class Type>
  40. typename enable_if<mpl::and_< is_container<Type>
  41. , mpl::not_<is_icl_container<Type> > >
  42. , std::size_t>::type
  43. iterative_size(const Type& object)
  44. {
  45. return object.size();
  46. }
  47. //==============================================================================
  48. //= Swap
  49. //==============================================================================
  50. template<class Type>
  51. typename enable_if<is_container<Type>, void>::type
  52. swap(Type& left, Type& right)
  53. {
  54. left.swap(right);
  55. }
  56. //==============================================================================
  57. //= Iteration
  58. //==============================================================================
  59. template<class Type>
  60. typename enable_if<is_container<Type>, typename Type::iterator>::type
  61. cyclic_prior(Type& object, typename Type::iterator it_)
  62. { return it_ == object.begin() ? object.end() : --it_; }
  63. template<class Type>
  64. typename enable_if<is_container<Type>, typename Type::const_iterator>::type
  65. cyclic_prior(const Type& object, typename Type::const_iterator it_)
  66. { return it_ == object.begin() ? object.end() : --it_; }
  67. }} // namespace boost icl
  68. #endif