Fixed MakeStatic()
authornvalimak <nvalimak@3cdefd35-fc62-479d-8e8d-bae585ffb9ca>
Mon, 9 Mar 2009 11:32:05 +0000 (11:32 +0000)
committernvalimak <nvalimak@3cdefd35-fc62-479d-8e8d-bae585ffb9ca>
Mon, 9 Mar 2009 11:32:05 +0000 (11:32 +0000)
git-svn-id: svn+ssh://idea.nguyen.vg/svn/sxsi/trunk/TextCollection@226 3cdefd35-fc62-479d-8e8d-bae585ffb9ca

StringIterator.h [new file with mode: 0644]

diff --git a/StringIterator.h b/StringIterator.h
new file mode 100644 (file)
index 0000000..44c3193
--- /dev/null
@@ -0,0 +1,198 @@
+/******************************************************************************
+ *   Copyright (C) 2009 by Niko Välimäki                                      *
+ *                                                                            *
+ *                                                                            *
+ *   This program is free software; you can redistribute it and/or modify     *
+ *   it under the terms of the GNU Lesser General Public License as published *
+ *   by the Free Software Foundation; either version 2 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 Lesser General Public License for more details.                      *
+ *                                                                            *
+ *   You should have received a copy of the GNU Lesser General Public License *
+ *   along with this program; if not, write to the                            *
+ *   Free Software Foundation, Inc.,                                          *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            *
+ *****************************************************************************/
+
+#ifndef _CSA_MYSTRINGITERATOR_
+#define _CSA_MYSTRINGITERATOR_
+
+#include "BitRank.h"
+#include <iterator>
+#include <utility>
+
+namespace SXSI
+{
+
+/**
+ * Custom random access iterator for a string of texts separated by '\0'.
+ * 
+ * Maps from char array [0.255] to unsigned alphabet [0...] so that
+ * each endmarker '\0' in original array is given an unique 
+ * number that retains ordering among the endmarkers.
+ * Other characters [1..255] are mapped to range [numberOfTexts..255+numberOfText].
+ */
+class StringIterator : public std::iterator<std::random_access_iterator_tag, 
+        unsigned, 
+        long>
+{
+protected:
+    uchar const * ptr_;
+    uchar const * begin_;
+    ulong n_;
+    unsigned numberOfTexts_;
+    BitRank * br_;
+    
+public:
+    
+    StringIterator()
+        : ptr_(0), begin_(0), n_(0), numberOfTexts_(0), br_(0)
+    {}
+    StringIterator(uchar const * ptr, uchar const * begin, ulong n, unsigned noTexts, BitRank* br)
+        : ptr_(ptr), begin_(begin), n_(n), numberOfTexts_(noTexts), br_(br)
+    {}
+    StringIterator( StringIterator const & other )
+        : ptr_( other.ptr_ ), begin_(other.begin_), n_(other.n_), numberOfTexts_(other.numberOfTexts_),
+        br_(other.br_)
+    {}
+
+/*    uchar const * operator &() 
+    { 
+        return( ptr_ );
+    }
+
+    uchar const * operator &() const 
+    { 
+        return( ptr_ );
+        }*/
+
+    StringIterator& operator=( StringIterator const & other )
+    {
+        ptr_ = other.ptr_;
+        begin_ = other.begin_;
+        n_ = other.n_;
+        numberOfTexts_ = other.numberOfTexts_;
+        br_ = other.br_;
+        return( *this );
+    }
+
+    bool operator<=( StringIterator const & other ) const
+    {
+        long d = other.ptr_ - ptr_;
+        return( d > 0 || ptr_ == other.ptr_ );
+    }
+
+    bool operator<( StringIterator const & other ) const
+    {
+        long d = other.ptr_ - ptr_;
+        return( d > 0 );
+    }
+
+    bool operator==( StringIterator const & other ) const
+    {
+        return( ptr_ == other.ptr_ );
+    }
+
+    bool operator!=( StringIterator const & other ) const
+    {
+        return( ptr_ != other.ptr_ );
+    }
+
+    unsigned operator*()
+    {
+        if (ptr_ - begin_ < 0 || (ulong)(ptr_ - begin_) >= n_) 
+            return 0;
+
+        uchar c = (uchar)*ptr_;
+        if (c == 0)
+            return br_->rank(ptr_-begin_);
+
+        unsigned result = (unsigned)c + numberOfTexts_;
+        
+        return result;
+    }
+
+    uchar const * operator->()
+    {
+        return( ptr_ );
+    }
+
+    StringIterator& operator++()
+    {
+        ++ptr_;
+        return( *this );
+    }
+
+    StringIterator operator++(int)
+    {
+        StringIterator tmp( *this );
+        ++ptr_;
+        return( tmp );
+    }
+
+    StringIterator& operator--()
+    {
+        --ptr_;
+        return( *this );
+    }
+
+    StringIterator operator--(int)
+    {
+        StringIterator tmp( *this );
+        --ptr_;
+        return( tmp );
+    }
+
+    long operator-( StringIterator const & other ) const
+    {
+        long d = ptr_ - other.ptr_;
+        return( d );
+    }
+
+    unsigned operator[]( size_t nPos ) const
+    {
+        StringIterator tmp( *this );
+        tmp.ptr_ += nPos;
+        return( *tmp );
+    }
+
+    StringIterator& operator+=( int nPos )
+    {
+        ptr_ += nPos;
+        return( *this );
+    }
+
+    StringIterator& operator-=( int nPos )
+    {
+        ptr_ -= nPos;
+        return( *this );
+    }
+
+    StringIterator operator+( int n ) const
+    {
+        StringIterator tmp( *this );
+        tmp += n;
+        return( tmp );
+    }
+
+    StringIterator operator-( int n ) const
+    {
+        StringIterator tmp( *this );
+        tmp -= n;
+        return( tmp );
+    }
+}; // class
+
+        
+/*static StringIterator operator+( int n, StringIterator const & iterator )
+{
+    return( iterator + n );
+    }*/
+
+} // namespace SXSI
+
+#endif // _CSA_MYSTRINGITERATOR_