txgraph: Add CountDistinctClusters function (feature)

This commit is contained in:
Pieter Wuille
2025-01-16 16:00:10 -05:00
parent b685d322c9
commit aded047019
3 changed files with 77 additions and 0 deletions

View File

@@ -454,6 +454,7 @@ public:
GraphIndex GetTransactionCount(bool main_only = false) noexcept final;
bool IsOversized(bool main_only = false) noexcept final;
std::strong_ordering CompareMainOrder(const Ref& a, const Ref& b) noexcept final;
GraphIndex CountDistinctClusters(std::span<const Ref* const> refs, bool main_only = false) noexcept final;
void SanityCheck() const final;
};
@@ -1781,6 +1782,33 @@ std::strong_ordering TxGraphImpl::CompareMainOrder(const Ref& a, const Ref& b) n
return entry_a.m_main_lin_index <=> entry_b.m_main_lin_index;
}
TxGraph::GraphIndex TxGraphImpl::CountDistinctClusters(std::span<const Ref* const> refs, bool main_only) noexcept
{
size_t level = GetSpecifiedLevel(main_only);
ApplyDependencies(level);
auto& clusterset = GetClusterSet(level);
Assume(clusterset.m_deps_to_add.empty());
// Build a vector of Clusters that the specified Refs occur in.
std::vector<Cluster*> clusters;
clusters.reserve(refs.size());
for (const Ref* ref : refs) {
if (ref == nullptr) continue;
if (GetRefGraph(*ref) == nullptr) continue;
Assume(GetRefGraph(*ref) == this);
auto cluster = FindCluster(GetRefIndex(*ref), level);
if (cluster != nullptr) clusters.push_back(cluster);
}
// Count the number of distinct elements in clusters.
std::sort(clusters.begin(), clusters.end());
Cluster* last{nullptr};
GraphIndex ret{0};
for (Cluster* cluster : clusters) {
ret += (cluster != last);
last = cluster;
}
return ret;
}
void Cluster::SanityCheck(const TxGraphImpl& graph, int level) const
{
// There must be an m_mapping for each m_depgraph position (including holes).