![]() |
ImFusion SDK 4.3
|
Utility functions for (STL) container access and manipulation. More...
Utility functions for (STL) container access and manipulation.
Classes | |
class | LRUCache< Key, Value, Hash, KeyEqual > |
Templated container class implementing a Least Recently Used Cache. More... | |
Search for elements | |
template<typename ContainerT, typename T> | |
auto | find (ContainerT &&container, const T &value) -> decltype(container.begin()) |
Convenience function wrapping std::find() for the given container. | |
template<typename ContainerT, typename UnaryPredicate> | |
auto | findIf (ContainerT &&container, UnaryPredicate &&predicate) -> decltype(container.begin()) |
Convenience function wrapping std::find_if() for the given container. | |
template<typename ContainerT, typename T> | |
auto | findPtr (ContainerT &&container, T &&pointer) -> decltype(container.begin()) |
Convenience function to search for a raw pointer in a container of smart pointers. | |
template<typename ContainerT, typename ElementType, typename T> | |
auto | findIfMember (ContainerT &&container, T ElementType::*member, const T &value) -> decltype(container.begin()) |
Return an iterator to the first element where member equals value. | |
template<typename ContainerT, typename T> | |
int64_t | indexOf (ContainerT &&container, const T &value) |
Find the index of the first element that is equal to value inside container. | |
template<typename ContainerT, typename UnaryPredicate> | |
int64_t | indexOfIf (ContainerT &&container, UnaryPredicate &&predicate) |
Find the index of the first element that matches predicate. | |
template<typename ContainerT, typename T> | |
int64_t | indexOfPtr (ContainerT &&container, T &&pointer) |
Convenience function to get the index of the first occurence of pointer in a container of smart pointers. | |
template<typename ContainerT, typename T> | |
bool | contains (const ContainerT &container, const T &value) |
Return true if container contains value or false otherwise. | |
template<typename ContainerT, typename UnaryPredicate> | |
bool | containsIf (const ContainerT &container, UnaryPredicate &&predicate) |
Return true if container contains any value that matches predicate. | |
template<typename ContainerT, typename T> | |
bool | containsPtr (const ContainerT &container, T &&pointer) |
Convenience function check whether a container of smart pointers contains a given raw pointer. | |
template<typename ContainerT, typename ElementType, typename T> | |
bool | containsIfMember (const ContainerT &container, T ElementType::*member, const T &value) |
Return true if the container contains an element where member equals value. | |
template<typename ContainerT, typename T> | |
int64_t | countValue (const ContainerT &c, const T &value) |
Return the number of elements in c that are equal to value. | |
template<typename ContainerT, class UnaryPredicate> | |
int64_t | countIf (const ContainerT &c, UnaryPredicate &&pred) |
Return the number of elements in c that match the given predicate. | |
Erase or extract elements from containers | |
template<typename ContainerT, typename T> | |
int64_t | erase (ContainerT &container, T &&value) |
Convenience function to remove and erase all elements matching value from container. | |
template<typename ContainerT, typename UnaryPredicate> | |
int64_t | eraseIf (ContainerT &container, UnaryPredicate &&predicate) |
Convenience function to remove and erase all elements from container that match the predicate. | |
template<typename ContainerT> | |
int64_t | eraseDuplicates (ContainerT &container) |
Removes all duplicates from the given container by calling std::sort() , std::unique() and std::erase() . | |
template<typename ContainerT> | |
int64_t | eraseDuplicatesStable (ContainerT &container) |
Removes all duplicates from the given container keeping the original order intact. | |
template<typename ContainerT, typename UnaryPredicate> | |
ContainerT | extractIf (ContainerT &container, UnaryPredicate &&predicate) |
Move-extracts all elements from container that match the given predicate. | |
Transform elements | |
template<typename ContainerT, typename UnaryOp> | |
auto | transformed (ContainerT &&container, UnaryOp &&op) -> std::vector< std::decay_t< decltype(op(*std::begin(container)))> > |
A container-based wrapper for std::transform . | |
template<typename T, class UnaryOperation> | |
auto | transformed (std::initializer_list< T > elements, UnaryOperation &&op) -> std::vector< std::decay_t< decltype(op(*elements.begin()))> > |
An overload for the container-based wrapper for std::transform , taking an initializer list as the container. | |
template<typename T, typename U, class BinaryOperation> | |
auto | transformed (const std::vector< T > &containerA, const std::vector< U > &containerB, BinaryOperation &&op) -> std::vector< std::decay_t< decltype(op(containerA.front(), containerB.front()))> > |
A container-based wrapper for std::transform taking two constainers. | |
template<typename T, template< typename... > class SmartPtr> | |
std::vector< T * > | asRawPointers (const std::vector< SmartPtr< T > > &container) |
Converts a vector of smart pointers (such as std::unique_ptr or std::shared_ptr) to a vector of corresponding raw pointers. | |
template<typename T, typename ContainerT> | |
std::vector< T > | casted (const ContainerT &container) |
Converts the input container to a std::vector<T> by casting each element to T . | |
template<typename T, typename U, class BinaryOperation> | |
void | forEachZip (const std::vector< T > &containerA, const std::vector< U > &containerB, BinaryOperation &&op) |
A convenience function which applies a binary operator to two containers. | |
template<typename ContainerT, typename UnaryPred> | |
ContainerT | filtered (const ContainerT &container, UnaryPred &&pred) |
Filters certain elements out of a container. | |
template<typename ContainerT, typename UnaryPred, typename UnaryOperation> | |
auto | filteredTransform (const ContainerT &container, UnaryPred &&predicate, UnaryOperation &&op) -> std::vector< std::decay_t< decltype(op(*container.begin()))> > |
Combines filtered() and transformed() into a single pass operation. | |
Accumulate elements | |
template<typename ContainerT> | |
auto | accumulate (const ContainerT &c) -> typename ContainerT::value_type |
A container-based wrapper for std::accumulate . | |
template<typename ContainerT, typename T> | |
T | accumulate (const ContainerT &c, T init) |
A container-based wrapper for std::accumulate . | |
template<typename ContainerT, typename T, typename BinaryOp> | |
T | accumulate (const ContainerT &c, T init, BinaryOp &&op) |
A container-based wrapper for std::accumulate . | |
Iterate over a range having acces to both the index and the value of each element. | |
template<class Iterator> | |
auto | enumerate (Iterator begin, Iterator end) |
Helper function to have access to both the index and value of each element during iteration. | |
template<class Container> | |
auto | enumerate (Container &content) |
Helper function to have access to both the index and value of each element during iteration. | |
template<class Container> | |
auto | enumerate (Container &&content) |
Helper function to have access to both the index and value of each element during iteration. | |
Other Functions | |
template<typename ContainerT, class UnaryPredicate> | |
bool | allOf (const ContainerT &container, UnaryPredicate &&predicate) |
Checks whether all elements in container match predicate. | |
template<typename ContainerT, class UnaryPredicate> | |
bool | anyOf (const ContainerT &container, UnaryPredicate &&predicate) |
Checks whether any element in container match predicate. | |
template<typename ContainerT, class UnaryPredicate> | |
bool | noneOf (const ContainerT &container, UnaryPredicate &&predicate) |
Checks whether no elements in container match predicate. | |
template<typename ContainerT, class BinaryPredicate> | |
bool | allEqual (const ContainerT &container, BinaryPredicate &&predicate) |
A container-based algorithms that checks whether all elements in a container are equal to each other. | |
template<typename ContainerT> | |
bool | allEqual (const ContainerT &container) |
A container-based algorithm that checks whether all elements in a container are equal to each other. | |
template<typename ContainerT> | |
void | naturalSort (ContainerT &container) |
Utility function implementing a "natural sort" algorithm for strings. | |
template<typename ContainerT> | |
void | renameKey (ContainerT &container, const typename ContainerT::key_type &oldKey, const typename ContainerT::key_type &newKey) |
Convenience function to change the key for a map entry. | |
auto findPtr | ( | ContainerT && | container, |
T && | pointer ) -> decltype(container.begin()) |
#include <ImFusion/Core/Container.h>
Convenience function to search for a raw pointer in a container of smart pointers.
auto findIfMember | ( | ContainerT && | container, |
T ElementType::* | member, | ||
const T & | value ) -> decltype(container.begin()) |
#include <ImFusion/Core/Container.h>
Return an iterator to the first element where member equals value.
Use a pointer to member to define which part of the element you want to compare, e.g.
int64_t indexOf | ( | ContainerT && | container, |
const T & | value ) |
#include <ImFusion/Core/Container.h>
Find the index of the first element that is equal to value inside container.
Return -1 if the value is not part of the container.
int64_t indexOfIf | ( | ContainerT && | container, |
UnaryPredicate && | predicate ) |
#include <ImFusion/Core/Container.h>
Find the index of the first element that matches predicate.
Return -1 if no such element is in the container.
int64_t indexOfPtr | ( | ContainerT && | container, |
T && | pointer ) |
#include <ImFusion/Core/Container.h>
Convenience function to get the index of the first occurence of pointer in a container of smart pointers.
Return -1 if no such element is in the container.
bool containsPtr | ( | const ContainerT & | container, |
T && | pointer ) |
#include <ImFusion/Core/Container.h>
Convenience function check whether a container of smart pointers contains a given raw pointer.
bool containsIfMember | ( | const ContainerT & | container, |
T ElementType::* | member, | ||
const T & | value ) |
#include <ImFusion/Core/Container.h>
Return true if the container contains an element where member equals value.
Use a pointer to member to define which part of the element you want to compare, e.g.
int64_t countValue | ( | const ContainerT & | c, |
const T & | value ) |
#include <ImFusion/Core/Container.h>
Return the number of elements in c that are equal to value.
c | A container of elements. |
value | A single value equals-comparable to the elements in c to count. |
int64_t countIf | ( | const ContainerT & | c, |
UnaryPredicate && | pred ) |
#include <ImFusion/Core/Container.h>
Return the number of elements in c that match the given predicate.
c | A container of elements. |
pred | A unary predicate which takes elements of the container returns a bool. |
int64_t erase | ( | ContainerT & | container, |
T && | value ) |
#include <ImFusion/Core/Container.h>
Convenience function to remove and erase all elements matching value from container.
Returns the number of elements that were removed.
int64_t eraseIf | ( | ContainerT & | container, |
UnaryPredicate && | predicate ) |
#include <ImFusion/Core/Container.h>
Convenience function to remove and erase all elements from container that match the predicate.
Returns the number of elements that were removed.
int64_t eraseDuplicates | ( | ContainerT & | container | ) |
#include <ImFusion/Core/Container.h>
Removes all duplicates from the given container by calling std::sort()
, std::unique()
and std::erase()
.
Returns the number of elements that were removed.
O(N log N)
time complexity. Use the slower eraseDuplicatesStable() variant if you require that the element remains unchanged or the element type is not sortable. int64_t eraseDuplicatesStable | ( | ContainerT & | container | ) |
#include <ImFusion/Core/Container.h>
Removes all duplicates from the given container keeping the original order intact.
Returns the number of elements that were removed.
O(n^2)
. Prefer using the faster eraseDuplicates() variant if you don't care about the element order. ContainerT extractIf | ( | ContainerT & | container, |
UnaryPredicate && | predicate ) |
#include <ImFusion/Core/Container.h>
Move-extracts all elements from container that match the given predicate.
ContainerT | Must be a re-orderable container (e.g. a std::vector works but a SequenceContainer such as std::set does not) |
UnaryPredicate | Function that can be applied to elements of container and returns a bool |
auto transformed | ( | ContainerT && | container, |
UnaryOp && | op ) -> std::vector<std::decay_t<decltype(op(*std::begin(container)))>> |
#include <ImFusion/Core/Container.h>
A container-based wrapper for std::transform
.
Returns a std::vector with the results of applying op to each element in container.
auto transformed | ( | std::initializer_list< T > | elements, |
UnaryOperation && | op ) -> std::vector<std::decay_t<decltype(op(*elements.begin()))>> |
#include <ImFusion/Core/Container.h>
An overload for the container-based wrapper for std::transform
, taking an initializer list as the container.
auto transformed | ( | const std::vector< T > & | containerA, |
const std::vector< U > & | containerB, | ||
BinaryOperation && | op ) -> std::vector<std::decay_t<decltype(op(containerA.front(), containerB.front()))>> |
#include <ImFusion/Core/Container.h>
A container-based wrapper for std::transform
taking two constainers.
containerA | A container of elements with type T. |
containerB | A container of elements with type U. |
op | A binary operation which transforms elements of type T and U to elements of type V |
std::vector< T * > asRawPointers | ( | const std::vector< SmartPtr< T > > & | container | ) |
#include <ImFusion/Core/Container.h>
Converts a vector of smart pointers (such as std::unique_ptr or std::shared_ptr) to a vector of corresponding raw pointers.
This is a convenience function implementing transformed(container, [](const auto& ptr) { return ptr.get(); })
.
container | A std::vector of a smart pointer type. The smart pointer must implement the .get() member function to retrieve the underlying raw pointer. |
std::vector< T > casted | ( | const ContainerT & | container | ) |
#include <ImFusion/Core/Container.h>
Converts the input container to a std::vector<T>
by casting each element to T
.
This is a convenience function implementing transformed(container, [](const auto& value) { return static_cast<T>(value); })
.
container | An iterable container where each element can be static_casted to T . |
void forEachZip | ( | const std::vector< T > & | containerA, |
const std::vector< U > & | containerB, | ||
BinaryOperation && | op ) |
#include <ImFusion/Core/Container.h>
A convenience function which applies a binary operator to two containers.
Conceptually the same as calling zip on the two containers, and then calling for_each on the result of that.
containerA | A container of elements with type T. |
containerB | A container of elements with type U. It must have at least as many elements as containerA. |
op | A binary operation which does something with elements of type T and U, op(T,U) -> void . |
Example:
will result in the console output:
Note that the extra element in ys
was ignored.
ContainerT filtered | ( | const ContainerT & | container, |
UnaryPred && | pred ) |
#include <ImFusion/Core/Container.h>
Filters certain elements out of a container.
container | A container of elements. |
pred | A callable predicate taking elements of the container and returning true for those elements that should be kept in the filtered container. |
Example:
auto filteredTransform | ( | const ContainerT & | container, |
UnaryPred && | predicate, | ||
UnaryOperation && | op ) -> std::vector<std::decay_t<decltype(op(*container.begin()))>> |
#include <ImFusion/Core/Container.h>
Combines filtered() and transformed() into a single pass operation.
Iterates over all elements in container. All elements where predicate yields true are passed to op and the results are accumulated in a std::vector which is eventually returned.
Example:
auto accumulate | ( | const ContainerT & | c | ) | -> typename ContainerT::value_type |
#include <ImFusion/Core/Container.h>
A container-based wrapper for std::accumulate
.
c | A container of elements with type T. |
operator+()
to all elements of the containerExample:
T accumulate | ( | const ContainerT & | c, |
T | init ) |
#include <ImFusion/Core/Container.h>
A container-based wrapper for std::accumulate
.
c | A container of elements with type T. |
init | The initial value of the accumulation. |
Example:
T accumulate | ( | const ContainerT & | c, |
T | init, | ||
BinaryOp && | op ) |
#include <ImFusion/Core/Container.h>
A container-based wrapper for std::accumulate
.
c | A container of elements with type T. |
init | The initial value of the accumulation. It must have the same type U as returned by the given binary operator. |
op | The binary operation to be applied to the elements of the container. It has the signature op(U, T) -> U. |
Example:
auto enumerate | ( | Iterator | begin, |
Iterator | end ) |
#include <ImFusion/Core/Container.h>
Helper function to have access to both the index and value of each element during iteration.
The function returns an iterable object where each element is a small index-value pair, providing pair.index
and pair.value
to access the index and current value of the iteration respectively.
Example usage
auto enumerate | ( | Container & | content | ) |
#include <ImFusion/Core/Container.h>
Helper function to have access to both the index and value of each element during iteration.
The function returns an iterable object where each element is a small index-value pair, providing pair.index
and pair.value
to access the index and current value of the iteration respectively.
Example usage
auto enumerate | ( | Container && | content | ) |
#include <ImFusion/Core/Container.h>
Helper function to have access to both the index and value of each element during iteration.
The function returns an iterable object where each element is a small index-value pair, providing pair.index
and pair.value
to access the index and current value of the iteration respectively.
Example usage
bool allEqual | ( | const ContainerT & | container, |
BinaryPredicate && | predicate ) |
#include <ImFusion/Core/Container.h>
A container-based algorithms that checks whether all elements in a container are equal to each other.
container | A container of elements. |
predicate | Binary predicate accepting two elements from container and returning if they are equal. |
Example:
bool allEqual | ( | const ContainerT & | container | ) |
#include <ImFusion/Core/Container.h>
A container-based algorithm that checks whether all elements in a container are equal to each other.
container | A container of elements. |
operator==()
must be defined for the type of elements that are in the container.Example:
void naturalSort | ( | ContainerT & | container | ) |
#include <ImFusion/Core/Container.h>
Utility function implementing a "natural sort" algorithm for strings.
The natural sort order is the same as the alphanumerical sort order except that multi-digit numbers are treated atomically. E.g.:
container | Container with random access iterator and std::string elements |
|
inline |
#include <ImFusion/Core/Container.h>
Convenience function to change the key for a map entry.
container | A std::map or a std::unordered_map. |
oldKey | the old value of the key |
newKey | the new value of the key |