Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ add_library(
INTERFACE
include/hash23/hash23.h
include/hash23/sha2_512.h
include/hash23/fnv_1.h
include/hash23/fnv_1a.h
)
add_library(hash23::hash23 ALIAS hash23)

Expand Down
66 changes: 66 additions & 0 deletions include/hash23/fnv_1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// Created by Rene Windegger on 22/03/2026.
//

#pragma once

#include <span>
#include <vector>
Comment thread
rwindegger marked this conversation as resolved.
Outdated

namespace hash23 {
class fnv_1 {
private:
std::vector<std::uint8_t> buffer_;

[[nodiscard]] static consteval std::size_t prime() {
if constexpr (sizeof(std::size_t) == 4) {
return 0x01000193uz;
} else {
return 0x00000100000001b3uz;
}
return 0uz;
}
Comment thread
rwindegger marked this conversation as resolved.
Outdated

[[nodiscard]] static consteval std::size_t offset_basis() {
if constexpr (sizeof(std::size_t) == 4) {
return 0x811c9dc5uz;
} else {
return 0xcbf29ce484222325uz;
}
return 0uz;
Comment thread
rwindegger marked this conversation as resolved.
Outdated
}

template<typename T>
requires std::ranges::contiguous_range<T>
constexpr void update(T const &data) {
auto const start_offset = buffer_.size();
buffer_.resize(start_offset + data.size());
std::copy_n(data.data(), data.size(), buffer_.data() + start_offset);
}

[[nodiscard]] constexpr std::size_t finalize() const {
std::size_t hash = offset_basis();

for (auto const &byte : buffer_) {
hash *= prime();
hash ^= byte;
}

return hash;
}
Comment thread
rwindegger marked this conversation as resolved.
public:
template<typename T>
requires std::ranges::contiguous_range<T>
[[nodiscard]] static constexpr std::size_t calculate(T const &data) {
fnv_1 r;
r.update(data);
return r.finalize();
Comment thread
rwindegger marked this conversation as resolved.
}

template<std::size_t N>
[[nodiscard]] static constexpr std::size_t calculate(char const (&str)[N]) {
auto const block = std::span(str, N - 1);
return calculate(block);
}
};
}
66 changes: 66 additions & 0 deletions include/hash23/fnv_1a.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// Created by Rene Windegger on 22/03/2026.
//

#pragma once

#include <span>
#include <vector>

Copilot AI Mar 22, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This header uses std::uint8_t, std::copy_n, and std::ranges::contiguous_range but only includes and . This will fail to compile on stricter standard library implementations; please add the required standard headers explicitly (e.g., , , , and any others actually used).

Suggested change
#include <vector>
#include <vector>
#include <cstdint>
#include <cstddef>
#include <algorithm>
#include <ranges>

Copilot uses AI. Check for mistakes.

namespace hash23 {
class fnv_1a {
private:
std::vector<std::uint8_t> buffer_;

[[nodiscard]] static consteval std::size_t prime() {
if constexpr (sizeof(std::size_t) == 4) {
return 0x01000193uz;
} else {
return 0x00000100000001b3uz;
}
return 0uz;
}
Comment thread
rwindegger marked this conversation as resolved.
Outdated

[[nodiscard]] static consteval std::size_t offset_basis() {
if constexpr (sizeof(std::size_t) == 4) {
return 0x811c9dc5uz;
} else {
return 0xcbf29ce484222325uz;
}
Comment thread
rwindegger marked this conversation as resolved.
return 0uz;
}
Comment thread
rwindegger marked this conversation as resolved.
Outdated

template<typename T>
requires std::ranges::contiguous_range<T>
constexpr void update(T const &data) {
auto const start_offset = buffer_.size();
buffer_.resize(start_offset + data.size());
std::copy_n(data.data(), data.size(), buffer_.data() + start_offset);
}

[[nodiscard]] constexpr std::size_t finalize() const {
std::size_t hash = offset_basis();

for (auto const &byte : buffer_) {
hash ^= byte;
hash *= prime();
}

return hash;
}
Comment thread
rwindegger marked this conversation as resolved.
public:
template<typename T>
requires std::ranges::contiguous_range<T>
[[nodiscard]] static constexpr std::size_t calculate(T const &data) {
fnv_1a r;
r.update(data);
return r.finalize();
}
Comment thread
rwindegger marked this conversation as resolved.

template<std::size_t N>
[[nodiscard]] static constexpr std::size_t calculate(char const (&str)[N]) {
auto const block = std::span(str, N - 1);
return calculate(block);
}
};
}
2 changes: 2 additions & 0 deletions include/hash23/hash23.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

#pragma once

#include <hash23/fnv_1.h>
#include <hash23/fnv_1a.h>
#include <hash23/sha2_512.h>
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ add_executable(
hash23_tests
main.cpp
sha2_512_tests.cpp
fnv_1_tests.cpp
fnv_1a_tests.cpp
)

if(MSVC)
Expand Down
14 changes: 14 additions & 0 deletions tests/fnv_1_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Created by Rene Windegger on 22/03/2026.
//

#include <gtest/gtest.h>
#include <hash23/hash23.h>

namespace {
TEST(fnv_1, single_block_test) {
auto const actual = hash23::fnv_1::calculate("Hello, World!");
constexpr std::size_t expected = 0x7b5ea4c513c14886uz;
EXPECT_EQ(expected, actual);
Comment thread
rwindegger marked this conversation as resolved.
Outdated
}
Comment thread
rwindegger marked this conversation as resolved.
}
14 changes: 14 additions & 0 deletions tests/fnv_1a_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Created by Rene Windegger on 22/03/2026.
//

#include <gtest/gtest.h>
#include <hash23/hash23.h>

namespace {
TEST(fnv_1a, single_block_test) {
auto const actual = hash23::fnv_1a::calculate("Hello, World!");
constexpr std::size_t expected = 0x6ef05bd7cc857c54uz;
EXPECT_EQ(expected, actual);
Comment thread
rwindegger marked this conversation as resolved.
Outdated
}
Comment thread
rwindegger marked this conversation as resolved.
}
Loading