Rcpp Version 1.0.9
algo.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // algo.h: Rcpp R/C++ interface class library -- STL-style algorithms
4 //
5 // Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
6 //
7 // This file is part of Rcpp.
8 //
9 // Rcpp is free software: you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // Rcpp is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21 
22 #ifndef Rcpp__algo_h
23 #define Rcpp__algo_h
24 
25 #include <iterator>
26 #include <algorithm>
27 
28 namespace Rcpp{
29 
30 /* generic implementation for the input iterator case */
31 template<class InputIterator, class T>
32 inline bool __any( InputIterator first, InputIterator last, const T& value, std::input_iterator_tag ){
33  for ( ;first!=last; first++) if ( *first==value ) return true;
34  return false;
35 }
36 
37 /* RAI case */
38 template<class RandomAccessIterator, class T>
39 inline bool __any( RandomAccessIterator __first, RandomAccessIterator __last, const T& __val, std::random_access_iterator_tag ){
40 
41  typename std::iterator_traits<RandomAccessIterator>::difference_type __trip_count = (__last - __first) >> 2;
42 
43  for ( ; __trip_count > 0 ; --__trip_count) {
44  if (*__first == __val)
45  return true;
46  ++__first;
47 
48  if (*__first == __val)
49  return true;
50  ++__first;
51 
52  if (*__first == __val)
53  return true;
54  ++__first;
55 
56  if (*__first == __val)
57  return true;
58  ++__first;
59  }
60 
61  switch (__last - __first)
62  {
63  case 3:
64  if (*__first == __val)
65  return true;
66  ++__first;
67  case 2:
68  if (*__first == __val)
69  return true;
70  ++__first;
71  case 1:
72  if (*__first == __val)
73  return true;
74  ++__first;
75  case 0:
76  default:
77  return false;
78  }
79 
80 
81 }
82 
83 
88 template<class InputIterator, class T>
89 inline bool any( InputIterator first, InputIterator last, const T& value){
90  return __any( first, last, value, typename std::iterator_traits<InputIterator>::iterator_category() ) ;
91 }
92 
93 
94 
95 
96 /* generic implementation for the input iterator case */
97 template<class InputIterator, class Predicate>
98 inline bool __any_if( InputIterator first, InputIterator last, Predicate pred, std::input_iterator_tag ){
99  for ( ; first!=last ; first++ ) if ( pred(*first) ) return true ;
100  return false;
101 }
102 
103 /* RAI case */
104 template<class RandomAccessIterator, class Predicate>
105 inline bool __any_if( RandomAccessIterator __first, RandomAccessIterator __last, Predicate __pred, std::random_access_iterator_tag ){
106 
107  typename std::iterator_traits<RandomAccessIterator>::difference_type __trip_count = (__last - __first) >> 2;
108 
109  for ( ; __trip_count > 0 ; --__trip_count) {
110  if (__pred(*__first))
111  return true;
112  ++__first;
113 
114  if (__pred(*__first))
115  return true;
116  ++__first;
117 
118  if (__pred(*__first))
119  return true;
120  ++__first;
121 
122  if (__pred(*__first))
123  return true;
124  ++__first;
125  }
126 
127  switch (__last - __first)
128  {
129  case 3:
130  if (__pred(*__first))
131  return true;
132  ++__first;
133  case 2:
134  if (__pred(*__first))
135  return true;
136  ++__first;
137  case 1:
138  if (__pred(*__first))
139  return true;
140  ++__first;
141  case 0:
142  default:
143  return false;
144  }
145 
146 
147 }
148 
149 
154 template<class InputIterator, class Predicate>
155 inline bool any_if( InputIterator first, InputIterator last, Predicate pred){
156  return __any_if( first, last, pred, typename std::iterator_traits<InputIterator>::iterator_category() ) ;
157 }
158 
159 }
160 
161 #endif
Rcpp API.
Definition: algo.h:28
bool any_if(InputIterator first, InputIterator last, Predicate pred)
Definition: algo.h:155
bool any(InputIterator first, InputIterator last, const T &value)
Definition: algo.h:89
bool __any_if(InputIterator first, InputIterator last, Predicate pred, std::input_iterator_tag)
Definition: algo.h:98
bool __any(InputIterator first, InputIterator last, const T &value, std::input_iterator_tag)
Definition: algo.h:32