Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
IndexOf.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Templates/IdentityFunctor.h"
6#include "Templates/Invoke.h"
7#include "Templates/UnrealTemplate.h"
8
9namespace AlgoImpl
10{
11 template <typename RangeType, typename ValueType, typename ProjectionType>
12 auto IndexOfBy(RangeType&& Range, const ValueType& Value, ProjectionType Proj)
13 {
14 auto Num = GetNum(Range);
15 auto Data = GetData(Range);
16
17 using SizeType = decltype(Num);
18 for (SizeType Index = 0; Index < Num; ++Index)
19 {
20 if (Invoke(Proj, Data[Index]) == Value)
21 {
22 return Index;
23 }
24 }
25
26 return (SizeType)-1;
27 }
28
29 template <typename RangeType, typename PredicateType>
30 auto IndexOfByPredicate(RangeType&& Range, PredicateType Pred)
31 {
32 auto Num = GetNum(Range);
33 auto Data = GetData(Range);
34
35 using SizeType = decltype(Num);
36 for (SizeType Index = 0; Index < Num; ++Index)
37 {
38 if (Invoke(Pred, Data[Index]))
39 {
40 return Index;
41 }
42 }
43
44 return (SizeType)-1;
45 }
46}
47
48namespace Algo
49{
50 /**
51 * Returns the index of the first element in the range which is equal to the given value.
52 *
53 * @param Range The range to search.
54 * @param Value The value to search for.
55 *
56 * @return The index of the first element found, or (SizeType)-1 if none was found (where SizeType = decltype(GetNum(Range)))
57 */
58 template <typename RangeType, typename ValueType>
59 FORCEINLINE auto IndexOf(RangeType&& Range, const ValueType& Value)
60 {
61 static_assert(TIsContiguousContainer<RangeType>::Value, "Must only be used with contiguous containers");
62 return AlgoImpl::IndexOfBy(Forward<RangeType>(Range), Value, FIdentityFunctor());
63 }
64
65 /**
66 * Returns the index of the first element in the range whose projection is equal to the given value.
67 *
68 * @param Range The range to search.
69 * @param Value The value to search for.
70 * @param Proj The projection to apply to the element.
71 *
72 * @return The index of the first element found, or (SizeType)-1 if none was found (where SizeType = decltype(GetNum(Range)))
73 */
74 template <typename RangeType, typename ValueType, typename ProjectionType>
75 FORCEINLINE auto IndexOfBy(RangeType&& Range, const ValueType& Value, ProjectionType Proj)
76 {
77 static_assert(TIsContiguousContainer<RangeType>::Value, "Must only be used with contiguous containers");
78 return AlgoImpl::IndexOfBy(Forward<RangeType>(Range), Value, MoveTemp(Proj));
79 }
80
81 /**
82 * Returns the index of the first element in the range which matches the predicate.
83 *
84 * @param Range The range to search.
85 * @param Pred The predicate to search for.
86 *
87 * @return The index of the first element found, or (SizeType)-1 if none was found (where SizeType = decltype(GetNum(Range)))
88 */
89 template <typename RangeType, typename PredicateType>
90 FORCEINLINE auto IndexOfByPredicate(RangeType&& Range, PredicateType Pred)
91 {
92 static_assert(TIsContiguousContainer<RangeType>::Value, "Must only be used with contiguous containers");
93 return AlgoImpl::IndexOfByPredicate(Forward<RangeType>(Range), MoveTemp(Pred));
94 }
95}
#define FORCEINLINE
Definition Platform.h:644
Definition Heapify.h:12
FORCEINLINE auto IndexOf(RangeType &&Range, const ValueType &Value)
Definition IndexOf.h:59
FORCEINLINE auto IndexOfByPredicate(RangeType &&Range, PredicateType Pred)
Definition IndexOf.h:90
FORCEINLINE auto IndexOfBy(RangeType &&Range, const ValueType &Value, ProjectionType Proj)
Definition IndexOf.h:75
auto IndexOfByPredicate(RangeType &&Range, PredicateType Pred)
Definition IndexOf.h:30
auto IndexOfBy(RangeType &&Range, const ValueType &Value, ProjectionType Proj)
Definition IndexOf.h:12