ContainerFunctions.hpp
Go to the documentation of this file.
00001 // ========================================================================================
00002 //  ApproxMVBB
00003 //  Copyright (C) 2014 by Gabriel Nützi <nuetzig (at) imes (d0t) mavt (d0t) ethz (døt) ch>
00004 //
00005 //  This Source Code Form is subject to the terms of the Mozilla Public
00006 //  License, v. 2.0. If a copy of the MPL was not distributed with this
00007 //  file, You can obtain one at http://mozilla.org/MPL/2.0/.
00008 // ========================================================================================
00009 
00010 #ifndef ApproxMVBB_ContainerFunctions_hpp
00011 #define ApproxMVBB_ContainerFunctions_hpp
00012 
00013 namespace ApproxMVBB{
00014 namespace ContainerFunctions {
00015 
00023 template<typename Iterator, typename Func>
00024 Iterator moveElementsToFrontIf(Iterator b, Iterator  e, Func f) {
00025 
00026     Iterator w = b;  // write pointer
00027     while  ( b != e ) {
00028         if( !f(*b) ) { // if not move to front increment read pointer
00029             b++;
00030             continue;
00031         }
00032 
00033         if(w!=b) {   // copy only if not at same position!
00034             *w = *b; // copy  value to front (test is not true)
00035         }
00036         ++w;
00037         ++b;
00038     }
00039     return w;
00040 }
00041 
00049 template<typename Iterator,typename Func>
00050 Iterator moveElementsToBackIf(Iterator b, Iterator  e, Func f) {
00051 
00052     if( b == e  ) {
00053         return b;
00054     }
00055     while(b != e){
00056 
00057         if(f(*b)){
00058             // Move end to next swappable item
00059             while(f(*e)){
00060                 --e;
00061                 if( e == b){
00062                     return e;
00063                 };
00064             }
00065 
00066             // swap with back
00067             if(b!=e){
00068                 std::swap(*b,*e);
00069             }
00070         }
00071         ++b;
00072     }
00073     return f(*e)? e : ++e;
00074 }
00075 
00076 
00084 template<typename Iterator, typename Comp>
00085 Iterator moveConsecutiveToFrontIf(Iterator b, Iterator  e, Comp c) {
00086 
00087 
00088     if( std::distance(b,e)<2 ) {
00089         return e;
00090     }
00091 
00092     Iterator comp = b;
00093     Iterator write = ++b;
00094     while  ( b != e ) {
00095         if( !c(*comp, *b ) ) { // if we can skip element or if
00096             ++b;
00097             continue;
00098         }
00099 
00100         if(write!=b) {   // copy only if not at same position!
00101             *write = *b; // copy  value to front (test is not true)
00102         }
00103         //std::cout << *dest << std::endl;
00104         comp = write++;
00105         ++b;
00106     }
00107 
00108     return write;
00109 }
00118 template<typename Iterator, typename Func, typename Skip>
00119 Iterator moveConsecutiveToFrontIf(Iterator b, Iterator  e, Func f, Skip s) {
00120 
00121 
00122     if( std::distance(b,e)<2 ) {
00123         return e;
00124     }
00125     Iterator comp;
00126     Iterator write = b;
00127     // skip all at beginning
00128         while(b!=e && s(*b)){
00129                 ++b;
00130         }
00131         if(b!=e){
00132                 // write first element to front
00133                 if(write!=b){
00134                         *write = *b;
00135                 }
00136                 comp = write++; // shift write pointer (comp is previous)
00137 
00138         // Start loop over all elements
00139             while  ( b != e ) {
00140                 if(s(*b) || !f(*comp, *b ) ) {
00141                     ++b;
00142                     continue;
00143                 }
00144 
00145                 if(write!=b) {   // copy only if not at same position!
00146                     *write = *b; // copy  value to front (test is not true)
00147                 }
00148                 //std::cout << *dest << std::endl;
00149                 comp = write++;
00150                 ++b;
00151             }
00152                 }
00153 
00154     return write;
00155 }
00156 
00157 
00158 
00159 }
00160 }
00161 #endif // ContainerFunctions_hpp


asr_approx_mvbb
Author(s): Gassner Nikolai
autogenerated on Sat Jun 8 2019 20:21:49