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.3 KiB
C++
161 lines
5.3 KiB
C++
#pragma once
|
|
|
|
#include <algorithm>
|
|
#include <cstddef>
|
|
#include <optional>
|
|
#include <stdexcept>
|
|
#include <unordered_map>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace Void::Histogram
|
|
{
|
|
|
|
// ── Histogram<K> ──────────────────────────────────────────────────────────────
|
|
// Frequency counter for discrete keys.
|
|
|
|
template <typename K>
|
|
class Histogram
|
|
{
|
|
public:
|
|
// Increment the count for key by n (default 1).
|
|
void add(const K& key, std::size_t n = 1)
|
|
{
|
|
counts_[key] += n;
|
|
total_ += n;
|
|
}
|
|
|
|
// Remove a key entirely.
|
|
bool erase(const K& key)
|
|
{
|
|
const auto it = counts_.find(key);
|
|
if (it == counts_.end()) return false;
|
|
total_ -= it->second;
|
|
counts_.erase(it);
|
|
return true;
|
|
}
|
|
|
|
// Get count for key (0 if not present).
|
|
[[nodiscard]] std::size_t count(const K& key) const
|
|
{
|
|
const auto it = counts_.find(key);
|
|
return it != counts_.end() ? it->second : 0;
|
|
}
|
|
|
|
// Frequency as a fraction of total (0 if total == 0).
|
|
[[nodiscard]] double frequency(const K& key) const
|
|
{
|
|
if (total_ == 0) return 0.0;
|
|
return static_cast<double>(count(key)) / static_cast<double>(total_);
|
|
}
|
|
|
|
// True if key has been observed.
|
|
[[nodiscard]] bool contains(const K& key) const { return counts_.count(key) > 0; }
|
|
|
|
// Most common key (nullopt if empty).
|
|
[[nodiscard]] std::optional<K> mode() const
|
|
{
|
|
if (counts_.empty()) return std::nullopt;
|
|
const auto it = std::max_element(counts_.begin(), counts_.end(),
|
|
[](const auto& a, const auto& b) { return a.second < b.second; });
|
|
return it->first;
|
|
}
|
|
|
|
// Least common key (nullopt if empty).
|
|
[[nodiscard]] std::optional<K> least_common() const
|
|
{
|
|
if (counts_.empty()) return std::nullopt;
|
|
const auto it = std::min_element(counts_.begin(), counts_.end(),
|
|
[](const auto& a, const auto& b) { return a.second < b.second; });
|
|
return it->first;
|
|
}
|
|
|
|
// All key-count pairs sorted by count descending.
|
|
[[nodiscard]] std::vector<std::pair<K, std::size_t>> top_k(std::size_t k) const
|
|
{
|
|
std::vector<std::pair<K, std::size_t>> v{counts_.begin(), counts_.end()};
|
|
const std::size_t n = std::min(k, v.size());
|
|
std::partial_sort(v.begin(), v.begin() + static_cast<std::ptrdiff_t>(n), v.end(),
|
|
[](const auto& a, const auto& b) { return a.second > b.second; });
|
|
v.resize(n);
|
|
return v;
|
|
}
|
|
|
|
// Total observations.
|
|
[[nodiscard]] std::size_t total() const noexcept { return total_; }
|
|
[[nodiscard]] std::size_t distinct() const noexcept { return counts_.size(); }
|
|
[[nodiscard]] bool empty() const noexcept { return counts_.empty(); }
|
|
|
|
void clear() noexcept { counts_.clear(); total_ = 0; }
|
|
|
|
// Merge another histogram into this one.
|
|
void merge(const Histogram& other)
|
|
{
|
|
for (const auto& [k, v] : other.counts_) { counts_[k] += v; }
|
|
total_ += other.total_;
|
|
}
|
|
|
|
[[nodiscard]] const std::unordered_map<K, std::size_t>& counts() const noexcept
|
|
{
|
|
return counts_;
|
|
}
|
|
|
|
private:
|
|
std::unordered_map<K, std::size_t> counts_;
|
|
std::size_t total_{0};
|
|
};
|
|
|
|
// ── FixedBinHistogram ─────────────────────────────────────────────────────────
|
|
// Numeric histogram with fixed-width equal-width bins over [min, max).
|
|
|
|
class FixedBinHistogram
|
|
{
|
|
public:
|
|
// n_bins bins covering [min_val, max_val).
|
|
FixedBinHistogram(double min_val, double max_val, std::size_t n_bins)
|
|
: min_{min_val}, max_{max_val}, bins_(n_bins, 0)
|
|
{
|
|
if (n_bins == 0) throw std::invalid_argument{"n_bins must be > 0"};
|
|
if (min_ >= max_) throw std::invalid_argument{"min must be < max"};
|
|
width_ = (max_ - min_) / static_cast<double>(n_bins);
|
|
}
|
|
|
|
// Record a value. Values outside [min, max) are clamped to edge bins.
|
|
void add(double v)
|
|
{
|
|
std::size_t idx;
|
|
if (v <= min_) {
|
|
idx = 0;
|
|
} else if (v >= max_) {
|
|
idx = bins_.size() - 1;
|
|
} else {
|
|
idx = static_cast<std::size_t>((v - min_) / width_);
|
|
if (idx >= bins_.size()) idx = bins_.size() - 1;
|
|
}
|
|
++bins_[idx];
|
|
++total_;
|
|
}
|
|
|
|
[[nodiscard]] std::size_t bin_count(std::size_t idx) const
|
|
{
|
|
if (idx >= bins_.size()) throw std::out_of_range{"bin index out of range"};
|
|
return bins_[idx];
|
|
}
|
|
|
|
[[nodiscard]] double bin_low(std::size_t idx) const noexcept { return min_ + width_ * static_cast<double>(idx); }
|
|
[[nodiscard]] double bin_high(std::size_t idx) const noexcept { return bin_low(idx) + width_; }
|
|
|
|
[[nodiscard]] std::size_t n_bins() const noexcept { return bins_.size(); }
|
|
[[nodiscard]] std::size_t total() const noexcept { return total_; }
|
|
[[nodiscard]] double width() const noexcept { return width_; }
|
|
|
|
void clear() noexcept { for (auto& b : bins_) b = 0; total_ = 0; }
|
|
|
|
private:
|
|
double min_, max_, width_;
|
|
std::vector<std::size_t> bins_;
|
|
std::size_t total_{0};
|
|
};
|
|
|
|
} // namespace Void::Histogram
|