1 /******************************************************************************
2 * Copyright (C) 2009 by Niko Välimäki *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Lesser General Public License as published *
7 * by the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU Lesser General Public License for more details. *
15 * You should have received a copy of the GNU Lesser General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 *****************************************************************************/
21 #ifndef _CSA_MYSTRINGITERATOR_
22 #define _CSA_MYSTRINGITERATOR_
32 * Custom random access iterator for a string of texts separated by '\0'.
34 * Maps from char array [0.255] to unsigned alphabet [0...] so that
35 * each endmarker '\0' in original array is given an unique
36 * number that retains ordering among the endmarkers.
37 * Other characters [1..255] are mapped to range [numberOfTexts..255+numberOfText].
39 class StringIterator : public std::iterator<std::random_access_iterator_tag,
47 unsigned numberOfTexts_;
53 : ptr_(0), begin_(0), n_(0), numberOfTexts_(0), br_(0)
55 StringIterator(uchar const * ptr, uchar const * begin, ulong n, unsigned noTexts, BitRank* br)
56 : ptr_(ptr), begin_(begin), n_(n), numberOfTexts_(noTexts), br_(br)
58 StringIterator( StringIterator const & other )
59 : ptr_( other.ptr_ ), begin_(other.begin_), n_(other.n_), numberOfTexts_(other.numberOfTexts_),
63 /* uchar const * operator &()
68 uchar const * operator &() const
73 StringIterator& operator=( StringIterator const & other )
76 begin_ = other.begin_;
78 numberOfTexts_ = other.numberOfTexts_;
83 bool operator<=( StringIterator const & other ) const
85 long d = other.ptr_ - ptr_;
86 return( d > 0 || ptr_ == other.ptr_ );
89 bool operator<( StringIterator const & other ) const
91 long d = other.ptr_ - ptr_;
95 bool operator==( StringIterator const & other ) const
97 return( ptr_ == other.ptr_ );
100 bool operator!=( StringIterator const & other ) const
102 return( ptr_ != other.ptr_ );
107 if (ptr_ - begin_ < 0 || (ulong)(ptr_ - begin_) >= n_)
110 uchar c = (uchar)*ptr_;
112 return br_->rank(ptr_-begin_);
114 unsigned result = (unsigned)c + numberOfTexts_;
119 uchar const * operator->()
124 StringIterator& operator++()
130 StringIterator operator++(int)
132 StringIterator tmp( *this );
137 StringIterator& operator--()
143 StringIterator operator--(int)
145 StringIterator tmp( *this );
150 long operator-( StringIterator const & other ) const
152 long d = ptr_ - other.ptr_;
156 unsigned operator[]( size_t nPos ) const
158 StringIterator tmp( *this );
163 StringIterator& operator+=( int nPos )
169 StringIterator& operator-=( int nPos )
175 StringIterator operator+( int n ) const
177 StringIterator tmp( *this );
182 StringIterator operator-( int n ) const
184 StringIterator tmp( *this );
191 /*static StringIterator operator+( int n, StringIterator const & iterator )
193 return( iterator + n );
198 #endif // _CSA_MYSTRINGITERATOR_