Docs
Loading...
Searching...
No Matches
spatial_cache.hpp
Go to the documentation of this file.
1#pragma once
2
7
9#include <Eigen/Dense>
10#include <vector>
11
17
18class SpatialCache : public ClusterInfo {
19public:
20 // Important to declare the struct useful.
21 // HINT: If the name changed modified the file which uses it accordingly.
27 struct ClusterStats {
28 int spatial_sum = 0;
29 };
30
31 std::vector<std::vector<int>> neighbor_cache;
32
33private:
34 std::vector<ClusterStats> cluster_stats;
35 Eigen::VectorXi* allocations_ptr;
36
45
46public:
47 const Eigen::MatrixXi W;
48
49 SpatialCache(const Eigen::VectorXi &allocations_ref, const Eigen::MatrixXi &W)
50 : W(W) {
51
52 const int K = allocations_ref.maxCoeff() + 1;
54 recompute(K > 0 ? K : 0, allocations_ref);
55 }
56
64 void set_allocation(int index, int cluster, int old_cluster) override;
65
66 void set_allocation_ptr(const Eigen::VectorXi *new_allocations) {
67 allocations_ptr = const_cast<Eigen::VectorXi *>(new_allocations);
68 }
69
75 inline ClusterStats get_cluster_stats(int cluster) const { return cluster_stats[cluster]; }
76
82 inline const ClusterStats &get_cluster_stats_ref(int cluster) const { return cluster_stats[cluster]; }
83
89 void recompute(const int K, const Eigen::VectorXi &allocations_in) override;
90
96 inline void move_cluster_info(int from_cluster, int to_cluster) override {
97 cluster_stats[to_cluster] = std::move(cluster_stats[from_cluster]);
98 };
99
104 void remove_info(int cluster) override {
105 if (cluster == static_cast<int>(cluster_stats.size()) - 1) {
106 cluster_stats.pop_back();
107 } else {
108 cluster_stats.erase(cluster_stats.begin() + cluster);
109 }
110 }
111};
Abstract base class for managing cluster information and caches.
ClusterInfo()=default
std::vector< std::vector< int > > neighbor_cache
Definition spatial_cache.hpp:31
SpatialCache(const Eigen::VectorXi &allocations_ref, const Eigen::MatrixXi &W)
Definition spatial_cache.hpp:49
void neighbor_cache_compute()
Precomputes and stores neighbor indices for all observations.
Definition spatial_cache.cpp:8
ClusterStats get_cluster_stats(int cluster) const
Get cluster statistics for a specific cluster.
Definition spatial_cache.hpp:75
const ClusterStats & get_cluster_stats_ref(int cluster) const
Get cluster statistics reference for a specific cluster.
Definition spatial_cache.hpp:82
std::vector< ClusterStats > cluster_stats
Definition spatial_cache.hpp:34
void set_allocation_ptr(const Eigen::VectorXi *new_allocations)
Definition spatial_cache.hpp:66
void move_cluster_info(int from_cluster, int to_cluster) override
Moves cluster information from one cluster to another.
Definition spatial_cache.hpp:96
void recompute(const int K, const Eigen::VectorXi &allocations_in) override
Recomputes all cluster information from current allocations.
Definition spatial_cache.cpp:25
void remove_info(int cluster) override
Removes information related to a specific cluster.
Definition spatial_cache.hpp:104
void set_allocation(int index, int cluster, int old_cluster) override
Assigns a point to a cluster.
Definition spatial_cache.cpp:45
Eigen::VectorXi * allocations_ptr
Definition spatial_cache.hpp:35
const Eigen::MatrixXi W
Definition spatial_cache.hpp:47
Structure to hold statistics for each cluster.
Definition spatial_cache.hpp:27
int spatial_sum
Definition spatial_cache.hpp:28