Skip to content

Commit e91baac

Browse files
committed
Release 1.0
1 parent b3370a2 commit e91baac

28 files changed

+8925
-0
lines changed

Thumbnail.png

7.81 KB
Loading

Toolbox/Algorithms.h

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
// .h
2+
// Static Algorithm Functions
3+
// by Kyle Furey
4+
5+
#pragma once
6+
#include "Vector.h"
7+
#include "Sorting.h"
8+
9+
/** A collection of useful template types in C++. */
10+
namespace Toolbox {
11+
12+
// ALGORITHMS
13+
14+
/** Useful algorithms for collections. */
15+
namespace Algorithms {
16+
17+
// MAP
18+
19+
/** Applies the given operation on each element in the given collection. */
20+
template <typename CollectionType, typename Type>
21+
static CollectionType& Map(CollectionType& Collection, void(*Operation)(Type&)) {
22+
for (auto& Element : Collection) {
23+
Operation(Element);
24+
}
25+
return Collection;
26+
}
27+
28+
/** Applies the given operation on each element in the given collection. */
29+
template <typename CollectionType, typename Type>
30+
static CollectionType& Map(size_t Size, CollectionType& Collection, void(*Operation)(Type&)) {
31+
for (auto& Element : Collection) {
32+
if (Size == 0) {
33+
break;
34+
}
35+
Operation(Element);
36+
--Size;
37+
}
38+
return Collection;
39+
}
40+
41+
42+
// FILTER
43+
44+
/** Returns a vector of pointers to elements in the given collection that match the given predicate. */
45+
template <typename CollectionType, typename Type>
46+
static Vector<Type*> Filter(CollectionType& Collection, bool(*Predicate)(const Type&)) {
47+
Vector<Type*> NewCollection;
48+
for (auto& Element : Collection) {
49+
if (Predicate(Element)) {
50+
NewCollection.PushBack(&Element);
51+
}
52+
}
53+
return NewCollection;
54+
}
55+
56+
/** Returns a vector of pointers to elements in the given collection that match the given predicate. */
57+
template <typename CollectionType, typename Type>
58+
static Vector<Type*> Filter(size_t Size, CollectionType& Collection, bool(*Predicate)(const Type&)) {
59+
Vector<Type*> NewCollection(Size);
60+
for (auto& Element : Collection) {
61+
if (Size == 0) {
62+
break;
63+
}
64+
if (Predicate(Element)) {
65+
NewCollection.PushBack(&Element);
66+
}
67+
--Size;
68+
}
69+
return NewCollection;
70+
}
71+
72+
/** Returns a vector of constant pointers to elements in the given collection that match the given predicate. */
73+
template <typename CollectionType, typename Type>
74+
static Vector<const Type*> Filter(const CollectionType& Collection, bool(*Predicate)(const Type&)) {
75+
Vector<const Type*> NewCollection;
76+
for (auto& Element : Collection) {
77+
if (Predicate(Element)) {
78+
NewCollection.PushBack(&Element);
79+
}
80+
}
81+
return NewCollection;
82+
}
83+
84+
/** Returns a vector of constant pointers to elements in the given collection that match the given predicate. */
85+
template <typename CollectionType, typename Type>
86+
static Vector<const Type*> Filter(size_t Size, const CollectionType& Collection, bool(*Predicate)(const Type&)) {
87+
Vector<const Type*> NewCollection(Size);
88+
for (auto& Element : Collection) {
89+
if (Size == 0) {
90+
break;
91+
}
92+
if (Predicate(Element)) {
93+
NewCollection.PushBack(&Element);
94+
}
95+
--Size;
96+
}
97+
return NewCollection;
98+
}
99+
100+
101+
// REDUCE
102+
103+
/** Returns the result of combining all elements in the given collection with the given accumulator. */
104+
template <typename CollectionType, typename Type>
105+
static Type Reduce(const CollectionType& Collection, Type(*Accumulator)(const Type&, const Type&)) {
106+
Type Result{};
107+
for (auto& Element : Collection) {
108+
Result = Accumulator(Result, Element);
109+
}
110+
return Result;
111+
}
112+
113+
/** Returns the result of combining all elements in the given collection with the given accumulator. */
114+
template <typename CollectionType, typename Type>
115+
static Type Reduce(size_t Size, const CollectionType& Collection, Type(*Accumulator)(const Type&, const Type&)) {
116+
Type Result{};
117+
for (auto& Element : Collection) {
118+
if (Size == 0) {
119+
break;
120+
}
121+
Result = Accumulator(Result, Element);
122+
--Size;
123+
}
124+
return Result;
125+
}
126+
127+
/** Returns the result of combining all elements in the given collection with the given accumulator. */
128+
template <typename CollectionType, typename Type>
129+
static Type Reduce(const CollectionType& Collection, Type Start, Type(*Accumulator)(const Type&, const Type&)) {
130+
for (auto& Element : Collection) {
131+
Start = Accumulator(Start, Element);
132+
}
133+
return Start;
134+
}
135+
136+
/** Returns the result of combining all elements in the given collection with the given accumulator. */
137+
template <typename CollectionType, typename Type>
138+
static Type Reduce(size_t Size, const CollectionType& Collection, Type Start, Type(*Accumulator)(const Type&, const Type&)) {
139+
for (auto& Element : Collection) {
140+
if (Size == 0) {
141+
break;
142+
}
143+
Start = Accumulator(Start, Element);
144+
--Size;
145+
}
146+
return Start;
147+
}
148+
149+
/** Returns the result of combining all elements in the given collection with the given accumulator. */
150+
template <typename CollectionType, typename Type>
151+
static Type Reduce(const CollectionType& Collection, void(*Accumulator)(Type&, const Type&)) {
152+
Type Result{};
153+
for (auto& Element : Collection) {
154+
Accumulator(Result, Element);
155+
}
156+
return Result;
157+
}
158+
159+
/** Returns the result of combining all elements in the given collection with the given accumulator. */
160+
template <typename CollectionType, typename Type>
161+
static Type Reduce(size_t Size, const CollectionType& Collection, void(*Accumulator)(Type&, const Type&)) {
162+
Type Result{};
163+
for (auto& Element : Collection) {
164+
if (Size == 0) {
165+
break;
166+
}
167+
Accumulator(Result, Element);
168+
--Size;
169+
}
170+
return Result;
171+
}
172+
173+
/** Returns the result of combining all elements in the given collection with the given accumulator. */
174+
template <typename CollectionType, typename Type>
175+
static Type Reduce(const CollectionType& Collection, Type Start, void(*Accumulator)(Type&, const Type&)) {
176+
for (auto& Element : Collection) {
177+
Accumulator(Start, Element);
178+
}
179+
return Start;
180+
}
181+
182+
/** Returns the result of combining all elements in the given collection with the given accumulator. */
183+
template <typename CollectionType, typename Type>
184+
static Type Reduce(size_t Size, const CollectionType& Collection, Type Start, void(*Accumulator)(Type&, const Type&)) {
185+
for (auto& Element : Collection) {
186+
if (Size == 0) {
187+
break;
188+
}
189+
Accumulator(Start, Element);
190+
--Size;
191+
}
192+
return Start;
193+
}
194+
195+
196+
// SORT
197+
198+
/** Quick sorts the given collection with the given comparer. */
199+
template <typename CollectionType, typename Type>
200+
static CollectionType& Sort(CollectionType& Collection, bool(*Comparer)(const Type&, const Type&) = Sorting::GreaterThan) {
201+
Sorting::QuickSort<CollectionType, Type>(Collection.Size(), Collection, Comparer);
202+
return Collection;
203+
}
204+
205+
/** Quick sorts the given collection with the given comparer. */
206+
template <typename CollectionType, typename Type>
207+
static CollectionType& Sort(size_t Size, CollectionType& Collection, bool(*Comparer)(const Type&, const Type&) = Sorting::GreaterThan) {
208+
Sorting::QuickSort<CollectionType, Type>(Size, Collection, Comparer);
209+
return Collection;
210+
}
211+
212+
213+
// MOVE
214+
215+
/** Moves the From value to To, and returns a reference to To. */
216+
template<typename Type>
217+
static Type& Move(Type& From, Type& To) {
218+
To = static_cast<Type&&>(From);
219+
return To;
220+
}
221+
222+
223+
// SWAP
224+
225+
/** Swaps the given values. */
226+
template<typename Type>
227+
static void Swap(Type& Left, Type& Right) {
228+
Type Temp = Left;
229+
Left = Right;
230+
Right = Temp;
231+
}
232+
233+
234+
// COPY
235+
236+
/** Copies the From value to To, and returns a reference to To. */
237+
template<typename Type>
238+
static Type& Copy(const Type& From, Type& To) {
239+
To = From;
240+
return To;
241+
}
242+
}
243+
}

0 commit comments

Comments
 (0)