|
|
|
@ -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 */
|