ContainerFunctions.hpp
Go to the documentation of this file.
1 // ========================================================================================
2 // ApproxMVBB
3 // Copyright (C) 2014 by Gabriel Nützi <nuetzig (at) imes (d0t) mavt (d0t) ethz (døt) ch>
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 // ========================================================================================
9 
10 #ifndef ApproxMVBB_ContainerFunctions_hpp
11 #define ApproxMVBB_ContainerFunctions_hpp
12 
13 namespace ApproxMVBB{
14 namespace ContainerFunctions {
15 
23 template<typename Iterator, typename Func>
24 Iterator moveElementsToFrontIf(Iterator b, Iterator e, Func f) {
25 
26  Iterator w = b; // write pointer
27  while ( b != e ) {
28  if( !f(*b) ) { // if not move to front increment read pointer
29  b++;
30  continue;
31  }
32 
33  if(w!=b) { // copy only if not at same position!
34  *w = *b; // copy value to front (test is not true)
35  }
36  ++w;
37  ++b;
38  }
39  return w;
40 }
41 
49 template<typename Iterator,typename Func>
50 Iterator moveElementsToBackIf(Iterator b, Iterator e, Func f) {
51 
52  if( b == e ) {
53  return b;
54  }
55  while(b != e){
56 
57  if(f(*b)){
58  // Move end to next swappable item
59  while(f(*e)){
60  --e;
61  if( e == b){
62  return e;
63  };
64  }
65 
66  // swap with back
67  if(b!=e){
68  std::swap(*b,*e);
69  }
70  }
71  ++b;
72  }
73  return f(*e)? e : ++e;
74 }
75 
76 
84 template<typename Iterator, typename Comp>
85 Iterator moveConsecutiveToFrontIf(Iterator b, Iterator e, Comp c) {
86 
87 
88  if( std::distance(b,e)<2 ) {
89  return e;
90  }
91 
92  Iterator comp = b;
93  Iterator write = ++b;
94  while ( b != e ) {
95  if( !c(*comp, *b ) ) { // if we can skip element or if
96  ++b;
97  continue;
98  }
99 
100  if(write!=b) { // copy only if not at same position!
101  *write = *b; // copy value to front (test is not true)
102  }
103  //std::cout << *dest << std::endl;
104  comp = write++;
105  ++b;
106  }
107 
108  return write;
109 }
118 template<typename Iterator, typename Func, typename Skip>
119 Iterator moveConsecutiveToFrontIf(Iterator b, Iterator e, Func f, Skip s) {
120 
121 
122  if( std::distance(b,e)<2 ) {
123  return e;
124  }
125  Iterator comp;
126  Iterator write = b;
127  // skip all at beginning
128  while(b!=e && s(*b)){
129  ++b;
130  }
131  if(b!=e){
132  // write first element to front
133  if(write!=b){
134  *write = *b;
135  }
136  comp = write++; // shift write pointer (comp is previous)
137 
138  // Start loop over all elements
139  while ( b != e ) {
140  if(s(*b) || !f(*comp, *b ) ) {
141  ++b;
142  continue;
143  }
144 
145  if(write!=b) { // copy only if not at same position!
146  *write = *b; // copy value to front (test is not true)
147  }
148  //std::cout << *dest << std::endl;
149  comp = write++;
150  ++b;
151  }
152  }
153 
154  return write;
155 }
156 
157 
158 
159 }
160 }
161 #endif // ContainerFunctions_hpp
These are some container definitions.
Iterator moveConsecutiveToFrontIf(Iterator b, Iterator e, Comp c)
Iterator moveElementsToFrontIf(Iterator b, Iterator e, Func f)
Iterator moveElementsToBackIf(Iterator b, Iterator e, Func f)


asr_approx_mvbb
Author(s): Gassner Nikolai
autogenerated on Mon Jun 10 2019 12:38:08