Ignore:
Timestamp:
Jun 28, 2015, 12:18:14 AM (4 years ago)
Author:
nmedfort
Message:

More modifications to the UnicodeSet? class. Default iterator computes code point range intervals as expected by the UCD compiler.

File:
1 edited

Legend:

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

    r4618 r4620  
    4545};
    4646
    47 class UnicodeSet;
     47class UnicodeSet {
     48public:
     49
    4850    using codepoint_t = re::codepoint_t;
    4951    using interval_t = re::interval_t;
    5052    using RunVector = std::vector<RunStructure>;
    5153    using QuadVector = std::vector<bitquad_t>;
    52 class UnicodeSet {
    53 public:
    5454
    55     class iterator : public boost::iterator_facade<iterator, re::interval_t, boost::forward_traversal_tag, re::interval_t> {
     55    class iterator : public boost::iterator_facade<iterator, interval_t, boost::forward_traversal_tag, interval_t> {
    5656        friend class UnicodeSet;
    5757        friend class boost::iterator_core_access;
    5858    protected:
    5959        iterator(RunVector::const_iterator runIterator, QuadVector::const_iterator quadIterator)
    60         : mRunIterator(runIterator), mQuadIterator(quadIterator), mQuadOffset(0)
    61         , mQuadPosition(0), mBaseCodePoint(0), mMinCodePoint(0), mMaxCodePoint(0)
     60        : mRunIterator(runIterator), mQuadIterator(quadIterator)
     61        , mQuadOffset(0), mQuadPosition(0), mBaseCodePoint(0), mMinCodePoint(0), mMaxCodePoint(0)
    6262        {
    6363
    6464        }
    6565
    66         void advance(unsigned n);
     66        void advance(const unsigned n);
    6767
    6868        re::interval_t dereference() const {
     
    7575
    7676        inline bool equal(iterator const & other) const {
    77             assert (&(mUnicodeSet) == &(other.mUnicodeSet));
    7877            return (mRunIterator == other.mRunIterator) && (mQuadIterator == other.mQuadIterator);
    7978        }
    8079    private:
    81         RunVector::const_iterator   mRunIterator;
    82         QuadVector::const_iterator  mQuadIterator;
     80        RunVector::const_iterator           mRunIterator;
     81        const RunVector::const_iterator     mRunEnd;
     82        QuadVector::const_iterator          mQuadIterator;
     83
     84
    8385        bitquad_t                   mQuadOffset;
    8486        unsigned                    mQuadPosition;
     
    8991
    9092    inline iterator begin() const {
    91         return iterator(runs.cbegin(), quads.cbegin());
     93        // note: pre-increment is intentional to move the iterator onto the first non-Empty interval.
     94        return ++iterator(mRuns.cbegin(), mQuads.cbegin());
    9295    }
    9396
    9497    inline iterator end() const {
    95         return iterator(runs.cend(), quads.cend());
     98        return iterator(mRuns.cend(), mQuads.cend());
     99    }
     100
     101    class quad_iterator : public boost::iterator_facade<quad_iterator, std::pair<RunStructure, bitquad_t>, boost::random_access_traversal_tag> {
     102        friend class UnicodeSet;
     103        friend class boost::iterator_core_access;
     104    public:
     105        quad_iterator(RunVector::const_iterator runIterator, QuadVector::const_iterator quadIterator)
     106            : mRunIterator(runIterator), mQuadIterator(quadIterator), mOffset(0) {}
     107
     108        void advance(unsigned n);
     109
     110        inline const std::pair<RunStructure, bitquad_t> dereference() const {
     111            return std::make_pair(getRun(), getQuad());
     112        }
     113
     114        inline void increment() {
     115            advance(1);
     116        }
     117
     118        inline RunStructure getRun() const {
     119            const auto & t = *mRunIterator;
     120            return RunStructure(t.mType, t.mRunLength - mOffset);
     121        }
     122
     123        inline bitquad_t getQuad() const {
     124            return *mQuadIterator;
     125        }
     126
     127        inline bool equal(const quad_iterator & other) const {
     128            return (mRunIterator == other.mRunIterator) && (mQuadIterator == other.mQuadIterator);
     129        }
     130
     131    private:
     132        RunVector::const_iterator   mRunIterator;
     133        QuadVector::const_iterator  mQuadIterator;
     134        unsigned                    mOffset;
     135    };
     136
     137    inline quad_iterator quad_begin() const {
     138        return quad_iterator(mRuns.cbegin(), mQuads.cbegin());
     139    }
     140
     141    inline quad_iterator quad_end() const {
     142        return quad_iterator(mRuns.cend(), mQuads.cend());
    96143    }
    97144
     
    109156    UnicodeSet(const codepoint_t codepoint);
    110157    UnicodeSet(const codepoint_t lo_codepoint, const codepoint_t hi_codepoint);
    111     UnicodeSet(std::initializer_list<RunStructure> r, std::initializer_list<bitquad_t> q) : runs(r), quads(q) {}
    112 
    113 protected:
    114 
    115     class quad_iterator : public boost::iterator_facade<quad_iterator, std::pair<RunStructure, bitquad_t>, boost::random_access_traversal_tag> {
    116         friend class UnicodeSet;
    117         friend class boost::iterator_core_access;
    118     public:
    119         quad_iterator(const UnicodeSet & set, unsigned runIndex) : mUnicodeSet(set), mRunIndex(runIndex), mOffset(0), mQuadIndex(0) {}
    120 
    121         void advance(unsigned n);
    122 
    123         inline const std::pair<RunStructure, bitquad_t> dereference() const {
    124             return std::make_pair(getRun(), getQuad());
    125         }
    126 
    127         inline void increment() {
    128             advance(1);
    129         }
    130 
    131         inline RunStructure getRun() const {
    132             const auto & t = mUnicodeSet.runs[mRunIndex];
    133             return RunStructure(t.mType, t.mRunLength - mOffset);
    134         }
    135 
    136         inline bitquad_t getQuad() const {
    137             return mUnicodeSet.quads[mQuadIndex];
    138         }
    139 
    140         inline bool equal(const quad_iterator & other) const {
    141             assert (&(mUnicodeSet) == &(other.mUnicodeSet));
    142             return (mRunIndex == other.mRunIndex);
    143         }
    144 
    145     private:
    146         const UnicodeSet &          mUnicodeSet;
    147         unsigned                    mRunIndex;
    148         unsigned                    mOffset;
    149         unsigned                    mQuadIndex;
    150     };
    151 
    152     inline quad_iterator quad_begin() const {
    153         return quad_iterator(*this, 0);
    154     }
    155 
    156     inline quad_iterator quad_end() const {
    157         return quad_iterator(*this, runs.size());
    158     }
    159 
    160     friend class UnicodeSet::quad_iterator;
    161 
    162     // Internal helper functions
    163     void append_run(const run_type_t type, const unsigned length);
    164     void append_quad(const bitquad_t quad);
     158    UnicodeSet(std::initializer_list<RunStructure> r, std::initializer_list<bitquad_t> q) : mRuns(r), mQuads(q) {}
     159    UnicodeSet(std::vector<RunStructure> && r, std::vector<bitquad_t> && q) : mRuns(r), mQuads(q) {}
    165160
    166161private:
    167162
    168     // The internal fields for a UnicodeSet.
    169     std::vector<RunStructure>   runs;
    170     std::vector<bitquad_t>      quads;
    171    
    172 
     163    std::vector<RunStructure>   mRuns;
     164    std::vector<bitquad_t>      mQuads;
    173165};
    174166
Note: See TracChangeset for help on using the changeset viewer.