[PATCH] D17311: allow lambdas in mapped_iterator

Mike Aizatsky via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 16 15:26:18 PDT 2016


aizatsky updated this revision to Diff 50884.
aizatsky added a comment.

added unittest.


http://reviews.llvm.org/D17311

Files:
  include/llvm/ADT/STLExtras.h
  unittests/ADT/CMakeLists.txt
  unittests/ADT/MappedIteratorTest.cpp

Index: unittests/ADT/MappedIteratorTest.cpp
===================================================================
--- /dev/null
+++ unittests/ADT/MappedIteratorTest.cpp
@@ -0,0 +1,51 @@
+//===- llvm/unittest/ADT/APInt.cpp - APInt unit tests ---------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include <vector>
+
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/iterator_range.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+namespace {
+
+template <class T, class Fn>
+auto map_range(const T &range, Fn fn)
+    -> decltype(make_range(map_iterator(range.begin(), fn),
+                           map_iterator(range.end(), fn))) {
+  return make_range(map_iterator(range.begin(), fn),
+                    map_iterator(range.end(), fn));
+}
+
+static char add1(char C) { return C + 1; }
+
+TEST(MappedIterator, FnTest) {
+  std::string S("abc");
+  std::string T;
+
+  for (char C : map_range(S, add1)) {
+    T.push_back(C);
+  }
+
+  EXPECT_STREQ("bcd", T.c_str());
+}
+
+TEST(MappedIterator, LambdaTest) {
+  std::string S("abc");
+  std::string T;
+
+  for (char C : map_range(S, [](char C) { return C + 1; })) {
+    T.push_back(C);
+  }
+
+  EXPECT_STREQ("bcd", T.c_str());
+}
+}
Index: unittests/ADT/CMakeLists.txt
===================================================================
--- unittests/ADT/CMakeLists.txt
+++ unittests/ADT/CMakeLists.txt
@@ -22,6 +22,7 @@
   IntervalMapTest.cpp
   IntrusiveRefCntPtrTest.cpp
   MakeUniqueTest.cpp
+  MappedIteratorTest.cpp
   MapVectorTest.cpp
   OptionalTest.cpp
   PackedVectorTest.cpp
Index: include/llvm/ADT/STLExtras.h
===================================================================
--- include/llvm/ADT/STLExtras.h
+++ include/llvm/ADT/STLExtras.h
@@ -117,7 +117,7 @@
           iterator_category;
   typedef typename std::iterator_traits<RootIt>::difference_type
           difference_type;
-  typedef typename UnaryFunc::result_type value_type;
+  typedef decltype(Fn(*current)) value_type;
 
   typedef void pointer;
   //typedef typename UnaryFunc::result_type *pointer;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D17311.50884.patch
Type: text/x-patch
Size: 2293 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160316/88acdad1/attachment.bin>


More information about the llvm-commits mailing list