Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
AlgorithmFind.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/Assert.h"
5
6namespace SC
7{
9namespace Algorithms
10{
11
14
23template <typename ForwardIterator, typename UnaryPredicate>
24constexpr ForwardIterator findIf(ForwardIterator first, ForwardIterator last, UnaryPredicate&& predicate,
25 size_t* index = nullptr)
26{
27 SC_ASSERT_DEBUG(first <= last);
28 for (auto it = first; it != last; ++it)
29 {
30 if (predicate(*it))
31 {
32 if (index)
33 {
34 *index = static_cast<size_t>(it - first);
35 }
36 return it;
37 }
38 }
39 return last;
40}
41
49template <typename Container, typename T>
50constexpr bool contains(Container container, const T& value, size_t* index = nullptr)
51{
52 return findIf(
53 container.begin(), container.end(), [&](auto& item) { return item == value; }, index) != container.end();
54}
55
57
58} // namespace Algorithms
59} // namespace SC
constexpr bool contains(Container container, const T &value, size_t *index=nullptr)
Check if the container contains a given value.
Definition AlgorithmFind.h:50
constexpr ForwardIterator findIf(ForwardIterator first, ForwardIterator last, UnaryPredicate &&predicate, size_t *index=nullptr)
Find item satisfying the given predicate.
Definition AlgorithmFind.h:24
#define SC_ASSERT_DEBUG(e)
Assert expression e to be true.
Definition Assert.h:82