[libcxx] r303833 - Add non-parallel version of for_each_n (+tests) from the Parallelism TS
Marshall Clow via cfe-commits
cfe-commits at lists.llvm.org
Wed May 24 19:29:54 PDT 2017
Author: marshall
Date: Wed May 24 21:29:54 2017
New Revision: 303833
URL: http://llvm.org/viewvc/llvm-project?rev=303833&view=rev
Log:
Add non-parallel version of for_each_n (+tests) from the Parallelism TS
Added:
libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.foreach/for_each_n.pass.cpp
Modified:
libcxx/trunk/include/algorithm
Modified: libcxx/trunk/include/algorithm
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/algorithm?rev=303833&r1=303832&r2=303833&view=diff
==============================================================================
--- libcxx/trunk/include/algorithm (original)
+++ libcxx/trunk/include/algorithm Wed May 24 21:29:54 2017
@@ -35,6 +35,9 @@ template <class InputIterator, class Fun
Function
for_each(InputIterator first, InputIterator last, Function f);
+template<class InputIterator, class Size, class Function>
+ InputIterator for_each_n(InputIterator first, Size n, Function f); // C++17
+
template <class InputIterator, class T>
InputIterator
find(InputIterator first, InputIterator last, const T& value);
@@ -961,6 +964,24 @@ for_each(_InputIterator __first, _InputI
return __f;
}
+// for_each_n
+
+template <class _InputIterator, class _Size, class _Function>
+inline _LIBCPP_INLINE_VISIBILITY
+_InputIterator
+for_each_n(_InputIterator __first, _Size __orig_n, _Function __f)
+{
+ typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
+ _IntegralSize __n = __orig_n;
+ while (__n > 0)
+ {
+ __f(*__first);
+ ++__first;
+ --__n;
+ }
+ return __first;
+}
+
// find
template <class _InputIterator, class _Tp>
Added: libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.foreach/for_each_n.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.foreach/for_each_n.pass.cpp?rev=303833&view=auto
==============================================================================
--- libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.foreach/for_each_n.pass.cpp (added)
+++ libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.foreach/for_each_n.pass.cpp Wed May 24 21:29:54 2017
@@ -0,0 +1,61 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+// UNSUPPORTED: c++98, c++03, c++11, c++14
+
+// template<class InputIterator, class Size, class Function>
+// InputIterator for_each_n(InputIterator first, Size n, Function f);
+
+
+#include <algorithm>
+#include <cassert>
+
+#include "test_iterators.h"
+
+struct for_each_test
+{
+ for_each_test(int c) : count(c) {}
+ int count;
+ void operator()(int& i) {++i; ++count;}
+};
+
+int main()
+{
+ typedef input_iterator<int*> Iter;
+ int ia[] = {0, 1, 2, 3, 4, 5};
+ const unsigned s = sizeof(ia)/sizeof(ia[0]);
+
+ {
+ auto f = for_each_test(0);
+ Iter it = std::for_each_n(Iter(ia), 0, std::ref(f));
+ assert(it == Iter(ia));
+ assert(f.count == 0);
+ }
+
+ {
+ auto f = for_each_test(0);
+ Iter it = std::for_each_n(Iter(ia), s, std::ref(f));
+
+ assert(it == Iter(ia+s));
+ assert(f.count == s);
+ for (unsigned i = 0; i < s; ++i)
+ assert(ia[i] == static_cast<int>(i+1));
+ }
+
+ {
+ auto f = for_each_test(0);
+ Iter it = std::for_each_n(Iter(ia), 1, std::ref(f));
+
+ assert(it == Iter(ia+1));
+ assert(f.count == 1);
+ for (unsigned i = 0; i < 1; ++i)
+ assert(ia[i] == static_cast<int>(i+2));
+ }
+}
More information about the cfe-commits
mailing list