#pragma once #include "public.h" #include "intrusive_ptr.h" #include "range.h" #include "ref_counted.h" #include #include #include namespace NYT { //////////////////////////////////////////////////////////////////////////////// template class TCompactVector; //////////////////////////////////////////////////////////////////////////////// struct TSharedRangeHolderCloneOptions { bool KeepMemoryReferenceTracking = true; }; struct TSharedRangeHolder : public TRefCounted { //! Clones the holder possibly adjusting its flavor based on #options. /*! * The default implementation just returns this. */ virtual TSharedRangeHolderPtr Clone(const TSharedRangeHolderCloneOptions& options); //! Returns the (estimated) total number of bytes being held or |null| if unable to estimate. /*! * The returned value is static and never changes. * The default implementation returns |null|. */ virtual std::optional GetTotalByteSize() const; }; DEFINE_REFCOUNTED_TYPE(TSharedRangeHolder) //////////////////////////////////////////////////////////////////////////////// //! TRange with ownership semantics. template class TSharedRange : public TRange { public: //! Constructs a null TSharedRange. TSharedRange() { } //! Constructs an empty TSharedRange from a nullptr expression. TSharedRange(std::nullptr_t) : TRange(nullptr, 0UL) , Holder_(nullptr) { } //! Constructs a TSharedRange from TRange. TSharedRange(TRange range, TSharedRangeHolderPtr holder) : TRange(range) , Holder_(std::move(holder)) { } //! Constructs a TSharedRange from a pointer and length. TSharedRange(const T* data, size_t length, TSharedRangeHolderPtr holder) : TRange(data, length) , Holder_(std::move(holder)) { } //! Constructs a TSharedRange from a range. TSharedRange(const T* begin, const T* end, TSharedRangeHolderPtr holder) : TRange(begin, end) , Holder_(std::move(holder)) { } //! Constructs a TSharedRange from a TCompactVector. template TSharedRange(const TCompactVector& elements, TSharedRangeHolderPtr holder) : TRange(elements) , Holder_(std::move(holder)) { } //! Constructs a TSharedRange from an std::vector. TSharedRange(const std::vector& elements, TSharedRangeHolderPtr holder) : TRange(elements) , Holder_(std::move(holder)) { } //! Constructs a TSharedRange from a C array. template TSharedRange(const T (& elements)[N], TSharedRangeHolderPtr holder) : TRange(elements) , Holder_(std::move(holder)) { } TSharedRange(const TSharedRange& other) = default; TSharedRange(TSharedRange&& other) noexcept : TSharedRange() { other.Swap(*this); } TSharedRange& operator=(TSharedRange other) noexcept { other.Swap(*this); return *this; } void Swap(TSharedRange& other) noexcept { DoSwap(TRange::Data_, other.Data_); DoSwap(TRange::Length_, other.Length_); Holder_.Swap(other.Holder_); } void Reset() { TRange::Data_ = nullptr; TRange::Length_ = 0; Holder_.Reset(); } TSharedRange Slice(size_t startOffset, size_t endOffset) const { YT_ASSERT(startOffset <= this->Size()); YT_ASSERT(endOffset >= startOffset && endOffset <= this->Size()); return TSharedRange(this->Begin() + startOffset, endOffset - startOffset, Holder_); } TSharedRange Slice(const T* begin, const T* end) const { YT_ASSERT(begin >= this->Begin()); YT_ASSERT(end <= this->End()); return TSharedRange(begin, end, Holder_); } const TSharedRangeHolderPtr& GetHolder() const { return Holder_; } TSharedRangeHolderPtr&& ReleaseHolder() { return std::move(Holder_); } protected: TSharedRangeHolderPtr Holder_; }; //////////////////////////////////////////////////////////////////////////////// //! Constructs a combined holder from a vector of typed holders. TSharedRangeHolderPtr MakeCompositeSharedRangeHolder(std::vector holders); //! Constructs a combined holder instance by taking ownership of a given list of holders. template TSharedRangeHolderPtr MakeSharedRangeHolder(THolders&&... holders) { struct THolder : public TSharedRangeHolder { std::tuple::type...> Holders; }; auto holder = New(); holder->Holders = std::tuple(std::forward(holders)...); return holder; } template TSharedRange DoMakeSharedRange(TContainer&& elements, THolders&&... holders) { struct THolder : public TSharedRangeHolder { typename std::decay::type Elements; std::tuple::type...> Holders; }; auto holder = New(); holder->Holders = std::tuple(std::forward(holders)...); holder->Elements = std::forward(elements); auto range = MakeRange(holder->Elements); return TSharedRange(range, std::move(holder)); } //! Constructs a TSharedRange by taking ownership of an std::vector. template TSharedRange MakeSharedRange(std::vector&& elements, THolders&&... holders) { return DoMakeSharedRange(std::move(elements), std::forward(holders)...); } //! Constructs a TSharedRange by taking ownership of an TCompactVector. template TSharedRange MakeSharedRange(TCompactVector&& elements, THolders&&... holders) { return DoMakeSharedRange(std::move(elements), std::forward(holders)...); } //! Constructs a TSharedRange by copying an std::vector. template TSharedRange MakeSharedRange(const std::vector& elements, THolders&&... holders) { return DoMakeSharedRange(elements, std::forward(holders)...); } template TSharedRange MakeSharedRange(TRange range, THolders&&... holders) { return TSharedRange(range, MakeSharedRangeHolder(std::forward(holders)...)); } template TSharedRange MakeSharedRange(TRange range, TSharedRangeHolderPtr holder) { return TSharedRange(range, std::move(holder)); } template TSharedRange ReinterpretCastRange(const TSharedRange& range) { static_assert(sizeof(T) == sizeof(U), "T and U must have equal sizes."); return TSharedRange(reinterpret_cast(range.Begin()), range.Size(), range.GetHolder()); } //////////////////////////////////////////////////////////////////////////////// //! TMutableRange with ownership semantics. //! Use with caution :) template class TSharedMutableRange : public TMutableRange { public: //! Constructs a null TSharedMutableRange. TSharedMutableRange() { } //! Constructs a TSharedMutableRange from TMutableRange. TSharedMutableRange(TMutableRange range, TSharedRangeHolderPtr holder) : TMutableRange(range) , Holder_(std::move(holder)) { } //! Constructs a TSharedMutableRange from a pointer and length. TSharedMutableRange(T* data, size_t length, TSharedRangeHolderPtr holder) : TMutableRange(data, length) , Holder_(std::move(holder)) { } //! Constructs a TSharedMutableRange from a range. TSharedMutableRange(T* begin, T* end, TSharedRangeHolderPtr holder) : TMutableRange(begin, end) , Holder_(std::move(holder)) { } //! Constructs a TSharedMutableRange from a TCompactVector. template TSharedMutableRange(TCompactVector& elements, TSharedRangeHolderPtr holder) : TMutableRange(elements) , Holder_(std::move(holder)) { } //! Constructs a TSharedMutableRange from an std::vector. TSharedMutableRange(std::vector& elements, TSharedRangeHolderPtr holder) : TMutableRange(elements) , Holder_(std::move(holder)) { } //! Constructs a TSharedMutableRange from a C array. template TSharedMutableRange(T (& elements)[N], TSharedRangeHolderPtr holder) : TMutableRange(elements) , Holder_(std::move(holder)) { } TSharedMutableRange(const TSharedMutableRange& other) = default; TSharedMutableRange(TSharedMutableRange&& other) noexcept : TSharedMutableRange() { other.Swap(*this); } TSharedMutableRange& operator=(TSharedMutableRange other) noexcept { other.Swap(*this); return *this; } void Swap(TSharedMutableRange& other) noexcept { DoSwap(TRange::Data_, other.Data_); DoSwap(TRange::Length_, other.Length_); Holder_.Swap(other.Holder_); } void Reset() { TRange::Data_ = nullptr; TRange::Length_ = 0; Holder_.Reset(); } TSharedMutableRange Slice(size_t startOffset, size_t endOffset) const { YT_ASSERT(startOffset <= this->Size()); YT_ASSERT(endOffset >= startOffset && endOffset <= this->Size()); return TSharedMutableRange(this->Begin() + startOffset, endOffset - startOffset, Holder_); } TSharedMutableRange Slice(T* begin, T* end) const { YT_ASSERT(begin >= this->Begin()); YT_ASSERT(end <= this->End()); return TSharedMutableRange(begin, end, Holder_); } TSharedRangeHolderPtr GetHolder() const { return Holder_; } TSharedRangeHolderPtr&& ReleaseHolder() { return std::move(Holder_); } protected: TSharedRangeHolderPtr Holder_; }; template TSharedMutableRange DoMakeSharedMutableRange(TContainer&& elements, THolders&&... holders) { struct THolder : public TSharedRangeHolder { typename std::decay::type Elements; std::tuple::type...> Holders; }; auto holder = New(); holder->Holders = std::tuple(std::forward(holders)...); holder->Elements = std::forward(elements); auto range = TMutableRange(holder->Elements); return TSharedMutableRange(range, holder); } //! Constructs a TSharedMutableRange by taking ownership of an std::vector. template TSharedMutableRange MakeSharedMutableRange(std::vector&& elements, THolders&&... holders) { return DoMakeSharedMutableRange(std::move(elements), std::forward(holders)...); } //////////////////////////////////////////////////////////////////////////////// } // namespace NYT