3 // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 3, or (at your option) any later
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file parallel/unique_copy.h
26 * @brief Parallel implementations of std::unique_copy().
27 * This file is a GNU parallel extension to the Standard C++ Library.
30 // Written by Robert Geisberger and Robin Dapp.
32 #ifndef _GLIBCXX_PARALLEL_UNIQUE_COPY_H
33 #define _GLIBCXX_PARALLEL_UNIQUE_COPY_H 1
35 #include <parallel/parallel.h>
36 #include <parallel/multiseq_selection.h>
38 namespace __gnu_parallel
41 /** @brief Parallel std::unique_copy(), w/o explicit equality predicate.
42 * @param first Begin iterator of input sequence.
43 * @param last End iterator of input sequence.
44 * @param result Begin iterator of result sequence.
45 * @param binary_pred Equality predicate.
46 * @return End iterator of result sequence. */
47 template<typename InputIterator
,
49 class BinaryPredicate
>
51 parallel_unique_copy(InputIterator first
, InputIterator last
,
52 OutputIterator result
, BinaryPredicate binary_pred
)
54 _GLIBCXX_CALL(last
- first
)
56 typedef std::iterator_traits
<InputIterator
> traits_type
;
57 typedef typename
traits_type::value_type value_type
;
58 typedef typename
traits_type::difference_type difference_type
;
60 difference_type size
= last
- first
;
65 // Let the first thread process two parts.
66 difference_type
*counter
;
67 difference_type
*borders
;
69 thread_index_t num_threads
= get_max_threads();
70 // First part contains at least one element.
71 # pragma omp parallel num_threads(num_threads)
75 num_threads
= omp_get_num_threads();
76 borders
= new difference_type
[num_threads
+ 2];
77 equally_split(size
, num_threads
+ 1, borders
);
78 counter
= new difference_type
[num_threads
+ 1];
81 thread_index_t iam
= omp_get_thread_num();
83 difference_type begin
, end
;
85 // Check for length without duplicates
86 // Needed for position in output
87 difference_type i
= 0;
88 OutputIterator out
= result
;
92 begin
= borders
[0] + 1; // == 1
93 end
= borders
[iam
+ 1];
98 for (InputIterator iter
= first
+ begin
; iter
< first
+ end
; ++iter
)
100 if (!binary_pred(*iter
, *(iter
-1)))
109 begin
= borders
[iam
]; //one part
110 end
= borders
[iam
+ 1];
112 for (InputIterator iter
= first
+ begin
; iter
< first
+ end
; ++iter
)
114 if (!binary_pred(*iter
, *(iter
- 1)))
120 // Last part still untouched.
121 difference_type begin_output
;
125 // Store result in output on calculated positions.
130 for (int t
= 0; t
< num_threads
; ++t
)
131 begin_output
+= counter
[t
];
135 OutputIterator iter_out
= result
+ begin_output
;
137 begin
= borders
[num_threads
];
140 for (InputIterator iter
= first
+ begin
; iter
< first
+ end
; ++iter
)
142 if (iter
== first
|| !binary_pred(*iter
, *(iter
- 1)))
149 counter
[num_threads
] = i
;
153 for (int t
= 0; t
< iam
; t
++)
154 begin_output
+= counter
[t
];
156 OutputIterator iter_out
= result
+ begin_output
;
157 for (InputIterator iter
= first
+ begin
; iter
< first
+ end
; ++iter
)
159 if (!binary_pred(*iter
, *(iter
-1)))
165 difference_type end_output
= 0;
166 for (int t
= 0; t
< num_threads
+ 1; t
++)
167 end_output
+= counter
[t
];
171 return result
+ end_output
;
174 /** @brief Parallel std::unique_copy(), without explicit equality predicate
175 * @param first Begin iterator of input sequence.
176 * @param last End iterator of input sequence.
177 * @param result Begin iterator of result sequence.
178 * @return End iterator of result sequence. */
179 template<typename InputIterator
, class OutputIterator
>
180 inline OutputIterator
181 parallel_unique_copy(InputIterator first
, InputIterator last
,
182 OutputIterator result
)
184 typedef typename
std::iterator_traits
<InputIterator
>::value_type
186 return parallel_unique_copy(first
, last
, result
,
187 std::equal_to
<value_type
>());
190 }//namespace __gnu_parallel
192 #endif /* _GLIBCXX_PARALLEL_UNIQUE_COPY_H */