Skip to main content

algorithms.ixx File

Core algorithms and hash functions for the helios engine. More...

Included Headers

#include <cstdint> #include <string>

Namespaces Index

namespacehelios
namespacecore

Core utilities shared across the helios engine. More...

namespacealgorithms

Description

Core algorithms and hash functions for the helios engine.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file algorithms.ixx
3 * @brief Core algorithms and hash functions for the helios engine.
4 */
5module;
6
7#include <cstdint>
8#include <string>
9
10export module helios.core.algorithms;
11
12
13
14export namespace helios::core::algorithms {
15
16 /**
17 * @brief Computes a 32-bit FNV-1a hash from a string.
18 *
19 * @details Implements the Fowler–Noll–Vo hash function (FNV-1a variant),
20 * a fast, non-cryptographic hash suitable for hash tables and identifier
21 * generation. The function is `constexpr`, enabling compile-time hashing
22 * of string literals.
23 *
24 * ## Algorithm
25 *
26 * 1 Initialize hash with FNV offset basis (2166136261)
27 * 2 For each byte: XOR with hash, then multiply by FNV prime (16777619)
28 *
29 * ## Usage
30 *
31 * ```cpp
32 * // Compile-time hash for type-safe IDs
33 * constexpr uint32_t id = fnv1a_hash("enemy_spawn");
34 *
35 * // Used internally by strongly-typed ID constructors
36 * constexpr GameObjectPoolId POOL{"bullets"}; // calls fnv1a_hash
37 * ```
38 *
39 * @param str The string view to hash.
40 *
41 * @return 32-bit hash value.
42 *
43 * @note This is not a cryptographic hash. Do not use for security purposes.
44 *
45 * @see https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
46 * @see GameObjectPoolId
47 * @see SpawnProfileId
48 * @see SpawnRuleId
49 */
50 [[nodiscard]] constexpr uint32_t fnv1a_hash(const std::string_view str) noexcept {
51 uint32_t hash = 2166136261U;
52 for (char c: str) {
53 hash ^= static_cast<uint8_t>(c);
54 hash *= 16777619U;
55 }
56 return hash;
57 }
58
59
60}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.