1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
/* sdsl - succinct data structures library
Copyright (C) 2008 Simon Gog
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/ .
*/
/*! \file iterators.hpp
\brief iterators.hpp contains an generic iterator for random access containers.
\author Simon Gog
*/
#ifndef INCLUDED_SDSL_ITERATORS
#define INCLUDED_SDSL_ITERATORS
#include <iterator>
namespace sdsl
{
//! Generic iterator for a random access container
/*! \tparam t_rac Type of random access container.
*/
template<class t_rac>
class random_access_const_iterator: public std::iterator<std::random_access_iterator_tag, typename t_rac::value_type, typename t_rac::difference_type>
{
public:
typedef const typename t_rac::value_type const_reference;
typedef typename t_rac::size_type size_type;
typedef random_access_const_iterator<t_rac> iterator;
typedef typename t_rac::difference_type difference_type;
private:
const t_rac* m_rac;// pointer to the random access container
typename t_rac::size_type m_idx;
template<class t_RAC>
friend typename random_access_const_iterator<t_RAC>::difference_type operator-(const random_access_const_iterator<t_RAC>& x,
const random_access_const_iterator<t_RAC>& y);
public:
//! Constructor
random_access_const_iterator(const t_rac* rac, size_type idx = 0) : m_rac(rac), m_idx(idx) { }
//! Dereference operator for the Iterator.
const_reference operator*()const
{
return (*m_rac)[m_idx];
}
//! Prefix increment of the Iterator.
iterator& operator++()
{
++m_idx;
return *this;
}
//! Postfix increment of the Iterator.
iterator operator++(int)
{
random_access_const_iterator it = *this;
++(*this);
return it;
}
//! Prefix decrement of the Iterator.
iterator& operator--()
{
--m_idx;
return *this;
}
//! Postfix decrement of the Iterator.
iterator operator--(int)
{
random_access_const_iterator it = *this;
--(*this);
return it;
}
iterator& operator+=(difference_type i)
{
if (i<0)
return *this -= (-i);
m_idx += i;
return *this;
}
iterator& operator-=(difference_type i)
{
if (i<0)
return *this += (-i);
m_idx -= i;
return *this;
}
iterator operator+(difference_type i) const
{
iterator it = *this;
return it += i;
}
iterator operator-(difference_type i) const
{
iterator it = *this;
return it -= i;
}
const_reference operator[](difference_type i) const
{
return *(*this + i);
}
bool operator==(const iterator& it)const
{
return it.m_rac == m_rac && it.m_idx == m_idx;
}
bool operator!=(const iterator& it)const
{
return !(*this==it);
}
bool operator<(const iterator& it)const
{
return m_idx < it.m_idx;
}
bool operator>(const iterator& it)const
{
return m_idx > it.m_idx;
}
bool operator>=(const iterator& it)const
{
return !(*this < it);
}
bool operator<=(const iterator& it)const
{
return !(*this > it);
}
};
template<class t_rac>
inline typename random_access_const_iterator<t_rac>::difference_type operator-(const random_access_const_iterator<t_rac>& x, const random_access_const_iterator<t_rac>& y)
{
return (typename random_access_const_iterator<t_rac>::difference_type)x.m_idx
- (typename random_access_const_iterator<t_rac>::difference_type)y.m_idx;
}
template<class t_rac>
inline random_access_const_iterator<t_rac> operator+(typename random_access_const_iterator<t_rac>::difference_type n, const random_access_const_iterator<t_rac>& it)
{
return it+n;
}
template<typename t_F>
struct random_access_container {
typedef int_vector<>::size_type size_type;
typedef int_vector<>::difference_type difference_type;
typedef typename std::result_of<t_F(size_type)>::type value_type;
typedef random_access_const_iterator<random_access_container> iterator_type;
t_F f;
size_type m_size;
random_access_container() {};
random_access_container(t_F ff, size_type size) : f(ff), m_size(size) { }
value_type operator[](size_type i) const { return f(i); }
size_type size() const { return m_size; }
iterator_type begin() const
{
return iterator_type(this, 0);
}
iterator_type end() const
{
return iterator_type(this, size());
}
};
} // end namespace sdsl
#endif
|