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

OpenTTD Source 13.1

smallvec_type.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 SMALLVEC_TYPE_HPP
11 #define SMALLVEC_TYPE_HPP
12
13 #include "alloc_func.hpp"
14 #include "mem_func.hpp"
15 #include <vector>
16
26 template <typename T>
27 inline bool include(std::vector<T>& vec, const T &item)
28 {
29 const bool is_member = std::find(vec.begin(), vec.end(), item) != vec.end();
30 if (!is_member) vec.emplace_back(item);
31 return is_member;
32 }
33
43 template <typename T>
44 int find_index(std::vector<T> const& vec, T const& item)
45 {
46 auto const it = std::find(vec.begin(), vec.end(), item);
47 if (it != vec.end()) return it - vec.begin();
48
49 return -1;
50 }
51
52 #endif /* SMALLVEC_TYPE_HPP */
mem_func.hpp
include
bool include(std::vector< T > &vec, const T &item)
Helper function to append an item to a vector if it is not already contained Consider using std::set,...
Definition: smallvec_type.hpp:27
find_index
int find_index(std::vector< T > const &vec, T const &item)
Helper function to get the index of an item Consider using std::set, std::unordered_set or std::flat_...
Definition: smallvec_type.hpp:44
alloc_func.hpp