RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
Range.h
Go to the documentation of this file.
1/*
2 RawSpeed - RAW file decoder.
3
4 Copyright (C) 2017 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 "rawspeedconfig.h"
24#include <algorithm>
25#include <cassert>
26#include <iterator>
27#include <type_traits>
28#include <utility>
29
30namespace rawspeed {
31
32template <typename T> class Range final {
34 std::make_unsigned_t<T> size;
35
36public:
37 constexpr Range() = default;
38
39 template <typename T2>
40 requires std::is_unsigned_v<T2>
41 constexpr Range(T base_, T2 size_) : base(base_), size(size_) {}
42
43 constexpr T RAWSPEED_READNONE begin() const { return base; }
44
45 constexpr T RAWSPEED_READNONE end() const { return base + T(size); }
46};
47
48template <typename T> bool operator<(const Range<T>& lhs, const Range<T>& rhs) {
49 return std::pair(lhs.begin(), lhs.end()) < std::pair(rhs.begin(), rhs.end());
50}
51
52template <typename Tr, typename Tv>
53constexpr bool RAWSPEED_READNONE RangeContains(const Tr& r, Tv pos) {
54 if (pos < std::begin(r))
55 return false;
56
57 assert(pos >= std::begin(r));
58 return std::end(r) > pos;
59}
60
61template <typename T>
62constexpr bool RAWSPEED_READNONE RangesOverlap(const T& lhs, const T& rhs) {
63 if (&lhs == &rhs)
64 return true;
65
66 if (std::begin(lhs) == std::begin(rhs))
67 return true;
68
69 const std::pair<const T&, const T&> ordered =
70 std::minmax(lhs, rhs, [](const T& r0, const T& r1) {
71 assert(std::begin(r0) != std::begin(r1));
72 return std::begin(r0) < std::begin(r1);
73 });
74
75 assert(std::begin(ordered.first) < std::begin(ordered.second));
76 return RangeContains(ordered.first, std::begin(ordered.second));
77}
78
79} // namespace rawspeed
assert(dim.area() >=area)
constexpr Range()=default
constexpr Range(T base_, T2 size_)
Definition Range.h:41
std::make_unsigned_t< T > size
Definition Range.h:34
constexpr T RAWSPEED_READNONE end() const
Definition Range.h:45
constexpr T RAWSPEED_READNONE begin() const
Definition Range.h:43
constexpr Range()=default
bool operator<(const Range< T > &lhs, const Range< T > &rhs)
Definition Range.h:48
throw T(buf.data())
constexpr bool RAWSPEED_READNONE RangesOverlap(const T &lhs, const T &rhs)
Definition Range.h:62
constexpr bool RAWSPEED_READNONE RangeContains(const Tr &r, Tv pos)
Definition Range.h:53