JaffarPlus
High-performance best-first search optimizer for tool-assisted speedruns
Loading...
Searching...
No Matches
inputHistoryFactory.hpp
Go to the documentation of this file.
1#pragma once
2
11#include "inputHistory.hpp"
12#include "inputHistoryNone.hpp"
13#include "inputHistoryRaw.hpp"
14#include "inputHistoryTrie.hpp"
15#include <jaffarCommon/exceptions.hpp>
16#include <jaffarCommon/json.hpp>
17
18namespace jaffarPlus
19{
20
21namespace inputHistory
22{
23
26__INLINE__ std::string getType(const nlohmann::json& config) { return jaffarCommon::json::getString(config, "Type"); }
27
35__INLINE__ std::shared_ptr<void> createSharedBacking(const nlohmann::json& config, const uint32_t numShards)
36{
37 if (getType(config) == "Trie") return std::make_shared<SequenceInputTrie>(numShards);
38 return nullptr;
39}
40
44__INLINE__ size_t getSharedBackingMaxMemoryBytes(const std::shared_ptr<void>& backing)
45{
46 if (backing == nullptr) return 0;
47 return static_cast<SequenceInputTrie*>(backing.get())->getMaxMemoryBytes();
48}
49
53__INLINE__ size_t getSharedBackingApproxMemoryBytes(const std::shared_ptr<void>& backing)
54{
55 if (backing == nullptr) return 0;
56 return static_cast<SequenceInputTrie*>(backing.get())->getApproxMemoryBytes();
57}
58
61__INLINE__ bool isSharedBackingExhausted(const std::shared_ptr<void>& backing)
62{
63 if (backing == nullptr) return false;
64 return static_cast<SequenceInputTrie*>(backing.get())->isExhausted();
65}
66
75__INLINE__ std::unique_ptr<InputHistory> create(const nlohmann::json& config, const uint32_t maxInputIndex, const uint32_t numShards, const uint32_t shardId,
76 const std::shared_ptr<void>& backing)
77{
78 // Strictly consume the config: pop the recognized keys, then reject any leftover (unknown) key.
79 auto cfg = config;
80 const auto type = jaffarCommon::json::popString(cfg, "Type");
81 std::unique_ptr<InputHistory> result;
82 if (type == "None")
83 result = std::make_unique<InputHistoryNone>();
84 else if (type == "Raw")
85 result = std::make_unique<InputHistoryRaw>(maxInputIndex, jaffarCommon::json::popNumber<uint32_t>(cfg, "Max Size"));
86 else if (type == "Trie")
87 result = std::make_unique<InputHistoryTrie>(static_cast<SequenceInputTrie*>(backing.get()), shardId, numShards - 1, maxInputIndex,
88 jaffarCommon::json::popNumber<uint32_t>(cfg, "Max Size"));
89 else
90 JAFFAR_THROW_LOGIC("Unrecognized 'Store Input History' Type: '%s' (expected None, Raw or Trie)", type.c_str());
91 jaffarCommon::json::checkEmpty(cfg, "Runner Configuration > Store Input History");
92 return result;
93}
94
95} // namespace inputHistory
96
97} // namespace jaffarPlus
size_t getSharedBackingMaxMemoryBytes(const std::shared_ptr< void > &backing)
Hard upper bound (bytes) on the shared backing's memory: the trie's node-storage ceiling for the "Tri...
size_t getSharedBackingApproxMemoryBytes(const std::shared_ptr< void > &backing)
Current (approximate) live memory of the shared backing: the trie's node-storage footprint for the "T...
bool isSharedBackingExhausted(const std::shared_ptr< void > &backing)
True if the shared backing (the trie) has hit its hard node-storage ceiling.
std::shared_ptr< void > createSharedBacking(const nlohmann::json &config, const uint32_t numShards)
Creates the shared backing for the configured strategy (the trie for "Trie"; nullptr otherwise).
std::string getType(const nlohmann::json &config)
The configured strategy name ("None" | "Raw" | "Trie").
std::unique_ptr< InputHistory > create(const nlohmann::json &config, const uint32_t maxInputIndex, const uint32_t numShards, const uint32_t shardId, const std::shared_ptr< void > &backing)
Creates one runner's input-history instance bound to backing.
Input-history strategy that records nothing but the step counter.
Input-history strategy that stores the full bit-packed input sequence in every state.
Input-history strategy that stores each state's path as a single node id into a shared,...
jaffarCommon::sequenceTrie::SequenceTrie< uint16_t > SequenceInputTrie
The shared trie type backing every InputHistoryTrie instance of one search.
Abstract interface for how a search remembers the sequence of inputs ("path") that produced each stat...