Skip to content

Commit 54bcedd

Browse files
committed
txgraph: Multiple inputs to Get{Ancestors,Descendant}Refs (preparation)
This is a preparation for the next commit, which adds a feature to request the Refs to multiple ancestors/descendants at once.
1 parent aded047 commit 54bcedd

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

src/txgraph.cpp

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,12 @@ class Cluster
139139

140140
// Functions that implement the Cluster-specific side of public TxGraph functions.
141141

142-
/** Get a vector of Refs for the ancestors of a given Cluster element. */
143-
std::vector<TxGraph::Ref*> GetAncestorRefs(const TxGraphImpl& graph, DepGraphIndex idx) noexcept;
144-
/** Get a vector of Refs for the descendants of a given Cluster element. */
145-
std::vector<TxGraph::Ref*> GetDescendantRefs(const TxGraphImpl& graph, DepGraphIndex idx) noexcept;
142+
/** Process elements from the front of args that apply to this cluster, and append Refs for the
143+
* union of their ancestors to output. */
144+
void GetAncestorRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept;
145+
/** Process elements from the front of args that apply to this cluster, and append Refs for the
146+
* union of their descendants to output. */
147+
void GetDescendantRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept;
146148
/** Get a vector of Refs for all elements of this Cluster, in linearization order. */
147149
std::vector<TxGraph::Ref*> GetClusterRefs(const TxGraphImpl& graph) noexcept;
148150
/** Get the individual transaction feerate of a Cluster element. */
@@ -1469,30 +1471,42 @@ bool TxGraphImpl::Exists(const Ref& arg, bool main_only) noexcept
14691471
return cluster != nullptr;
14701472
}
14711473

1472-
std::vector<TxGraph::Ref*> Cluster::GetAncestorRefs(const TxGraphImpl& graph, DepGraphIndex idx) noexcept
1474+
void Cluster::GetAncestorRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept
14731475
{
1474-
std::vector<TxGraph::Ref*> ret;
1475-
ret.reserve(m_depgraph.Ancestors(idx).Count());
1476+
/** The union of all ancestors to be returned. */
1477+
SetType ancestors_union;
1478+
// Process elements from the front of args, as long as they apply.
1479+
while (!args.empty()) {
1480+
if (args.front().first != this) break;
1481+
ancestors_union |= m_depgraph.Ancestors(args.front().second);
1482+
args = args.subspan(1);
1483+
}
1484+
Assume(ancestors_union.Any());
14761485
// Translate all ancestors (in arbitrary order) to Refs (if they have any), and return them.
1477-
for (auto idx : m_depgraph.Ancestors(idx)) {
1486+
for (auto idx : ancestors_union) {
14781487
const auto& entry = graph.m_entries[m_mapping[idx]];
14791488
Assume(entry.m_ref != nullptr);
1480-
ret.push_back(entry.m_ref);
1489+
output.push_back(entry.m_ref);
14811490
}
1482-
return ret;
14831491
}
14841492

1485-
std::vector<TxGraph::Ref*> Cluster::GetDescendantRefs(const TxGraphImpl& graph, DepGraphIndex idx) noexcept
1493+
void Cluster::GetDescendantRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept
14861494
{
1487-
std::vector<TxGraph::Ref*> ret;
1488-
ret.reserve(m_depgraph.Descendants(idx).Count());
1495+
/** The union of all descendants to be returned. */
1496+
SetType descendants_union;
1497+
// Process elements from the front of args, as long as they apply.
1498+
while (!args.empty()) {
1499+
if (args.front().first != this) break;
1500+
descendants_union |= m_depgraph.Descendants(args.front().second);
1501+
args = args.subspan(1);
1502+
}
1503+
Assume(descendants_union.Any());
14891504
// Translate all descendants (in arbitrary order) to Refs (if they have any), and return them.
1490-
for (auto idx : m_depgraph.Descendants(idx)) {
1505+
for (auto idx : descendants_union) {
14911506
const auto& entry = graph.m_entries[m_mapping[idx]];
14921507
Assume(entry.m_ref != nullptr);
1493-
ret.push_back(entry.m_ref);
1508+
output.push_back(entry.m_ref);
14941509
}
1495-
return ret;
14961510
}
14971511

14981512
std::vector<TxGraph::Ref*> Cluster::GetClusterRefs(const TxGraphImpl& graph) noexcept
@@ -1539,7 +1553,11 @@ std::vector<TxGraph::Ref*> TxGraphImpl::GetAncestors(const Ref& arg, bool main_o
15391553
auto cluster = FindCluster(GetRefIndex(arg), level);
15401554
if (cluster == nullptr) return {};
15411555
// Dispatch to the Cluster.
1542-
return cluster->GetAncestorRefs(*this, m_entries[GetRefIndex(arg)].m_locator[cluster->m_level].index);
1556+
std::pair<Cluster*, DepGraphIndex> match = {cluster, m_entries[GetRefIndex(arg)].m_locator[cluster->m_level].index};
1557+
auto matches = std::span(&match, 1);
1558+
std::vector<TxGraph::Ref*> ret;
1559+
cluster->GetAncestorRefs(*this, matches, ret);
1560+
return ret;
15431561
}
15441562

15451563
std::vector<TxGraph::Ref*> TxGraphImpl::GetDescendants(const Ref& arg, bool main_only) noexcept
@@ -1556,7 +1574,11 @@ std::vector<TxGraph::Ref*> TxGraphImpl::GetDescendants(const Ref& arg, bool main
15561574
auto cluster = FindCluster(GetRefIndex(arg), level);
15571575
if (cluster == nullptr) return {};
15581576
// Dispatch to the Cluster.
1559-
return cluster->GetDescendantRefs(*this, m_entries[GetRefIndex(arg)].m_locator[cluster->m_level].index);
1577+
std::pair<Cluster*, DepGraphIndex> match = {cluster, m_entries[GetRefIndex(arg)].m_locator[cluster->m_level].index};
1578+
auto matches = std::span(&match, 1);
1579+
std::vector<TxGraph::Ref*> ret;
1580+
cluster->GetDescendantRefs(*this, matches, ret);
1581+
return ret;
15601582
}
15611583

15621584
std::vector<TxGraph::Ref*> TxGraphImpl::GetCluster(const Ref& arg, bool main_only) noexcept

0 commit comments

Comments
 (0)