container: add free 'contains' query

This commit is contained in:
Danny Robson 2020-01-16 11:58:52 +11:00
parent 3c768daf81
commit ac168e86b5
2 changed files with 33 additions and 0 deletions

View File

@ -288,6 +288,7 @@ list (
cmdopt.hpp cmdopt.hpp
colour.cpp colour.cpp
colour.hpp colour.hpp
container.hpp
coord.hpp coord.hpp
coord/fwd.hpp coord/fwd.hpp
coord/base.hpp coord/base.hpp

32
container.hpp Normal file
View File

@ -0,0 +1,32 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2020, Danny Robson <danny@nerdcruft.net>
*/
#pragma once
#include <algorithm>
#include <utility>
namespace cruft {
/// Tests if a value is present in a container.
///
/// Defaults to a naive search of the container.
///
/// \return true if the value is present in the container, otherwise false.
template <typename ContainerT, typename ValueT>
bool
contains (ValueT &&value, ContainerT &&container)
{
auto const pos = std::find (
std::cbegin (container),
std::cend (container),
std::move (value)
);
return pos != std::cend (container);
}
}