[libcxx-commits] [libcxx] [libc++] Optimize most of the __tree search algorithms (PR #155245)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Sep 2 10:52:16 PDT 2025
================
@@ -0,0 +1,114 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___UTILITY_THREE_WAY_COMPARATOR_H
+#define _LIBCPP___UTILITY_THREE_WAY_COMPARATOR_H
+
+#include <__config>
+#include <__type_traits/desugars_to.h>
+#include <__type_traits/enable_if.h>
+#include <__type_traits/is_arithmetic.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+// This file adds a __lazy_synth_three_way_comparator, which tries to build an efficient three way comparison from a
+// binary comparator. That is done in multiple steps:
+// 1) Check whether the comparator desugars to a less than operator
+// If that is the case, check whether there exists a specialization of `__default_three_way_comparator`, which
+// can be specialized to implement a three way comparator for the specific types.
+// 2) Fall back to doing a lazy less than/greater than comparison
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+// This struct can be specialized to provide a thee way comparator between _LHS and _RHS.
+// The return value should be
+// - less than zero if (lhs_val < rhs_val)
+// - greater than zero if (rhs_val < lhs_val)
+// - zero otherwise
+template <class _LHS, class _RHS, class = void>
+struct __default_three_way_comparator;
+
+template <class _Tp>
+struct __default_three_way_comparator<_Tp, _Tp, __enable_if_t<is_arithmetic<_Tp>::value> > {
----------------
ldionne wrote:
I would suggest moving the `__default_foo` to its own header, since they are fundamentally different utilities. I think it will help clarify what are the moving parts here.
https://github.com/llvm/llvm-project/pull/155245
More information about the libcxx-commits
mailing list