Molassembler  3.0.0
Molecule graph and conformer library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Transform.h
Go to the documentation of this file.
1 
8 #ifndef INCLUDE_MOLASSEMBLER_TEMPLE_TRANSFORM_ADAPTOR_H
9 #define INCLUDE_MOLASSEMBLER_TEMPLE_TRANSFORM_ADAPTOR_H
10 
14 
15 namespace Scine {
16 namespace Molassembler {
17 namespace Temple {
18 namespace Adaptors {
19 namespace Detail {
20 
21 template<class Container, typename UnaryFunction>
22 struct Transformer {
25  using BoundContainer = typename Binding<Container>::type;
26 
27  using ContainerValueType = decltype(
28  *std::begin(
29  std::declval<const Container>()
30  )
31  );
32 
33  using ReturnType = decltype(
35  std::declval<UnaryFunction>(),
36  std::declval<ContainerValueType>()
37  )
38  );
39 
40  using ContainerIteratorType = decltype(std::begin(std::declval<const Container>()));
42 
45  BoundContainer container;
46  UnaryFunction function;
48 
51  Transformer(
52  Container&& passContainer,
53  UnaryFunction&& passFunction
54  ) : container(std::forward<Container>(passContainer)),
55  function(passFunction)
56  {}
58 
61  std::enable_if_t<
62  Traits::hasSize<Container>::value,
63  std::size_t
64  > size() const {
65  return container.size();
66  }
68 
71  class iterator {
72  private:
73  const Transformer* basePtr_;
74  ContainerIteratorType iter_;
75 
76  public:
77  using iterator_category = std::forward_iterator_tag;
78  using value_type = ReturnType;
79  using difference_type = int;
80  using pointer = const ReturnType*;
81  using reference = const ReturnType&;
82 
83  iterator() = default;
84  iterator(
85  const Transformer& base,
86  ContainerIteratorType&& iter
87  ) : basePtr_(&base),
88  iter_ {iter}
89  {}
90 
91  iterator& operator ++ () {
92  ++iter_;
93  return *this;
94  }
95 
96  iterator operator ++ (int) {
97  iterator prior = *this;
98  ++(*this);
99  return prior;
100  }
101 
102  bool operator == (const iterator& other) const {
103  return iter_ == other.iter_;
104  }
105 
106  bool operator != (const iterator& other) const {
107  return !(*this == other);
108  }
109 
110  ReturnType operator * () const {
111  return Temple::invoke(basePtr_->function, *iter_);
112  }
113  };
114 
115  iterator begin() const {
116  return {
117  *this,
118  std::begin(container)
119  };
120  }
121 
122  iterator end() const {
123  return {
124  *this,
125  std::end(container)
126  };
127  }
129 };
130 
131 } // namespace Detail
132 
133 template<class Container, typename UnaryFunction>
134 auto transform(
135  Container&& container,
136  UnaryFunction&& function
137 ) {
138  return Detail::Transformer<Container, UnaryFunction>(
139  std::forward<Container>(container),
140  std::forward<UnaryFunction>(function)
141  );
142 }
143 
144 } // namespace Adaptors
145 } // namespace Temple
146 } // namespace Molassembler
147 } // namespace Scine
148 
149 #endif
Uniform callable invoke from arguments or tuple of arguments.
Provides an identity functor.
unsigned size(Shape shape)
Fetch the number of vertices of a shape.
Compile-time container type traits.
auto invoke(Function &&function, const boost::tuples::cons< HT, TT > &tuple)
Invokes a function with all values in a given boost tuple.
Definition: Invoke.h:44