Donner
C++20 SVG rendering library
Loading...
Searching...
No Matches
CompileTimeMap.h File Reference

Defines a constexpr-friendly associative container built on top of a perfect-hash layout for fixed key sets. More...

#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <limits>
#include <optional>
#include <string_view>
#include <type_traits>
#include <utility>
#include "donner/base/Utils.h"
Include dependency graph for CompileTimeMap.h:

Classes

struct  donner::CompileTimeMapTables< N >
 Perfect-hash metadata used to resolve keys into storage slots. More...
struct  donner::CompileTimeMapDiagnostics
 Diagnostics describing how a CompileTimeMap was constructed. More...
class  donner::CompileTimeMap< Key, Value, N, Hasher, KeyEqual >
 Compile-time associative container backed by a perfect hash layout. More...
struct  donner::detail::CompileTimeMapResult< Key, Value, N, Hasher, KeyEqual >
 Contains the constructed map and associated build status. More...

Namespaces

namespace  donner
 Top-level Donner namespace, which is split into different sub-namespaces such as donner::svg and donner::css.

Macros

#define makeCompileTimeMap(...)
 Primary API for building a CompileTimeMap with compile-time error checking.

Enumerations

enum class  donner::CompileTimeMapStatus {
  donner::kOk ,
  donner::kUsingFallbackHash ,
  donner::kDuplicateKey ,
  donner::kSeedSearchFailed ,
  donner::kConstexprHashUnsupported
}
 Indicates the result of building a CompileTimeMap. More...

Functions

constexpr std::size_t donner::mixHash (std::size_t baseHash, std::uint32_t seed)
template<typename Key>
constexpr bool donner::supportsConstexprHash ()
template<typename Key>
constexpr std::size_t donner::constexprHashValue (const Key &key)
template<typename Key, std::size_t N, typename KeyEqual>
constexpr bool donner::hasDuplicateKeys (const std::array< Key, N > &keys, KeyEqual keyEqual)
 Returns true when the provided keys contain duplicates.
template<typename Key, typename Value, std::size_t N, typename Hasher = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
constexpr CompileTimeMapResult< Key, Value, N, Hasher, KeyEqual > donner::detail::makeCompileTimeMapWithDiagnostics (const std::array< std::pair< Key, Value >, N > &entries, Hasher hasher=Hasher{}, KeyEqual keyEqual=KeyEqual{})
 Builds a CompileTimeMap from an array of key/value pairs with full diagnostics. Most users should use the wrapper makeCompileTimeMap instead.

Detailed Description

Defines a constexpr-friendly associative container built on top of a perfect-hash layout for fixed key sets.


Class Documentation

◆ donner::CompileTimeMapTables

struct donner::CompileTimeMapTables
template<std::size_t N>
struct donner::CompileTimeMapTables< N >

Perfect-hash metadata used to resolve keys into storage slots.

Collaboration diagram for donner::CompileTimeMapTables< N >:
[legend]
Class Members
uint32_t bucketCount = 0 Number of buckets used by the first-level table; zero enables fallback lookup.
array< uint32_t, N > primary {} First-level table storing direct indices or bucket seeds.
array< uint32_t, N > secondary {} Secondary slot table addressed with the bucket seed and key hash.

◆ donner::CompileTimeMapDiagnostics

struct donner::CompileTimeMapDiagnostics

Diagnostics describing how a CompileTimeMap was constructed.

Class Members
bool constexprHashSupported = true Whether constexpr hashing was available for the provided key type.
uint32_t failedBucket = kEmptySlot Index of the bucket that failed to place, or kEmptySlot when successful.
uint32_t maxBucketSize = 0 Largest bucket size observed while building the table.
uint32_t seedAttempts = 0 Total seed attempts across all buckets.

Macro Definition Documentation

◆ makeCompileTimeMap

#define makeCompileTimeMap ( ...)
Value:
[]() constexpr { \
constexpr auto _compiletime_map_result = \
::donner::detail::makeCompileTimeMapWithDiagnostics(__VA_ARGS__); \
static_assert(_compiletime_map_result.status == ::donner::CompileTimeMapStatus::kOk, \
"CompileTimeMap construction failed. Check for duplicate keys or use " \
"detail::makeCompileTimeMapWithDiagnostics for diagnostics."); \
return _compiletime_map_result.map; \
}()
@ kOk
Perfect-hash tables were constructed successfully.
Definition CompileTimeMap.h:85

Primary API for building a CompileTimeMap with compile-time error checking.

Usage:

static constexpr auto kColors = makeCompileTimeMap(std::to_array<std::pair<...>>({
{"key1"sv, value1},
{"key2"sv, value2},
#define makeCompileTimeMap(...)
Primary API for building a CompileTimeMap with compile-time error checking.
Definition CompileTimeMap.h:427

}));