Skip to content

Commit 013de1f

Browse files
authored
feat(compat): add pair and make_pair (#13)
Co-authored-by: io-eric <io-eric@users.noreply.github.com>
1 parent 4fdc5d2 commit 013de1f

2 files changed

Lines changed: 119 additions & 0 deletions

File tree

include/webcc/compat/utility

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ namespace std
77
using webcc::forward;
88
using webcc::swap;
99
using webcc::remove_reference;
10+
using webcc::pair;
11+
using webcc::make_pair;
1012
} // namespace std

include/webcc/core/utility.h

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,121 @@ namespace webcc
2828
a = move(b);
2929
b = move(temp);
3030
}
31+
32+
// Pair implementation
33+
template<typename T1, typename T2>
34+
struct pair
35+
{
36+
typedef T1 first_type;
37+
typedef T2 second_type;
38+
39+
T1 first;
40+
T2 second;
41+
42+
// Default constructor
43+
pair() : first(), second() {}
44+
45+
// Constructor with values
46+
pair(const T1& a, const T2& b) : first(a), second(b) {}
47+
48+
// Move constructor with values
49+
template<typename U1, typename U2>
50+
pair(U1&& a, U2&& b)
51+
: first(forward<U1>(a)), second(forward<U2>(b)) {}
52+
53+
// Copy constructor
54+
pair(const pair& other) = default;
55+
56+
// Move constructor
57+
pair(pair&& other) = default;
58+
59+
// Copy constructor from different pair type
60+
template<typename U1, typename U2>
61+
pair(const pair<U1, U2>& other)
62+
: first(other.first), second(other.second) {}
63+
64+
// Move constructor from different pair type
65+
template<typename U1, typename U2>
66+
pair(pair<U1, U2>&& other)
67+
: first(forward<U1>(other.first)), second(forward<U2>(other.second)) {}
68+
69+
// Copy assignment
70+
pair& operator=(const pair& other) = default;
71+
72+
// Move assignment
73+
pair& operator=(pair&& other) = default;
74+
75+
// Copy assignment from different pair type
76+
template<typename U1, typename U2>
77+
pair& operator=(const pair<U1, U2>& other)
78+
{
79+
first = other.first;
80+
second = other.second;
81+
return *this;
82+
}
83+
84+
// Move assignment from different pair type
85+
template<typename U1, typename U2>
86+
pair& operator=(pair<U1, U2>&& other)
87+
{
88+
first = forward<U1>(other.first);
89+
second = forward<U2>(other.second);
90+
return *this;
91+
}
92+
93+
void swap(pair& other)
94+
{
95+
webcc::swap(first, other.first);
96+
webcc::swap(second, other.second);
97+
}
98+
};
99+
100+
// Comparison operators
101+
template<typename T1, typename T2>
102+
bool operator==(const pair<T1, T2>& lhs, const pair<T1, T2>& rhs)
103+
{
104+
return lhs.first == rhs.first && lhs.second == rhs.second;
105+
}
106+
107+
template<typename T1, typename T2>
108+
bool operator!=(const pair<T1, T2>& lhs, const pair<T1, T2>& rhs)
109+
{
110+
return !(lhs == rhs);
111+
}
112+
113+
template<typename T1, typename T2>
114+
bool operator<(const pair<T1, T2>& lhs, const pair<T1, T2>& rhs)
115+
{
116+
if (lhs.first < rhs.first) return true;
117+
if (rhs.first < lhs.first) return false;
118+
return lhs.second < rhs.second;
119+
}
120+
121+
template<typename T1, typename T2>
122+
bool operator<=(const pair<T1, T2>& lhs, const pair<T1, T2>& rhs)
123+
{
124+
return !(rhs < lhs);
125+
}
126+
127+
template<typename T1, typename T2>
128+
bool operator>(const pair<T1, T2>& lhs, const pair<T1, T2>& rhs)
129+
{
130+
return rhs < lhs;
131+
}
132+
133+
template<typename T1, typename T2>
134+
bool operator>=(const pair<T1, T2>& lhs, const pair<T1, T2>& rhs)
135+
{
136+
return !(lhs < rhs);
137+
}
138+
139+
// make_pair helper function
140+
template<typename T1, typename T2>
141+
pair<typename remove_reference<T1>::type, typename remove_reference<T2>::type>
142+
make_pair(T1&& a, T2&& b)
143+
{
144+
return pair<typename remove_reference<T1>::type,
145+
typename remove_reference<T2>::type>(
146+
forward<T1>(a), forward<T2>(b));
147+
}
31148
} // namespace webcc

0 commit comments

Comments
 (0)