tracktion-engine 3.0-10-g034fdde4aa5
Tracktion Engine — High level data model for audio applications

« « « Anklang Documentation
Loading...
Searching...
No Matches
tracktion_AlgorithmAdapters.h
Go to the documentation of this file.
1 /*
2 ,--. ,--. ,--. ,--.
3 ,-' '-.,--.--.,--,--.,---.| |,-.,-' '-.`--' ,---. ,--,--, Copyright 2024
4 '-. .-'| .--' ,-. | .--'| /'-. .-',--.| .-. || \ Tracktion Software
5 | | | | \ '-' \ `--.| \ \ | | | |' '-' '| || | Corporation
6 `---' `--' `--`--'`---'`--'`--' `---' `--' `---' `--''--' www.tracktion.com
7
8 Tracktion Engine uses a GPL/commercial licence - see LICENCE.md for details.
9*/
10
11#pragma once
12
13namespace tracktion { inline namespace core
14{
15
16template<class Container, class T, class BinaryOperation>
17inline T accumulate (const Container& container, T init)
18{
19 return std::accumulate (std::begin (container), std::end (container), init);
20}
21
22template<class Container, class T, class BinaryOperation>
23inline T accumulate (const Container& container, T init, BinaryOperation op)
24{
25 return std::accumulate (std::begin (container), std::end (container), init, op);
26}
27
28template<class Container, class UnaryFunction>
29UnaryFunction for_each (const Container& container, UnaryFunction f)
30{
31 return std::for_each (std::begin (container), std::end (container), f);
32}
33
34template<class Container, class UnaryFunction>
35bool contains (const Container& container, UnaryFunction f)
36{
37 return std::find_if (std::begin (container), std::end (container), f)
38 != std::end (container);
39}
40
42template<class Container>
43bool contains_v (const Container& container, typename Container::value_type v)
44{
45 return std::find (std::begin (container), std::end (container), v)
46 != std::end (container);
47}
48
49template<class Container>
50inline void sort (Container& container)
51{
52 return std::sort (std::begin (container), std::end (container));
53}
54
55template<class Container, class Compare>
56inline void sort (Container& container, Compare comp)
57{
58 return std::sort (std::begin (container), std::end (container), comp);
59}
60
61template<class Container>
62inline void stable_sort (Container& container)
63{
64 return std::stable_sort (std::begin (container), std::end (container));
65}
66
67template<class Container, class Compare>
68inline void stable_sort (Container& container, Compare comp)
69{
70 return std::stable_sort (std::begin (container), std::end (container), comp);
71}
72
73template<class Container>
74inline std::optional<size_t> index_of (const Container& container, typename Container::value_type v)
75{
76 if (auto iter = std::find (container.begin(), container.end(), v);
77 iter != container.end())
78 return std::distance (container.begin(), iter);
79
80 return {};
81}
82
83template<class Container, class Predicate>
84inline std::optional<size_t> index_if (const Container& container, Predicate p)
85{
86 if (auto iter = std::find_if (container.begin(), container.end(), std::forward<decltype(p)> (p));
87 iter != container.end())
88 return std::distance (container.begin(), iter);
89
90 return {};
91}
92
93template<class Container, class IndexType>
94inline std::optional<typename Container::value_type> get_checked (const Container& container, IndexType index)
95{
96 const auto i = static_cast<typename Container::size_type> (index);
97
98 if (i >= 0 && i < container.size())
99 return container[i];
100
101 return {};
102}
103
104template<class Container, class IndexType>
105inline typename Container::value_type get_or (const Container& container, IndexType index, const typename Container::value_type& defaultValue)
106{
107 const auto i = static_cast<typename Container::size_type> (index);
108
109 if (i >= 0 && i < container.size())
110 return container[i];
111
112 return defaultValue;
113}
114
115template<class Type>
116bool assign_if_valid (Type& dest, const std::optional<Type>& src)
117{
118 if (! src)
119 return false;
120
121 dest = *src;
122 return true;
123}
124
125template<class SmartPointerContainer>
126SmartPointerContainer& erase_if_null (SmartPointerContainer& container)
127{
128 container.erase (std::remove_if (container.begin(), container.end(),
129 [] (auto& c) { return ! c; }),
130 container.end());
131 return container;
132}
133
137template<class Container>
138Container& stable_remove_duplicates (Container& container)
139{
141
142 auto new_end = std::remove_if (container.begin(), container.end(),
143 [&seen] (const auto& value)
144 {
145 if (seen.find (value) != seen.end())
146 return true;
147
148 seen.insert (value);
149 return false;
150 });
151 container.erase (new_end, container.end());
152
153 return container;
154}
155
156}}
T accumulate(T... args)
T begin(T... args)
T distance(T... args)
T end(T... args)
T find_if(T... args)
T for_each(T... args)
T forward(T... args)
bool contains_v(const Container &container, typename Container::value_type v)
Returns true if a specific value is found in a container.
Container & stable_remove_duplicates(Container &container)
Removes duplicates from a container maintaining the order.
T remove_if(T... args)
T sort(T... args)
T stable_sort(T... args)