Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
Accumulate.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6#include "Templates/Invoke.h"
7
8// TPlus<T> specifically takes const T& and returns T.
9// TPlus<> (empty angle brackets) is late-binding, taking whatever is passed and returning the correct result type for (A+B)
10template<typename T = void>
11struct TPlus
12{
13 FORCEINLINE T operator()(const T& A, const T& B) { return A + B; }
14};
15template<>
16struct TPlus<void>
17{
18 template<typename U, typename V>
19 FORCEINLINE auto operator()(U&& A, V&& B) -> decltype(A + B) { return A + B; }
20};
21
22
23namespace Algo
24{
25 /**
26 * Sums a range by successively applying Op.
27 *
28 * @param Input Any iterable type
29 * @param Init Initial value for the summation
30 * @param Op Summing Operation (the default is TPlus<>)
31 *
32 * @return the result of summing all the elements of Input
33 */
34 template <typename T, typename A, typename OpT>
35 FORCEINLINE T Accumulate(const A& Input, T Init, OpT Op)
36 {
38 for (const auto& InputElem : Input)
39 {
41 }
42 return Result;
43 }
44
45 /**
46 * Sums a range.
47 *
48 * @param Input Any iterable type
49 * @param Init Initial value for the summation
50 *
51 * @return the result of summing all the elements of Input
52 */
53 template <typename T, typename A>
54 FORCEINLINE T Accumulate(const A& Input, T Init)
55 {
56 return Accumulate(Input, MoveTemp(Init), TPlus<>());
57 }
58
59 /**
60 * Sums a range by applying MapOp to each element, and then summing the results.
61 *
62 * @param Input Any iterable type
63 * @param Init Initial value for the summation
64 * @param MapOp Mapping Operation
65 * @param Op Summing Operation (the default is TPlus<>)
66 *
67 * @return the result of mapping and then summing all the elements of Input
68 */
69 template <typename T, typename A, typename MapT, typename OpT>
70 FORCEINLINE T TransformAccumulate(const A& Input, MapT MapOp, T Init, OpT Op)
71 {
73 for (const auto& InputElem : Input)
74 {
76 }
77 return Result;
78 }
79
80 /**
81 * Sums a range by applying MapOp to each element, and then summing the results.
82 *
83 * @param Input Any iterable type
84 * @param Init Initial value for the summation
85 * @param MapOp Mapping Operation
86 *
87 * @return the result of mapping and then summing all the elements of Input
88 */
89 template <typename T, typename A, typename MapT>
90 FORCEINLINE T TransformAccumulate(const A& Input, MapT MapOp, T Init)
91 {
93 }
94}
#define FORCEINLINE
Definition Platform.h:644
Definition Heapify.h:12
FORCEINLINE T TransformAccumulate(const A &Input, MapT MapOp, T Init, OpT Op)
Definition Accumulate.h:70
FORCEINLINE T Accumulate(const A &Input, T Init, OpT Op)
Definition Accumulate.h:35
FORCEINLINE T TransformAccumulate(const A &Input, MapT MapOp, T Init)
Definition Accumulate.h:90
FORCEINLINE T Accumulate(const A &Input, T Init)
Definition Accumulate.h:54
FORCEINLINE auto operator()(U &&A, V &&B) -> decltype(A+B)
Definition Accumulate.h:19
FORCEINLINE T operator()(const T &A, const T &B)
Definition Accumulate.h:13