Pārlūkot izejas kodu

refactor: move all iterators into folder, add adaptor to help build other versions of any_iterator

Sam Jaffe 2 gadi atpakaļ
vecāks
revīzija
5a83cc60e0

+ 30 - 0
include/stream/detail/adaptor.h

@@ -0,0 +1,30 @@
+//
+//  adaptor.h
+//  stream
+//
+//  Created by Sam Jaffe on 4/6/23.
+//
+
+#pragma once
+
+#include <stream/forward.h>
+
+namespace stream::detail {
+template <typename T, typename It = void> struct adaptor {
+  using dereference_t = T (*)(void *);
+  using equal_to_t = bool (*)(void *, void *);
+  using increment_t = void (*)(void *);
+
+  static dereference_t dereference() {
+    return [](void * p) -> T { return **((It *)p); };
+  }
+
+  static equal_to_t equal_to() {
+    return [](void * l, void * r) { return *((It *)l) == *((It *)r); };
+  }
+
+  static increment_t increment() {
+    return [](void * p) { ++(*(It *)(p)); };
+  }
+};
+}

+ 39 - 0
include/stream/iterator/any_iterator.h

@@ -0,0 +1,39 @@
+//
+//  any_iterator.h
+//  stream
+//
+//  Created by Sam Jaffe on 4/6/23.
+//
+
+#pragma once
+
+#include <iterator/facade.h>
+
+#include <stream/forward.h>
+
+#include <stream/detail/adaptor.h>
+
+namespace stream::ranges {
+template <typename T> class any_iterator : public facade<any_iterator<T>> {
+private:
+  typename detail::adaptor<T>::dereference_t dereference_;
+  typename detail::adaptor<T>::equal_to_t equal_to_;
+  typename detail::adaptor<T>::increment_t increment_;
+  std::shared_ptr<void> impl_{nullptr};
+
+public:
+  template <typename It, typename IFD = detail::adaptor<T, It>>
+  any_iterator(It impl)
+      : dereference_(IFD::dereference()), equal_to_(IFD::equal_to()),
+        increment_(IFD::increment()), impl_(std::make_shared<It>(impl)) {}
+
+  T dereference() const { return dereference_(impl_.get()); }
+
+  void increment() { increment_(impl_.get()); }
+
+  bool equal_to(any_iterator const & other) const {
+    return impl_ == other.impl_ ||
+           (impl_ && other.impl_ && equal_to_(impl_.get(), other.impl_.get()));
+  }
+};
+}

+ 40 - 0
include/stream/iterator/iota_iterator.h

@@ -0,0 +1,40 @@
+//
+//  iota_iterator.h
+//  stream
+//
+//  Created by Sam Jaffe on 4/6/23.
+//
+
+#pragma once
+
+#include <iterator/facade.h>
+
+#include <stream/forward.h>
+
+namespace stream::ranges {
+
+template <typename T, typename Bound>
+class iota_iterator : public facade<iota_iterator<T, Bound>> {
+public:
+  using sentinal_type = Bound;
+
+  iota_iterator() = default;
+  iota_iterator(T value, Bound bound) : value_(value), bound_(bound) {}
+
+  T dereference() const { return value_; }
+  void increment() { ++value_; }
+  bool at_end() const { return value_ == bound_; }
+  // Some weird bug in dependent template instantiation???
+  bool equal_to(iota_iterator const & other) const {
+    return value_ == other.value_;
+  }
+  std::ptrdiff_t distance_to(iota_iterator const & other) const {
+    return other.value_ - value_;
+  }
+
+private:
+  T value_;
+  Bound bound_;
+};
+
+}

+ 4 - 30
include/stream/view/any.h

@@ -12,36 +12,10 @@
 #include <stream/forward.h>
 
 #include <stream/detail/traits.h>
