Bläddra i källkod

refactor: naming...

Sam Jaffe 2 år sedan
förälder
incheckning
13ca4090bb

+ 6 - 8
include/stream/streams/make_stream.hpp

@@ -11,18 +11,18 @@ namespace stream {
    * capture cont by reference to maximise performance.
    */
   template <typename C>
-  auto make_stream(C && cont) -> detail::stream_base<decltype(*cont.begin())> {
+  auto of(C && cont) -> detail::stream_base<decltype(*cont.begin())> {
     return std::make_shared<detail::source_stream<C>>(std::forward<C>(cont));
   }
 
-  template <typename T> detail::stream_base<T &> make_empty_stream() {
+  template <typename T> detail::stream_base<T &> empty() {
     return std::make_shared<detail::range_stream<T *, T &>>(nullptr, nullptr);
   }
 
   /**
    * Construct a single element stream containing the pointer given
    */
-  template <typename T> detail::stream_base<T &> make_stream(T * ptr) {
+  template <typename T> detail::stream_base<T &> of(T * ptr) {
     return std::make_shared<detail::range_stream<T *, T &>>(ptr, ptr + 1);
   }
 
@@ -32,7 +32,7 @@ namespace stream {
    * 'reference'
    */
   template <typename It>
-  detail::stream_base<typename It::reference> make_stream(It begin, It end) {
+  detail::stream_base<typename It::reference> of(It begin, It end) {
     return std::make_shared<detail::range_stream<It>>(begin, end);
   }
 
@@ -40,8 +40,7 @@ namespace stream {
    * Construct a stream given certain start and end bounds.
    * e.g. stream::make_range_stream(0, 10)
    */
-  template <typename T>
-  detail::stream_base<T &> make_range_stream(T start, T const & end) {
+  template <typename T> detail::stream_base<T &> range(T start, T const & end) {
     std::vector<T> vec;
     vec.reserve(end - start);
     while (start < end) {
@@ -55,8 +54,7 @@ namespace stream {
    * increment amount. e.g. stream::make_range_stream(0, 10, 2)
    */
   template <typename T>
-  detail::stream_base<T &> make_range_stream(T start, T const & end,
-                                             T const & increment) {
+  detail::stream_base<T &> range(T start, T const & end, T const & increment) {
     int elems{(end - start) / increment};
     if (elems < 0 || end == start) { return {}; }
     std::vector<T> vec{start};

+ 2 - 2
include/stream/streams/source.hpp

@@ -39,10 +39,10 @@ namespace stream { namespace detail {
     C source_;
   };
 
-  template <typename It, typename V = typename It::value_type>
+  template <typename It, typename R = typename It::reference>
   class range_stream {
   public:
-    typedef typename It::reference reference;
+    using reference = R;
 
     explicit range_stream(It b, It e) : begin_(b), end_(e) {}
 

+ 4 - 4
include/stream/streams/streams.hpp

@@ -119,9 +119,8 @@ namespace stream {
         return coll;
       }
 
-      template <typename C,
-                typename = typename std::enable_if<
-                    !std::is_void<typename C::value_type>::value, void>::type>
+      template <typename C, typename = std::enable_if_t<
+                                !std::is_void_v<typename C::value_type>>>
       C & collect(C & coll) const {
         std::copy(begin(), end(), std::inserter(coll, coll.end()));
         return coll;
@@ -148,7 +147,8 @@ namespace stream {
         return std::accumulate(begin(), end(), accum, fold);
       }
 
-      template <typename F>
+      template <typename F, typename = std::enable_if_t<
+                                std::is_invocable_v<F, value_type, value_type>>>
       std::optional<value_type> accumulate(F && fold) const {
         if (empty()) { return std::nullopt; }
         value_type first = *begin();

+ 8 - 0
include/stream/streams/traits.hpp

@@ -42,6 +42,14 @@ namespace stream { namespace detail {
     type operator()(T const & val) const { return (val.*mem)(); }
     R (T::*mem)() const;
   };
+
+  template <typename F> struct map_member_function;
+  template <typename T, typename R>
+  struct map_member_function<R (T::*)() const noexcept> {
+    using type = R;
+    type operator()(T const & val) const noexcept { return (val.*mem)(); }
+    R (T::*mem)() const;
+  };
 }}
 
 namespace stream { namespace traits {

+ 2 - 2
stream.xcodeproj/project.pbxproj

@@ -345,6 +345,7 @@
 			isa = XCBuildConfiguration;
 			buildSettings = {
 				CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
+				CLANG_CXX_LANGUAGE_STANDARD = "c++17";
 				CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
 				CLANG_WARN_BOOL_CONVERSION = YES;
 				CLANG_WARN_COMMA = YES;
@@ -383,6 +384,7 @@
 			isa = XCBuildConfiguration;
 			buildSettings = {
 				CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
+				CLANG_CXX_LANGUAGE_STANDARD = "c++17";
 				CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
 				CLANG_WARN_BOOL_CONVERSION = YES;
 				CLANG_WARN_COMMA = YES;
@@ -485,7 +487,6 @@
 				ALWAYS_SEARCH_USER_PATHS = NO;
 				CLANG_ANALYZER_NONNULL = YES;
 				CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
-				CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
 				CLANG_CXX_LIBRARY = "libc++";
 				CLANG_ENABLE_MODULES = YES;
 				CLANG_ENABLE_OBJC_ARC = YES;
@@ -522,7 +523,6 @@
 				ALWAYS_SEARCH_USER_PATHS = NO;
 				CLANG_ANALYZER_NONNULL = YES;
 				CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
-				CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
 				CLANG_CXX_LIBRARY = "libc++";
 				CLANG_ENABLE_MODULES = YES;
 				CLANG_ENABLE_OBJC_ARC = YES;

+ 8 - 8
test/stream_fluent_test.cxx

@@ -17,7 +17,7 @@ using ::testing::Eq;
 
 TEST(FluentStreamTest, CollectToObjectPreservesElements) {
   std::vector<int> input{1, 2, 3, 4, 5};
-  auto s = stream::make_stream(input);
+  auto s = stream::of(input);
   std::set<int> out{};
 
   EXPECT_THAT(s > out, ElementsAreArray(input));
@@ -26,7 +26,7 @@ TEST(FluentStreamTest, CollectToObjectPreservesElements) {
 TEST(FluentStreamTest, MapToSelfIsSelfs) {
   std::vector<int> input{1, 2, 3, 4, 5};
   auto identity = [](int i) { return i; };
-  auto s = stream::make_stream(input) | identity;
+  auto s = stream::of(input) | identity;
 
   EXPECT_THAT(s.collect(), Eq(input));
 }
@@ -35,7 +35,7 @@ TEST(FluentStreamTest, MapCanAlterValues) {
   std::vector<int> input{1, 2, 3, 4, 5};
   std::vector<int> expected{3, 5, 7, 9, 11};
   auto fmap = [](int i) { return 2 * i + 1; };
-  auto s = stream::make_stream(input) | fmap;
+  auto s = stream::of(input) | fmap;
 
   EXPECT_THAT(s.collect(), Eq(expected));
 }
@@ -44,7 +44,7 @@ TEST(FluentStreamTest, CanMapElementToBool) {
   std::vector<int> input{1, 2, 3, 4, 5};
   std::vector<bool> expected{false, true, false, true, false};
   auto even = [](int i) { return i % 2 == 0; };
-  auto s = stream::make_stream(input) | even;
+  auto s = stream::of(input) | even;
 
   EXPECT_THAT(s.collect(), Eq(expected));
 }
@@ -53,7 +53,7 @@ TEST(FluentStreamTest, CanFilterOutElements) {
   std::vector<int> input{1, 2, 3, 4, 5};
   std::vector<int> expected{2, 4};
   auto even = [](int i) { return i % 2 == 0; };
-  auto s = stream::make_stream(input) % even;
+  auto s = stream::of(input) % even;
 
   EXPECT_THAT(s.collect(), Eq(expected));
 }
@@ -61,7 +61,7 @@ TEST(FluentStreamTest, CanFilterOutElements) {
 TEST(FluentStreamTest, NoOpFilterReturnOriginal) {
   std::vector<int> input{1, 2, 3, 4, 5};
   auto pass = [](int) { return true; };
-  auto s = stream::make_stream(input) % pass;
+  auto s = stream::of(input) % pass;
 
   EXPECT_THAT(s.collect(), Eq(input));
 }
@@ -70,7 +70,7 @@ TEST(FluentStreamTest, AccumulateDefaultsToAdd) {
   std::vector<int> input{1, 2, 3, 4, 5};
   auto even = [](int i) { return i % 2 == 0; };
   auto sum = [](int lhs, int rhs) { return lhs + rhs; };
-  auto s = stream::make_stream(input) % even;
+  auto s = stream::of(input) % even;
   EXPECT_THAT(s > sum, Eq(6));
 }
 
@@ -78,7 +78,7 @@ TEST(FluentStreamTest, FlatmapJoinsIterableOutputs) {
   std::vector<int> vv{1, 2, 3, 4, 5};
   auto next3 = [](int i) { return std::vector<int>{i, i + 1, i + 2}; };
   std::vector<int> expected{1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6, 5, 6, 7};
-  auto s = stream::make_stream(vv) || next3;
+  auto s = stream::of(vv) || next3;
 
   EXPECT_THAT(s.collect(), Eq(expected));
 }

+ 20 - 20
test/stream_test.cxx

@@ -21,7 +21,7 @@ template class std::basic_string<char>;
 
 TEST(StreamTest, IteratorPreservesElements) {
   std::vector<int> input{1, 2, 3, 4, 5};
-  auto s = stream::make_stream(input);
+  auto s = stream::of(input);
   std::vector<int> out{s.begin(), s.end()};
 
   EXPECT_THAT(out, Eq(input));
@@ -29,7 +29,7 @@ TEST(StreamTest, IteratorPreservesElements) {
 
 TEST(MapStreamTest, IteratorPreservesElements) {
   std::map<int, int> input{{1, 1}, {2, 2}};
-  auto s = stream::make_stream(input);
+  auto s = stream::of(input);
   std::map<int, int> out{s.begin(), s.end()};
 
   EXPECT_THAT(out, Eq(input));
@@ -37,7 +37,7 @@ TEST(MapStreamTest, IteratorPreservesElements) {
 
 TEST(StreamTest, CollectPreservesElements) {
   std::vector<int> input{1, 2, 3, 4, 5};
-  auto s = stream::make_stream(input);
+  auto s = stream::of(input);
   std::vector<int> out{s.collect()};
 
   EXPECT_THAT(out, Eq(input));
@@ -45,7 +45,7 @@ TEST(StreamTest, CollectPreservesElements) {
 
 TEST(StreamTest, CollectToObjectPreservesElements) {
   std::vector<int> input{1, 2, 3, 4, 5};
-  auto s = stream::make_stream(input);
+  auto s = stream::of(input);
   std::set<int> out{};
   s.collect(out);
 
@@ -55,7 +55,7 @@ TEST(StreamTest, CollectToObjectPreservesElements) {
 TEST(StreamTest, MapToSelfIsSelfs) {
   std::vector<int> input{1, 2, 3, 4, 5};
   auto identity = [](int i) { return i; };
-  auto s = stream::make_stream(input).map(identity);
+  auto s = stream::of(input).map(identity);
 
   EXPECT_THAT(s.collect(), Eq(input));
 }
@@ -64,7 +64,7 @@ TEST(StreamTest, MapCanAlterValues) {
   std::vector<int> input{1, 2, 3, 4, 5};
   std::vector<int> expected{3, 5, 7, 9, 11};
   auto fmap = [](int i) { return 2 * i + 1; };
-  auto s = stream::make_stream(input).map(fmap);
+  auto s = stream::of(input).map(fmap);
 
   EXPECT_THAT(s.collect(), Eq(expected));
 }
@@ -88,7 +88,7 @@ TEST(MapStreamTest, MapToValue) {
     return tmp;
   }();
   auto fmap = [](auto & pair) -> auto & { return pair.second; };
-  auto s = stream::make_stream(input).map(fmap);
+  auto s = stream::of(input).map(fmap);
   std::vector<int> out(s.begin(), s.end());
   std::vector<int> const expected{1, 2};
 
@@ -98,7 +98,7 @@ TEST(MapStreamTest, MapToValue) {
 TEST(StreamTest, CanBuildFromSingleElement) {
   int value = 11;
   auto even = [](int i) { return i % 2 == 0; };
-  auto s = stream::make_stream(&value).filter(even);
+  auto s = stream::of(&value).filter(even);
 
   EXPECT_TRUE(s.empty());
 }
@@ -107,7 +107,7 @@ TEST(StreamTest, CanBuildFromIterators) {
   std::vector<int> input{1, 2, 3, 4, 5};
   std::vector<int> expected{5, 7};
   auto fmap = [](int i) { return 2 * i + 1; };
-  auto s = stream::make_stream(input.begin() + 1, input.begin() + 3).map(fmap);
+  auto s = stream::of(input.begin() + 1, input.begin() + 3).map(fmap);
 
   EXPECT_THAT(s.collect(), Eq(expected));
 }
@@ -115,7 +115,7 @@ TEST(StreamTest, CanBuildFromIterators) {
 TEST(StreamTest, NoOpFilterReturnOriginal) {
   std::vector<int> input{1, 2, 3, 4, 5};
   auto pass = [](int) { return true; };
-  auto s = stream::make_stream(input).filter(pass);
+  auto s = stream::of(input).filter(pass);
 
   EXPECT_THAT(s.collect(), Eq(input));
 }
@@ -124,7 +124,7 @@ TEST(StreamTest, CanFilterOutElements) {
   std::vector<int> input{1, 2, 3, 4, 5};
   std::vector<int> expected{2, 4};
   auto even = [](int i) { return i % 2 == 0; };
-  auto s = stream::make_stream(input).filter(even);
+  auto s = stream::of(input).filter(even);
 
   EXPECT_THAT(s.collect(), Eq(expected));
 }
@@ -132,7 +132,7 @@ TEST(StreamTest, CanFilterOutElements) {
 TEST(StreamTest, AccumulateDefaultsToAdd) {
   std::vector<int> input{1, 2, 3, 4, 5};
   auto even = [](int i) { return i % 2 == 0; };
-  auto s = stream::make_stream(input).filter(even);
+  auto s = stream::of(input).filter(even);
 
   EXPECT_THAT(s.accumulate(0), Eq(6));
 }
@@ -141,7 +141,7 @@ TEST(StreamTest, AccumulateCanTakeCustomAccumulator) {
   std::vector<int> input{1, 2, 3, 4, 5};
   auto even = [](int i) { return i % 2 == 0; };
   auto prod = [](int lhs, int rhs) { return lhs * rhs; };
-  auto s = stream::make_stream(input).filter(even);
+  auto s = stream::of(input).filter(even);
 
   EXPECT_THAT(s.accumulate(prod, 0), Eq(0));
   EXPECT_THAT(s.accumulate(prod, 1), Eq(8));
@@ -151,7 +151,7 @@ TEST(StreamTest, FlatmapJoinsIterableOutputs) {
   std::vector<int> vv{1, 2, 3, 4, 5};
   auto next3 = [](int i) { return std::vector<int>{i, i + 1, i + 2}; };
   std::vector<int> expected{1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6, 5, 6, 7};
-  auto s = stream::make_stream(vv).flatmap(next3);
+  auto s = stream::of(vv).flatmap(next3);
 
   EXPECT_THAT(s.collect(), Eq(expected));
 }
@@ -159,7 +159,7 @@ TEST(StreamTest, FlatmapJoinsIterableOutputs) {
 TEST(StreamTest, CanDereferenceElements) {
   int val = 5;
   std::vector<int *> input{&val};
-  auto data = stream::make_stream(input).deref().collect();
+  auto data = stream::of(input).deref().collect();
 
   EXPECT_THAT(data.front(), Eq(val));
 }
@@ -167,7 +167,7 @@ TEST(StreamTest, CanDereferenceElements) {
 TEST(StreamTest, CanForEachConsume) {
   int hits = 0;
   std::vector<int> input{1, 2, 3, 4, 5};
-  stream::make_stream(input).each([&hits](int) { ++hits; });
+  stream::of(input).each([&hits](int) { ++hits; });
 
   EXPECT_THAT(hits, Eq(5));
 }
@@ -178,7 +178,7 @@ TEST(StreamTest, CanFetchMemPtr) {
   };
   std::vector<test> input{{1}, {3}, {2}};
   std::vector<int> expected{1, 3, 2};
-  auto out = stream::make_stream(input).map(&test::val).collect();
+  auto out = stream::of(input).map(&test::val).collect();
 
   EXPECT_THAT(out, Eq(expected));
 }
@@ -186,7 +186,7 @@ TEST(StreamTest, CanFetchMemPtr) {
 TEST(StreamTest, CanMapToMemFn) {
   std::vector<std::string> input{"hello", "goodbye"};
   std::vector<std::string::size_type> expected{5, 7};
-  auto out = stream::make_stream(input).map(&std::string::size).collect();
+  auto out = stream::of(input).map(&std::string::size).collect();
 
   EXPECT_THAT(out, Eq(expected));
 }
@@ -203,8 +203,8 @@ TEST(StreamTest, CastStreamToParentType) {
 
   auto addressof_void = [](auto const & p) { return (void *)&p; };
 
-  auto strm = stream::make_stream(input).cast<base>();
-  auto first = stream::make_stream(input).map(addressof_void).collect();
+  auto strm = stream::of(input).cast<base>();
+  auto first = stream::of(input).map(addressof_void).collect();
   auto second = strm.map(addressof_void).collect();
 
   EXPECT_THAT(first, second);

+ 1 - 0
test/xcode_gtest_helper.h

@@ -12,6 +12,7 @@
 
 #pragma clang diagnostic push
 #pragma clang diagnostic ignored "-Wquoted-include-in-framework-header"
+#pragma clang diagnostic ignored "-Wcomma"
 
 #include <gmock/gmock.h>
 #include <gtest/gtest.h>