fennec
Loading...
Searching...
No Matches
pair.h
Go to the documentation of this file.
1// =====================================================================================================================
2// fennec, a free and open source game engine
3// Copyright © 2025 Medusa Slockbower
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17// =====================================================================================================================
18
30
31#ifndef FENNEC_CONTAINERS_PAIR_H
32#define FENNEC_CONTAINERS_PAIR_H
33
35#include <fennec/lang/hashing.h>
36#include <fennec/lang/utility.h>
37
38namespace fennec
39{
40
41// TODO: Document
42
47template<typename TypeT0, typename TypeT1>
48struct pair {
49
50// Members =============================================================================================================
51
52 TypeT0 first;
53 TypeT1 second;
54
55// Constructors ========================================================================================================
56
59
62 constexpr pair() = default;
63
66 constexpr ~pair() = default;
67
72 constexpr pair(const TypeT0& x, const TypeT1& y)
73 : first(x)
74 , second(y) {
75 }
76
81 constexpr pair(TypeT0&& x, TypeT1&& y) noexcept
82 : first(fennec::forward<TypeT0>(x))
83 , second(fennec::forward<TypeT1>(y)) {
84 }
85
90 template<typename Arg1T, typename Arg2T>
91 constexpr pair(Arg1T&& arg1, Arg2T&& arg2)
92 : first(fennec::forward<Arg1T>(arg1))
93 , second(fennec::forward<Arg2T>(arg2)) {
94 }
95
98 constexpr pair(const pair& pair)
99 : first(fennec::copy(pair.first))
100 , second(fennec::copy(pair.second)) {
101 }
102
105 constexpr pair(pair&& pair) noexcept
106 : first(fennec::move(pair.first))
107 , second(fennec::move(pair.second)) {
108 }
109
112 constexpr pair& operator=(const pair& pair) {
115 return *this;
116 }
117
120 constexpr pair& operator=(pair&& pair) {
123 return *this;
124 }
125
127
128
129// Comparison ==========================================================================================================
130
133
138 constexpr bool operator==(const pair& p) const {
139 return first == p.first and second == p.second;
140 }
141
146 constexpr bool operator!=(const pair& p) const {
147 return first != p.first or second != p.second;
148 }
149
155 constexpr bool operator<(const pair& p) const {
156 return first < p.first or (first == p.first and second < p.second);
157 }
158
164 constexpr bool operator<=(const pair& p) const {
165 return first < p.first or (first == p.first and second <= p.second);
166 }
167
173 constexpr bool operator>(const pair& p) const {
174 return first > p.first or (first == p.first and second > p.second);
175 }
176
182 constexpr bool operator>=(const pair& p) const {
183 return first > p.first or (first == p.first and second >= p.second);
184 }
185
187};
188
189template<typename TypeT0, typename TypeT1>
190struct hash<pair<TypeT0, TypeT1>> : hash<TypeT0>, hash<TypeT1> {
191 constexpr size_t operator()(const pair<TypeT0, TypeT1>& p) const {
192 return fennec::pair_hash( // pair the hashes of both elements
193 hash<TypeT0>::operator()(p.first),
194 hash<TypeT1>::operator()(p.second)
195 );
196 }
197};
198
199}
200
201#endif // FENNEC_CONTAINERS_PAIR_H
constexpr genType y()
Definition constants.h:672
Struct for holding a pair of values.
Definition pair.h:48
TypeT1 second
The second value in the pair.
Definition pair.h:53
constexpr bool operator>(const pair &p) const
Greater Than Operator.
Definition pair.h:173
constexpr pair(const pair &pair)
Copy Constructor, copies both elements.
Definition pair.h:98
constexpr bool operator<(const pair &p) const
Less Than Operator.
Definition pair.h:155
constexpr pair(pair &&pair) noexcept
Move Constructor, moves both elements.
Definition pair.h:105
constexpr bool operator>=(const pair &p) const
Greater Equal Operator.
Definition pair.h:182
constexpr ~pair()=default
Destructor, invokes destructor for both elements.
constexpr bool operator!=(const pair &p) const
Inequality Operator.
Definition pair.h:146
constexpr pair & operator=(const pair &pair)
Copy Assignment, copies both elements.
Definition pair.h:112
constexpr bool operator<=(const pair &p) const
Less Equal Operator.
Definition pair.h:164
constexpr pair(Arg1T &&arg1, Arg2T &&arg2)
Pair Implicit Constructor.
Definition pair.h:91
constexpr pair(const TypeT0 &x, const TypeT1 &y)
Pair Copy Constructor.
Definition pair.h:72
constexpr pair()=default
Default Constructor, invokes default constructor for both elements.
constexpr pair & operator=(pair &&pair)
Move Assignment, moves both elements.
Definition pair.h:120
TypeT0 first
The first value in the pair.
Definition pair.h:52
constexpr bool operator==(const pair &p) const
Equality Operator.
Definition pair.h:138
constexpr pair(TypeT0 &&x, TypeT1 &&y) noexcept
Pair Move Constructor.
Definition pair.h:81
A header containing the definition for a container with multiple values of differing types.
Utility
constexpr T && forward(remove_reference_t< T > &x) noexcept
forwards reference types to extend their lifetime
Definition utility.h:75
constexpr const remove_reference_t< T > & copy(T &&x) noexcept
produces an r-value type to indicate x may be "copied"
Definition utility.h:104
constexpr remove_reference_t< T > && move(T &&x) noexcept
produces an x-value type to indicate x may be "moved"
Definition utility.h:92