idol
A C++ Framework for Optimization
Loading...
Searching...
No Matches
Pair.h
1//
2// Created by henri on 15.11.23.
3//
4
5#ifndef IDOL_PAIR_H
6#define IDOL_PAIR_H
7
8#include <iostream>
9
10namespace idol {
11 template<class T1, class T2> struct Pair;
12 template<class T, class CompareT> struct CommutativePair;
13}
14
15template<class T1, class T2>
16struct idol::Pair {
17 T1 first;
18 T2 second;
19
20 Pair(const T1& t_1, const T2& t_2) : first(t_1), second(t_2) {}
21 Pair(T1&& t_1, T2&& t_2) : first(std::move(t_1)), second(std::move(t_2)) {}
22 Pair(const T1& t_1, T2&& t_2) : first(t_1), second(std::move(t_2)) {}
23 Pair(T1&& t_1, const T2& t_2) : first(std::move(t_1)), second(t_2) {}
24
25 Pair(const Pair&) = default;
26 Pair(Pair&&) = default;
27
28 Pair& operator=(const Pair&) = default;
29 Pair& operator=(Pair&&) = default;
30
31 bool operator==(const Pair& t_rhs) const {
32 return std::equal_to<T1>()(first, t_rhs.first) && std::equal_to<T2>()(second, t_rhs.second);
33 }
34};
35
36template<class T, class CompareT = std::less<T>>
37struct idol::CommutativePair : public idol::Pair<T, T> {
38 CommutativePair(const T& t_1, const T& t_2) : Pair<T, T>(CompareT()(t_1, t_2) ? t_1 : t_2, CompareT()(t_1, t_2) ? t_2 : t_1) {}
39 CommutativePair(T&& t_1, T&& t_2) : Pair<T, T>(CompareT()(t_1, t_2) ? std::move(t_1) : std::move(t_2), CompareT()(t_1, t_2) ? std::move(t_2) : std::move(t_1)) {}
40 CommutativePair(const T& t_1, T&& t_2) : Pair<T, T>(CompareT()(t_1, t_2) ? t_1 : std::move(t_2), CompareT()(t_1, t_2) ? std::move(t_2) : t_1) {}
41 CommutativePair(T&& t_1, const T& t_2) : Pair<T, T>(CompareT()(t_1, t_2) ? std::move(t_1) : t_2, CompareT()(t_1, t_2) ? t_2 : std::move(t_1)) {}
42
43 CommutativePair(const CommutativePair&) = default;
45
46 CommutativePair& operator=(const CommutativePair&) = default;
47 CommutativePair& operator=(CommutativePair&&) = default;
48
49 bool operator==(const CommutativePair& t_rhs) const {
50 return std::equal_to<T>()(this->first, t_rhs.first) && std::equal_to<T>()(this->second, t_rhs.second);
51 }
52};
53
54namespace idol {
55 template<class T1, class T2>
56 static std::ostream &operator<<(std::ostream &t_os, const Pair<T1, T2> &t_pair) {
57 return t_os << t_pair.first << " " << t_pair.second;
58 }
59}
60
61#endif //IDOL_PAIR_H