OpenTTD Source: src/core/container_func.hpp Source File

OpenTTD Source 14.0-beta1

container_func.hpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10 #ifndef CONTAINER_FUNC_HPP
11 #define CONTAINER_FUNC_HPP
12
23 template <typename Container>
24 inline bool include(Container &container, typename Container::const_reference &item)
25 {
26 const bool is_member = std::find(container.begin(), container.end(), item) != container.end();
27 if (!is_member) container.emplace_back(item);
28 return is_member;
29 }
30
40 template <typename Container>
41 int find_index(Container const &container, typename Container::const_reference item)
42 {
43 auto const it = std::find(container.begin(), container.end(), item);
44 if (it != container.end()) return std::distance(container.begin(), it);
45
46 return -1;
47 }
48
49 #endif /* CONTAINER_FUNC_HPP */
find_index
int find_index(Container const &container, typename Container::const_reference item)
Helper function to get the index of an item Consider using std::set, std::unordered_set or std::flat_...
Definition: container_func.hpp:41
include
bool include(Container &container, typename Container::const_reference &item)
Helper function to append an item to a container if it is not already contained.
Definition: container_func.hpp:24