#pragma once #include #include #include #include #include #include #include namespace Void::Histogram { // ── Histogram ────────────────────────────────────────────────────────────── // Frequency counter for discrete keys. template 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(count(key)) / static_cast(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 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 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> top_k(std::size_t k) const { std::vector> v{counts_.begin(), counts_.end()}; const std::size_t n = std::min(k, v.size()); std::partial_sort(v.begin(), v.begin() + static_cast(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& counts() const noexcept { return counts_; } private: std::unordered_map 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(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((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(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 bins_; std::size_t total_{0}; }; } // namespace Void::Histogram