aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/containers/bitseq/bititerator.h
blob: efe7ba873bb1201cf3be6e9b80af8f6f19d06702 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#pragma once

#include "traits.h"

#include <library/cpp/pop_count/popcount.h>

template <typename T>
class TBitIterator {
public:
    using TWord = T;
    using TTraits = TBitSeqTraits<TWord>;

public:
    TBitIterator(const T* data = nullptr)
        : Current(0)
        , Mask(0)
        , Data(data)
    {
    }

    /// Get the word next to the one we are currenlty iterating over.
    const TWord* NextWord() const {
        return Data;
    }

    /// Get the next bit without moving the iterator.
    bool Peek() const {
        return Mask ? (Current & Mask) : (*Data & 1);
    }

    /// Get the next bit and move forward.
    /// TODO: Implement inversed iteration as well.
    bool Next() {
        if (!Mask) {
            Current = *Data++;
            Mask = 1;
        }
        const bool bit = Current & Mask;
        Mask <<= 1;
        return bit;
    }

    /// Get the next count bits without moving the iterator.
    TWord Peek(ui8 count) const {
        if (!count)
            return 0;
        Y_DEBUG_ABORT_UNLESS(count <= TTraits::NumBits);

        if (!Mask)
            return *Data & TTraits::ElemMask(count);

        auto usedBits = (size_t)PopCount(Mask - 1);
        TWord result = Current >> usedBits;
        auto leftInCurrent = TTraits::NumBits - usedBits;
        if (count <= leftInCurrent)
            return result & TTraits::ElemMask(count);

        count -= leftInCurrent;
        result |= (*Data & TTraits::ElemMask(count)) << leftInCurrent;
        return result;
    }

    /// Get the next count bits and move forward by count bits.
    TWord Read(ui8 count) {
        if (!count)
            return 0;
        Y_DEBUG_ABORT_UNLESS(count <= TTraits::NumBits);

        if (!Mask) {
            Current = *Data++;
            Mask = 1 << count;
            return Current & TTraits::ElemMask(count);
        }

        auto usedBits = (size_t)PopCount(Mask - 1);
        TWord result = Current >> usedBits;
        auto leftInCurrent = TTraits::NumBits - usedBits;
        if (count < leftInCurrent) {
            Mask <<= count;
            return result & TTraits::ElemMask(count);
        }

        count -= leftInCurrent;
        if (count) {
            Current = *Data++;
            Mask = 1 << count;
            result |= (Current & TTraits::ElemMask(count)) << leftInCurrent;
        } else {
            Mask = 0;
        }

        return result;
    }

    /// Move the iterator forward by count bits.
    void Forward(int count) {
        if (!count)
            return;

        int leftInCurrent = (size_t)PopCount(~(Mask - 1));
        if (count < leftInCurrent) {
            Mask <<= count;
            return;
        }

        count -= leftInCurrent;
        Data += count >> TTraits::DivShift;
        auto remainder = count & TTraits::ModMask;

        if (remainder) {
            Current = *Data++;
            Mask = 1 << remainder;
        } else {
            Current = 0;
            Mask = 0;
        }
    }

    /// Skip trailing bits of the current word and move by count words forward.
    void Align(int count = 0) {
        Current = 0;
        if (Mask)
            Mask = 0;
        Data += count;
    }

    /// Initialize the iterator.
    void Reset(const TWord* data) {
        Current = 0;
        Mask = 0;
        Data = data;
    }

private:
    TWord Current;
    TWord Mask;
    const TWord* Data;
};