Commit fbfde2cb authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

An ordering type

Summary: [Folly] An `ordering` type, lifted from `FixedString`.

Reviewed By: stevegury

Differential Revision: D7596713

fbshipit-source-id: ba19e1e36e75b946e14f6db9c72ac1ac3c7270e9
parent 832f234a
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#include <folly/Range.h> #include <folly/Range.h>
#include <folly/Utility.h> #include <folly/Utility.h>
#include <folly/lang/Exception.h> #include <folly/lang/Exception.h>
#include <folly/lang/Ordering.h>
#include <folly/portability/Constexpr.h> #include <folly/portability/Constexpr.h>
namespace folly { namespace folly {
...@@ -99,8 +100,6 @@ constexpr const Char (&checkNullTerminated(const Char (&a)[N]) noexcept)[N] { ...@@ -99,8 +100,6 @@ constexpr const Char (&checkNullTerminated(const Char (&a)[N]) noexcept)[N] {
: (assertNotNullTerminated(), decltype(a)(a)); : (assertNotNullTerminated(), decltype(a)(a));
} }
enum class Cmp : int { LT = -1, EQ = 0, GT = 1 };
// Rather annoyingly, GCC's -Warray-bounds warning issues false positives for // Rather annoyingly, GCC's -Warray-bounds warning issues false positives for
// this code. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61971 // this code. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61971
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ <= 5 #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ <= 5
...@@ -109,7 +108,7 @@ enum class Cmp : int { LT = -1, EQ = 0, GT = 1 }; ...@@ -109,7 +108,7 @@ enum class Cmp : int { LT = -1, EQ = 0, GT = 1 };
#endif #endif
template <class Left, class Right> template <class Left, class Right>
constexpr Cmp compare_( constexpr ordering compare_(
const Left& left, const Left& left,
std::size_t left_pos, std::size_t left_pos,
std::size_t left_size, std::size_t left_size,
...@@ -117,12 +116,12 @@ constexpr Cmp compare_( ...@@ -117,12 +116,12 @@ constexpr Cmp compare_(
std::size_t right_pos, std::size_t right_pos,
std::size_t right_size) noexcept { std::size_t right_size) noexcept {
return left_pos == left_size return left_pos == left_size
? (right_pos == right_size ? Cmp::EQ : Cmp::LT) ? (right_pos == right_size ? ordering::eq : ordering::lt)
: (right_pos == right_size ? Cmp::GT : (right_pos == right_size ? ordering::gt
: (left[left_pos] < right[right_pos] : (left[left_pos] < right[right_pos]
? Cmp::LT ? ordering::lt
: (left[left_pos] > right[right_pos] : (left[left_pos] > right[right_pos]
? Cmp::GT ? ordering::gt
: fixedstring::compare_( : fixedstring::compare_(
left, left,
left_pos + 1u, left_pos + 1u,
...@@ -139,7 +138,7 @@ constexpr bool equal_( ...@@ -139,7 +138,7 @@ constexpr bool equal_(
const Right& right, const Right& right,
std::size_t right_size) noexcept { std::size_t right_size) noexcept {
return left_size == right_size && return left_size == right_size &&
Cmp::EQ == compare_(left, 0u, left_size, right, 0u, right_size); ordering::eq == compare_(left, 0u, left_size, right, 0u, right_size);
} }
template <class Char, class Left, class Right> template <class Char, class Left, class Right>
...@@ -2701,7 +2700,7 @@ class BasicFixedString : private detail::fixedstring::FixedStringBase { ...@@ -2701,7 +2700,7 @@ class BasicFixedString : private detail::fixedstring::FixedStringBase {
friend constexpr bool operator<( friend constexpr bool operator<(
const Char* a, const Char* a,
const BasicFixedString& b) noexcept { const BasicFixedString& b) noexcept {
return detail::fixedstring::Cmp::LT == return ordering::lt ==
detail::fixedstring::compare_( detail::fixedstring::compare_(
a, 0u, folly::constexpr_strlen(a), b.data_, 0u, b.size_); a, 0u, folly::constexpr_strlen(a), b.data_, 0u, b.size_);
} }
...@@ -2712,7 +2711,7 @@ class BasicFixedString : private detail::fixedstring::FixedStringBase { ...@@ -2712,7 +2711,7 @@ class BasicFixedString : private detail::fixedstring::FixedStringBase {
friend constexpr bool operator<( friend constexpr bool operator<(
const BasicFixedString& a, const BasicFixedString& a,
const Char* b) noexcept { const Char* b) noexcept {
return detail::fixedstring::Cmp::LT == return ordering::lt ==
detail::fixedstring::compare_( detail::fixedstring::compare_(
a.data_, 0u, a.size_, b, 0u, folly::constexpr_strlen(b)); a.data_, 0u, a.size_, b, 0u, folly::constexpr_strlen(b));
} }
...@@ -2723,7 +2722,7 @@ class BasicFixedString : private detail::fixedstring::FixedStringBase { ...@@ -2723,7 +2722,7 @@ class BasicFixedString : private detail::fixedstring::FixedStringBase {
friend constexpr bool operator<( friend constexpr bool operator<(
Range<const Char*> a, Range<const Char*> a,
const BasicFixedString& b) noexcept { const BasicFixedString& b) noexcept {
return detail::fixedstring::Cmp::LT == return ordering::lt ==
detail::fixedstring::compare_( detail::fixedstring::compare_(
a.begin(), 0u, a.size(), b.data_, 0u, b.size_); a.begin(), 0u, a.size(), b.data_, 0u, b.size_);
} }
...@@ -2734,7 +2733,7 @@ class BasicFixedString : private detail::fixedstring::FixedStringBase { ...@@ -2734,7 +2733,7 @@ class BasicFixedString : private detail::fixedstring::FixedStringBase {
friend constexpr bool operator<( friend constexpr bool operator<(
const BasicFixedString& a, const BasicFixedString& a,
Range<const Char*> b) noexcept { Range<const Char*> b) noexcept {
return detail::fixedstring::Cmp::LT == return ordering::lt ==
detail::fixedstring::compare_( detail::fixedstring::compare_(
a.data_, 0u, a.size_, b.begin(), 0u, b.size()); a.data_, 0u, a.size_, b.begin(), 0u, b.size());
} }
...@@ -2933,7 +2932,7 @@ template <class Char, std::size_t A, std::size_t B> ...@@ -2933,7 +2932,7 @@ template <class Char, std::size_t A, std::size_t B>
constexpr bool operator<( constexpr bool operator<(
const BasicFixedString<Char, A>& a, const BasicFixedString<Char, A>& a,
const BasicFixedString<Char, B>& b) noexcept { const BasicFixedString<Char, B>& b) noexcept {
return detail::fixedstring::Cmp::LT == return ordering::lt ==
detail::fixedstring::compare_( detail::fixedstring::compare_(
detail::fixedstring::Helper::data_(a), detail::fixedstring::Helper::data_(a),
0u, 0u,
......
/*
* Copyright 2018-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
namespace folly {
enum class ordering : int { lt = -1, eq = 0, gt = 1 };
} // namespace folly
/*
* Copyright 2018-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/lang/Ordering.h>
#include <folly/portability/GTest.h>
using namespace folly;
class OrderingTest : public testing::Test {};
TEST_F(OrderingTest, ordering) {
EXPECT_EQ(-1, int(ordering::lt));
EXPECT_EQ(0, int(ordering::eq));
EXPECT_EQ(+1, int(ordering::gt));
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment