SLIP  1.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stl_algo_ext.hpp
Go to the documentation of this file.
1 /*
2  * Copyright(c):
3  * Signal Image and Communications (SIC) Department
4  * http://www.sic.sp2mi.univ-poitiers.fr/
5  * - University of Poitiers, France http://www.univ-poitiers.fr
6  * - XLIM Institute UMR CNRS 7252 http://www.xlim.fr/
7  *
8  * and
9  *
10  * D2 Fluid, Thermic and Combustion
11  * - University of Poitiers, France http://www.univ-poitiers.fr
12  * - PPRIME Institute - UPR CNRS 3346 http://www.pprime.fr
13  * - ISAE-ENSMA http://www.ensma.fr
14  *
15  * Contributor(s):
16  * The SLIP team,
17  * Benoit Tremblais <tremblais_AT_sic.univ-poitiers.fr>,
18  * Laurent David <laurent.david_AT_lea.univ-poitiers.fr>,
19  * Ludovic Chatellier <ludovic.chatellier_AT_univ-poitiers.fr>,
20  * Lionel Thomas <lionel.thomas_AT_univ-poitiers.fr>,
21  * Denis Arrivault <arrivault_AT_sic.univ-poitiers.fr>,
22  * Julien Dombre <julien.dombre_AT_univ-poitiers.fr>.
23  *
24  * Description:
25  * The Simple Library of Image Processing (SLIP) is a new image processing
26  * library. It is written in the C++ language following as much as possible
27  * the ISO/ANSI C++ standard. It is consequently compatible with any system
28  * satisfying the ANSI C++ complience. It works on different Unix , Linux ,
29  * Mircrosoft Windows and Mac OS X plateforms. SLIP is a research library that
30  * was created by the Signal, Image and Communications (SIC) departement of
31  * the XLIM, UMR 7252 CNRS Institute in collaboration with the Fluids, Thermic
32  * and Combustion departement of the P', UPR 3346 CNRS Institute of the
33  * University of Poitiers.
34  *
35  * The SLIP Library source code has been registered to the APP (French Agency
36  * for the Protection of Programs) by the University of Poitiers and CNRS,
37  * under registration number IDDN.FR.001.300034.000.S.P.2010.000.21000.
38 
39  * http://www.sic.sp2mi.univ-poitiers.fr/slip/
40  *
41  * This software is governed by the CeCILL-C license under French law and
42  * abiding by the rules of distribution of free software. You can use,
43  * modify and/ or redistribute the software under the terms of the CeCILL-C
44  * license as circulated by CEA, CNRS and INRIA at the following URL
45  * http://www.cecill.info.
46  * As a counterpart to the access to the source code and rights to copy,
47  * modify and redistribute granted by the license, users are provided only
48  * with a limited warranty and the software's author, the holder of the
49  * economic rights, and the successive licensors have only limited
50  * liability.
51  *
52  * In this respect, the user's attention is drawn to the risks associated
53  * with loading, using, modifying and/or developing or reproducing the
54  * software by the user in light of its specific status of free software,
55  * that may mean that it is complicated to manipulate, and that also
56  * therefore means that it is reserved for developers and experienced
57  * professionals having in-depth computer knowledge. Users are therefore
58  * encouraged to load and test the software's suitability as regards their
59  * requirements in conditions enabling the security of their systems and/or
60  * data to be ensured and, more generally, to use and operate it in the
61  * same conditions as regards security.
62  *
63  * The fact that you are presently reading this means that you have had
64  * knowledge of the CeCILL-C license and that you accept its terms.
65  */
66 
67 
74 #ifndef SLIP_STL_ALGO_EXT_HPP
75 #define SLIP_STL_ALGO_EXT_HPP
76 #include <iostream>
77 #include <cassert>
78 
79 namespace slip
80 {
81 
83  /* @{ */
84 
85 
86 
147  template<typename _InputIterator,
148  typename _MaskIterator,
149  typename _OutputIterator,
150  typename _UnaryOperation>
151  _OutputIterator
152  transform_mask_un(_InputIterator first, _InputIterator last,
153  _MaskIterator mask_first,
154  _OutputIterator result,
155  _UnaryOperation unary_op,
156  typename std::iterator_traits<_MaskIterator>::value_type value=typename std::iterator_traits<_MaskIterator>::value_type(1))
157  {
158  assert(first != last);
159  for ( ; first != last; ++first, ++result, ++mask_first)
160  {
161  if(*mask_first == value)
162  {
163  *result = unary_op(*first);
164  }
165  }
166  return result;
167  }
168 
234  template<typename _InputIterator1,
235  typename _InputIterator2,
236  typename _MaskIterator,
237  typename _OutputIterator,
238  typename _BinaryOperation>
239  _OutputIterator
240  transform_mask_bin(_InputIterator1 first1, _InputIterator1 last1,
241  _MaskIterator mask_first,
242  _InputIterator2 first2,
243  _OutputIterator result,
244  _BinaryOperation binary_op,
245  typename std::iterator_traits<_MaskIterator>::value_type value=typename std::iterator_traits<_MaskIterator>::value_type(1))
246  {
247  assert(first1 != last1);
248  for ( ; first1 != last1; ++first1, ++first2,++result, ++mask_first)
249  {
250  if(*mask_first == value)
251  {
252  *result = binary_op(*first1, *first2);
253  }
254  }
255  return result;
256 
257  }
258 
259 
303  template<typename _InputIterator,
304  typename _OutputIterator,
305  typename _UnaryOperation,
306  typename _Predicate>
307  _OutputIterator
308  transform_if(_InputIterator first, _InputIterator last,
309  _OutputIterator result,
310  _UnaryOperation unary_op,
311  _Predicate pred)
312  {
313  assert(first != last);
314  for ( ; first != last; ++first, ++result)
315  {
316  if(pred(*first))
317  {
318  *result = unary_op(*first);
319  }
320  }
321  return result;
322  }
323 
324 
372  template<typename _InputIterator1,
373  typename _InputIterator2,
374  typename _OutputIterator,
375  typename _BinaryOperation,
376  typename _Predicate>
377  _OutputIterator
378  transform_if(_InputIterator1 first1, _InputIterator1 last1,
379  _InputIterator2 first2,
380  _OutputIterator result,
381  _BinaryOperation binary_op,
382  _Predicate pred)
383  {
384  assert(first1 != last1);
385  for ( ; first1 != last1; ++first1, ++first2,++result)
386  {
387  if(pred(*first1))
388  {
389  *result = binary_op(*first1, *first2);
390  }
391  }
392  return result;
393 
394  }
395 
396  /* @} */
397 
399  /* @{ */
400 
426  template<typename _ForwardIterator,typename _MaskIterator>
427 
428  _ForwardIterator max_element_mask( _ForwardIterator first,
429  _ForwardIterator last,
430  _MaskIterator mask_first,
431  typename std::iterator_traits<_MaskIterator>::value_type value=typename std::iterator_traits<_MaskIterator>::value_type(1))
432  {
433  if (first == last) return first;
434  bool nul_mask = true;
435  _ForwardIterator result = first;
436  ++mask_first;
437 
438  while (++first != last)
439  {
440  if(*mask_first == value)
441  {
442  if(*result < *first)
443  {
444  result = first;
445  }
446  nul_mask = false;
447  }
448  ++mask_first;
449  }
450  return (nul_mask ? last : result);
451 
452  }
453 
481  template<typename _ForwardIterator,typename Predicate>
482 
483  _ForwardIterator max_element_if( _ForwardIterator first, _ForwardIterator last,
484  Predicate pred)
485  {
486  if (first == last) return first;
487  //find the first value verifying the predicate
488  while(!pred(*first++)&& first != last)
489  {}
490 
491  if(first == last) return last;
492 
493  _ForwardIterator result = first;
494  while (first != last)
495  {
496  if(pred(*first) && (*first > *result))
497  {
498  result = first;
499  }
500  ++first;
501  }
502 
503  return result;
504 
505  }
539  template<typename _ForwardIterator,typename _MaskIterator,typename _Compare>
540 
541  _ForwardIterator max_element_mask_compare( _ForwardIterator first, _ForwardIterator last,
542  _MaskIterator mask_first,_Compare comp,
543  typename std::iterator_traits<_MaskIterator>::value_type value=typename std::iterator_traits<_MaskIterator>::value_type(1))
544  {
545  if (first == last) return first;
546  bool nul_mask = true;
547  _ForwardIterator result = first;
548  ++mask_first;
549 
550  while (++first != last)
551  {
552  if(*mask_first == value)
553  {
554  if(comp(*result,*first))
555  {
556  result = first;
557  }
558  nul_mask = false;
559  }
560  ++mask_first;
561  }
562  return (nul_mask ? last : result);
563  if (first == last) return first;
564 
565 
566  }
567 
601  template<typename _ForwardIterator,typename _Compare,typename Predicate>
602 
603  _ForwardIterator max_element_if_compare( _ForwardIterator first, _ForwardIterator last,
604  Predicate pred,_Compare comp)
605  {
606  if (first == last) return first;
607  //find the first value verifying the predicate
608  while(!pred(*first++)&& first != last)
609  {}
610 
611  if(first == last) return last;
612 
613  _ForwardIterator result = first;
614  while (first != last)
615  {
616  if(pred(*first) && comp(*result,*first))
617  {
618  result = first;
619  }
620  ++first;
621  }
622 
623  return result;
624  }
625 
626 /* @} */
627 
629  /* @{ */
656  template<typename _ForwardIterator,typename _MaskIterator>
657 
658  _ForwardIterator min_element_mask( _ForwardIterator first, _ForwardIterator last,
659  _MaskIterator mask_first,
660  typename std::iterator_traits<_MaskIterator>::value_type value=typename std::iterator_traits<_MaskIterator>::value_type(1))
661  {
662  if (first == last) return first;
663  bool nul_mask = true;
664  _ForwardIterator result = first;
665  ++mask_first;
666 
667  while (++first != last)
668  {
669  if(*mask_first == value)
670  {
671  if(*result > *first)
672  {
673  result = first;
674  }
675  nul_mask = false;
676  }
677  ++mask_first;
678  }
679  return (nul_mask ? last : result);
680 
681 
682  }
715  template<typename _ForwardIterator,typename _MaskIterator,typename _Compare>
716 
717  _ForwardIterator min_element_mask_compare( _ForwardIterator first, _ForwardIterator last,
718  _MaskIterator mask_first,_Compare comp,
719  typename std::iterator_traits<_MaskIterator>::value_type value=typename std::iterator_traits<_MaskIterator>::value_type(1))
720  {
721 
722  return slip::max_element_mask_compare(first,last,mask_first,comp,value);
723 
724  }
725 
752  template<typename _ForwardIterator,typename Predicate>
753 
754  _ForwardIterator min_element_if( _ForwardIterator first, _ForwardIterator last,
755  Predicate pred)
756  {
757  if (first == last) return first;
758  //find the first value verifying the predicate
759  while(!pred(*first++)&& first != last)
760  {}
761 
762  if(first == last) return last;
763 
764  _ForwardIterator result = first;
765  while (first != last)
766  {
767  if(pred(*first) && (*first < *result))
768  {
769  result = first;
770  }
771  ++first;
772  }
773 
774  return result;
775 
776 
777  }
778 
814  template<typename _ForwardIterator,typename _Compare,typename Predicate>
815 
816  _ForwardIterator min_element_if_compare( _ForwardIterator first, _ForwardIterator last,
817  Predicate pred,_Compare comp)
818  {
819  return slip::max_element_if_compare(first,last,pred,comp);
820  }
821 
822 /* @} */
823 }//slip::
824 
825 #endif //SLIP_STL_ALGO_EXT_HPP
826 
_OutputIterator transform_if(_InputIterator first, _InputIterator last, _OutputIterator result, _UnaryOperation unary_op, _Predicate pred)
Perform an operation on a sequence according to a Predicate.
_OutputIterator transform_mask_bin(_InputIterator1 first1, _InputIterator1 last1, _MaskIterator mask_first, _InputIterator2 first2, _OutputIterator result, _BinaryOperation binary_op, typename std::iterator_traits< _MaskIterator >::value_type value=typename std::iterator_traits< _MaskIterator >::value_type(1))
Perform an operation on corresponding elements of two sequences according to a mask sequence...
_ForwardIterator max_element_if(_ForwardIterator first, _ForwardIterator last, Predicate pred)
Return the maximum element in a range according to a predicate or last if the predicate is never veri...
_ForwardIterator min_element_if_compare(_ForwardIterator first, _ForwardIterator last, Predicate pred, _Compare comp)
Return the minimum element in a range using comparison functor according to a predicate or return las...
_ForwardIterator min_element_mask_compare(_ForwardIterator first, _ForwardIterator last, _MaskIterator mask_first, _Compare comp, typename std::iterator_traits< _MaskIterator >::value_type value=typename std::iterator_traits< _MaskIterator >::value_type(1))
Return the minimum element in a range using comparison functor according to a mask sequence or return...
_ForwardIterator max_element_mask(_ForwardIterator first, _ForwardIterator last, _MaskIterator mask_first, typename std::iterator_traits< _MaskIterator >::value_type value=typename std::iterator_traits< _MaskIterator >::value_type(1))
Return the maximum element in a range according to a mask sequence or return last if no value verify ...
_ForwardIterator min_element_mask(_ForwardIterator first, _ForwardIterator last, _MaskIterator mask_first, typename std::iterator_traits< _MaskIterator >::value_type value=typename std::iterator_traits< _MaskIterator >::value_type(1))
Return the minimum element in a range according to a mask sequence or return last if no value verify ...
_OutputIterator transform_mask_un(_InputIterator first, _InputIterator last, _MaskIterator mask_first, _OutputIterator result, _UnaryOperation unary_op, typename std::iterator_traits< _MaskIterator >::value_type value=typename std::iterator_traits< _MaskIterator >::value_type(1))
Perform an operation on a sequence according to a mask sequence.
_ForwardIterator min_element_if(_ForwardIterator first, _ForwardIterator last, Predicate pred)
Return the minimum element in a range according to a predicate or return last if the predicate is nev...
_ForwardIterator max_element_if_compare(_ForwardIterator first, _ForwardIterator last, Predicate pred, _Compare comp)
Return the maximum element in a range using comparison functor according to a predicate or return las...
_ForwardIterator max_element_mask_compare(_ForwardIterator first, _ForwardIterator last, _MaskIterator mask_first, _Compare comp, typename std::iterator_traits< _MaskIterator >::value_type value=typename std::iterator_traits< _MaskIterator >::value_type(1))
Return the maximum element in a range using comparison functor according to a mask sequence or return...