FireSTARR
Loading...
Searching...
No Matches
MergeIterator.h
1/* Copyright (c) His Majesty the King in Right of Canada as represented by the Minister of Natural Resources, 2021-2025. */
2
3/* SPDX-License-Identifier: AGPL-3.0-or-later */
4
5#include "stdafx.h"
6#include "Location.h"
7#include "Cell.h"
8#include "InnerPos.h"
9#include "CellPoints.h"
10
11namespace fs::sim
12{
13using topo::Location;
14using topo::SpreadKey;
15
16// mangled version of std::transform_reduce() that calls .begin() and .end()
17template <typename _ForwardIteratorSource, class _Tp, class _BinaryOperation, class _UnaryOperation>
18_Tp do_transform_reduce(
19 _ForwardIteratorSource&& container,
20 _Tp __init,
21 _BinaryOperation __binary_op,
22 _UnaryOperation __unary_op)
23{
24 // to help compiler determine type
25 using _ForwardIterator = decltype(container.begin());
26 return std::transform_reduce(
27 std::execution::par_unseq,
28 static_cast<_ForwardIterator>(container.begin()),
29 static_cast<_ForwardIterator>(container.end()),
30 __init,
31 __binary_op,
32 __unary_op);
33}
34
35template <typename M, class F>
36const M merge_maps_generic(
37 const M& lhs,
38 const M& rhs,
39 F f)
40{
41 using const_iterator = typename M::const_iterator;
42 using value_type = typename M::value_type;
43 using key_type = typename M::key_type;
44 using mapped_type = typename M::mapped_type;
45 std::function<mapped_type(const mapped_type&, const mapped_type&)> fct_merge = f;
46 M out{};
47 const_iterator it_lhs = lhs.begin();
48 const_iterator it_rhs = rhs.begin();
49 const_iterator end_lhs = lhs.end();
50 const_iterator end_rhs = rhs.end();
51 while (true)
52 {
53 if (end_lhs == it_lhs)
54 {
55 while (end_rhs != it_rhs)
56 {
57 out.emplace(*it_rhs);
58 ++it_rhs;
59 }
60 break;
61 }
62 if (end_rhs == it_rhs)
63 {
64 while (end_lhs != it_lhs)
65 {
66 out.emplace(*it_lhs);
67 ++it_lhs;
68 }
69 break;
70 }
71 // at end of neither so pick lower value
72 const value_type& pair0 = *it_lhs;
73 const value_type& pair1 = *it_rhs;
74 const key_type& k0 = pair0.first;
75 const key_type& k1 = pair1.first;
76 const mapped_type& m0 = pair0.second;
77 const mapped_type& m1 = pair1.second;
78 if (k0 < k1)
79 {
80 out.emplace(pair0);
81 ++it_lhs;
82 }
83 else if (k0 > k1)
84 {
85 out.emplace(pair1);
86 ++it_rhs;
87 }
88 else
89 {
90 assert(k0 == k1);
91 const mapped_type merged = fct_merge(m0, m1);
92 out.emplace(pair<const key_type, const mapped_type>(k0, merged));
93 ++it_lhs;
94 ++it_rhs;
95 }
96 }
97 return out;
98}
99}