streams.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #pragma once
  2. #include <algorithm>
  3. #include <memory>
  4. #include <numeric>
  5. #define DELEGATE_ITERATOR_IMPL_BASE(impl) \
  6. bool operator==(iterator const&other) const { \
  7. return impl == other.impl; \
  8. }
  9. #define DELEGATE_ITERATOR_IMPL(impl) \
  10. DELEGATE_ITERATOR_IMPL_BASE(impl) \
  11. iterator& operator++() { \
  12. ++impl; \
  13. return *this; \
  14. }
  15. std::map<void*, bool> data;
  16. namespace stream {
  17. #define STREAM_ITERATOR_COPY() \
  18. copy(other.copy), dereference(other.dereference), compare(other.compare), destroy(other.destroy), advance(other.advance), type_(other.type_)
  19. template <typename T>
  20. class iterator {
  21. public:
  22. using value_type = typename std::remove_reference<T>::type;
  23. using reference = value_type &;
  24. using pointer = value_type *;
  25. using difference_type = std::ptrdiff_t;
  26. using iterator_category = std::forward_iterator_tag;
  27. public:
  28. iterator() = default;
  29. template <typename Iter>
  30. iterator(Iter impl) {
  31. copy = [](void * p) { return (void*) new Iter(*(Iter*)(p)); };
  32. dereference = [](void * p) -> T { return **((Iter*)p); };
  33. compare = [](void * l, void * r) { return *((Iter*)l) == *((Iter*)r); };
  34. destroy = [](void * p) { delete (Iter*)(p); };
  35. advance = [](void * p) { ++(*(Iter*)(p)); };
  36. type_ = typeid(Iter).name();
  37. impl_ = copy(&impl);
  38. }
  39. iterator(iterator const& other)
  40. : STREAM_ITERATOR_COPY()
  41. , impl_(copy(other.impl_)) {
  42. }
  43. iterator(iterator && other)
  44. : STREAM_ITERATOR_COPY()
  45. , impl_(other.impl_) {
  46. other.impl_ = nullptr;
  47. }
  48. iterator & operator=(iterator const & other) {
  49. return *this = iterator{other};
  50. }
  51. iterator & operator=(iterator && other) {
  52. swap(*this, other);
  53. return *this;
  54. }
  55. ~iterator() { if (destroy) destroy(impl_); }
  56. T operator*() const { return dereference(impl_); }
  57. iterator& operator++() { advance(impl_); return *this; }
  58. bool operator==(iterator const&other) const {
  59. if (strcmp(type_, other.type_)) { return false; }
  60. return compare(impl_, other.impl_);
  61. }
  62. bool operator!=(iterator const&other) const {
  63. if (strcmp(type_, other.type_)) { return false; }
  64. return !compare(impl_, other.impl_);
  65. }
  66. private:
  67. friend void swap(iterator & lhs, iterator & rhs) {
  68. using std::swap;
  69. swap(lhs.copy, rhs.copy);
  70. swap(lhs.dereference, rhs.dereference);
  71. swap(lhs.compare, rhs.compare);
  72. swap(lhs.destroy, rhs.destroy);
  73. swap(lhs.advance, rhs.advance);
  74. swap(lhs.type_, rhs.type_);
  75. swap(lhs.impl_, rhs.impl_);
  76. }
  77. using delegate = void(*)(void*);
  78. void* (*copy)(void*){nullptr};
  79. T (*dereference)(void*){nullptr};
  80. bool (*compare)(void*, void*){nullptr};
  81. delegate destroy{nullptr}, advance{nullptr};
  82. char const * type_{nullptr};
  83. void * impl_{nullptr};
  84. };
  85. namespace detail {
  86. template <typename T, typename = void>
  87. class stream_base_pointer_impl {};
  88. template <typename T>
  89. class stream_base_pointer_impl<T, typename std::enable_if<detail::is_dereferencable<typename std::remove_reference<T>::type>::value>::type> {
  90. private:
  91. using self = stream_base<T>;
  92. using noref = typename std::remove_reference<T>::type;
  93. using element_type = typename std::pointer_traits<noref>::element_type;
  94. public:
  95. auto deref() const -> stream_base<element_type &> {
  96. return static_cast<stream_base<T> const *>(this)->map([](T const & p) -> element_type & { return *p; });
  97. }
  98. };
  99. #define STREAM_BASE_COPY() \
  100. copy(other.copy), do_begin(other.do_begin), do_end(other.do_end), destroy(other.destroy)
  101. template <typename T>
  102. class stream_base : public stream_base_pointer_impl<T> {
  103. private:
  104. template <typename F>
  105. using map_f = decltype(std::declval<F>()(std::declval<T>()));
  106. template <typename F>
  107. using flatmap_f = typename decltype(std::declval<F>()(std::declval<T>()))::value_type;
  108. using self = stream_base<T>;
  109. using noref = typename std::remove_reference<T>::type;
  110. using clean = typename std::decay<T>::type;
  111. public:
  112. template <typename Stream>
  113. stream_base(Stream && impl) {
  114. copy = [](void * p) { return (void*) new Stream(*(Stream*)(p)); };
  115. do_begin = [](void * p) -> iterator<T> { return ((Stream*) p)->begin(); };
  116. do_end = [](void * p) -> iterator<T> { return ((Stream*) p)->end(); };
  117. destroy = [](void * p) { delete ((Stream*) p); };
  118. impl_ = copy(&impl);
  119. }
  120. stream_base(stream_base const & other)
  121. : STREAM_BASE_COPY()
  122. , impl_(copy(other.impl_)) {
  123. }
  124. stream_base(stream_base && other)
  125. : STREAM_BASE_COPY()
  126. , impl_(other.impl_) {
  127. other.impl_ = nullptr;
  128. }
  129. stream_base & operator=(stream_base const & other) {
  130. return *this = stream_base{other};
  131. }
  132. stream_base & operator=(stream_base && other) {
  133. swap(*this, other);
  134. return *this;
  135. }
  136. ~stream_base() { if (destroy) destroy(impl_); }
  137. ::stream::iterator<T> begin() const { return do_begin(impl_); }
  138. ::stream::iterator<T> end () const { return do_end (impl_); }
  139. bool empty() const { return begin() == end(); }
  140. std::vector<clean> collect() const {
  141. std::vector<clean> coll;
  142. collect(coll);
  143. return coll;
  144. }
  145. template <typename C, typename = typename std::enable_if<!std::is_void<typename C::value_type>::value, void>::type>
  146. C& collect(C & coll) const {
  147. std::copy(begin(), end(), std::inserter(coll, coll.end()));
  148. return coll;
  149. }
  150. template <typename F>
  151. clean accumulate(F&& fold, clean const& accum) {
  152. return std::accumulate(begin(), end(), accum, fold);
  153. }
  154. clean accumulate(clean const& accum) {
  155. return std::accumulate(begin(), end(), accum);
  156. }
  157. template <typename F>
  158. void each(F && consumer) {
  159. std::for_each(begin(), end(), consumer);
  160. }
  161. template <typename F>
  162. stream_base<map_f<F>> map(F&& func) const;
  163. template <typename F, typename = typename std::enable_if<std::is_member_object_pointer<F>::value>::type>
  164. auto map(F && memvar) const -> stream_base<typename map_member_object<F>::type> {
  165. return map(map_member_object<F>{memvar});
  166. }
  167. template <typename F, typename = typename std::enable_if<std::is_member_function_pointer<F>::value>::type>
  168. auto map(F && memvar) const -> stream_base<typename map_member_function<F>::type> {
  169. return map(map_member_function<F>{memvar});
  170. }
  171. template <typename Cast>
  172. auto cast() const -> stream_base<Cast const &> {
  173. return map([](T const & p) -> Cast const & { return p; });
  174. }
  175. template <typename F>
  176. stream_base<T> filter(F&& func) const;
  177. template <typename F>
  178. stream_base<flatmap_f<F>> flatmap(F&& func) const;
  179. private:
  180. void* (*copy)(void*){nullptr};
  181. iterator<T> (*do_begin)(void*){nullptr};
  182. iterator<T> (*do_end)(void*){nullptr};
  183. void(*destroy)(void*){nullptr};
  184. void* impl_{nullptr};
  185. };
  186. }
  187. }