Skip to content

Commit ef5e30d

Browse files
committed
refactor : optimize state management : use std::execution::unseq
1 parent c8062a6 commit ef5e30d

2 files changed

Lines changed: 16 additions & 20 deletions

File tree

src/utils/state_management.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
///*****************************************************************************
66

77
#include <cmath>
8+
#include <execution>
89
#include <iterator>
910

1011
#include "state_management.hpp"
@@ -43,9 +44,10 @@ void Caretaker::garbage_collector() noexcept {
4344
});
4445

4546
// Step 3: for each originator, erase blacklisted nodes.
46-
for(auto& ptr : originators)
47+
for_each(execution::unseq, begin(originators), end(originators), [&](auto const& ptr) {
4748
if(auto originator = ptr.lock(); originator)
4849
originator->erase(to_remove);
50+
});
4951

5052
// Step 4: erase annotations related to blacklisted nodes.
5153
erase_if(annotations, [&](auto const& item) {
@@ -181,9 +183,10 @@ void Caretaker::remember_current_timepoint() noexcept {
181183
bool Caretaker::go_without_remembering(Timepoint* t) noexcept {
182184
if(t && t->root() == history_root.get()) {
183185
current_timepoint = t;
184-
for(auto& it : originators)
185-
if(auto ptr = it.lock(); ptr)
186-
ptr->go(t);
186+
for_each(execution::unseq, begin(originators), end(originators), [&](auto const& ptr) {
187+
if(auto originator = ptr.lock(); originator)
188+
originator->go(t);
189+
});
187190
return true;
188191
} else {
189192
return false;
@@ -199,7 +202,7 @@ bool Caretaker::go_and_remember(Timepoint* t) noexcept {
199202
}
200203

201204
//******************************************************************************
202-
void Caretaker::annotate_current_timepoint(std::unique_ptr<IAnnotation> annotation) noexcept {
205+
void Caretaker::annotate_current_timepoint(unique_ptr<IAnnotation> annotation) noexcept {
203206
annotations.emplace(current_timepoint, std::move(annotation));
204207
}
205208

@@ -214,7 +217,7 @@ IAnnotation* Caretaker::get_annotation(Timepoint* t) noexcept {
214217
// In predicate, you will probably want to downcast to a well-known user-defined
215218
// type.
216219
//******************************************************************************
217-
Timepoint* Caretaker::find_first_ancestor_with_annotation_that(std::function<bool (IAnnotation const*)> const& predicate, bool include_itself) noexcept{
220+
Timepoint* Caretaker::find_first_ancestor_with_annotation_that(function<bool (IAnnotation const*)> const& predicate, bool include_itself) noexcept{
218221
for(auto* t : current_timepoint->ancestors(include_itself))
219222
if(annotations.contains(t) && predicate(annotations.at(t).get()))
220223
return t;

src/utils/tree_node.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44
/// @author Thomas Lepoix <[email protected]>
55
///*****************************************************************************
66

7-
#include <list>
8-
#include <string>
9-
#include <cstddef>
10-
#include <iostream>
117
#include <algorithm>
12-
#include <vector>
8+
#include <execution>
9+
#include <string>
1310

1411
#include "id_generator.hpp"
1512

@@ -107,20 +104,16 @@ vector<TreeNode*> TreeNode::cluster(bool include_itself) {
107104
//******************************************************************************
108105
TreeNode* TreeNode::common_ancestor(TreeNode& node, bool include_themselves) {
109106
for(auto* it_a : ancestors(include_themselves))
110-
for(auto* it_b : node.ancestors(include_themselves))
111-
if(it_a == it_b)
112-
return it_a;
107+
if(auto node_ancestors = node.ancestors(include_themselves)
108+
; any_of(execution::unseq, begin(node_ancestors), end(node_ancestors), [&](auto const& it_b) { return it_a == it_b; }))
109+
return it_a;
110+
113111
return nullptr;
114112
}
115113

116114
//******************************************************************************
117115
TreeNode* common_ancestor(TreeNode& a, TreeNode& b, bool include_themselves) {
118-
// return a.common_ancestor(b, include_themselves);
119-
for(auto* it_a : a.ancestors(include_themselves))
120-
for(auto* it_b : b.ancestors(include_themselves))
121-
if(it_a == it_b)
122-
return it_a;
123-
return nullptr;
116+
return a.common_ancestor(b, include_themselves);
124117
}
125118

126119
//******************************************************************************

0 commit comments

Comments
 (0)