ML Reference
mlStdAlgorithms.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2010, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef ML_STD_ALGORITHMS_H
14#define ML_STD_ALGORITHMS_H
15
16#include "mlUtilsSystem.h"
17
18#include <ThirdPartyWarningsDisable.h>
19#include <algorithm>
20#include <vector>
21#include <ThirdPartyWarningsRestore.h>
22
23ML_UTILS_START_NAMESPACE
24
26template <typename T>
27inline bool vector_contains(const std::vector<T> &vec, const T& value) {
28 return std::find(vec.begin(), vec.end(), value) != vec.end();
29}
30
32template <typename T>
33inline bool vector_remove(std::vector<T> &vec, const T& value) {
34 auto it = std::find(vec.begin(), vec.end(), value);
35 if (it != vec.end()) {
36 vec.erase(it);
37 return true;
38 } else {
39 return false;
40 }
41}
42
44template <typename T>
45inline void vector_remove_all(std::vector<T> &vec, const T& value) {
46 auto it = vec.begin();
47 while (it != vec.end()) {
48 if (*it == value) {
49 it = vec.erase(it);
50 } else {
51 ++it;
52 }
53 }
54}
55
57template <typename T>
58inline bool vector_replace_by_NULL(std::vector<T> &vec, const T& value) {
59 auto it = std::find(vec.begin(), vec.end(), value);
60 if (it != vec.end()) {
61 *it = nullptr;
62 return true;
63 } else {
64 return false;
65 }
66}
67
68ML_UTILS_END_NAMESPACE
69
70#endif // __mlStdAlgorithms_H
bool vector_contains(const std::vector< T > &vec, const T &value)
Helper template that searches for value in given vector vec and returns true if it was found.
bool vector_replace_by_NULL(std::vector< T > &vec, const T &value)
Helper template that replaces the first occurrence of value in given vector vec with NULL and returns...
bool vector_remove(std::vector< T > &vec, const T &value)
Helper template that removes the first occurrence of value in given vector vec and returns true if it...
void vector_remove_all(std::vector< T > &vec, const T &value)
Helper template that removes all occurrences of value in given vector vec.