Molassembler  3.0.0
Molecule graph and conformer library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
OrderedPair.h
Go to the documentation of this file.
1 
11 #ifndef INCLUDE_MOLASSEMBLER_TEMPLE_ORDERED_PAIR_H
12 #define INCLUDE_MOLASSEMBLER_TEMPLE_ORDERED_PAIR_H
13 
14 #include "OperatorSuppliers.h"
15 
16 #include <tuple>
17 #include <utility>
18 #include <iterator>
19 
20 namespace Scine {
21 namespace Molassembler {
22 namespace Temple {
23 
35 template<typename T>
36 struct OrderedPair : Crtp::LexicographicComparable<OrderedPair<T>> {
40  using value_type = T;
42  using iterator = T*;
44  using const_iterator = const T*;
46 
50  T first;
52  T second;
54 
58  constexpr OrderedPair() = default;
59 
61  constexpr OrderedPair(T a, T b) : first(std::move(a)), second(std::move(b)) {
62  if(second < first) {
63  std::swap(first, second);
64  }
65  }
67 
70  constexpr T front() const {
71  return first;
72  }
73 
74  constexpr T& front() {
75  return first;
76  }
77 
78  constexpr T back() const {
79  return second;
80  }
81 
82  constexpr T& back() {
83  return second;
84  }
86 
89  iterator begin() {
90  return &first;
91  }
92 
93  iterator end() {
94  return std::next(&second);
95  }
96 
97  const_iterator begin() const {
98  return &first;
99  }
100 
101  const_iterator end() const {
102  return std::next(&second);
103  }
104 
105  const_iterator cbegin() const {
106  return &first;
107  }
108 
109  const_iterator cend() const {
110  return std::next(&second);
111  }
113 
117  constexpr auto tie() const {
118  return std::tie(first, second);
119  }
121 
123  template<typename UnaryFunction>
124  auto map(UnaryFunction&& mapFunction) const {
125  return std::make_pair(
126  mapFunction(first),
127  mapFunction(second)
128  );
129  }
130 };
131 
132 } // namespace Temple
133 } // namespace Molassembler
134 } // namespace Scine
135 
136 #endif
T * iterator
Iterator type.
Definition: OrderedPair.h:42
Generates all operators using a method returning a tuple.
Definition: OperatorSuppliers.h:78
Operator-supplying CRTP base classes.
Encompasses the orientation of a shape along a fused bond.
Definition: Composites.h:56
A class that imitates std::pair&lt;T, U&gt;, but whose template arguments are homogeneous and the stored va...
Definition: OrderedPair.h:36
constexpr OrderedPair()=default
Default constructor.
T second
Second element of the pair.
Definition: OrderedPair.h:52
T first
First element of the pair.
Definition: OrderedPair.h:50
constexpr auto tie() const
Yields the result of std::tie(first, second)
Definition: OrderedPair.h:117
constexpr OrderedPair(T a, T b)
Reordering pair initializer.
Definition: OrderedPair.h:61
auto map(UnaryFunction &&mapFunction) const
Map the pair into an unordered std::pair.
Definition: OrderedPair.h:124
const T * const_iterator
Const iterator type.
Definition: OrderedPair.h:44