qxLib
remove.h
Go to the documentation of this file.
1 /**
2 
3  @file remove.h
4  @author Khrapov
5  @date 6.08.2022
6  @copyright (c) Nick Khrapov, 2022. All right reserved.
7 
8 **/
9 #pragma once
10 
11 #include <utility>
12 
13 namespace qx
14 {
15 
16 /**
17  @brief std::remove_if with index instead of data reference
18  @tparam fwd_it_t - iterator type
19  @tparam predicate_t - predicate type
20  @param itBegin - the range of elements begin iterator
21  @param itEnd - the range of elements end iterator
22  @param predicate - unary predicate which returns true if the element should be removed
23  @param nStartIndex - we can't find out the index of itBegin so you should specify it
24  @return - past-the-end iterator for the new range of values
25 **/
26 template<class fwd_it_t, class predicate_t>
27 fwd_it_t remove_if_i(fwd_it_t itBegin, fwd_it_t itEnd, const predicate_t& predicate, size_t nStartIndex = 0)
28 {
29  fwd_it_t itDest = itBegin;
30  size_t i = nStartIndex;
31  for (fwd_it_t it = itBegin; it != itEnd; ++it)
32  {
33  if (!predicate(i))
34  {
35  if (itDest != it)
36  *itDest = std::move(*it);
37 
38  ++itDest;
39  }
40 
41  ++i;
42  }
43  return itDest;
44 }
45 
46 /**
47  @brief std::remove_if with index instead of data reference
48  @tparam container_t - container type
49  @tparam predicate_t - predicate type
50  @param container - the range of elements
51  @param predicate - unary predicate which returns true if the element should be removed
52  @return - past-the-end iterator for the new range of values
53 **/
54 template<class container_t, class predicate_t>
55 auto remove_if_i(container_t& container, const predicate_t& predicate)
56 {
57  return remove_if_i(container.begin(), container.end(), predicate, 0);
58 }
59 
60 } // namespace qx
fwd_it_t remove_if_i(fwd_it_t itBegin, fwd_it_t itEnd, const predicate_t &predicate, size_t nStartIndex=0)
std::remove_if with index instead of data reference
Definition: remove.h:27