Commit 8e088c5f by Abseil Team Committed by Derek Mauro

Export of internal Abseil changes

--
77cd6291781bc39e8472c706163d6951fe2ae573 by Derek Mauro <dmauro@google.com>:

absl::uint128: Use intrinsics for more operations when available

This change also inlines the division and modulus operators when
intrinsics are available for better code generation.

Fixes #987

PiperOrigin-RevId: 389895706

--
fa23339584599e07ebcb4d0a857e2553b017757c by Abseil Team <absl-team@google.com>:

only hide retired flags if human-readable output is requested

PiperOrigin-RevId: 389835452

--
f1111f2b88359d4b253d4d81681c8a488458a36e by Martijn Vels <mvels@google.com>:

Add helpers IsFlat(), IsExternal(), etc to improve readability

PiperOrigin-RevId: 389779333

--
785b8712261e41695ebeeb64b4317f93b37adc11 by Martijn Vels <mvels@google.com>:

Split off 'concat' and 'btree' RepMemoryUsageLeaf and RepMemoryUsageDataEdge

PiperOrigin-RevId: 389701120

--
5264bffebffc2b377bf7e18f0ce69a3ed38c6629 by CJ Johnson <johnsoncj@google.com>:

Eagerly destroy `Callback` in `absl::Cleanup`

PiperOrigin-RevId: 389678813

--
a05312f0668458e97c50ca932c8f974c1508ebf2 by Abseil Team <absl-team@google.com>:

Have one instance of empty_group per program, rather than one per translation unit.

https://stackoverflow.com/questions/185624/static-variables-in-an-inlined-function

