]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/stdalgo.h
758845312c313828979b38df2e2d639e8dc31b4c
[user/henk/code/inspircd.git] / include / stdalgo.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2014 Attila Molnar <attilamolnar@hush.com>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #pragma once
21
22 namespace stdalgo
23 {
24         namespace vector
25         {
26                 /**
27                  * Erase a single element from a vector by overwriting it with a copy of the last element,
28                  * which is then removed. This, in contrast to vector::erase(), does not result in all
29                  * elements after the erased element being moved.
30                  * @param vect Vector to remove the element from
31                  * @param it Iterator to the element to remove
32                  * @return Nothing, but all iterators, references and pointers to the erased element and the
33                  * last element are invalidated
34                  */
35                 template <typename T>
36                 inline void swaperase(typename std::vector<T>& vect, const typename std::vector<T>::iterator& it)
37                 {
38                         *it = vect.back();
39                         vect.pop_back();
40                 }
41
42                 /**
43                  * Find and if exists, erase a single element from a vector by overwriting it with a
44                  * copy of the last element, which is then removed. This, in contrast to vector::erase(),
45                  * does not result in all elements after the erased element being moved.
46                  * If the given value occurs multiple times, the one with the lowest index is removed.
47                  * Individual elements are compared to the given value using operator==().
48                  * @param vect Vector to remove the element from
49                  * @param val Value of the element to look for and remove
50                  * @return True if the element was found and removed, false if it wasn't found.
51                  * If true, all iterators, references and pointers pointing to either the first element that
52                  * is equal to val or to the last element are invalidated.
53                  */
54                 template <typename T>
55                 inline bool swaperase(typename std::vector<T>& vect, const T& val)
56                 {
57                         const typename std::vector<T>::iterator it = std::find(vect.begin(), vect.end(), val);
58                         if (it != vect.end())
59                         {
60                                 swaperase(vect, it);
61                                 return true;
62                         }
63                         return false;
64                 }
65         }
66 }