Path: blob/master/thirdparty/openxr/src/common/extra_algorithms.h
9903 views
// Copyright (c) 2017-2025 The Khronos Group Inc.1// Copyright (c) 2017-2019 Valve Corporation2// Copyright (c) 2017-2019 LunarG, Inc.3// Copyright (c) 2019 Collabora, Ltd.4//5// SPDX-License-Identifier: Apache-2.0 OR MIT6//7// Initial Author: Rylie Pavlik <[email protected]>8//910/*!11* @file12*13* Additional functions along the lines of the standard library algorithms.14*/1516#pragma once1718#include <algorithm>19#include <vector>2021/// Like std::remove_if, except it works on associative containers and it actually removes this.22///23/// The iterator stuff in here is subtle - .erase() invalidates only that iterator, but it returns a non-invalidated iterator to the24/// next valid element which we can use instead of incrementing.25template <typename T, typename Pred>26static inline void map_erase_if(T &container, Pred &&predicate) {27for (auto it = container.begin(); it != container.end();) {28if (predicate(*it)) {29it = container.erase(it);30} else {31++it;32}33}34}3536/*!37* Moves all elements matching the predicate to the end of the vector then erases them.38*39* Combines the two parts of the erase-remove idiom to simplify things and avoid accidentally using the wrong erase overload.40*/41template <typename T, typename Alloc, typename Pred>42static inline void vector_remove_if_and_erase(std::vector<T, Alloc> &vec, Pred &&predicate) {43auto b = vec.begin();44auto e = vec.end();45vec.erase(std::remove_if(b, e, std::forward<Pred>(predicate)), e);46}474849