Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
[llvm-project.git] / libcxx / include / __chrono / time_point.h
blob5e79fa5d257fa430f706c3ba24b2b09139c755da
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___CHRONO_TIME_POINT_H
11 #define _LIBCPP___CHRONO_TIME_POINT_H
13 #include <__chrono/duration.h>
14 #include <__compare/ordering.h>
15 #include <__compare/three_way_comparable.h>
16 #include <__config>
17 #include <__type_traits/common_type.h>
18 #include <__type_traits/enable_if.h>
19 #include <__type_traits/is_convertible.h>
20 #include <limits>
22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23 # pragma GCC system_header
24 #endif
26 _LIBCPP_PUSH_MACROS
27 #include <__undef_macros>
29 _LIBCPP_BEGIN_NAMESPACE_STD
31 namespace chrono {
33 template <class _Clock, class _Duration = typename _Clock::duration>
34 class _LIBCPP_TEMPLATE_VIS time_point {
35 static_assert(__is_duration_v<_Duration>, "Second template parameter of time_point must be a std::chrono::duration");
37 public:
38 typedef _Clock clock;
39 typedef _Duration duration;
40 typedef typename duration::rep rep;
41 typedef typename duration::period period;
43 private:
44 duration __d_;
46 public:
47 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point() : __d_(duration::zero()) {}
48 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit time_point(const duration& __d) : __d_(__d) {}
50 // conversions
51 template <class _Duration2, __enable_if_t<is_convertible<_Duration2, duration>::value, int> = 0>
52 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point(const time_point<clock, _Duration2>& __t)
53 : __d_(__t.time_since_epoch()) {}
55 // observer
57 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 duration time_since_epoch() const { return __d_; }
59 // arithmetic
61 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 time_point& operator+=(const duration& __d) {
62 __d_ += __d;
63 return *this;
65 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 time_point& operator-=(const duration& __d) {
66 __d_ -= __d;
67 return *this;
70 // special values
72 _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR time_point min() _NOEXCEPT { return time_point(duration::min()); }
73 _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR time_point max() _NOEXCEPT { return time_point(duration::max()); }
76 } // namespace chrono
78 template <class _Clock, class _Duration1, class _Duration2>
79 struct _LIBCPP_TEMPLATE_VIS
80 common_type<chrono::time_point<_Clock, _Duration1>, chrono::time_point<_Clock, _Duration2> > {
81 typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type;
84 namespace chrono {
86 template <class _ToDuration, class _Clock, class _Duration>
87 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point<_Clock, _ToDuration>
88 time_point_cast(const time_point<_Clock, _Duration>& __t) {
89 return time_point<_Clock, _ToDuration>(chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));
92 #if _LIBCPP_STD_VER >= 17
93 template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration_v<_ToDuration>, int> = 0>
94 inline _LIBCPP_HIDE_FROM_ABI constexpr time_point<_Clock, _ToDuration> floor(const time_point<_Clock, _Duration>& __t) {
95 return time_point<_Clock, _ToDuration>{chrono::floor<_ToDuration>(__t.time_since_epoch())};
98 template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration_v<_ToDuration>, int> = 0>
99 inline _LIBCPP_HIDE_FROM_ABI constexpr time_point<_Clock, _ToDuration> ceil(const time_point<_Clock, _Duration>& __t) {
100 return time_point<_Clock, _ToDuration>{chrono::ceil<_ToDuration>(__t.time_since_epoch())};
103 template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration_v<_ToDuration>, int> = 0>
104 inline _LIBCPP_HIDE_FROM_ABI constexpr time_point<_Clock, _ToDuration> round(const time_point<_Clock, _Duration>& __t) {
105 return time_point<_Clock, _ToDuration>{chrono::round<_ToDuration>(__t.time_since_epoch())};
108 template <class _Rep, class _Period, enable_if_t<numeric_limits<_Rep>::is_signed, int> = 0>
109 inline _LIBCPP_HIDE_FROM_ABI constexpr duration<_Rep, _Period> abs(duration<_Rep, _Period> __d) {
110 return __d >= __d.zero() ? +__d : -__d;
112 #endif // _LIBCPP_STD_VER >= 17
114 // time_point ==
116 template <class _Clock, class _Duration1, class _Duration2>
117 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
118 operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
119 return __lhs.time_since_epoch() == __rhs.time_since_epoch();
122 #if _LIBCPP_STD_VER <= 17
124 // time_point !=
126 template <class _Clock, class _Duration1, class _Duration2>
127 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
128 operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
129 return !(__lhs == __rhs);
132 #endif // _LIBCPP_STD_VER <= 17
134 // time_point <
136 template <class _Clock, class _Duration1, class _Duration2>
137 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
138 operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
139 return __lhs.time_since_epoch() < __rhs.time_since_epoch();
142 // time_point >
144 template <class _Clock, class _Duration1, class _Duration2>
145 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
146 operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
147 return __rhs < __lhs;
150 // time_point <=
152 template <class _Clock, class _Duration1, class _Duration2>
153 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
154 operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
155 return !(__rhs < __lhs);
158 // time_point >=
160 template <class _Clock, class _Duration1, class _Duration2>
161 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
162 operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
163 return !(__lhs < __rhs);
166 #if _LIBCPP_STD_VER >= 20
168 template <class _Clock, class _Duration1, three_way_comparable_with<_Duration1> _Duration2>
169 _LIBCPP_HIDE_FROM_ABI constexpr auto
170 operator<=>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
171 return __lhs.time_since_epoch() <=> __rhs.time_since_epoch();
174 #endif // _LIBCPP_STD_VER >= 20
176 // time_point operator+(time_point x, duration y);
178 template <class _Clock, class _Duration1, class _Rep2, class _Period2>
179 inline _LIBCPP_HIDE_FROM_ABI
180 _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
181 operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
182 typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr;
183 return _Tr(__lhs.time_since_epoch() + __rhs);
186 // time_point operator+(duration x, time_point y);
188 template <class _Rep1, class _Period1, class _Clock, class _Duration2>
189 inline _LIBCPP_HIDE_FROM_ABI
190 _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type>
191 operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
192 return __rhs + __lhs;
195 // time_point operator-(time_point x, duration y);
197 template <class _Clock, class _Duration1, class _Rep2, class _Period2>
198 inline _LIBCPP_HIDE_FROM_ABI
199 _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
200 operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
201 typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Ret;
202 return _Ret(__lhs.time_since_epoch() - __rhs);
205 // duration operator-(time_point x, time_point y);
207 template <class _Clock, class _Duration1, class _Duration2>
208 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename common_type<_Duration1, _Duration2>::type
209 operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
210 return __lhs.time_since_epoch() - __rhs.time_since_epoch();
213 } // namespace chrono
215 _LIBCPP_END_NAMESPACE_STD
217 _LIBCPP_POP_MACROS
219 #endif // _LIBCPP___CHRONO_TIME_POINT_H