Codechange: rename smallvec_type to container_func and use only when needed

This commit is contained in:
Rubidium
2023-05-18 11:20:35 +02:00
committed by rubidium42
parent 80d8c01814
commit 3323402aaa
46 changed files with 45 additions and 43 deletions

View File

@@ -23,7 +23,7 @@ add_files(
random_func.cpp
random_func.hpp
smallstack_type.hpp
smallvec_type.hpp
container_func.hpp
span_type.hpp
strong_typedef_type.hpp
)

View File

@@ -5,46 +5,45 @@
* 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/>.
*/
/** @file smallvec_type.hpp Simple vector class that allows allocating an item without the need to copy this->data needlessly. */
/** @file container_func.hpp Some simple functions to help with accessing containers. */
#ifndef SMALLVEC_TYPE_HPP
#define SMALLVEC_TYPE_HPP
#include "mem_func.hpp"
#ifndef CONTAINER_FUNC_HPP
#define CONTAINER_FUNC_HPP
/**
* Helper function to append an item to a vector if it is not already contained
* Consider using std::set, std::unordered_set or std::flat_set in new code
* Helper function to append an item to a container if it is not already contained.
* The container must have a \c emplace_back function.
* Consider using std::set, std::unordered_set or std::flat_set in new code.
*
* @param vec A reference to the vector to be extended
* @param container A reference to the container to be extended
* @param item Reference to the item to be copy-constructed if not found
*
* @return Whether the item was already present
*/
template <typename T>
inline bool include(std::vector<T>& vec, const T &item)
template <typename Container>
inline bool include(Container &container, typename Container::const_reference &item)
{
const bool is_member = std::find(vec.begin(), vec.end(), item) != vec.end();
if (!is_member) vec.emplace_back(item);
const bool is_member = std::find(container.begin(), container.end(), item) != container.end();
if (!is_member) container.emplace_back(item);
return is_member;
}
/**
* Helper function to get the index of an item
* Consider using std::set, std::unordered_set or std::flat_set in new code
* Consider using std::set, std::unordered_set or std::flat_set in new code.
*
* @param vec A reference to the vector to be extended
* @param container A reference to the container to be searched.
* @param item Reference to the item to be search for
*
* @return Index of element if found, otherwise -1
*/
template <typename T>
int find_index(std::vector<T> const& vec, T const& item)
template <typename Container>
int find_index(Container const &container, typename Container::const_reference item)
{
auto const it = std::find(vec.begin(), vec.end(), item);
if (it != vec.end()) return it - vec.begin();
auto const it = std::find(container.begin(), container.end(), item);
if (it != container.end()) return std::distance(container.begin(), it);
return -1;
}
#endif /* SMALLVEC_TYPE_HPP */
#endif /* CONTAINER_FUNC_HPP */

View File

@@ -10,7 +10,6 @@
#ifndef POOL_TYPE_HPP
#define POOL_TYPE_HPP
#include "smallvec_type.hpp"
#include "enum_type.hpp"
/** Various types of a pool. */

View File

@@ -10,7 +10,6 @@
#ifndef SMALLSTACK_TYPE_HPP
#define SMALLSTACK_TYPE_HPP
#include "smallvec_type.hpp"
#include <mutex>
/**