SDSL 3.0.1
Succinct Data Structure Library
Loading...
Searching...
No Matches
iterators.hpp
Go to the documentation of this file.
1// Copyright (c) 2016, the SDSL Project Authors. All rights reserved.
2// Please see the AUTHORS file for details. Use of this source code is governed
3// by a BSD license that can be found in the LICENSE file.
8#ifndef INCLUDED_SDSL_ITERATORS
9#define INCLUDED_SDSL_ITERATORS
10
11#include <iterator>
12#include <type_traits> // std::invoke_result_t
13
14#include <sdsl/int_vector.hpp>
15
16namespace sdsl
17{
18
20
22template <class t_rac>
24{
25 public:
26 using iterator_category = std::random_access_iterator_tag;
27 using value_type = typename t_rac::value_type;
28 using difference_type = typename t_rac::difference_type;
31
32 typedef const typename t_rac::value_type const_reference;
33 typedef typename t_rac::size_type size_type;
35
36 private:
37 const t_rac * m_rac; // pointer to the random access container
38 typename t_rac::size_type m_idx;
39
40 template <class t_RAC>
44
45 public:
47 random_access_const_iterator(const t_rac * rac, size_type idx = 0)
48 : m_rac(rac)
49 , m_idx(idx)
50 {}
51
53 const_reference operator*() const { return (*m_rac)[m_idx]; }
54
57 {
58 ++m_idx;
59 return *this;
60 }
61
64 {
66 ++(*this);
67 return it;
68 }
69
72 {
73 --m_idx;
74 return *this;
75 }
76
79 {
81 --(*this);
82 return it;
83 }
84
86 {
87 if (i < 0) return *this -= (-i);
88 m_idx += i;
89 return *this;
90 }
91
93 {
94 if (i < 0) return *this += (-i);
95 m_idx -= i;
96 return *this;
97 }
98
100 {
101 iterator it = *this;
102 return it += i;
103 }
104
106 {
107 iterator it = *this;
108 return it -= i;
109 }
110
111 const_reference operator[](difference_type i) const { return *(*this + i); }
112
113 bool operator==(const iterator & it) const { return it.m_rac == m_rac && it.m_idx == m_idx; }
114
115 bool operator!=(const iterator & it) const { return !(*this == it); }
116
117 bool operator<(const iterator & it) const { return m_idx < it.m_idx; }
118
119 bool operator>(const iterator & it) const { return m_idx > it.m_idx; }
120
121 bool operator>=(const iterator & it) const { return !(*this < it); }
122
123 bool operator<=(const iterator & it) const { return !(*this > it); }
124};
125
126template <class t_rac>
130{
133}
134
135template <class t_rac>
138{
139 return it + n;
140}
141
142template <typename t_F>
144{
147 typedef typename std::invoke_result_t<t_F, size_type> value_type;
149
150 t_F f;
152
155 : f(ff)
156 , m_size(size)
157 {}
158
159 value_type operator[](size_type i) const { return f(i); }
160
161 size_type size() const { return m_size; }
162
163 iterator_type begin() const { return iterator_type(this, 0); }
164
165 iterator_type end() const { return iterator_type(this, size()); }
166};
167
168} // end namespace sdsl
169#endif
A generic vector class for integers of width .
Definition: int_vector.hpp:216
Generic iterator for a random access container.
Definition: iterators.hpp:24
random_access_const_iterator< t_rac > iterator
Definition: iterators.hpp:34
iterator operator++(int)
Postfix increment of the Iterator.
Definition: iterators.hpp:63
bool operator>=(const iterator &it) const
Definition: iterators.hpp:121
iterator & operator--()
Prefix decrement of the Iterator.
Definition: iterators.hpp:71
typename t_rac::difference_type difference_type
Definition: iterators.hpp:28
bool operator<=(const iterator &it) const
Definition: iterators.hpp:123
bool operator!=(const iterator &it) const
Definition: iterators.hpp:115
bool operator<(const iterator &it) const
Definition: iterators.hpp:117
std::random_access_iterator_tag iterator_category
Definition: iterators.hpp:26
friend 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)
iterator & operator+=(difference_type i)
Definition: iterators.hpp:85
iterator & operator-=(difference_type i)
Definition: iterators.hpp:92
bool operator>(const iterator &it) const
Definition: iterators.hpp:119
iterator & operator++()
Prefix increment of the Iterator.
Definition: iterators.hpp:56
iterator operator--(int)
Postfix decrement of the Iterator.
Definition: iterators.hpp:78
iterator operator+(difference_type i) const
Definition: iterators.hpp:99
const_reference operator[](difference_type i) const
Definition: iterators.hpp:111
const_reference operator*() const
Dereference operator for the Iterator.
Definition: iterators.hpp:53
typename t_rac::value_type value_type
Definition: iterators.hpp:27
random_access_const_iterator(const t_rac *rac, size_type idx=0)
Constructor.
Definition: iterators.hpp:47
const t_rac::value_type const_reference
Definition: iterators.hpp:32
iterator operator-(difference_type i) const
Definition: iterators.hpp:105
bool operator==(const iterator &it) const
Definition: iterators.hpp:113
int_vector.hpp contains the sdsl::int_vector class.
Namespace for the succinct data structure library.
int_vector_iterator< t_int_vector > operator+(typename int_vector_iterator< t_int_vector >::difference_type n, const int_vector_iterator< t_int_vector > &it)
int_vector_const_iterator< t_int_vector >::difference_type operator-(const int_vector_const_iterator< t_int_vector > &x, const int_vector_const_iterator< t_int_vector > &y)
iterator_type begin() const
Definition: iterators.hpp:163
int_vector ::difference_type difference_type
Definition: iterators.hpp:146
random_access_const_iterator< random_access_container > iterator_type
Definition: iterators.hpp:148
std::invoke_result_t< t_F, size_type > value_type
Definition: iterators.hpp:147
int_vector ::size_type size_type
Definition: iterators.hpp:145
iterator_type end() const
Definition: iterators.hpp:165
value_type operator[](size_type i) const
Definition: iterators.hpp:159
random_access_container(t_F ff, size_type size)
Definition: iterators.hpp:154