GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Adler32.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_FORMATS_ADLER32_HPP
5#define GREM_CORE_FORMATS_ADLER32_HPP
6
8
12
13#include <cstddef> // std::size_t
14#include <functional> // std::hash
15
16namespace grem {
17
18class alignas(4) Adler32 {
19public:
20 constexpr Adler32() noexcept = default;
21
22 constexpr explicit Adler32(uint32_t value) noexcept
23 : s1(static_cast<uint16_t>(value & 0xFFFF))
24 , s2(static_cast<uint16_t>(value >> 16)) {}
25
26 constexpr explicit Adler32(Span<const byte> bytes) noexcept {
27 append(bytes);
28 }
29
30 constexpr explicit Adler32(StringView chars) noexcept {
31 append(chars);
32 }
33
34 constexpr explicit operator uint32_t() const noexcept {
35 return static_cast<uint32_t>((uint32_t{s2} << 16) | uint32_t{s1});
36 }
37
38 constexpr bool operator==(const Adler32&) const noexcept = default;
39
40 constexpr void append(Span<const byte> bytes) noexcept {
41 for (const byte byte : bytes) {
42 s1 = static_cast<uint16_t>((uint32_t{s1} + uint32_t{bit_cast<uint8_t>(byte)}) % 65521);
43 s2 = static_cast<uint16_t>((uint32_t{s2} + uint32_t{s1}) % 65521);
44 }
45 }
46
47 constexpr void append(StringView chars) noexcept {
48 for (const char ch : chars) {
49 s1 = static_cast<uint16_t>((uint32_t{s1} + uint32_t{bit_cast<uint8_t>(ch)}) % 65521);
50 s2 = static_cast<uint16_t>((uint32_t{s2} + uint32_t{s1}) % 65521);
51 }
52 }
53
54 constexpr Adler32& operator+=(Span<const byte> bytes) noexcept {
55 append(bytes);
56 return *this;
57 }
58
59 constexpr Adler32& operator+=(StringView chars) noexcept {
60 append(chars);
61 return *this;
62 }
63
64 [[nodiscard]] friend constexpr Adler32 operator+(Adler32 a, Span<const byte> bytes) noexcept {
65 return a += bytes;
66 }
67
68 [[nodiscard]] friend constexpr Adler32 operator+(Adler32 a, StringView chars) noexcept {
69 return a += chars;
70 }
71
72 template <typename Stream>
73 friend constexpr Stream& operator<<(Stream& stream, Adler32 adler32) {
74 return stream << static_cast<uint32_t>(adler32);
75 }
76
77 template <typename Stream>
78 friend constexpr Stream& operator>>(Stream& stream, Adler32& adler32) {
79 uint32_t value{};
80 stream >> value;
81 adler32 = Adler32{value};
82 return stream;
83 }
84
85private:
86 uint16_t s1 = 1;
87 uint16_t s2 = 0;
88};
89
90} // namespace grem
91
92template <>
93struct std::hash<grem::Adler32> {
94 [[nodiscard]] std::size_t operator()(const grem::Adler32& adler32) const {
95 return hasher(static_cast<uint32_t>(adler32));
96 }
97
98private:
99 std::hash<uint32_t> hasher;
100};
101
102#endif
Definition Adler32.hpp:18
constexpr Adler32(StringView chars) noexcept
Definition Adler32.hpp:30
constexpr Adler32 & operator+=(StringView chars) noexcept
Definition Adler32.hpp:59
constexpr bool operator==(const Adler32 &) const noexcept=default
friend constexpr Stream & operator<<(Stream &stream, Adler32 adler32)
Definition Adler32.hpp:73
friend constexpr Adler32 operator+(Adler32 a, Span< const byte > bytes) noexcept
Definition Adler32.hpp:64
constexpr void append(StringView chars) noexcept
Definition Adler32.hpp:47
constexpr void append(Span< const byte > bytes) noexcept
Definition Adler32.hpp:40
constexpr Adler32() noexcept=default
friend constexpr Adler32 operator+(Adler32 a, StringView chars) noexcept
Definition Adler32.hpp:68
friend constexpr Stream & operator>>(Stream &stream, Adler32 &adler32)
Definition Adler32.hpp:78
constexpr Adler32(Span< const byte > bytes) noexcept
Definition Adler32.hpp:26
constexpr Adler32 & operator+=(Span< const byte > bytes) noexcept
Definition Adler32.hpp:54
Definition Span.hpp:146
Definition aliases.hpp:9
StringViewBase< char, std::char_traits< char > > StringView
Definition StringView.hpp:16
std::size_t operator()(const grem::Adler32 &adler32) const
Definition Adler32.hpp:94