CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Range.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
12
14
15#include <algorithm>
16#include <cstdint>
17
18namespace CeresEngine::inline Math {
19
20 template<typename OffsetT = UInt64, typename LengthT = OffsetT> struct TRange final {
23 OffsetT offset = 0;
24
27 LengthT length = 0;
28
30 constexpr TRange() noexcept = default;
31
33 constexpr TRange(OffsetT offset, LengthT length) noexcept : offset(offset), length(length) {}
34
35 constexpr TRange(const TRange&) noexcept = default;
36 constexpr TRange& operator=(const TRange&) noexcept = default;
37
39 [[nodiscard]] constexpr bool operator[](OffsetT off) const noexcept;
40
42 [[nodiscard]] constexpr explicit operator bool() const noexcept;
43
45 [[nodiscard]] constexpr bool empty() const noexcept;
46
48 constexpr void clear() noexcept;
49
50 [[nodiscard]] constexpr OffsetT begin() const noexcept { return offset; }
51 [[nodiscard]] constexpr OffsetT end() const noexcept { return offset + length; }
52 };
53
54 template<typename OffsetT, typename LengthT> constexpr bool TRange<OffsetT, LengthT>::operator[](OffsetT off) const noexcept {
55 return off >= offset && off < (offset + length);
56 }
57
58 template<typename OffsetT, typename LengthT> constexpr TRange<OffsetT, LengthT>::operator bool() const noexcept { return length != 0; }
59 template<typename OffsetT, typename LengthT> constexpr bool TRange<OffsetT, LengthT>::empty() const noexcept { return length == 0; }
60 template<typename OffsetT, typename LengthT> constexpr void TRange<OffsetT, LengthT>::clear() noexcept {
61 offset = 0;
62 length = 0;
63 }
64
71 template<typename OffsetT, typename LengthT>
73 OffsetT start = std::max<OffsetT>(a.offset, b.offset);
74 OffsetT end = std::min<OffsetT>(a.offset + a.length, b.offset + b.length);
75 if(start > end) { // no intersection
77 }
78
79 LengthT length = end - start;
80 return TRange<OffsetT, LengthT>(start, length);
81 }
82
89 template<typename OffsetT, typename LengthT> [[nodiscard]] constexpr bool operator==(TRange<OffsetT, LengthT> a, TRange<OffsetT, LengthT> b) noexcept {
90 return a.offset == b.offset && a.length == b.length;
91 }
92
99 template<typename OffsetT, typename LengthT> [[nodiscard]] constexpr bool operator!=(TRange<OffsetT, LengthT> a, TRange<OffsetT, LengthT> b) noexcept {
100 return a.offset != b.offset || a.length != b.length;
101 }
102
109 template<typename OffsetT, typename LengthT>
111 return offset < range.offset;
112 }
113
120 template<typename OffsetT, typename LengthT>
122 return offset < range || range[offset];
123 }
124
131 template<typename OffsetT, typename LengthT>
133 return offset >= (range.offset + range.length);
134 }
135
142 template<typename OffsetT, typename LengthT>
144 return offset > range || range[offset];
145 }
146
147 extern template struct CE_SCRIPT_EXPORT(name = "RangeFloat") TRange<float>;
148 extern template struct CE_SCRIPT_EXPORT(name = "Range") TRange<double>;
149 extern template struct TRange<Int8>;
150 extern template struct TRange<UInt8>;
151 extern template struct TRange<Int16>;
153 extern template struct TRange<Int32>;
157
158 using Range = TRange<double>;
161
162} // namespace CeresEngine::inline Math
163
164template<typename Offset, typename Length> struct std::hash<CeresEngine::Math::TRange<Offset, Length>> {
165 using Type = CeresEngine::Math::TRange<Offset, Length>;
166 constexpr size_t operator()(const Type& obj) const { return CeresEngine::hash(obj.offset, obj.length); }
167};
#define CE_SCRIPT_EXPORT(...)
The CE_SCRIPT_EXPORT macro marks a class or method as exportable and available in scripting environme...
Definition Macros.hpp:247
Definition Application.hpp:19
std::uint64_t UInt64
Definition DataTypes.hpp:26
auto range()
Returns an iterator that increases it's value from 0 to end by 1 for each step.
Definition Iterator.hpp:350
constexpr bool operator<(const Optional< A > &lhs, const Optional< B > &rhs)
Definition Optional.hpp:162
bool operator!=(const ShortAllocator< T, N, A1 > &x, const ShortAllocator< U, M, A2 > &y) noexcept
Definition Allocator.hpp:416
std::int32_t Int32
Definition DataTypes.hpp:21
std::uint16_t UInt16
Definition DataTypes.hpp:20
constexpr bool operator>=(const Optional< A > &lhs, const Optional< B > &rhs)
Definition Optional.hpp:157
std::uint8_t UInt8
Definition DataTypes.hpp:17
bool operator==(const ShortAllocator< T, N, A1 > &x, const ShortAllocator< U, M, A2 > &y) noexcept
Definition Allocator.hpp:411
std::int64_t Int64
Definition DataTypes.hpp:24
constexpr bool operator>(const Optional< A > &lhs, const Optional< B > &rhs)
Definition Optional.hpp:152
std::uint32_t UInt32
Definition DataTypes.hpp:23
constexpr Byte operator&(const Byte left, const Byte right) noexcept
Definition DataTypes.hpp:63
std::int8_t Int8
Definition DataTypes.hpp:15
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
std::int16_t Int16
Definition DataTypes.hpp:18
constexpr bool operator<=(const Optional< A > &lhs, const Optional< B > &rhs)
Definition Optional.hpp:167
Definition Angle.hpp:20
Definition Span.hpp:668
Definition Range.hpp:20
constexpr TRange & operator=(const TRange &) noexcept=default
constexpr OffsetT end() const noexcept
Definition Range.hpp:51
constexpr TRange(const TRange &) noexcept=default