Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
Partition.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5namespace Algo
6{
7 /**
8 * Rearranges the elements so that all the elements for which Predicate returns true precede all those for which it returns false. (not stable)
9 *
10 * @param First pointer to the first element
11 * @param Num the number of items
12 * @param Predicate unary predicate class
13 * @return index of the first element in the second group
14 */
15 template<class T, typename IndexType, class UnaryPredicate>
16 IndexType Partition(T* Elements, const IndexType Num, const UnaryPredicate& Predicate)
17 {
18 T* First = Elements;
19 T* Last = Elements + Num;
20
21 while (First != Last)
22 {
23 while (Predicate(*First))
24 {
25 ++First;
26 if (First == Last)
27 {
28 return (IndexType)(First - Elements);
29 }
30 }
31
32 do
33 {
34 --Last;
35 if (First == Last)
36 {
37 return (IndexType)(First - Elements);
38 }
39 } while (!Predicate(*Last));
40
42 ++First;
43 }
44
45 return (IndexType)(First - Elements);
46 }
47} //namespace Algo
Definition Heapify.h:12
IndexType Partition(T *Elements, const IndexType Num, const UnaryPredicate &Predicate)
Definition Partition.h:16