OpenTTD Source 20241224-master-gf74b0cf984
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
23template <typename Container>
24inline bool include(Container &container, typename Container::const_reference &item)
25{
26 const bool is_member = std::ranges::find(container, item) != container.end();
27 if (!is_member) container.emplace_back(item);
28 return is_member;
29}
30
40template <typename Container>
41int find_index(Container const &container, typename Container::const_reference item)
42{
43 auto const it = std::ranges::find(container, item);
44 if (it != container.end()) return std::distance(container.begin(), it);
45
46 return -1;
47}
48
56template <typename TIter>
57auto Slide(TIter first, TIter last, TIter position) -> std::pair<TIter, TIter>
58{
59 if (last < position) return { std::rotate(first, last, position), position };
60 if (position < first) return { position, std::rotate(position, first, last) };
61 return { first, last };
62}
63
64#endif /* CONTAINER_FUNC_HPP */
bool include(Container &container, typename Container::const_reference &item)
Helper function to append an item to a container if it is not already contained.
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_...
auto Slide(TIter first, TIter last, TIter position) -> std::pair< TIter, TIter >
Move elements between first and last to a new position, rotating elements in between as necessary.