Headers move, CMake targets merge, C++ namespaces stay unchanged: - Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant - Functional: Pipeline, Either, Result, Retry - Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval - Math: +Matrix, Numeric, Random, Histogram - Text: Str, Format, Tokenizer, Diff, Encoding, Semver - Data: Json, Csv, Ini - Async: Coro, Concurrency, EventBus, Observable - Time: +Bench - Log: Log facade + Logging framework (facade now header-only, breaking the Stacktrace<->Log link cycle the merge would have created) - System: FileSystem + Process - Configuration: +Argparse - Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail Module system fixes: - OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no longer depends on the alphabetical discovery order of src/ subdirs (Testing -> Text would have been silently dropped). - Every header-only module now has a tracked module.cmake; previously the empty src/<Name> marker dirs were untracked, so a fresh clone would have silently skipped those modules. - VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT. Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files prefixed: ResultBasic etc.). Test count verified identical (831) before and after. README/CLAUDE.md module tables updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
161 lines
5.2 KiB
C++
161 lines
5.2 KiB
C++
#pragma once
|
|
|
|
#include <algorithm>
|
|
#include <cstddef>
|
|
#include <functional>
|
|
#include <span>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace Void::Diff
|
|
{
|
|
|
|
// ── Edit operations ───────────────────────────────────────────────────────────
|
|
|
|
enum class Op { Equal, Insert, Delete };
|
|
|
|
template <typename T>
|
|
struct Edit {
|
|
Op op;
|
|
T value;
|
|
};
|
|
|
|
// ── Myers diff ────────────────────────────────────────────────────────────────
|
|
// Returns the shortest edit script (SES) to transform `a` into `b`.
|
|
// Uses Myers' O(ND) algorithm.
|
|
|
|
template <typename T, typename Eq = std::equal_to<T>>
|
|
[[nodiscard]] std::vector<Edit<T>>
|
|
diff(std::span<const T> a, std::span<const T> b, Eq eq = {})
|
|
{
|
|
const int n = static_cast<int>(a.size());
|
|
const int m = static_cast<int>(b.size());
|
|
const int max_d = n + m;
|
|
|
|
if (max_d == 0) return {};
|
|
|
|
// v[k + max_d] = furthest-reaching x in diagonal k
|
|
std::vector<int> v(2 * max_d + 1, -1);
|
|
// trace[d] = snapshot of v at step d (for backtracking)
|
|
std::vector<std::vector<int>> trace;
|
|
|
|
v[max_d + 1] = 0;
|
|
bool done = false;
|
|
int final_d = 0;
|
|
|
|
for (int d = 0; d <= max_d && !done; ++d) {
|
|
trace.push_back(v);
|
|
for (int k = -d; k <= d; k += 2) {
|
|
int x;
|
|
if (k == -d || (k != d && v[max_d + k - 1] < v[max_d + k + 1]))
|
|
x = v[max_d + k + 1];
|
|
else
|
|
x = v[max_d + k - 1] + 1;
|
|
int y = x - k;
|
|
while (x < n && y < m && eq(a[x], b[y])) { ++x; ++y; }
|
|
v[max_d + k] = x;
|
|
if (x == n && y == m) { final_d = d; done = true; break; }
|
|
}
|
|
}
|
|
|
|
// Backtrack to reconstruct the edit script
|
|
std::vector<Edit<T>> script;
|
|
int x = n, y = m;
|
|
for (int d = final_d; d > 0; --d) {
|
|
const auto& pv = trace[d];
|
|
const int k = x - y;
|
|
int prev_k;
|
|
if (k == -d || (k != d && pv[max_d + k - 1] < pv[max_d + k + 1]))
|
|
prev_k = k + 1;
|
|
else
|
|
prev_k = k - 1;
|
|
const int prev_x = pv[max_d + prev_k];
|
|
const int prev_y = prev_x - prev_k;
|
|
// snake (equal)
|
|
while (x > prev_x && y > prev_y) {
|
|
--x; --y;
|
|
script.push_back({Op::Equal, a[x]});
|
|
}
|
|
if (d > 0) {
|
|
if (x == prev_x) { --y; script.push_back({Op::Insert, b[y]}); }
|
|
else { --x; script.push_back({Op::Delete, a[x]}); }
|
|
}
|
|
x = prev_x; y = prev_y;
|
|
}
|
|
// Remaining snake at start
|
|
while (x > 0 && y > 0) { --x; --y; script.push_back({Op::Equal, a[x]}); }
|
|
|
|
std::reverse(script.begin(), script.end());
|
|
return script;
|
|
}
|
|
|
|
// Convenience overload for vectors
|
|
template <typename T, typename Eq = std::equal_to<T>>
|
|
[[nodiscard]] std::vector<Edit<T>>
|
|
diff(const std::vector<T>& a, const std::vector<T>& b, Eq eq = {})
|
|
{
|
|
return diff<T, Eq>(std::span<const T>{a}, std::span<const T>{b}, eq);
|
|
}
|
|
|
|
// ── string diff ───────────────────────────────────────────────────────────────
|
|
// Line-level diff on strings split by newline.
|
|
// LineDiff is Edit<string> — count_op and apply work on it directly.
|
|
|
|
using LineDiff = Edit<std::string>;
|
|
|
|
[[nodiscard]] inline std::vector<LineDiff>
|
|
line_diff(std::string_view a, std::string_view b)
|
|
{
|
|
auto split = [](std::string_view s) {
|
|
std::vector<std::string> lines;
|
|
std::size_t pos = 0;
|
|
while (pos <= s.size()) {
|
|
const auto nl = s.find('\n', pos);
|
|
lines.emplace_back(s.substr(pos, nl == std::string_view::npos ? nl : nl - pos));
|
|
if (nl == std::string_view::npos) break;
|
|
pos = nl + 1;
|
|
}
|
|
return lines;
|
|
};
|
|
|
|
return diff(split(a), split(b));
|
|
}
|
|
|
|
// ── helpers ───────────────────────────────────────────────────────────────────
|
|
|
|
// Count edits by type
|
|
template <typename T>
|
|
[[nodiscard]] std::size_t count_op(const std::vector<Edit<T>>& edits, Op op)
|
|
{
|
|
return static_cast<std::size_t>(std::count_if(edits.begin(), edits.end(),
|
|
[op](const Edit<T>& e) { return e.op == op; }));
|
|
}
|
|
|
|
// Reconstruct the "b" sequence from an edit script applied to "a"
|
|
template <typename T>
|
|
[[nodiscard]] std::vector<T> apply(const std::vector<Edit<T>>& edits)
|
|
{
|
|
std::vector<T> out;
|
|
for (const auto& e : edits)
|
|
if (e.op != Op::Delete) out.push_back(e.value);
|
|
return out;
|
|
}
|
|
|
|
// Format a unified-diff-style patch for string line diffs
|
|
[[nodiscard]] inline std::string format_patch(const std::vector<LineDiff>& edits)
|
|
{
|
|
std::string out;
|
|
for (const auto& e : edits) {
|
|
switch (e.op) {
|
|
case Op::Equal: out += ' '; break;
|
|
case Op::Insert: out += '+'; break;
|
|
case Op::Delete: out += '-'; break;
|
|
}
|
|
out += e.value + '\n';
|
|
}
|
|
return out;
|
|
}
|
|
|
|
} // namespace Void::Diff
|