SDSL: Succinct Data Structure Library
A C++ template library for succinct data structures
 All Classes Namespaces Files Functions Variables Typedefs Friends
sdsl/include/sdsl/iterators.hpp
Go to the documentation of this file.
00001 /* sdsl - succinct data structures library
00002     Copyright (C) 2008 Simon Gog
00003 
00004     This program is free software: you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation, either version 3 of the License, or
00007     (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with this program.  If not, see http://www.gnu.org/licenses/ .
00016 */
00021 #ifndef INCLUDED_SDSL_ITERATORS
00022 #define INCLUDED_SDSL_ITERATORS
00023 
00024 #include <iterator>
00025 
00026 namespace sdsl
00027 {
00028 
00029 template<class RandomAccessContainer>
00030 class random_access_const_iterator: public std::iterator<std::random_access_iterator_tag, typename RandomAccessContainer::value_type, typename RandomAccessContainer::difference_type>
00031 {
00032     public:
00033         typedef const typename RandomAccessContainer::value_type  const_reference;
00034         typedef typename RandomAccessContainer::size_type size_type;
00035         typedef random_access_const_iterator<RandomAccessContainer> iterator;
00036         typedef typename RandomAccessContainer::difference_type difference_type;
00037 
00038     private:
00039         const RandomAccessContainer* m_rac;// pointer to the random access container
00040         typename RandomAccessContainer::size_type m_idx;
00041 
00042         template<class RAC>
00043         friend typename random_access_const_iterator<RAC>::difference_type operator-(const random_access_const_iterator<RAC>& x,
00044                 const random_access_const_iterator<RAC>& y);
00045 
00046 
00047     public:
00049         random_access_const_iterator(const RandomAccessContainer* rac, size_type idx = 0) {
00050             m_rac = rac;
00051             m_idx = idx;
00052         }
00053 
00055         const_reference operator*()const {
00056             return (*m_rac)[m_idx];
00057         }
00058 
00060         iterator& operator++() {
00061             ++m_idx;
00062             return *this;
00063         }
00064 
00066         iterator operator++(int x) {
00067             random_access_const_iterator it = *this;
00068             ++(*this);
00069             return it;
00070         }
00071 
00073         iterator& operator--() {
00074             --m_idx;
00075             return *this;
00076         }
00077 
00079         iterator operator--(int x) {
00080             random_access_const_iterator it = *this;
00081             ++(*this);
00082             return it;
00083         }
00084 
00085         iterator& operator+=(difference_type i) {
00086             if (i<0)
00087                 return *this -= (-i);
00088             m_idx += i;
00089             return *this;
00090         }
00091 
00092         iterator& operator-=(difference_type i) {
00093             if (i<0)
00094                 return *this += (-i);
00095             m_idx -= i;
00096             return *this;
00097         }
00098 
00099         iterator operator+(difference_type i) const {
00100             iterator it = *this;
00101             return it += i;
00102         }
00103 
00104         iterator operator-(difference_type i) const {
00105             iterator it = *this;
00106             return it -= i;
00107         }
00108 
00109         const_reference operator[](difference_type i) const {
00110             return *(*this + i);
00111         }
00112 
00113         bool operator==(const iterator& it)const {
00114             return it.m_rac == m_rac && it.m_idx == m_idx;
00115         }
00116 
00117         bool operator!=(const iterator& it)const {
00118             return !(*this==it);
00119         }
00120 
00121         bool operator<(const iterator& it)const {
00122             return m_idx < it.m_idx;
00123         }
00124 
00125         bool operator>(const iterator& it)const {
00126             return m_idx > it.m_idx;
00127         }
00128 
00129         bool operator>=(const iterator& it)const {
00130             return !(*this < it);
00131         }
00132 
00133         bool operator<=(const iterator& it)const {
00134             return !(*this > it);
00135         }
00136 
00137 };
00138 
00139 template<class RandomAccessContainer>
00140 inline typename random_access_const_iterator<RandomAccessContainer>::difference_type operator-(const random_access_const_iterator<RandomAccessContainer>& x, const random_access_const_iterator<RandomAccessContainer>& y)
00141 {
00142     return (typename random_access_const_iterator<RandomAccessContainer>::difference_type)x.m_idx
00143            - (typename random_access_const_iterator<RandomAccessContainer>::difference_type)y.m_idx;
00144 }
00145 
00146 template<class RandomAccessContainer>
00147 inline random_access_const_iterator<RandomAccessContainer> operator+(typename random_access_const_iterator<RandomAccessContainer>::difference_type n, const random_access_const_iterator<RandomAccessContainer>& it)
00148 {
00149     return it+n;
00150 }
00151 
00152 } // end namespace sdsl
00153 
00154 #endif // end of include guard