diff --git a/include/boost/python/suite/indexing/iterator_pair.hpp b/include/boost/python/suite/indexing/iterator_pair.hpp new file mode 100755 index 00000000..e86e427b --- /dev/null +++ b/include/boost/python/suite/indexing/iterator_pair.hpp @@ -0,0 +1,137 @@ +// -*- mode:c++ -*- +// +// Header file iterator_pair.hpp +// +// Emulate an STL container using a pair of iterators. Doesn't support +// insertion or deletion, for the obvious reasons. +// +// Copyright (c) 2003 Raoul M. Gough +// +// This material is provided "as is", with absolutely no warranty expressed +// or implied. Any use is at your own risk. +// +// Permission to use or copy this material for any purpose is hereby +// granted without fee, provided the above notices are retained on all +// copies. Permission to modify the material and to distribute modified +// versions is granted, provided the above notices are retained, and a +// notice that the material was modified is included with the above +// copyright notice. +// +// History +// ======= +// 2003/ 9/ 9 rmg File creation +// +// $Id$ +// + +#ifndef iterator_pair_rmg_20030909_included +#define iterator_pair_rmg_20030909_included + +#include +#include +#include + +namespace indexing { + template + class iterator_pair + { + private: + typedef typename boost::call_traits::param_type iterator_param; + typedef std::iterator_traits std_traits; + + public: + typedef typename std_traits::reference reference; + typedef Iterator iterator; + typedef typename std_traits::difference_type size_type; + typedef typename std_traits::difference_type difference_type; + typedef typename std_traits::value_type value_type; + typedef typename std_traits::pointer pointer; + + // Can't provide: const_iterator, allocator_type, reverse_iterator + // or const_reverse_iterator. Could probably provide (but don't) + // const_reference and const_pointer. These would be the same + // as reference and pointer if Iterator is itself a const_iterator. + + public: + iterator_pair (iterator_param, iterator_param); + + iterator begin() const; + iterator end() const; + + public: + // Only sensible for random_access iterators + size_type size () const; + reference operator[] (size_type) const; + reference at (size_type) const; + + private: + iterator myBegin; + iterator myEnd; + }; + + template + iterator_pair::iterator_pair (iterator_param begin + , iterator_param end) + : myBegin (begin) + , myEnd (end) + { + } + + template + typename iterator_pair::iterator + iterator_pair::begin() const + { + return myBegin; + } + + template + typename iterator_pair::iterator + iterator_pair::end() const + { + return myEnd; + } + + template + typename iterator_pair::size_type + iterator_pair::size() const + { + return std::distance (begin(), end()); + } + + template + typename iterator_pair::reference + iterator_pair::operator[](size_type index) const + { + iterator temp (begin()); + std::advance (temp, index); + return *temp; + } + + template + typename iterator_pair::reference + iterator_pair::at (size_type index) const + { + if (index >= size()) + { + throw std::out_of_range + (std::string ("iterator_pair: index out of range")); + } + + else + { + return (*this)[index]; + } + } + + template + T *begin (T(&array)[N]) { + return array; + } + + template + T *end (T(&array)[N]) { + return array + N; + } +} + +#endif // iterator_pair_rmg_20030909_included