Add non-parallel version of for_each_n (+tests) from the Parallelism TS

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@303833 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/algorithm b/include/algorithm
index 08ca23f..168d2c4 100644
--- a/include/algorithm
+++ b/include/algorithm
@@ -35,6 +35,9 @@
     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 @@
     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>