Ignore:
Timestamp:
Jun 24, 2015, 5:26:17 PM (4 years ago)
Author:
nmedfort
Message:

Replaced USet_Iterator with a standard C++ UnicodeSet? iterator.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/UCD/unicode_set.h

    r4611 r4616  
    33#include <stdint.h>
    44#include <vector>
    5 #include <ostream>
     5#include <boost/iterator/iterator_facade.hpp>
    66
    77//
     
    2828// Default: 64 codepoints (k=6).
    2929//
     30
    3031typedef uint32_t bitquad_t;
    31 
    32 const size_t quad_bits = 8 * sizeof(bitquad_t);
    33 const size_t mod_quad_bit_mask = quad_bits - 1;
    34 const size_t UnicodeQuadCount = 0x110000 / quad_bits;
    35 const bitquad_t FullQuadMask = -1;
    3632
    3733// The representation for runs
     
    3935
    4036struct RunStructure {
    41   RunStructure(run_type_t r, uint16_t lgth) : run_type(r), run_length(lgth) {}
    42   run_type_t run_type;
    43   uint16_t run_length;
     37  RunStructure(run_type_t r, uint16_t lgth) : mType(r), mRunLength(lgth) {}
     38  run_type_t mType;
     39  uint16_t mRunLength;
    4440};
    4541
     42class UnicodeSet;
     43
    4644class UnicodeSet {
    47 friend class Uset_Iterator;
    4845public:
     46
     47    class iterator : public boost::iterator_facade<iterator, const UnicodeSet &, boost::forward_traversal_tag, std::pair<RunStructure, bitquad_t>> {
     48        friend class UnicodeSet;
     49    public:
     50        iterator(const UnicodeSet & set, unsigned runIndex) : mUnicodeSet(set), mRunIndex(runIndex), mOffset(0), mQuadIndex(0) {}
     51    protected:
     52        friend class boost::iterator_core_access;
     53        void advance(unsigned n);
     54        const std::pair<RunStructure, bitquad_t> dereference() const;
     55        inline void increment() {
     56            advance(1);
     57        }
     58        inline bool equal(iterator const& other) const {
     59            return (mRunIndex == other.mRunIndex) && (&(mUnicodeSet) == &(other.mUnicodeSet)) && (mQuadIndex == other.mQuadIndex) && (mOffset == other.mOffset);
     60        }
     61    private:
     62        const UnicodeSet &          mUnicodeSet;
     63        unsigned                    mRunIndex;
     64        unsigned                    mOffset;
     65        unsigned                    mQuadIndex;
     66    };
     67
     68    inline iterator begin() const {
     69        return iterator(*this, 0);
     70    }
     71
     72    inline iterator end() const {
     73        return iterator(*this, runs.size());
     74    }
     75
    4976//
    5077//  The internal fields for a UnicodeSet.
    51     std::vector<RunStructure> runs;
    52     std::vector<bitquad_t> quads;
    53     int quad_count;
     78    std::vector<RunStructure>   runs;
     79    std::vector<bitquad_t>      quads;
     80    unsigned quad_count;
    5481   
    5582// 
     
    5986//
    6087//  Nullary constructor for incremental building.
    61     UnicodeSet() : runs(std::vector<RunStructure>()), quads(std::vector<bitquad_t>()), quad_count(0) {}
     88    UnicodeSet() : quad_count(0) {}
    6289//
    6390//  Ternary constructor for constant construction using precomputed data.
    64     UnicodeSet(std::initializer_list<RunStructure> r, std::initializer_list<bitquad_t> q, int c) : runs(r), quads(q), quad_count(c) {}
     91    UnicodeSet(std::initializer_list<RunStructure> r, std::initializer_list<bitquad_t> q, unsigned c) : runs(r), quads(q), quad_count(c) {}
    6592};
    6693
     
    76103bool uset_member(const UnicodeSet & s, int codepoint);
    77104
    78 class Uset_Iterator {
    79 public:
    80     Uset_Iterator(const UnicodeSet & s) : uSet(s), run_no(0), offset(0), quad_no(0) {}
    81     bool at_end();
    82     RunStructure current_run();
    83     bitquad_t get_quad();
    84     void advance(int n);
    85 private:
    86     const UnicodeSet & uSet;
    87     int run_no;
    88     int offset;
    89     int quad_no;
    90 };
    91 
    92105#endif
    93106
Note: See TracChangeset for help on using the changeset viewer.