iterator.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*-----------------------------------------------------------------------------+
  2. Copyright (c) 2009-2009: 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_ITERATOR_HPP_JOFA_091003
  9. #define BOOST_ICL_ITERATOR_HPP_JOFA_091003
  10. #include <iterator>
  11. namespace boost{namespace icl
  12. {
  13. /** \brief Performes an addition using a container's memberfunction add, when operator= is called. */
  14. template<class ContainerT> class add_iterator
  15. {
  16. public:
  17. /// The container's type.
  18. typedef ContainerT container_type;
  19. typedef std::output_iterator_tag iterator_category;
  20. typedef void value_type;
  21. typedef void difference_type;
  22. typedef void pointer;
  23. typedef void reference;
  24. /** An add_iterator is constructed with a container and a position
  25. that has to be maintained. */
  26. add_iterator(ContainerT& cont, typename ContainerT::iterator iter)
  27. : _cont(&cont), _iter(iter) {}
  28. /** This assignment operator adds the \c value before the current position.
  29. It maintains it's position by incrementing after addition. */
  30. add_iterator& operator=(typename ContainerT::const_reference value)
  31. {
  32. _iter = icl::add(*_cont, _iter, value);
  33. if(_iter != _cont->end())
  34. ++_iter;
  35. return *this;
  36. }
  37. add_iterator& operator*() { return *this; }
  38. add_iterator& operator++() { return *this; }
  39. add_iterator& operator++(int){ return *this; }
  40. private:
  41. ContainerT* _cont;
  42. typename ContainerT::iterator _iter;
  43. };
  44. /** Function adder creates and initializes an add_iterator */
  45. template<class ContainerT, typename IteratorT>
  46. inline add_iterator<ContainerT> adder(ContainerT& cont, IteratorT iter_)
  47. {
  48. return add_iterator<ContainerT>(cont, typename ContainerT::iterator(iter_));
  49. }
  50. /** \brief Performes an insertion using a container's memberfunction add, when operator= is called. */
  51. template<class ContainerT> class insert_iterator
  52. {
  53. public:
  54. /// The container's type.
  55. typedef ContainerT container_type;
  56. typedef std::output_iterator_tag iterator_category;
  57. typedef void value_type;
  58. typedef void difference_type;
  59. typedef void pointer;
  60. typedef void reference;
  61. /** An insert_iterator is constructed with a container and a position
  62. that has to be maintained. */
  63. insert_iterator(ContainerT& cont, typename ContainerT::iterator iter)
  64. : _cont(&cont), _iter(iter) {}
  65. /** This assignment operator adds the \c value before the current position.
  66. It maintains it's position by incrementing after addition. */
  67. insert_iterator& operator=(typename ContainerT::const_reference value)
  68. {
  69. _iter = _cont->insert(_iter, value);
  70. if(_iter != _cont->end())
  71. ++_iter;
  72. return *this;
  73. }
  74. insert_iterator& operator*() { return *this; }
  75. insert_iterator& operator++() { return *this; }
  76. insert_iterator& operator++(int){ return *this; }
  77. private:
  78. ContainerT* _cont;
  79. typename ContainerT::iterator _iter;
  80. };
  81. /** Function inserter creates and initializes an insert_iterator */
  82. template<class ContainerT, typename IteratorT>
  83. inline insert_iterator<ContainerT> inserter(ContainerT& cont, IteratorT iter_)
  84. {
  85. return insert_iterator<ContainerT>(cont, typename ContainerT::iterator(iter_));
  86. }
  87. }} // namespace icl boost
  88. #endif // BOOST_ICL_ITERATOR_HPP_JOFA_091003