PiperOrigin-RevId: 389185845
GitOrigin-RevId: 77cd6291781bc39e8472c706163d6951fe2ae573
Change-Id: Iac8d9cb27707a9562c831c77a552d1fb4bb0405f
parent bf31a10b
...@@ -86,25 +86,25 @@ class ABSL_MUST_USE_RESULT Cleanup final { ...@@ -86,25 +86,25 @@ class ABSL_MUST_USE_RESULT Cleanup final {
"Callbacks that return values are not supported."); "Callbacks that return values are not supported.");
public: public:
Cleanup(Callback callback) // NOLINT Cleanup(Callback callback) : storage_(std::move(callback)) {} // NOLINT
: storage_(std::move(callback), /* is_callback_engaged = */ true) {}
Cleanup(Cleanup&& other) = default; Cleanup(Cleanup&& other) = default;
void Cancel() && { void Cancel() && {
ABSL_HARDENING_ASSERT(storage_.IsCallbackEngaged()); ABSL_HARDENING_ASSERT(storage_.IsCallbackEngaged());
storage_.DisengageCallback(); storage_.DestroyCallback();
} }
void Invoke() && { void Invoke() && {
ABSL_HARDENING_ASSERT(storage_.IsCallbackEngaged()); ABSL_HARDENING_ASSERT(storage_.IsCallbackEngaged());
storage_.DisengageCallback();
storage_.InvokeCallback(); storage_.InvokeCallback();
storage_.DestroyCallback();
} }
~Cleanup() { ~Cleanup() {
if (storage_.IsCallbackEngaged()) { if (storage_.IsCallbackEngaged()) {
storage_.InvokeCallback(); storage_.InvokeCallback();
storage_.DestroyCallback();
} }
} }
......
...@@ -264,4 +264,48 @@ TYPED_TEST(CleanupTest, Move) { ...@@ -264,4 +264,48 @@ TYPED_TEST(CleanupTest, Move) {
EXPECT_FALSE(called); // Destructor shouldn't invoke the callback EXPECT_FALSE(called); // Destructor shouldn't invoke the callback
} }
int DestructionCount = 0;
struct DestructionCounter {
void operator()() {}
~DestructionCounter() { ++DestructionCount; }
};
TYPED_TEST(CleanupTest, DestructorDestroys) {
{
auto cleanup =
absl::MakeCleanup(TypeParam::AsCallback(DestructionCounter()));
DestructionCount = 0;
}
EXPECT_EQ(DestructionCount, 1); // Engaged cleanup destroys
}
TYPED_TEST(CleanupTest, CancelDestroys) {
{
auto cleanup =
absl::MakeCleanup(TypeParam::AsCallback(DestructionCounter()));
DestructionCount = 0;
std::move(cleanup).Cancel();
EXPECT_EQ(DestructionCount, 1); // Cancel destroys
}
EXPECT_EQ(DestructionCount, 1); // Canceled cleanup does not double destroy
}
TYPED_TEST(CleanupTest, InvokeDestroys) {
{
auto cleanup =
absl::MakeCleanup(TypeParam::AsCallback(DestructionCounter()));
DestructionCount = 0;
std::move(cleanup).Invoke();
EXPECT_EQ(DestructionCount, 1); // Invoke destroys
}
EXPECT_EQ(DestructionCount, 1); // Invoked cleanup does not double destroy
}
} // namespace } // namespace
...@@ -15,10 +15,12 @@ ...@@ -15,10 +15,12 @@
#ifndef ABSL_CLEANUP_INTERNAL_CLEANUP_H_ #ifndef ABSL_CLEANUP_INTERNAL_CLEANUP_H_
#define ABSL_CLEANUP_INTERNAL_CLEANUP_H_ #define ABSL_CLEANUP_INTERNAL_CLEANUP_H_
#include <new>
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
#include "absl/base/internal/invoke.h" #include "absl/base/internal/invoke.h"
#include "absl/base/macros.h"
#include "absl/base/thread_annotations.h" #include "absl/base/thread_annotations.h"
#include "absl/utility/utility.h" #include "absl/utility/utility.h"
...@@ -45,14 +47,22 @@ class Storage { ...@@ -45,14 +47,22 @@ class Storage {
public: public:
Storage() = delete; Storage() = delete;
Storage(Callback callback, bool is_callback_engaged) explicit Storage(Callback callback) {
: callback_(std::move(callback)), // Placement-new into a character buffer is used for eager destruction when
is_callback_engaged_(is_callback_engaged) {} // the cleanup is invoked or cancelled. To ensure this optimizes well, the
// behavior is implemented locally instead of using an absl::optional.
::new (GetCallbackBuffer()) Callback(std::move(callback));
is_callback_engaged_ = true;
}
Storage(Storage&& other) {
ABSL_HARDENING_ASSERT(other.IsCallbackEngaged());
Storage(Storage&& other) ::new (GetCallbackBuffer()) Callback(std::move(other.GetCallback()));
: callback_(std::move(other.callback_)), is_callback_engaged_ = true;
is_callback_engaged_(
absl::exchange(other.is_callback_engaged_, false)) {} other.DestroyCallback();
}
Storage(const Storage& other) = delete; Storage(const Storage& other) = delete;
...@@ -60,17 +70,26 @@ class Storage { ...@@ -60,17 +70,26 @@ class Storage {
Storage& operator=(const Storage& other) = delete; Storage& operator=(const Storage& other) = delete;
void* GetCallbackBuffer() { return static_cast<void*>(+callback_buffer_); }
Callback& GetCallback() {
return *reinterpret_cast<Callback*>(GetCallbackBuffer());
}
bool IsCallbackEngaged() const { return is_callback_engaged_; } bool IsCallbackEngaged() const { return is_callback_engaged_; }
void DisengageCallback() { is_callback_engaged_ = false; } void DestroyCallback() {
is_callback_engaged_ = false;
GetCallback().~Callback();
}
void InvokeCallback() ABSL_NO_THREAD_SAFETY_ANALYSIS { void InvokeCallback() ABSL_NO_THREAD_SAFETY_ANALYSIS {
std::move(callback_)(); std::move(GetCallback())();
} }
private: private:
Callback callback_;
bool is_callback_engaged_; bool is_callback_engaged_;
alignas(Callback) char callback_buffer_[sizeof(Callback)];
}; };
} // namespace cleanup_internal } // namespace cleanup_internal
......
...@@ -23,6 +23,12 @@ namespace absl { ...@@ -23,6 +23,12 @@ namespace absl {
ABSL_NAMESPACE_BEGIN ABSL_NAMESPACE_BEGIN
namespace container_internal { namespace container_internal {
ABSL_CONST_INIT ABSL_DLL alignas(16) const ctrl_t kEmptyGroup[16] = {
ctrl_t::kSentinel, ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty,
ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty,
ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty,
ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty};
constexpr size_t Group::kWidth; constexpr size_t Group::kWidth;
// Returns "random" seed. // Returns "random" seed.
......
...@@ -291,13 +291,9 @@ static_assert(ctrl_t::kDeleted == static_cast<ctrl_t>(-2), ...@@ -291,13 +291,9 @@ static_assert(ctrl_t::kDeleted == static_cast<ctrl_t>(-2),
// A single block of empty control bytes for tables without any slots allocated. // A single block of empty control bytes for tables without any slots allocated.
// This enables removing a branch in the hot path of find(). // This enables removing a branch in the hot path of find().
ABSL_DLL extern const ctrl_t kEmptyGroup[16];
inline ctrl_t* EmptyGroup() { inline ctrl_t* EmptyGroup() {
alignas(16) static constexpr ctrl_t empty_group[] = { return const_cast<ctrl_t*>(kEmptyGroup);
ctrl_t::kSentinel, ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty,
ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty,
ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty,
ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty};
return const_cast<ctrl_t*>(empty_group);
} }
// Mixes a randomly generated per-process seed with `hash` and `ctrl` to // Mixes a randomly generated per-process seed with `hash` and `ctrl` to
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include <stdint.h> #include <stdint.h>
#include <algorithm>
#include <functional> #include <functional>
#include <map> #include <map>
#include <ostream> #include <ostream>
...@@ -255,9 +256,6 @@ void FlagsHelpImpl(std::ostream& out, PerFlagFilter filter_cb, ...@@ -255,9 +256,6 @@ void FlagsHelpImpl(std::ostream& out, PerFlagFilter filter_cb,
matching_flags; matching_flags;
flags_internal::ForEachFlag([&](absl::CommandLineFlag& flag) { flags_internal::ForEachFlag([&](absl::CommandLineFlag& flag) {
// Ignore retired flags.
if (flag.IsRetired()) return;
// If the flag has been stripped, pretend that it doesn't exist. // If the flag has been stripped, pretend that it doesn't exist.
if (flag.Help() == flags_internal::kStrippedFlagHelp) return; if (flag.Help() == flags_internal::kStrippedFlagHelp) return;
...@@ -275,6 +273,14 @@ void FlagsHelpImpl(std::ostream& out, PerFlagFilter filter_cb, ...@@ -275,6 +273,14 @@ void FlagsHelpImpl(std::ostream& out, PerFlagFilter filter_cb,
absl::string_view file_separator; // controls blank lines between files absl::string_view file_separator; // controls blank lines between files
for (auto& package : matching_flags) { for (auto& package : matching_flags) {
if (format == HelpFormat::kHumanReadable) { if (format == HelpFormat::kHumanReadable) {
// Hide packages with only retired flags
bool all_package_flags_are_retired = true;
for (const auto& flags_in_file : package.second) {
for (const auto* flag : flags_in_file.second) {
all_package_flags_are_retired &= flag->IsRetired();
}
}
if (all_package_flags_are_retired) continue;
out << package_separator; out << package_separator;
package_separator = "\n\n"; package_separator = "\n\n";
} }
...@@ -334,8 +340,11 @@ void FlagsHelpImpl(std::ostream& out, ...@@ -334,8 +340,11 @@ void FlagsHelpImpl(std::ostream& out,
// Produces the help message describing specific flag. // Produces the help message describing specific flag.
void FlagHelp(std::ostream& out, const CommandLineFlag& flag, void FlagHelp(std::ostream& out, const CommandLineFlag& flag,
HelpFormat format) { HelpFormat format) {
if (format == HelpFormat::kHumanReadable) if (format == HelpFormat::kHumanReadable) {
// Ignore retired flags
if (flag.IsRetired()) return;
flags_internal::FlagHelpHumanReadable(flag, out); flags_internal::FlagHelpHumanReadable(flag, out);
}
} }
// -------------------------------------------------------------------- // --------------------------------------------------------------------
......
...@@ -61,6 +61,9 @@ ABSL_FLAG( ...@@ -61,6 +61,9 @@ ABSL_FLAG(
"Even more long long long long long long long long long long long long " "Even more long long long long long long long long long long long long "
"help message."); "help message.");
ABSL_RETIRED_FLAG(int64_t, usage_reporting_test_flag_07, 1,
"usage_reporting_test_flag_07 help message");
namespace { namespace {
namespace flags = absl::flags_internal; namespace flags = absl::flags_internal;
......
...@@ -138,28 +138,21 @@ uint128::uint128(float v) : uint128(MakeUint128FromFloat(v)) {} ...@@ -138,28 +138,21 @@ uint128::uint128(float v) : uint128(MakeUint128FromFloat(v)) {}
uint128::uint128(double v) : uint128(MakeUint128FromFloat(v)) {} uint128::uint128(double v) : uint128(MakeUint128FromFloat(v)) {}
uint128::uint128(long double v) : uint128(MakeUint128FromFloat(v)) {} uint128::uint128(long double v) : uint128(MakeUint128FromFloat(v)) {}
#if !defined(ABSL_HAVE_INTRINSIC_INT128)
uint128 operator/(uint128 lhs, uint128 rhs) { uint128 operator/(uint128 lhs, uint128 rhs) {
#if defined(ABSL_HAVE_INTRINSIC_INT128)
return static_cast<unsigned __int128>(lhs) /
static_cast<unsigned __int128>(rhs);
#else // ABSL_HAVE_INTRINSIC_INT128
uint128 quotient = 0; uint128 quotient = 0;
uint128 remainder = 0; uint128 remainder = 0;
DivModImpl(lhs, rhs, &quotient, &remainder); DivModImpl(lhs, rhs, &quotient, &remainder);
return quotient; return quotient;
#endif // ABSL_HAVE_INTRINSIC_INT128
} }
uint128 operator%(uint128 lhs, uint128 rhs) { uint128 operator%(uint128 lhs, uint128 rhs) {
#if defined(ABSL_HAVE_INTRINSIC_INT128)
return static_cast<unsigned __int128>(lhs) %
static_cast<unsigned __int128>(rhs);
#else // ABSL_HAVE_INTRINSIC_INT128
uint128 quotient = 0; uint128 quotient = 0;
uint128 remainder = 0; uint128 remainder = 0;
DivModImpl(lhs, rhs, &quotient, &remainder); DivModImpl(lhs, rhs, &quotient, &remainder);
return remainder; return remainder;
#endif // ABSL_HAVE_INTRINSIC_INT128
} }
#endif // !defined(ABSL_HAVE_INTRINSIC_INT128)
namespace { namespace {
......
...@@ -18,6 +18,10 @@ ...@@ -18,6 +18,10 @@
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// //
// This header file defines 128-bit integer types, `uint128` and `int128`. // This header file defines 128-bit integer types, `uint128` and `int128`.
//
// TODO(absl-team): This module is inconsistent as many inline `uint128` methods
// are defined in this file, while many inline `int128` methods are defined in
// the `int128_*_intrinsic.inc` files.
#ifndef ABSL_NUMERIC_INT128_H_ #ifndef ABSL_NUMERIC_INT128_H_
#define ABSL_NUMERIC_INT128_H_ #define ABSL_NUMERIC_INT128_H_
...@@ -783,8 +787,13 @@ inline uint128::operator long double() const { ...@@ -783,8 +787,13 @@ inline uint128::operator long double() const {
// Comparison operators. // Comparison operators.
inline bool operator==(uint128 lhs, uint128 rhs) { inline bool operator==(uint128 lhs, uint128 rhs) {
#if defined(ABSL_HAVE_INTRINSIC_INT128)
return static_cast<unsigned __int128>(lhs) ==
static_cast<unsigned __int128>(rhs);
#else
return (Uint128Low64(lhs) == Uint128Low64(rhs) && return (Uint128Low64(lhs) == Uint128Low64(rhs) &&
Uint128High64(lhs) == Uint128High64(rhs)); Uint128High64(lhs) == Uint128High64(rhs));
#endif
} }
inline bool operator!=(uint128 lhs, uint128 rhs) { inline bool operator!=(uint128 lhs, uint128 rhs) {
...@@ -819,52 +828,76 @@ constexpr inline int128 operator+(int128 val) { ...@@ -819,52 +828,76 @@ constexpr inline int128 operator+(int128 val) {
} }
inline uint128 operator-(uint128 val) { inline uint128 operator-(uint128 val) {
#if defined(ABSL_HAVE_INTRINSIC_INT128)
return -static_cast<unsigned __int128>(val);
#else
uint64_t hi = ~Uint128High64(val); uint64_t hi = ~Uint128High64(val);
uint64_t lo = ~Uint128Low64(val) + 1; uint64_t lo = ~Uint128Low64(val) + 1;
if (lo == 0) ++hi; // carry if (lo == 0) ++hi; // carry
return MakeUint128(hi, lo); return MakeUint128(hi, lo);
#endif
} }
constexpr inline bool operator!(uint128 val) { constexpr inline bool operator!(uint128 val) {
#if defined(ABSL_HAVE_INTRINSIC_INT128)
return !static_cast<unsigned __int128>(val);
#else
return !Uint128High64(val) && !Uint128Low64(val); return !Uint128High64(val) && !Uint128Low64(val);
#endif
} }
// Logical operators. // Logical operators.
constexpr inline uint128 operator~(uint128 val) { constexpr inline uint128 operator~(uint128 val) {
#if defined(ABSL_HAVE_INTRINSIC_INT128)
return ~static_cast<unsigned __int128>(val);
#else
return MakeUint128(~Uint128High64(val), ~Uint128Low64(val)); return MakeUint128(~Uint128High64(val), ~Uint128Low64(val));
#endif
} }
constexpr inline uint128 operator|(uint128 lhs, uint128 rhs) { constexpr inline uint128 operator|(uint128 lhs, uint128 rhs) {
#if defined(ABSL_HAVE_INTRINSIC_INT128)
return static_cast<unsigned __int128>(lhs) |
static_cast<unsigned __int128>(rhs);
#else
return MakeUint128(Uint128High64(lhs) | Uint128High64(rhs), return MakeUint128(Uint128High64(lhs) | Uint128High64(rhs),
Uint128Low64(lhs) | Uint128Low64(rhs)); Uint128Low64(lhs) | Uint128Low64(rhs));
#endif
} }
constexpr inline uint128 operator&(uint128 lhs, uint128 rhs) { constexpr inline uint128 operator&(uint128 lhs, uint128 rhs) {
#if defined(ABSL_HAVE_INTRINSIC_INT128)
return static_cast<unsigned __int128>(lhs) &
static_cast<unsigned __int128>(rhs);
#else
return MakeUint128(Uint128High64(lhs) & Uint128High64(rhs), return MakeUint128(Uint128High64(lhs) & Uint128High64(rhs),
Uint128Low64(lhs) & Uint128Low64(rhs)); Uint128Low64(lhs) & Uint128Low64(rhs));
#endif
} }
constexpr inline uint128 operator^(uint128 lhs, uint128 rhs) { constexpr inline uint128 operator^(uint128 lhs, uint128 rhs) {
#if defined(ABSL_HAVE_INTRINSIC_INT128)
return static_cast<unsigned __int128>(lhs) ^
static_cast<unsigned __int128>(rhs);
#else
return MakeUint128(Uint128High64(lhs) ^ Uint128High64(rhs), return MakeUint128(Uint128High64(lhs) ^ Uint128High64(rhs),
Uint128Low64(lhs) ^ Uint128Low64(rhs)); Uint128Low64(lhs) ^ Uint128Low64(rhs));
#endif
} }
inline uint128& uint128::operator|=(uint128 other) { inline uint128& uint128::operator|=(uint128 other) {
hi_ |= other.hi_; *this = *this | other;
lo_ |= other.lo_;
return *this; return *this;
} }
inline uint128& uint128::operator&=(uint128 other) { inline uint128& uint128::operator&=(uint128 other) {
hi_ &= other.hi_; *this = *this & other;
lo_ &= other.lo_;
return *this; return *this;
} }
inline uint128& uint128::operator^=(uint128 other) { inline uint128& uint128::operator^=(uint128 other) {
hi_ ^= other.hi_; *this = *this ^ other;
lo_ ^= other.lo_;
return *this; return *this;
} }
...@@ -907,21 +940,31 @@ inline uint128 operator>>(uint128 lhs, int amount) { ...@@ -907,21 +940,31 @@ inline uint128 operator>>(uint128 lhs, int amount) {
} }
inline uint128 operator+(uint128 lhs, uint128 rhs) { inline uint128 operator+(uint128 lhs, uint128 rhs) {
#if defined(ABSL_HAVE_INTRINSIC_INT128)
return static_cast<unsigned __int128>(lhs) +
static_cast<unsigned __int128>(rhs);
#else
uint128 result = MakeUint128(Uint128High64(lhs) + Uint128High64(rhs), uint128 result = MakeUint128(Uint128High64(lhs) + Uint128High64(rhs),
Uint128Low64(lhs) + Uint128Low64(rhs)); Uint128Low64(lhs) + Uint128Low64(rhs));
if (Uint128Low64(result) < Uint128Low64(lhs)) { // check for carry if (Uint128Low64(result) < Uint128Low64(lhs)) { // check for carry
return MakeUint128(Uint128High64(result) + 1, Uint128Low64(result)); return MakeUint128(Uint128High64(result) + 1, Uint128Low64(result));
} }
return result; return result;
#endif
} }
inline uint128 operator-(uint128 lhs, uint128 rhs) { inline uint128 operator-(uint128 lhs, uint128 rhs) {
#if defined(ABSL_HAVE_INTRINSIC_INT128)
return static_cast<unsigned __int128>(lhs) -
static_cast<unsigned __int128>(rhs);
#else
uint128 result = MakeUint128(Uint128High64(lhs) - Uint128High64(rhs), uint128 result = MakeUint128(Uint128High64(lhs) - Uint128High64(rhs),
Uint128Low64(lhs) - Uint128Low64(rhs)); Uint128Low64(lhs) - Uint128Low64(rhs));
if (Uint128Low64(lhs) < Uint128Low64(rhs)) { // check for carry if (Uint128Low64(lhs) < Uint128Low64(rhs)) { // check for carry
return MakeUint128(Uint128High64(result) - 1, Uint128Low64(result)); return MakeUint128(Uint128High64(result) - 1, Uint128Low64(result));
} }
return result; return result;
#endif
} }
inline uint128 operator*(uint128 lhs, uint128 rhs) { inline uint128 operator*(uint128 lhs, uint128 rhs) {
...@@ -951,6 +994,18 @@ inline uint128 operator*(uint128 lhs, uint128 rhs) { ...@@ -951,6 +994,18 @@ inline uint128 operator*(uint128 lhs, uint128 rhs) {
#endif // ABSL_HAVE_INTRINSIC128 #endif // ABSL_HAVE_INTRINSIC128
} }
#if defined(ABSL_HAVE_INTRINSIC_INT128)
inline uint128 operator/(uint128 lhs, uint128 rhs) {
return static_cast<unsigned __int128>(lhs) /
static_cast<unsigned __int128>(rhs);
}
inline uint128 operator%(uint128 lhs, uint128 rhs) {
return static_cast<unsigned __int128>(lhs) %
static_cast<unsigned __int128>(rhs);
}
#endif
// Increment/decrement operators. // Increment/decrement operators.
inline uint128 uint128::operator++(int) { inline uint128 uint128::operator++(int) {
......
...@@ -216,6 +216,14 @@ struct CordRep { ...@@ -216,6 +216,14 @@ struct CordRep {
// padding space from the base class (clang and gcc do, MSVC does not, etc) // padding space from the base class (clang and gcc do, MSVC does not, etc)
uint8_t storage[3]; uint8_t storage[3];
// Returns true if this instance's tag matches the requested type.
constexpr bool IsRing() const { return tag == RING; }
constexpr bool IsConcat() const { return tag == CONCAT; }
constexpr bool IsSubstring() const { return tag == SUBSTRING; }
constexpr bool IsExternal() const { return tag == EXTERNAL; }
constexpr bool IsFlat() const { return tag >= FLAT; }
constexpr bool IsBtree() const { return tag == BTREE; }
inline CordRepRing* ring(); inline CordRepRing* ring();
inline const CordRepRing* ring() const; inline const CordRepRing* ring() const;
inline CordRepConcat* concat(); inline CordRepConcat* concat();
...@@ -226,7 +234,6 @@ struct CordRep { ...@@ -226,7 +234,6 @@ struct CordRep {
inline const CordRepExternal* external() const; inline const CordRepExternal* external() const;
inline CordRepFlat* flat(); inline CordRepFlat* flat();
inline const CordRepFlat* flat() const; inline const CordRepFlat* flat() const;
inline CordRepBtree* btree(); inline CordRepBtree* btree();
inline const CordRepBtree* btree() const; inline const CordRepBtree* btree() const;
...@@ -277,7 +284,7 @@ struct CordRepExternal : public CordRep { ...@@ -277,7 +284,7 @@ struct CordRepExternal : public CordRep {
ExternalReleaserInvoker releaser_invoker; ExternalReleaserInvoker releaser_invoker;
// Deletes (releases) the external rep. // Deletes (releases) the external rep.
// Requires rep != nullptr and rep->tag == EXTERNAL // Requires rep != nullptr and rep->IsExternal()
static void Delete(CordRep* rep); static void Delete(CordRep* rep);
}; };
...@@ -320,7 +327,7 @@ struct CordRepExternalImpl ...@@ -320,7 +327,7 @@ struct CordRepExternalImpl
}; };
inline void CordRepExternal::Delete(CordRep* rep) { inline void CordRepExternal::Delete(CordRep* rep) {
assert(rep != nullptr && rep->tag == EXTERNAL); assert(rep != nullptr && rep->IsExternal());
auto* rep_external = static_cast<CordRepExternal*>(rep); auto* rep_external = static_cast<CordRepExternal*>(rep);
assert(rep_external->releaser_invoker != nullptr); assert(rep_external->releaser_invoker != nullptr);
rep_external->releaser_invoker(rep_external); rep_external->releaser_invoker(rep_external);
...@@ -531,32 +538,32 @@ class InlineData { ...@@ -531,32 +538,32 @@ class InlineData {
static_assert(sizeof(InlineData) == kMaxInline + 1, ""); static_assert(sizeof(InlineData) == kMaxInline + 1, "");
inline CordRepConcat* CordRep::concat() { inline CordRepConcat* CordRep::concat() {
assert(tag == CONCAT); assert(IsConcat());
return static_cast<CordRepConcat*>(this); return static_cast<CordRepConcat*>(this);
} }
inline const CordRepConcat* CordRep::concat() const { inline const CordRepConcat* CordRep::concat() const {
assert(tag == CONCAT); assert(IsConcat());
return static_cast<const CordRepConcat*>(this); return static_cast<const CordRepConcat*>(this);
} }
inline CordRepSubstring* CordRep::substring() { inline CordRepSubstring* CordRep::substring() {
assert(tag == SUBSTRING); assert(IsSubstring());
return static_cast<CordRepSubstring*>(this); return static_cast<CordRepSubstring*>(this);
} }
inline const CordRepSubstring* CordRep::substring() const { inline const CordRepSubstring* CordRep::substring() const {
assert(tag == SUBSTRING); assert(IsSubstring());
return static_cast<const CordRepSubstring*>(this); return static_cast<const CordRepSubstring*>(this);
} }
inline CordRepExternal* CordRep::external() { inline CordRepExternal* CordRep::external() {
assert(tag == EXTERNAL); assert(IsExternal());
return static_cast<CordRepExternal*>(this); return static_cast<CordRepExternal*>(this);
} }
inline const CordRepExternal* CordRep::external() const { inline const CordRepExternal* CordRep::external() const {
assert(tag == EXTERNAL); assert(IsExternal());
return static_cast<const CordRepExternal*>(this); return static_cast<const CordRepExternal*>(this);
} }
......
...@@ -79,7 +79,7 @@ void DumpAll(const CordRep* rep, bool include_contents, std::ostream& stream, ...@@ -79,7 +79,7 @@ void DumpAll(const CordRep* rep, bool include_contents, std::ostream& stream,
// indented by two spaces per recursive depth. // indented by two spaces per recursive depth.
stream << std::string(depth * 2, ' ') << sharing << " (" << sptr << ") "; stream << std::string(depth * 2, ' ') << sharing << " (" << sptr << ") ";
if (rep->tag == BTREE) { if (rep->IsBtree()) {
const CordRepBtree* node = rep->btree(); const CordRepBtree* node = rep->btree();
std::string label = std::string label =
node->height() ? absl::StrCat("Node(", node->height(), ")") : "Leaf"; node->height() ? absl::StrCat("Node(", node->height(), ")") : "Leaf";
...@@ -378,7 +378,7 @@ bool CordRepBtree::IsValid(const CordRepBtree* tree, bool shallow) { ...@@ -378,7 +378,7 @@ bool CordRepBtree::IsValid(const CordRepBtree* tree, bool shallow) {
} }
NODE_CHECK_VALID(tree != nullptr); NODE_CHECK_VALID(tree != nullptr);
NODE_CHECK_EQ(tree->tag, BTREE); NODE_CHECK_VALID(tree->IsBtree());
NODE_CHECK_VALID(tree->height() <= kMaxHeight); NODE_CHECK_VALID(tree->height() <= kMaxHeight);
NODE_CHECK_VALID(tree->begin() < tree->capacity()); NODE_CHECK_VALID(tree->begin() < tree->capacity());
NODE_CHECK_VALID(tree->end() <= tree->capacity()); NODE_CHECK_VALID(tree->end() <= tree->capacity());
...@@ -387,7 +387,7 @@ bool CordRepBtree::IsValid(const CordRepBtree* tree, bool shallow) { ...@@ -387,7 +387,7 @@ bool CordRepBtree::IsValid(const CordRepBtree* tree, bool shallow) {
for (CordRep* edge : tree->Edges()) { for (CordRep* edge : tree->Edges()) {
NODE_CHECK_VALID(edge != nullptr); NODE_CHECK_VALID(edge != nullptr);
if (tree->height() > 0) { if (tree->height() > 0) {
NODE_CHECK_VALID(edge->tag == BTREE); NODE_CHECK_VALID(edge->IsBtree());
NODE_CHECK_VALID(edge->btree()->height() == tree->height() - 1); NODE_CHECK_VALID(edge->btree()->height() == tree->height() - 1);
} else { } else {
NODE_CHECK_VALID(IsDataEdge(edge)); NODE_CHECK_VALID(IsDataEdge(edge));
...@@ -889,7 +889,7 @@ Span<char> CordRepBtree::GetAppendBufferSlow(size_t size) { ...@@ -889,7 +889,7 @@ Span<char> CordRepBtree::GetAppendBufferSlow(size_t size) {
} }
CordRepBtree* CordRepBtree::CreateSlow(CordRep* rep) { CordRepBtree* CordRepBtree::CreateSlow(CordRep* rep) {
if (rep->tag == BTREE) return rep->btree(); if (rep->IsBtree()) return rep->btree();
CordRepBtree* node = nullptr; CordRepBtree* node = nullptr;
auto consume = [&node](CordRep* r, size_t offset, size_t length) { auto consume = [&node](CordRep* r, size_t offset, size_t length) {
...@@ -905,7 +905,7 @@ CordRepBtree* CordRepBtree::CreateSlow(CordRep* rep) { ...@@ -905,7 +905,7 @@ CordRepBtree* CordRepBtree::CreateSlow(CordRep* rep) {
} }
CordRepBtree* CordRepBtree::AppendSlow(CordRepBtree* tree, CordRep* rep) { CordRepBtree* CordRepBtree::AppendSlow(CordRepBtree* tree, CordRep* rep) {
if (ABSL_PREDICT_TRUE(rep->tag == BTREE)) { if (ABSL_PREDICT_TRUE(rep->IsBtree())) {
return MergeTrees(tree, rep->btree()); return MergeTrees(tree, rep->btree());
} }
auto consume = [&tree](CordRep* r, size_t offset, size_t length) { auto consume = [&tree](CordRep* r, size_t offset, size_t length) {
...@@ -917,7 +917,7 @@ CordRepBtree* CordRepBtree::AppendSlow(CordRepBtree* tree, CordRep* rep) { ...@@ -917,7 +917,7 @@ CordRepBtree* CordRepBtree::AppendSlow(CordRepBtree* tree, CordRep* rep) {
} }
CordRepBtree* CordRepBtree::PrependSlow(CordRepBtree* tree, CordRep* rep) { CordRepBtree* CordRepBtree::PrependSlow(CordRepBtree* tree, CordRep* rep) {
if (ABSL_PREDICT_TRUE(rep->tag == BTREE)) { if (ABSL_PREDICT_TRUE(rep->IsBtree())) {
return MergeTrees(rep->btree(), tree); return MergeTrees(rep->btree(), tree);
} }
auto consume = [&tree](CordRep* r, size_t offset, size_t length) { auto consume = [&tree](CordRep* r, size_t offset, size_t length) {
......
...@@ -153,7 +153,7 @@ class CordRepBtree : public CordRep { ...@@ -153,7 +153,7 @@ class CordRepBtree : public CordRep {
}; };
// Creates a btree from the given input. Adopts a ref of `rep`. // Creates a btree from the given input. Adopts a ref of `rep`.
// If the input `rep` is itself a btree, i.e., `tag == BTREE`, then this // If the input `rep` is itself a btree, i.e., `IsBtree()`, then this
// function immediately returns `rep->btree()`. If the input is a valid data // function immediately returns `rep->btree()`. If the input is a valid data
// edge (see IsDataEdge()), then a new leaf node is returned containing `rep` // edge (see IsDataEdge()), then a new leaf node is returned containing `rep`
// as the sole data edge. Else, the input is assumed to be a (legacy) concat // as the sole data edge. Else, the input is assumed to be a (legacy) concat
...@@ -514,12 +514,12 @@ class CordRepBtree : public CordRep { ...@@ -514,12 +514,12 @@ class CordRepBtree : public CordRep {
}; };
inline CordRepBtree* CordRep::btree() { inline CordRepBtree* CordRep::btree() {
assert(tag == BTREE); assert(IsBtree());
return static_cast<CordRepBtree*>(this); return static_cast<CordRepBtree*>(this);
} }
inline const CordRepBtree* CordRep::btree() const { inline const CordRepBtree* CordRep::btree() const {
assert(tag == BTREE); assert(IsBtree());
return static_cast<const CordRepBtree*>(this); return static_cast<const CordRepBtree*>(this);
} }
...@@ -589,7 +589,7 @@ inline CordRepBtree* CordRepBtree::New(int height) { ...@@ -589,7 +589,7 @@ inline CordRepBtree* CordRepBtree::New(int height) {
inline CordRepBtree* CordRepBtree::New(CordRep* rep) { inline CordRepBtree* CordRepBtree::New(CordRep* rep) {
CordRepBtree* tree = new CordRepBtree; CordRepBtree* tree = new CordRepBtree;
int height = rep->tag == BTREE ? rep->btree()->height() + 1 : 0; int height = rep->IsBtree() ? rep->btree()->height() + 1 : 0;
tree->length = rep->length; tree->length = rep->length;
tree->InitInstance(height, /*begin=*/0, /*end=*/1); tree->InitInstance(height, /*begin=*/0, /*end=*/1);
tree->edges_[0] = rep; tree->edges_[0] = rep;
......
...@@ -40,7 +40,7 @@ using index_type = CordRepRing::index_type; ...@@ -40,7 +40,7 @@ using index_type = CordRepRing::index_type;
enum class Direction { kForward, kReversed }; enum class Direction { kForward, kReversed };
inline bool IsFlatOrExternal(CordRep* rep) { inline bool IsFlatOrExternal(CordRep* rep) {
return rep->tag >= FLAT || rep->tag == EXTERNAL; return rep->IsFlat() || rep->IsExternal();
} }
// Verifies that n + extra <= kMaxCapacity: throws std::length_error otherwise. // Verifies that n + extra <= kMaxCapacity: throws std::length_error otherwise.
...@@ -229,7 +229,7 @@ void CordRepRing::SetCapacityForTesting(size_t capacity) { ...@@ -229,7 +229,7 @@ void CordRepRing::SetCapacityForTesting(size_t capacity) {
} }
void CordRepRing::Delete(CordRepRing* rep) { void CordRepRing::Delete(CordRepRing* rep) {
assert(rep != nullptr && rep->tag == RING); assert(rep != nullptr && rep->IsRing());
#if defined(__cpp_sized_deallocation) #if defined(__cpp_sized_deallocation)
size_t size = AllocSize(rep->capacity_); size_t size = AllocSize(rep->capacity_);
rep->~CordRepRing(); rep->~CordRepRing();
...@@ -360,7 +360,7 @@ CordRepRing* CordRepRing::Create(CordRep* child, size_t extra) { ...@@ -360,7 +360,7 @@ CordRepRing* CordRepRing::Create(CordRep* child, size_t extra) {
if (IsFlatOrExternal(child)) { if (IsFlatOrExternal(child)) {
return CreateFromLeaf(child, 0, length, extra); return CreateFromLeaf(child, 0, length, extra);
} }
if (child->tag == RING) { if (child->IsRing()) {
return Mutable(child->ring(), extra); return Mutable(child->ring(), extra);
} }
return CreateSlow(child, extra); return CreateSlow(child, extra);
...@@ -433,7 +433,7 @@ CordRepRing* CordRepRing::AddRing(CordRepRing* rep, CordRepRing* ring, ...@@ -433,7 +433,7 @@ CordRepRing* CordRepRing::AddRing(CordRepRing* rep, CordRepRing* ring,
CordRepRing* CordRepRing::AppendSlow(CordRepRing* rep, CordRep* child) { CordRepRing* CordRepRing::AppendSlow(CordRepRing* rep, CordRep* child) {
Consume(child, [&rep](CordRep* child_arg, size_t offset, size_t len) { Consume(child, [&rep](CordRep* child_arg, size_t offset, size_t len) {
if (child_arg->tag == RING) { if (child_arg->IsRing()) {
rep = AddRing<AddMode::kAppend>(rep, child_arg->ring(), offset, len); rep = AddRing<AddMode::kAppend>(rep, child_arg->ring(), offset, len);
} else { } else {
rep = AppendLeaf(rep, child_arg, offset, len); rep = AppendLeaf(rep, child_arg, offset, len);
...@@ -460,7 +460,7 @@ CordRepRing* CordRepRing::Append(CordRepRing* rep, CordRep* child) { ...@@ -460,7 +460,7 @@ CordRepRing* CordRepRing::Append(CordRepRing* rep, CordRep* child) {
if (IsFlatOrExternal(child)) { if (IsFlatOrExternal(child)) {
return AppendLeaf(rep, child, 0, length); return AppendLeaf(rep, child, 0, length);
} }
if (child->tag == RING) { if (child->IsRing()) {
return AddRing<AddMode::kAppend>(rep, child->ring(), 0, length); return AddRing<AddMode::kAppend>(rep, child->ring(), 0, length);
} }
return AppendSlow(rep, child); return AppendSlow(rep, child);
...@@ -496,7 +496,7 @@ CordRepRing* CordRepRing::Prepend(CordRepRing* rep, CordRep* child) { ...@@ -496,7 +496,7 @@ CordRepRing* CordRepRing::Prepend(CordRepRing* rep, CordRep* child) {
if (IsFlatOrExternal(child)) { if (IsFlatOrExternal(child)) {
return PrependLeaf(rep, child, 0, length); return PrependLeaf(rep, child, 0, length);
} }
if (child->tag == RING) { if (child->IsRing()) {
return AddRing<AddMode::kPrepend>(rep, child->ring(), 0, length); return AddRing<AddMode::kPrepend>(rep, child->ring(), 0, length);
} }
return PrependSlow(rep, child); return PrependSlow(rep, child);
......
...@@ -570,12 +570,12 @@ inline CordRepRing::Position CordRepRing::FindTail(index_type head, ...@@ -570,12 +570,12 @@ inline CordRepRing::Position CordRepRing::FindTail(index_type head,
// Now that CordRepRing is defined, we can define CordRep's helper casts: // Now that CordRepRing is defined, we can define CordRep's helper casts:
inline CordRepRing* CordRep::ring() { inline CordRepRing* CordRep::ring() {
assert(tag == RING); assert(IsRing());
return static_cast<CordRepRing*>(this); return static_cast<CordRepRing*>(this);
} }
inline const CordRepRing* CordRep::ring() const { inline const CordRepRing* CordRep::ring() const {
assert(tag == RING); assert(IsRing());
return static_cast<const CordRepRing*>(this); return static_cast<const CordRepRing*>(this);
} }
......
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