Docs
Loading...
Searching...
No Matches
binary_cache.hpp
Go to the documentation of this file.
1#pragma once
2
7
9#include <vector>
10
16
17class BinaryCache : public ClusterInfo {
18public:
19 // Important to declare the struct useful.
20 // HINT: If the name changed modified the file which uses it accordingly.
26 struct ClusterStats {
27 int binary_sum = 0;
28 int n = 0;
29 };
30
31private:
32 std::vector<ClusterStats> cluster_stats;
33
34public:
35 const Eigen::VectorXi binary_covariates;
36
37 BinaryCache(const Eigen::VectorXi &allocations_ref, const Eigen::VectorXi &binary_covariates)
39
40 const int K = allocations_ref.maxCoeff() + 1;
41 recompute(K > 0 ? K : 0, allocations_ref);
42 }
43
51 void set_allocation(int index, int cluster, int old_cluster) override;
52
58 inline ClusterStats get_cluster_stats(int cluster) const { return cluster_stats[cluster]; }
59
65 inline const ClusterStats &get_cluster_stats_ref(int cluster) const { return cluster_stats[cluster]; }
66
72 void recompute(const int K, const Eigen::VectorXi &allocations_in) override;
73
79 inline void move_cluster_info(int from_cluster, int to_cluster) override {
80 cluster_stats[to_cluster] = std::move(cluster_stats[from_cluster]);
81 };
82
87 void remove_info(int cluster) override {
88 if (cluster == static_cast<int>(cluster_stats.size()) - 1) {
89 cluster_stats.pop_back();
90 } else {
91 cluster_stats.erase(cluster_stats.begin() + cluster);
92 }
93 }
94};
Abstract base class for managing cluster information and caches.
const Eigen::VectorXi binary_covariates
Definition binary_cache.hpp:35
void recompute(const int K, const Eigen::VectorXi &allocations_in) override
Recomputes all cluster information from current allocations.
Definition binary_cache.cpp:8
const ClusterStats & get_cluster_stats_ref(int cluster) const
Get cluster statistics reference for a specific cluster.
Definition binary_cache.hpp:65
void remove_info(int cluster) override
Removes information related to a specific cluster.
Definition binary_cache.hpp:87
ClusterStats get_cluster_stats(int cluster) const
Get cluster statistics for a specific cluster.
Definition binary_cache.hpp:58
void move_cluster_info(int from_cluster, int to_cluster) override
Moves cluster information from one cluster to another.
Definition binary_cache.hpp:79
BinaryCache(const Eigen::VectorXi &allocations_ref, const Eigen::VectorXi &binary_covariates)
Definition binary_cache.hpp:37
std::vector< ClusterStats > cluster_stats
Definition binary_cache.hpp:32
void set_allocation(int index, int cluster, int old_cluster) override
Assigns a point to a cluster.
Definition binary_cache.cpp:27
ClusterInfo()=default
Structure to hold statistics for each cluster.
Definition binary_cache.hpp:26
int binary_sum
Definition binary_cache.hpp:27
int n
Definition binary_cache.hpp:28