RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
BitIterator.h
Go to the documentation of this file.
1/*
2 RawSpeed - RAW file decoder.
3
4 Copyright (C) 2023 Roman Lebedev
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19*/
20
21#pragma once
22
23#include "adt/Bit.h"
24#include "adt/Invariant.h"
25#include <cstddef>
26#include <iterator>
27#include <type_traits>
28
29namespace rawspeed {
30
31template <typename T>
32 requires std::is_unsigned_v<T>
33struct BitMSBIterator final {
35 int bitIdx;
36
37 using iterator_category = std::input_iterator_tag;
38 using difference_type = std::ptrdiff_t;
39 using value_type = bool;
40 using pointer = const value_type*; // Unusable, but must be here.
41 using reference = const value_type&; // Unusable, but must be here.
42
43 BitMSBIterator(T bitsPat_, int bitIdx_) : bitsPat(bitsPat_), bitIdx(bitIdx_) {
44 invariant(bitIdx < static_cast<int>(bitwidth<T>()) && bitIdx >= -1);
45 }
46
48 invariant(static_cast<unsigned>(bitIdx) < bitwidth<T>() &&
49 "Iterator overflow");
50 return (bitsPat >> bitIdx) & 0b1;
51 }
53 --bitIdx; // We go from MSB to LSB.
54 invariant(bitIdx >= -1);
55 return *this;
56 }
57 friend bool operator==(const BitMSBIterator& a, const BitMSBIterator& b) {
58 invariant(a.bitsPat == b.bitsPat && "Comparing unrelated iterators.");
59 return a.bitIdx == b.bitIdx;
60 }
61};
62
63} // namespace rawspeed
#define invariant(expr)
Definition Invariant.h:27
throw T(buf.data())
constexpr unsigned RAWSPEED_READNONE bitwidth(T unused={})
Definition Bit.h:43
value_type operator*() const
Definition BitIterator.h:47
BitMSBIterator(T bitsPat_, int bitIdx_)
Definition BitIterator.h:43
const value_type & reference
Definition BitIterator.h:41
std::input_iterator_tag iterator_category
Definition BitIterator.h:37
const value_type * pointer
Definition BitIterator.h:40
friend bool operator==(const BitMSBIterator &a, const BitMSBIterator &b)
Definition BitIterator.h:57
BitMSBIterator & operator++()
Definition BitIterator.h:52
std::ptrdiff_t difference_type
Definition BitIterator.h:38