OpenTTD
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 #include <algorithm>
17 
27 template <typename T>
28 inline bool include(std::vector<T>& vec, const T &item)
29 {
30  const bool is_member = std::find(vec.begin(), vec.end(), item) != vec.end();
31  if (!is_member) vec.emplace_back(item);
32  return is_member;
33 }
34 
44 template <typename T>
45 int find_index(std::vector<T> const& vec, T const& item)
46 {
47  auto const it = std::find(vec.begin(), vec.end(), item);
48  if (it != vec.end()) return it - vec.begin();
49 
50  return -1;
51 }
52 
53 #endif /* SMALLVEC_TYPE_HPP */
Functions related to the allocation of memory.
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...
Functions related to memory operations.
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_...