CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Algorithm.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
11
12#include <algorithm>
13#include <iterator>
14
15namespace CeresEngine {
16
22 template<typename Container, typename Value> void erase(Container& container, const Value& value) {
23 container.erase(std::remove(container.begin(), container.end(), value), container.end());
24 }
25
32 template<typename Container, typename Predicate> void eraseIf(Container& container, Predicate&& predicate) {
33 container.erase(std::remove_if(container.begin(), container.end(), predicate), container.end());
34 }
35
39 template<typename Container> void eraseDuplicates(Container& container) {
40 container.erase(std::unique(container.begin(), container.end()), container.end());
41 }
42
43 // ---------------------------------------------------------------------------------------------
44
51 template<typename Container, typename Value> decltype(std::declval<Container>().begin()) findContainer(Container& container, const Value& value) {
52 return std::find(container.begin(), container.end(), value);
53 }
54
61 template<typename Container, typename Predicate> decltype(std::declval<Container>().begin()) findIf(Container& container, Predicate&& predicate) {
62 return std::find_if(container.begin(), container.end(), predicate);
63 }
64
71 template<typename Container, typename Predicate> decltype(std::declval<Container>().begin()) findIfNot(Container& container, Predicate&& predicate) {
72 return std::find_if_not(container.begin(), container.end(), predicate);
73 }
74
75 // ---------------------------------------------------------------------------------------------
76
80 template<typename Container> void sort(Container& container) { std::sort(container.begin(), container.end()); }
81
87 template<typename Container, typename Compare> void sort(Container& container, Compare&& compare) {
88 std::sort(container.begin(), container.end(), compare);
89 }
90
91
92 // ---------------------------------------------------------------------------------------------
93
94 template<typename Container, typename Transformation> void transformInPlace(Container& container, Transformation&& transformation) {
95 std::transform(container.begin(), container.end(), container.begin(), transformation);
96 }
97
98 template<typename InContainer, typename OutContainer, typename Transformation>
100 std::transform(inContainer.begin(), inContainer.end(), std::inserter(outContainer, outContainer.end()), transformation);
101 }
102
103 template<typename Container, typename Transformation>
104 Vector<decltype(std::declval<Transformation>()(*std::declval<Container>().begin()))> transformTo(const Container& container, Transformation&& transformation) {
105 Vector<decltype(transformation(*container.begin()))> output;
106 std::transform(container.begin(), container.end(), std::back_inserter(output), transformation);
107 return output;
108 }
109
110 // ---------------------------------------------------------------------------------------------
111
112 template<typename Container, typename Range> Container toContainer(Range& range) { return Container(range.begin(), range.end()); }
113
114 template<typename Container, typename Range> Container toContainer(const Range& range) { return Container(range.begin(), range.end()); }
115
116} // namespace CeresEngine
Definition Application.hpp:19
decltype(std::declval< Container >().begin()) findContainer(Container &container, const Value &value)
Finds the first item that is equal to value.
Definition Algorithm.hpp:51
auto range()
Returns an iterator that increases it's value from 0 to end by 1 for each step.
Definition Iterator.hpp:350
void transformInPlace(Container &container, Transformation &&transformation)
Definition Algorithm.hpp:94
decltype(std::declval< Container >().begin()) findIf(Container &container, Predicate &&predicate)
Finds the first item that the given predicate returns true.
Definition Algorithm.hpp:61
decltype(std::declval< Container >().begin()) findIfNot(Container &container, Predicate &&predicate)
Finds the first item that the given predicate returns false.
Definition Algorithm.hpp:71
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
void eraseIf(Container &container, Predicate &&predicate)
Removes all items that match the given predicate from the given container.
Definition Algorithm.hpp:32
void transformTo(const InContainer &inContainer, OutContainer &outContainer, Transformation &&transformation)
Definition Algorithm.hpp:99
void erase(Container &container, const Value &value)
Erases all items value from the given container.
Definition Algorithm.hpp:22
void eraseDuplicates(Container &container)
Removes all duplicates items from the given container.
Definition Algorithm.hpp:39
void sort(Container &container)
Sorts all items in the container.
Definition Algorithm.hpp:80
constexpr auto predicate(Callable &&callable) noexcept
Helper function to create a new predicate from a lambda.
Definition Predicate.hpp:390
Container toContainer(Range &range)
Definition Algorithm.hpp:112
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
A type that predicate types must extend to allow automatic operator overloading.
Definition Predicate.hpp:19