-#include <stream/view/any.h>
-#include <stream/view/common.h>
+#include <stream/iterator/any_iterator.h>
 
 namespace stream::ranges {
 
-template <typename T> class view_iterator : public facade<view_iterator<T>> {
-private:
-  T (*dereference_)(void *){nullptr};
-  bool (*equal_to_)(void *, void *){nullptr};
-  void (*increment_)(void *){nullptr};
-  std::shared_ptr<void> impl_{nullptr};
-
-public:
-  template <typename It>
-  view_iterator(It impl)
-      : dereference_([](void * p) -> T { return **((It *)p); }),
-        equal_to_([](void * l, void * r) { return *((It *)l) == *((It *)r); }),
-        increment_([](void * p) { ++(*(It *)(p)); }),
-        impl_(std::make_shared<It>(impl)) {}
-
-  T dereference() const { return dereference_(impl_.get()); }
-
-  void increment() { increment_(impl_.get()); }
-
-  bool equal_to(view_iterator const & other) const {
-    return impl_ == other.impl_ ||
-           (impl_ && other.impl_ && equal_to_(impl_.get(), other.impl_.get()));
-  }
-};
-
 template <typename T> class any_view {
 private:
   template <typename S>
@@ -51,7 +25,7 @@ private:
   using iter_cast_t =
       std::conditional_t<detail::is_sentinal_v<S>, sentinel_iterator<S>, It>;
 
-  using make_iter_t = view_iterator<T> (*)(void *);
+  using make_iter_t = any_iterator<T> (*)(void *);
 
 private:
   std::shared_ptr<void> impl_{nullptr};
@@ -78,13 +52,13 @@ public:
 
 private:
   template <typename S> static make_iter_t begin_function() {
-    return [](void * p) -> view_iterator<T> {
+    return [](void * p) -> any_iterator<T> {
       return iter_cast_t<S, detail::begin_t<S>>(static_cast<S *>(p)->begin());
     };
   }
 
   template <typename S> static make_iter_t end_function() {
-    return [](void * p) -> view_iterator<T> {
+    return [](void * p) -> any_iterator<T> {
       return iter_cast_t<S, detail::end_t<S>>(static_cast<S *>(p)->end());
     };
   }

+ 2 - 26
include/stream/view/iota.h

@@ -7,35 +7,11 @@
 
 #pragma once
 
-#include <iterator/facade.h>
-
 #include <stream/forward.h>
 
-namespace stream::ranges {
-template <typename T, typename Bound>
-class iota_iterator : public facade<iota_iterator<T, Bound>> {
-public:
-  using sentinal_type = Bound;
-
-  iota_iterator() = default;
-  iota_iterator(T value, Bound bound) : value_(value), bound_(bound) {}
-
-  T const & dereference() const { return value_; }
-  void increment() { ++value_; }
-  bool at_end() const { return value_ == bound_; }
-  // Some weird bug in dependent template instantiation???
-  bool equal_to(iota_iterator const & other) const {
-    return value_ == other.value_;
-  }
-  std::ptrdiff_t distance_to(iota_iterator const & other) const {
-    return other.value_ - value_;
-  }
-
-private:
-  T value_;
-  Bound bound_;
-};
+#include <stream/iterator/iota_iterator.h>
 
+namespace stream::ranges {
 template <typename T, typename Bound> struct iota_view {
 public:
   constexpr static inline bool has_size_v = std::is_constructible_v<T, Bound>;

+ 6 - 0
stream.xcodeproj/project.pbxproj

@@ -109,6 +109,9 @@
 		CD5A55A529DF8DEE00881E7E /* for_each_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = for_each_test.cxx; sourceTree = "<group>"; };
 		CD5A55A729DF8E7B00881E7E /* subrange_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = subrange_test.cxx; sourceTree = "<group>"; };
 		CD5A55A929DF8EDB00881E7E /* ref_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ref_test.cxx; sourceTree = "<group>"; };
+		CD5A55AB29DF920100881E7E /* iota_iterator.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = iota_iterator.h; sourceTree = "<group>"; };
+		CD5A55AC29DF92D900881E7E /* any_iterator.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = any_iterator.h; sourceTree = "<group>"; };
+		CD5A55AD29DF938800881E7E /* adaptor.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = adaptor.h; sourceTree = "<group>"; };
 		CD5A8D3529D63C05008C2A4F /* count.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = count.h; sourceTree = "<group>"; };
 		CD5A8D3B29D63DF5008C2A4F /* named_pair.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = named_pair.h; sourceTree = "<group>"; };
 		CD64CCB926232D6900770A30 /* xcode_gtest_helper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = xcode_gtest_helper.h; sourceTree = "<group>"; };
@@ -204,6 +207,7 @@
 		CD5281F229D3B173001A84DE /* detail */ = {
 			isa = PBXGroup;
 			children = (
+				CD5A55AD29DF938800881E7E /* adaptor.h */,
 				CD52828029D51166001A84DE /* identity.h */,
 				CDA54F9729DA4BFC006C0FAA /* invoke.h */,
 				CDA37D3929D9BDED000A1F97 /* macro.h */,
@@ -284,7 +288,9 @@
 		CDA37D3629D9BB24000A1F97 /* iterator */ = {
 			isa = PBXGroup;
 			children = (
+				CD5A55AC29DF92D900881E7E /* any_iterator.h */,
 				CDA37D3B29D9C30B000A1F97 /* common_iterator.h */,
+				CD5A55AB29DF920100881E7E /* iota_iterator.h */,
 				CDA37D3C29D9C311000A1F97 /* transform_iterator.h */,
 			);
 			path = iterator;