40# ifndef TPL_TREE_SNAPSHOT_H
41# define TPL_TREE_SNAPSHOT_H
50# include <type_traits>
72 [[
nodiscard]]
constexpr std::array<char, Ordered_Tree_Snapshot_Magic_Size>
75 std::array<char, Ordered_Tree_Snapshot_Magic_Size>
ret = {};
76 constexpr size_t limit =
81 for (
size_t i = 0; i <
limit; ++i)
89 return std::ifstream(file_path, std::ios::binary).good();
95 namespace fs = std::filesystem;
97 const fs::path path(file_path);
98 if (
not path.has_parent_path())
102 (
void) fs::create_directories(path.parent_path(),
ec);
104 << kind <<
": cannot create directory " << path.parent_path().string()
105 <<
" for snapshot " << file_path <<
" (" <<
ec.message() <<
")";
108 template <
typename Key>
110 const std::string & file_path,
112 const std::uint64_t index)
114 static_assert(std::is_trivially_copyable_v<Key>,
115 "Persistent tree snapshots require trivially copyable keys");
117 std::array<
char,
sizeof(Key)> bytes = {};
118 in.read(bytes.data(), bytes.size());
120 << kind <<
": cannot read key #" << index <<
" from snapshot "
123 return std::bit_cast<Key>(bytes);
126 template <
typename Key>
129 const std::string & file_path,
131 const std::uint64_t index)
133 static_assert(std::is_trivially_copyable_v<Key>,
134 "Persistent tree snapshots require trivially copyable keys");
136 const auto bytes = std::bit_cast<std::array<
char,
sizeof(Key)>>(key);
137 out.write(bytes.data(), bytes.size());
139 << kind <<
": cannot write key #" << index <<
" to snapshot "
145 const std::array<char, Ordered_Tree_Snapshot_Magic_Size> &
expected_magic,
148 const std::string & file_path,
153 << kind <<
": snapshot " << file_path <<
" has an unexpected format";
156 << kind <<
": snapshot " << file_path <<
" uses unsupported version "
160 << kind <<
": snapshot " << file_path <<
" was created for key size "
161 << header.
key_size <<
", but this instantiation expects "
165 << kind <<
": snapshot " << file_path <<
" was created with minimum "
166 <<
"degree " << header.
min_degree <<
", but this instantiation expects "
170 template <
typename Key>
172 const std::string & file_path,
173 const std::array<char, Ordered_Tree_Snapshot_Magic_Size> &
expected_magic,
177 static_assert(std::is_trivially_copyable_v<Key>,
178 "Persistent tree snapshots require trivially copyable keys");
180 std::ifstream
in(file_path, std::ios::binary);
182 << kind <<
": cannot open snapshot " << file_path <<
" for reading";
185 in.read(
reinterpret_cast<char *
>(&header),
sizeof(header));
187 << kind <<
": cannot read snapshot header from " << file_path;
194 for (std::uint64_t i = 0; i < header.
count; ++i)
200 template <
typename Key>
202 const std::string & file_path,
203 const std::array<char, Ordered_Tree_Snapshot_Magic_Size> &
expected_magic,
208 static_assert(std::is_trivially_copyable_v<Key>,
209 "Persistent tree snapshots require trivially copyable keys");
213 std::ofstream
out(file_path, std::ios::binary | std::ios::trunc);
215 << kind <<
": cannot open snapshot " << file_path <<
" for writing";
225 out.write(
reinterpret_cast<const char *
>(&header),
sizeof(header));
227 << kind <<
": cannot write snapshot header to " << file_path;
229 for (
size_t i = 0; i <
keys.size(); ++i)
234 << kind <<
": cannot flush snapshot " << file_path;
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
Simple dynamic array with automatic resizing and functional operations.
void reserve(size_t cap)
Reserves cap cells into the array.
void ordered_tree_validate_header(const Ordered_Tree_Snapshot_Header &header, const std::array< char, Ordered_Tree_Snapshot_Magic_Size > &expected_magic, const std::uint32_t expected_key_size, const std::uint64_t expected_min_degree, const std::string &file_path, const char *kind)
constexpr std::uint32_t Ordered_Tree_Snapshot_Version
Array< Key > load_ordered_tree_snapshot(const std::string &file_path, const std::array< char, Ordered_Tree_Snapshot_Magic_Size > &expected_magic, const std::uint64_t expected_min_degree, const char *kind)
void ensure_ordered_tree_snapshot_parent_dir(const std::string &file_path, const char *kind)
constexpr std::array< char, Ordered_Tree_Snapshot_Magic_Size > ordered_tree_snapshot_magic(const char(&text)[N]) noexcept
void ordered_tree_write_key(std::ostream &out, const Key &key, const std::string &file_path, const char *kind, const std::uint64_t index)
bool ordered_tree_snapshot_exists(const std::string &file_path)
void save_ordered_tree_snapshot(const std::string &file_path, const std::array< char, Ordered_Tree_Snapshot_Magic_Size > &expected_magic, const std::uint64_t expected_min_degree, const Array< Key > &keys, const char *kind)
Key ordered_tree_read_key(std::istream &in, const std::string &file_path, const char *kind, const std::uint64_t index)
constexpr size_t Ordered_Tree_Snapshot_Magic_Size
Main namespace for Aleph-w library functions.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
Dynamic array container with automatic resizing.