diff options
author | Alexander Fokin <apfokin@gmail.com> | 2022-02-10 16:45:38 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:38 +0300 |
commit | 863a59a65247c24db7cb06789bc5cf79d04da32f (patch) | |
tree | 139dc000c8cd4a40f5659e421b7c75135d080307 /util | |
parent | f64e95a9eb9ab03240599eb9581c5a9102426a96 (diff) | |
download | ydb-863a59a65247c24db7cb06789bc5cf79d04da32f.tar.gz |
Restoring authorship annotation for Alexander Fokin <apfokin@gmail.com>. Commit 1 of 2.
Diffstat (limited to 'util')
159 files changed, 4061 insertions, 4061 deletions
diff --git a/util/charset/wide.cpp b/util/charset/wide.cpp index a287438ddd..83390b7836 100644 --- a/util/charset/wide.cpp +++ b/util/charset/wide.cpp @@ -186,7 +186,7 @@ static bool ModifySequence(const TCharType*& p, const TCharType* const pe, TChar } template <class TStringType> -static void DetachAndFixPointers(TStringType& text, typename TStringType::value_type*& p, const typename TStringType::value_type*& pe) { +static void DetachAndFixPointers(TStringType& text, typename TStringType::value_type*& p, const typename TStringType::value_type*& pe) { const auto pos = p - text.data(); const auto count = pe - p; p = text.Detach() + pos; @@ -203,7 +203,7 @@ static bool ModifyStringSymbolwise(TStringType& text, size_t pos, size_t count, // TUtf16String is refcounted and it's `data` method return pointer to the constant memory. // To simplify the code we do a `const_cast`, though first write to the memory will be done only // after we call `Detach()` and get pointer to a writable piece of memory. - auto* p = const_cast<typename TStringType::value_type*>(text.data() + pos); + auto* p = const_cast<typename TStringType::value_type*>(text.data() + pos); const auto* pe = text.data() + pos + count; if (ModifySequence<true>(p, pe, f)) { diff --git a/util/datetime/base_ut.cpp b/util/datetime/base_ut.cpp index afc3f802eb..4c7b44db25 100644 --- a/util/datetime/base_ut.cpp +++ b/util/datetime/base_ut.cpp @@ -4,7 +4,7 @@ #include <util/generic/utility.h> #include <util/generic/ylimits.h> -#include <util/generic/ymath.h> +#include <util/generic/ymath.h> #include <util/string/cast.h> #include <util/stream/output.h> #include <util/system/compat.h> diff --git a/util/datetime/cputimer.cpp b/util/datetime/cputimer.cpp index 516d372c37..74b0060d21 100644 --- a/util/datetime/cputimer.cpp +++ b/util/datetime/cputimer.cpp @@ -18,7 +18,7 @@ TTimer::TTimer(const TStringBuf message) { static const int SMALL_DURATION_CHAR_LENGTH = 9; // strlen("0.123456s") - Message_.Reserve(message.length() + SMALL_DURATION_CHAR_LENGTH + 1); // +"\n" + Message_.Reserve(message.length() + SMALL_DURATION_CHAR_LENGTH + 1); // +"\n" Message_ << message; // Do not measure the allocations above. Start_ = TInstant::Now(); diff --git a/util/draft/ip.h b/util/draft/ip.h index eb947cd2cd..5a4ff7f46b 100644 --- a/util/draft/ip.h +++ b/util/draft/ip.h @@ -46,8 +46,8 @@ struct THash<TIp6> { static inline TIp6 Ip6FromIp4(TIp4 addr) { TIp6 res; memset(res.Data, 0, sizeof(res.Data)); - res.Data[10] = '\xFF'; - res.Data[11] = '\xFF'; + res.Data[10] = '\xFF'; + res.Data[11] = '\xFF'; memcpy(res.Data + 12, &addr, 4); return res; } diff --git a/util/folder/dirent_win.c b/util/folder/dirent_win.c index 7e6db74ce5..5bc2634dd6 100644 --- a/util/folder/dirent_win.c +++ b/util/folder/dirent_win.c @@ -12,10 +12,10 @@ static void SetErrno() { _dosmaperr(GetLastError()); } #else -void __cdecl __acrt_errno_map_os_error(unsigned long const oserrno); - -static void SetErrno() { - __acrt_errno_map_os_error(GetLastError()); +void __cdecl __acrt_errno_map_os_error(unsigned long const oserrno); + +static void SetErrno() { + __acrt_errno_map_os_error(GetLastError()); } #endif diff --git a/util/folder/dirut.cpp b/util/folder/dirut.cpp index ffc9b09f96..4c77f35aa9 100644 --- a/util/folder/dirut.cpp +++ b/util/folder/dirut.cpp @@ -381,7 +381,7 @@ void RemoveDirWithContents(TString dirName) { TDirIterator dir(dirName, TDirIterator::TOptions(FTS_NOSTAT)); - for (auto it = dir.begin(); it != dir.end(); ++it) { + for (auto it = dir.begin(); it != dir.end(); ++it) { switch (it->fts_info) { case FTS_F: case FTS_DEFAULT: diff --git a/util/folder/filelist.cpp b/util/folder/filelist.cpp index b21fcdbf20..79ba2f9b9c 100644 --- a/util/folder/filelist.cpp +++ b/util/folder/filelist.cpp @@ -19,7 +19,7 @@ void TFileEntitiesList::Fill(const TString& dirname, TStringBuf prefix, TStringB --dirNameLength; } - for (auto file = dir.begin(); file != dir.end(); ++file) { + for (auto file = dir.begin(); file != dir.end(); ++file) { if (file->fts_pathlen == file->fts_namelen || file->fts_pathlen <= dirNameLength) { continue; } diff --git a/util/folder/filelist.h b/util/folder/filelist.h index 3f615fa4c2..1c200eb4e1 100644 --- a/util/folder/filelist.h +++ b/util/folder/filelist.h @@ -3,21 +3,21 @@ #include <util/generic/buffer.h> #include <util/generic/string.h> #include <util/generic/strbuf.h> -#include <util/generic/flags.h> +#include <util/generic/flags.h> class TFileEntitiesList { public: - enum EMaskFlag { + enum EMaskFlag { EM_FILES = 1, EM_DIRS = 2, EM_SLINKS = 4, - - EM_FILES_DIRS = EM_FILES | EM_DIRS, - EM_FILES_SLINKS = EM_FILES | EM_SLINKS, - EM_DIRS_SLINKS = EM_DIRS | EM_SLINKS, - EM_FILES_DIRS_SLINKS = EM_FILES | EM_DIRS | EM_SLINKS + + EM_FILES_DIRS = EM_FILES | EM_DIRS, + EM_FILES_SLINKS = EM_FILES | EM_SLINKS, + EM_DIRS_SLINKS = EM_DIRS | EM_SLINKS, + EM_FILES_DIRS_SLINKS = EM_FILES | EM_DIRS | EM_SLINKS }; - Y_DECLARE_FLAGS(EMask, EMaskFlag) + Y_DECLARE_FLAGS(EMask, EMaskFlag) TFileEntitiesList(EMask mask) : Mask(mask) @@ -62,8 +62,8 @@ protected: EMask Mask; }; -Y_DECLARE_OPERATORS_FOR_FLAGS(TFileEntitiesList::EMask) - +Y_DECLARE_OPERATORS_FOR_FLAGS(TFileEntitiesList::EMask) + class TFileList: public TFileEntitiesList { public: TFileList() diff --git a/util/folder/iterator.h b/util/folder/iterator.h index 69e025b9c4..f6d953f9c4 100644 --- a/util/folder/iterator.h +++ b/util/folder/iterator.h @@ -9,7 +9,7 @@ /// Note this magic API traverses directory hierarchy -class TDirIterator: public TInputRangeAdaptor<TDirIterator> { +class TDirIterator: public TInputRangeAdaptor<TDirIterator> { struct TFtsDestroy { static inline void Destroy(FTS* f) noexcept { yfts_close(f); diff --git a/util/folder/iterator_ut.cpp b/util/folder/iterator_ut.cpp index 936becd139..acbcb6a336 100644 --- a/util/folder/iterator_ut.cpp +++ b/util/folder/iterator_ut.cpp @@ -93,7 +93,7 @@ private: inline void TestLocal() { TString dirname("." LOCSLASH_S); TDirIterator d(dirname, FTS_NOCHDIR); - for (auto it = d.begin(); it != d.end(); ++it) { + for (auto it = d.begin(); it != d.end(); ++it) { } } @@ -129,7 +129,7 @@ private: TDirIterator d(dir); - for (auto it = d.begin(); it != d.end(); ++it) { + for (auto it = d.begin(); it != d.end(); ++it) { UNIT_ASSERT(hier.Have(it->fts_path, it->fts_info != FTS_F)); } } @@ -197,7 +197,7 @@ private: { TDirIterator d(dir, TDirIterator::TOptions().SetSortByName()); - for (auto it = d.begin(); it != d.end(); ++it) { + for (auto it = d.begin(); it != d.end(); ++it) { if (it->fts_info == FTS_F) { fnames.push_back(it->fts_name); } diff --git a/util/generic/adaptor.h b/util/generic/adaptor.h index b88a65fc81..0c5aa16c87 100644 --- a/util/generic/adaptor.h +++ b/util/generic/adaptor.h @@ -5,136 +5,136 @@ namespace NPrivate { template <class Range> - class TReverseRangeStorage { - public: + class TReverseRangeStorage { + public: TReverseRangeStorage(Range&& range) : Base_(std::forward<Range>(range)) { } - decltype(auto) Base() const { - return *Base_.Ptr(); - } - - decltype(auto) Base() { - return *Base_.Ptr(); - } - - private: - TAutoEmbedOrPtrPolicy<Range> Base_; - }; - - template <class Range> - constexpr bool HasReverseIterators(i32, decltype(std::declval<Range>().rbegin())*) { - return true; - } - - template <class Range> - constexpr bool HasReverseIterators(char, std::nullptr_t*) { - return false; - } - - template <class Range, bool hasReverseIterators = HasReverseIterators<Range>((i32)0, nullptr)> - class TReverseRangeBase: public TReverseRangeStorage<Range> { - using TBase = TReverseRangeStorage<Range>; - - public: + decltype(auto) Base() const { + return *Base_.Ptr(); + } + + decltype(auto) Base() { + return *Base_.Ptr(); + } + + private: + TAutoEmbedOrPtrPolicy<Range> Base_; + }; + + template <class Range> + constexpr bool HasReverseIterators(i32, decltype(std::declval<Range>().rbegin())*) { + return true; + } + + template <class Range> + constexpr bool HasReverseIterators(char, std::nullptr_t*) { + return false; + } + + template <class Range, bool hasReverseIterators = HasReverseIterators<Range>((i32)0, nullptr)> + class TReverseRangeBase: public TReverseRangeStorage<Range> { + using TBase = TReverseRangeStorage<Range>; + + public: using TBase::Base; - using TBase::TBase; - + using TBase::TBase; + auto begin() const { - return Base().rbegin(); + return Base().rbegin(); } auto end() const { - return Base().rend(); + return Base().rend(); } auto begin() { - return Base().rbegin(); + return Base().rbegin(); } auto end() { - return Base().rend(); + return Base().rend(); } }; - template <class Range> - class TReverseRangeBase<Range, false>: public TReverseRangeStorage<Range> { - using TBase = TReverseRangeStorage<Range>; + template <class Range> + class TReverseRangeBase<Range, false>: public TReverseRangeStorage<Range> { + using TBase = TReverseRangeStorage<Range>; - public: + public: using TBase::Base; - using TBase::TBase; - - auto begin() const { + using TBase::TBase; + + auto begin() const { using std::end; - return std::make_reverse_iterator(end(Base())); + return std::make_reverse_iterator(end(Base())); } - auto end() const { + auto end() const { using std::begin; - return std::make_reverse_iterator(begin(Base())); + return std::make_reverse_iterator(begin(Base())); } - auto begin() { + auto begin() { using std::end; - return std::make_reverse_iterator(end(Base())); + return std::make_reverse_iterator(end(Base())); } - auto end() { + auto end() { using std::begin; - return std::make_reverse_iterator(begin(Base())); + return std::make_reverse_iterator(begin(Base())); } }; - template <class Range> + template <class Range> class TReverseRange: public TReverseRangeBase<Range> { - using TBase = TReverseRangeBase<Range>; + using TBase = TReverseRangeBase<Range>; - public: + public: using TBase::Base; - using TBase::TBase; + using TBase::TBase; - TReverseRange(TReverseRange&&) = default; - TReverseRange(const TReverseRange&) = default; + TReverseRange(TReverseRange&&) = default; + TReverseRange(const TReverseRange&) = default; auto rbegin() const { using std::begin; - return begin(Base()); + return begin(Base()); } auto rend() const { using std::end; - return end(Base()); + return end(Base()); } auto rbegin() { using std::begin; - return begin(Base()); + return begin(Base()); } auto rend() { using std::end; - return end(Base()); + return end(Base()); } }; } -/** - * Provides a reverse view into the provided container. - * - * Example usage: - * @code - * for(auto&& value: Reversed(container)) { - * // use value here. - * } - * @endcode - * - * @param cont Container to provide a view into. Must be an lvalue. - * @returns A reverse view into the provided container. - */ -template <class Range> -constexpr ::NPrivate::TReverseRange<Range> Reversed(Range&& range) { - return ::NPrivate::TReverseRange<Range>(std::forward<Range>(range)); +/** + * Provides a reverse view into the provided container. + * + * Example usage: + * @code + * for(auto&& value: Reversed(container)) { + * // use value here. + * } + * @endcode + * + * @param cont Container to provide a view into. Must be an lvalue. + * @returns A reverse view into the provided container. + */ +template <class Range> +constexpr ::NPrivate::TReverseRange<Range> Reversed(Range&& range) { + return ::NPrivate::TReverseRange<Range>(std::forward<Range>(range)); } diff --git a/util/generic/adaptor_ut.cpp b/util/generic/adaptor_ut.cpp index 721f849f93..adff58615b 100644 --- a/util/generic/adaptor_ut.cpp +++ b/util/generic/adaptor_ut.cpp @@ -65,8 +65,8 @@ Y_UNIT_TEST_SUITE(TReverseAdaptor) { const TStub cstub; using namespace NPrivate; - UNIT_ASSERT_TYPES_EQUAL(decltype(Reversed(stub)), TReverseRange<TStub&>); - UNIT_ASSERT_TYPES_EQUAL(decltype(Reversed(cstub)), TReverseRange<const TStub&>); + UNIT_ASSERT_TYPES_EQUAL(decltype(Reversed(stub)), TReverseRange<TStub&>); + UNIT_ASSERT_TYPES_EQUAL(decltype(Reversed(cstub)), TReverseRange<const TStub&>); } Y_UNIT_TEST(CopyMoveTest) { diff --git a/util/generic/algorithm.h b/util/generic/algorithm.h index badfb88993..028aa29218 100644 --- a/util/generic/algorithm.h +++ b/util/generic/algorithm.h @@ -55,7 +55,7 @@ static inline void Sort(TContainer& container, TCompare compare) { template <class TIterator, typename TGetKey> static inline void SortBy(TIterator begin, TIterator end, const TGetKey& getKey) { - Sort(begin, end, [&](auto&& left, auto&& right) { return getKey(left) < getKey(right); }); + Sort(begin, end, [&](auto&& left, auto&& right) { return getKey(left) < getKey(right); }); } template <class TContainer, typename TGetKey> @@ -64,12 +64,12 @@ static inline void SortBy(TContainer& container, const TGetKey& getKey) { } template <class T> -static inline void StableSort(T f, T l) { +static inline void StableSort(T f, T l) { std::stable_sort(f, l); } template <class T, class C> -static inline void StableSort(T f, T l, C c) { +static inline void StableSort(T f, T l, C c) { std::stable_sort(f, l, c); } @@ -85,7 +85,7 @@ static inline void StableSort(TContainer& container, TCompare compare) { template <class TIterator, typename TGetKey> static inline void StableSortBy(TIterator begin, TIterator end, const TGetKey& getKey) { - StableSort(begin, end, [&](auto&& left, auto&& right) { return getKey(left) < getKey(right); }); + StableSort(begin, end, [&](auto&& left, auto&& right) { return getKey(left) < getKey(right); }); } template <class TContainer, typename TGetKey> @@ -118,14 +118,14 @@ static inline I Find(I f, I l, const T& v) { return std::find(f, l, v); } -template <class C, class T> -static inline auto Find(C&& c, const T& v) { - using std::begin; - using std::end; - - return std::find(begin(c), end(c), v); -} - +template <class C, class T> +static inline auto Find(C&& c, const T& v) { + using std::begin; + using std::end; + + return std::find(begin(c), end(c), v); +} + // FindPtr - return NULL if not found. Works for arrays, containers, iterators template <class I, class T> static inline auto FindPtr(I f, I l, const T& v) -> decltype(&*f) { @@ -155,7 +155,7 @@ static inline auto FindIf(C&& c, P p) { template <class I, class P> static inline bool AllOf(I f, I l, P pred) { - return std::all_of(f, l, pred); + return std::all_of(f, l, pred); } template <class C, class P> @@ -167,7 +167,7 @@ static inline bool AllOf(const C& c, P pred) { template <class I, class P> static inline bool AnyOf(I f, I l, P pred) { - return std::any_of(f, l, pred); + return std::any_of(f, l, pred); } template <class C, class P> @@ -185,7 +185,7 @@ static inline auto FindIfPtr(I f, I l, P pred) -> decltype(&*f) { } template <class C, class P> -static inline auto FindIfPtr(C&& c, P pred) { +static inline auto FindIfPtr(C&& c, P pred) { using std::begin; using std::end; return FindIfPtr(begin(c), end(c), pred); @@ -529,7 +529,7 @@ auto MaxElementBy(C& c, F&& func) { } template <class C, class F> -auto MaxElementBy(const C& c, F&& func) { +auto MaxElementBy(const C& c, F&& func) { return MaxElementBy(std::begin(c), std::end(c), std::forward<F>(func)); } @@ -545,7 +545,7 @@ auto MinElementBy(C& c, F&& func) { } template <class C, class F> -auto MinElementBy(const C& c, F&& func) { +auto MinElementBy(const C& c, F&& func) { return MinElementBy(std::begin(c), std::end(c), std::forward<F>(func)); } @@ -644,12 +644,12 @@ static inline auto Count(const TContainer& container, const TValue& value) { } template <class It, class P> -static inline auto CountIf(It first, It last, P p) { +static inline auto CountIf(It first, It last, P p) { return std::count_if(first, last, p); } template <class C, class P> -static inline auto CountIf(const C& c, P pred) { +static inline auto CountIf(const C& c, P pred) { using std::begin; using std::end; return CountIf(begin(c), end(c), pred); @@ -713,16 +713,16 @@ template <class It, class Val, class Comp> static inline std::pair<It, It> EqualRange(It begin, It end, const Val& val, Comp comp) { return std::equal_range(begin, end, val, comp); } - -template <class ForwardIt> -bool IsSorted(ForwardIt begin, ForwardIt end) { + +template <class ForwardIt> +bool IsSorted(ForwardIt begin, ForwardIt end) { return std::is_sorted(begin, end); -} - -template <class ForwardIt, class Compare> -bool IsSorted(ForwardIt begin, ForwardIt end, Compare comp) { +} + +template <class ForwardIt, class Compare> +bool IsSorted(ForwardIt begin, ForwardIt end, Compare comp) { return std::is_sorted(begin, end, comp); -} +} template <class TIterator, typename TGetKey> bool IsSortedBy(TIterator begin, TIterator end, const TGetKey& getKey) { diff --git a/util/generic/algorithm_ut.cpp b/util/generic/algorithm_ut.cpp index 8d732fcc0c..b281753f1a 100644 --- a/util/generic/algorithm_ut.cpp +++ b/util/generic/algorithm_ut.cpp @@ -415,21 +415,21 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { UNIT_ASSERT_EQUAL(tmpR.second - tmpR.first, 2); } } - + Y_UNIT_TEST(IsSortedTest) { TVector<int> v0; - UNIT_ASSERT_VALUES_EQUAL(IsSorted(v0.begin(), v0.end()), true); - + UNIT_ASSERT_VALUES_EQUAL(IsSorted(v0.begin(), v0.end()), true); + TVector<int> v1 = {1, 2, 3, 4, 5, 5, 5, 6, 6, 7, 8}; - UNIT_ASSERT_VALUES_EQUAL(IsSorted(v1.begin(), v1.end()), true); - UNIT_ASSERT_VALUES_EQUAL(IsSorted(v1.begin(), v1.end(), TLess<int>()), true); - UNIT_ASSERT_VALUES_EQUAL(IsSorted(v1.begin(), v1.end(), TGreater<int>()), false); - + UNIT_ASSERT_VALUES_EQUAL(IsSorted(v1.begin(), v1.end()), true); + UNIT_ASSERT_VALUES_EQUAL(IsSorted(v1.begin(), v1.end(), TLess<int>()), true); + UNIT_ASSERT_VALUES_EQUAL(IsSorted(v1.begin(), v1.end(), TGreater<int>()), false); + TVector<int> v2 = {1, 2, 1}; - UNIT_ASSERT_VALUES_EQUAL(IsSorted(v2.begin(), v2.end()), false); - UNIT_ASSERT_VALUES_EQUAL(IsSorted(v2.begin(), v2.end(), TLess<int>()), false); - UNIT_ASSERT_VALUES_EQUAL(IsSorted(v2.begin(), v2.end(), TGreater<int>()), false); - } + UNIT_ASSERT_VALUES_EQUAL(IsSorted(v2.begin(), v2.end()), false); + UNIT_ASSERT_VALUES_EQUAL(IsSorted(v2.begin(), v2.end(), TLess<int>()), false); + UNIT_ASSERT_VALUES_EQUAL(IsSorted(v2.begin(), v2.end(), TGreater<int>()), false); + } Y_UNIT_TEST(IsSortedByTest) { TVector<int> v0; @@ -835,13 +835,13 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { UNIT_ASSERT((it + 1) == data.end()); } - + Y_UNIT_TEST(TestFindInContainer) { - std::vector<int> v = {1, 2, 1000, 15, 100}; - UNIT_ASSERT(Find(v, 5) == v.end()); - UNIT_ASSERT(Find(v, 1) == v.begin()); - UNIT_ASSERT(Find(v, 100) == v.end() - 1); - } + std::vector<int> v = {1, 2, 1000, 15, 100}; + UNIT_ASSERT(Find(v, 5) == v.end()); + UNIT_ASSERT(Find(v, 1) == v.begin()); + UNIT_ASSERT(Find(v, 100) == v.end() - 1); + } Y_UNIT_TEST(AccumulateWithBinOp) { std::vector<int> v = {1, 2, 777}; diff --git a/util/generic/array_ref.h b/util/generic/array_ref.h index 1ac60ac7d3..a67cf93bf3 100644 --- a/util/generic/array_ref.h +++ b/util/generic/array_ref.h @@ -3,30 +3,30 @@ #include <util/generic/yexception.h> #include <algorithm> -#include <initializer_list> +#include <initializer_list> #include <iterator> -/** - * `TArrayRef` works pretty much like `std::span` with dynamic extent, presenting - * an array-like interface into a contiguous sequence of objects. - * - * It can be used at interface boundaries instead of `TVector` or - * pointer-size pairs, and is actually a preferred way to pass contiguous data - * into functions. - * - * Note that `TArrayRef` can be auto-constructed from any contiguous container - * (with `size` and `data` members), and thus you don't have to change client code +/** + * `TArrayRef` works pretty much like `std::span` with dynamic extent, presenting + * an array-like interface into a contiguous sequence of objects. + * + * It can be used at interface boundaries instead of `TVector` or + * pointer-size pairs, and is actually a preferred way to pass contiguous data + * into functions. + * + * Note that `TArrayRef` can be auto-constructed from any contiguous container + * (with `size` and `data` members), and thus you don't have to change client code * when switching over from passing `TVector` to `TArrayRef`. - * - * Note that `TArrayRef` has the same const-semantics as raw pointers: - * - `TArrayRef<T>` is a non-const reference to non-const data (like `T*`); - * - `TArrayRef<const T>` is a non-const reference to const data (like `const T*`); - * - `const TArrayRef<T>` is a const reference to non-const data (like `T* const`); - * - `const TArrayRef<const T>` is a const reference to const data (like `const T* const`). - */ -template <class T> + * + * Note that `TArrayRef` has the same const-semantics as raw pointers: + * - `TArrayRef<T>` is a non-const reference to non-const data (like `T*`); + * - `TArrayRef<const T>` is a non-const reference to const data (like `const T*`); + * - `const TArrayRef<T>` is a const reference to non-const data (like `T* const`); + * - `const TArrayRef<const T>` is a const reference to const data (like `const T* const`). + */ +template <class T> class TArrayRef { -public: +public: using iterator = T*; using const_iterator = const T*; using reference = T&; @@ -36,55 +36,55 @@ public: using const_reverse_iterator = std::reverse_iterator<const_iterator>; constexpr inline TArrayRef() noexcept - : T_(nullptr) - , S_(0) - { - } + : T_(nullptr) + , S_(0) + { + } constexpr inline TArrayRef(T* data, size_t len) noexcept - : T_(data) - , S_(len) - { - } + : T_(data) + , S_(len) + { + } constexpr inline TArrayRef(T* begin, T* end) noexcept - : T_(begin) - , S_(end - begin) - { - } + : T_(begin) + , S_(end - begin) + { + } constexpr inline TArrayRef(std::initializer_list<T> list) noexcept - : T_(list.begin()) - , S_(list.size()) - { - } - - template <class Container> + : T_(list.begin()) + , S_(list.size()) + { + } + + template <class Container> constexpr inline TArrayRef(Container&& container, decltype(std::declval<T*&>() = container.data(), nullptr) = nullptr) noexcept - : T_(container.data()) - , S_(container.size()) - { - } + : T_(container.data()) + , S_(container.size()) + { + } - template <size_t N> + template <size_t N> constexpr inline TArrayRef(T (&array)[N]) noexcept - : T_(array) - , S_(N) - { - } + : T_(array) + , S_(N) + { + } template <class TT, typename = std::enable_if_t<std::is_same<std::remove_const_t<T>, std::remove_const_t<TT>>::value>> - bool operator==(const TArrayRef<TT>& other) const noexcept { + bool operator==(const TArrayRef<TT>& other) const noexcept { return (S_ == other.size()) && std::equal(begin(), end(), other.begin()); - } + } constexpr inline T* data() const noexcept { - return T_; - } + return T_; + } constexpr inline size_t size() const noexcept { - return S_; - } + return S_; + } constexpr size_t size_bytes() const noexcept { return (size() * sizeof(T)); @@ -153,7 +153,7 @@ public: constexpr inline explicit operator bool() const noexcept { return (S_ > 0); } - + /** * Obtains a ref that is a view over the first `count` elements of this TArrayRef. * @@ -220,11 +220,11 @@ public: return static_cast<yssize_t>(this->size()); } -private: - T* T_; - size_t S_; -}; - +private: + T* T_; + size_t S_; +}; + /** * Obtains a view to the object representation of the elements of the TArrayRef arrayRef. * @@ -251,14 +251,14 @@ TArrayRef<char> as_writable_bytes(TArrayRef<T> arrayRef) noexcept { template <class Range> constexpr TArrayRef<const typename Range::value_type> MakeArrayRef(const Range& range) { - return TArrayRef<const typename Range::value_type>(range); + return TArrayRef<const typename Range::value_type>(range); } template <class Range> constexpr TArrayRef<typename Range::value_type> MakeArrayRef(Range& range) { - return TArrayRef<typename Range::value_type>(range); + return TArrayRef<typename Range::value_type>(range); } - + template <class Range> constexpr TArrayRef<const typename Range::value_type> MakeConstArrayRef(const Range& range) { return TArrayRef<const typename Range::value_type>(range); @@ -269,10 +269,10 @@ constexpr TArrayRef<const typename Range::value_type> MakeConstArrayRef(Range& r return TArrayRef<const typename Range::value_type>(range); } -template <class T> +template <class T> constexpr TArrayRef<T> MakeArrayRef(T* data, size_t size) { - return TArrayRef<T>(data, size); -} + return TArrayRef<T>(data, size); +} template <class T> constexpr TArrayRef<T> MakeArrayRef(T* begin, T* end) { diff --git a/util/generic/array_ref_ut.cpp b/util/generic/array_ref_ut.cpp index 4c8eaf7135..c926b55704 100644 --- a/util/generic/array_ref_ut.cpp +++ b/util/generic/array_ref_ut.cpp @@ -168,23 +168,23 @@ Y_UNIT_TEST_SUITE(TestArrayRef) { //Testing if operator== compares values, not pointers UNIT_ASSERT_EQUAL(cRef, bSubRef); } - + Y_UNIT_TEST(TestImplicitConstructionFromContainer) { - /* Just test compilation. */ - auto fc = [](TArrayRef<const int>) {}; - auto fm = [](TArrayRef<int>) {}; - + /* Just test compilation. */ + auto fc = [](TArrayRef<const int>) {}; + auto fm = [](TArrayRef<int>) {}; + fc(TVector<int>({1})); - + const TVector<int> ac = {1}; TVector<int> am = {1}; - - fc(ac); - fc(am); - fm(am); - // fm(ac); // This one shouldn't compile. - } - + + fc(ac); + fc(am); + fm(am); + // fm(ac); // This one shouldn't compile. + } + Y_UNIT_TEST(TestFirstLastSubspan) { const int arr[] = {1, 2, 3, 4, 5}; TArrayRef<const int> aRef(arr); @@ -198,20 +198,20 @@ Y_UNIT_TEST_SUITE(TestArrayRef) { Y_UNIT_TEST(TestSlice) { const int a0[] = {1, 2, 3}; TArrayRef<const int> r0(a0); - TArrayRef<const int> s0 = r0.Slice(2); - - UNIT_ASSERT_VALUES_EQUAL(s0.size(), 1); - UNIT_ASSERT_VALUES_EQUAL(s0[0], 3); - + TArrayRef<const int> s0 = r0.Slice(2); + + UNIT_ASSERT_VALUES_EQUAL(s0.size(), 1); + UNIT_ASSERT_VALUES_EQUAL(s0[0], 3); + const int a1[] = {1, 2, 3, 4}; TArrayRef<const int> r1(a1); - TArrayRef<const int> s1 = r1.Slice(2, 1); - - UNIT_ASSERT_VALUES_EQUAL(s1.size(), 1); - UNIT_ASSERT_VALUES_EQUAL(s1[0], 3); + TArrayRef<const int> s1 = r1.Slice(2, 1); + + UNIT_ASSERT_VALUES_EQUAL(s1.size(), 1); + UNIT_ASSERT_VALUES_EQUAL(s1[0], 3); //FIXME: size checks are implemented via Y_ASSERT, hence there is no way to test them - } + } Y_UNIT_TEST(SubRegion) { TVector<char> x; diff --git a/util/generic/bitmap.h b/util/generic/bitmap.h index f77d182460..8d7e427009 100644 --- a/util/generic/bitmap.h +++ b/util/generic/bitmap.h @@ -1,6 +1,6 @@ #pragma once -#include "fwd.h" +#include "fwd.h" #include "ptr.h" #include "bitops.h" #include "typetraits.h" @@ -1077,7 +1077,7 @@ inline TBitMapOps<X> operator~(const TBitMapOps<X>& x) { /////////////////// Specialization /////////////////////////// -template <size_t BitCount, typename TChunkType /*= ui64*/> +template <size_t BitCount, typename TChunkType /*= ui64*/> class TBitMap: public TBitMapOps<TFixedBitMapTraits<BitCount, TChunkType>> { private: using TBase = TBitMapOps<TFixedBitMapTraits<BitCount, TChunkType>>; diff --git a/util/generic/bitops.h b/util/generic/bitops.h index 2db15fc59b..704540da9d 100644 --- a/util/generic/bitops.h +++ b/util/generic/bitops.h @@ -269,7 +269,7 @@ Y_FORCE_INLINE ui64 InverseMaskLowerBits(ui64 bits, ui64 skipbits) { } /* - * Returns 0-based position of the most significant bit that is set. 0 for 0. + * Returns 0-based position of the most significant bit that is set. 0 for 0. */ Y_FORCE_INLINE ui64 MostSignificantBit(ui64 v) { #ifdef __GNUC__ @@ -287,28 +287,28 @@ Y_FORCE_INLINE ui64 MostSignificantBit(ui64 v) { return res; } -/** - * Returns 0-based position of the least significant bit that is set. 0 for 0. - */ -Y_FORCE_INLINE ui64 LeastSignificantBit(ui64 v) { -#ifdef __GNUC__ - ui64 res = v ? __builtin_ffsll(v) - 1 : 0; -#elif defined(_MSC_VER) && defined(_64_) - unsigned long res = 0; - if (v) - _BitScanForward64(&res, v); -#else - ui64 res = 0; - if (v) { - while (!(v & 1)) { +/** + * Returns 0-based position of the least significant bit that is set. 0 for 0. + */ +Y_FORCE_INLINE ui64 LeastSignificantBit(ui64 v) { +#ifdef __GNUC__ + ui64 res = v ? __builtin_ffsll(v) - 1 : 0; +#elif defined(_MSC_VER) && defined(_64_) + unsigned long res = 0; + if (v) + _BitScanForward64(&res, v); +#else + ui64 res = 0; + if (v) { + while (!(v & 1)) { ++res; - v >>= 1; - } - } -#endif - return res; -} - + v >>= 1; + } + } +#endif + return res; +} + /* * Returns 0 - based position of the most significant bit (compile time) * 0 for 0. diff --git a/util/generic/bitops_ut.cpp b/util/generic/bitops_ut.cpp index d23c2b5c27..3d1f7e9575 100644 --- a/util/generic/bitops_ut.cpp +++ b/util/generic/bitops_ut.cpp @@ -65,7 +65,7 @@ Y_UNIT_TEST_SUITE(TBitOpsTest) { UNIT_ASSERT(IsPowerOf2(0x1000000000000000ull)); UNIT_ASSERT(!IsPowerOf2(0x1000000000000001ull)); } - + Y_UNIT_TEST(TestFastClp2) { TestFastClp2ForEachPowerOf2<unsigned>(); TestFastClp2ForEachPowerOf2<unsigned long>(); @@ -89,7 +89,7 @@ Y_UNIT_TEST_SUITE(TBitOpsTest) { for (ui32 i = 0; i < 64; ++i) { UNIT_ASSERT_VALUES_EQUAL(i, MostSignificantBit(ui64{1} << i)); } - + for (ui32 i = 0; i < 63; ++i) { UNIT_ASSERT_VALUES_EQUAL(i + 1, MostSignificantBit(ui64{3} << i)); } @@ -99,17 +99,17 @@ Y_UNIT_TEST_SUITE(TBitOpsTest) { for (ui32 i = 0; i < 64; ++i) { UNIT_ASSERT_VALUES_EQUAL(i, LeastSignificantBit(ui64{1} << i)); } - + for (ui32 i = 0; i < 63; ++i) { UNIT_ASSERT_VALUES_EQUAL(i, LeastSignificantBit(ui64{3} << i)); } - + for (ui32 i = 0; i < 64; ++i) { ui64 value = (ui64(-1)) << i; UNIT_ASSERT_VALUES_EQUAL(i, LeastSignificantBit(value)); } - } - + } + Y_UNIT_TEST(TestCeilLog2) { UNIT_ASSERT_VALUES_EQUAL(CeilLog2(ui64{1}), 1); diff --git a/util/generic/cast_ut.cpp b/util/generic/cast_ut.cpp index 718a8de79d..677afacdf9 100644 --- a/util/generic/cast_ut.cpp +++ b/util/generic/cast_ut.cpp @@ -85,7 +85,7 @@ private: void TestBitCast() { // Change sign of float { - const float floatValue = 17.33f; + const float floatValue = 17.33f; ui32 ui32Value = BitCast<ui32>(floatValue); ui32Value ^= (ui32)1 << 31; UNIT_ASSERT_VALUES_EQUAL(-floatValue, BitCast<float>(ui32Value)); diff --git a/util/generic/deque.h b/util/generic/deque.h index 2dabaf3177..501256d55c 100644 --- a/util/generic/deque.h +++ b/util/generic/deque.h @@ -1,11 +1,11 @@ #pragma once #include "fwd.h" - + #include <util/memory/alloc.h> #include <deque> -#include <memory> +#include <memory> #include <initializer_list> template <class T, class A> diff --git a/util/generic/explicit_type.cpp b/util/generic/explicit_type.cpp index 65bc80093a..207a62a474 100644 --- a/util/generic/explicit_type.cpp +++ b/util/generic/explicit_type.cpp @@ -1 +1 @@ -#include "explicit_type.h" +#include "explicit_type.h" diff --git a/util/generic/explicit_type.h b/util/generic/explicit_type.h index 34e269f155..636b5499cb 100644 --- a/util/generic/explicit_type.h +++ b/util/generic/explicit_type.h @@ -1,42 +1,42 @@ -#pragma once - -#include "typetraits.h" - -/** - * Helper type that can be used as one of the parameters in function declaration - * to limit the number of types this function can be called with. - * - * Example usage: - * @code - * void CharOnlyFunction(TExplicitType<char> value); - * void AnythingFunction(char value); - * - * CharOnlyFunction('c'); // Works. - * CharOnlyFunction(1); // Compilation error. - * CharOnlyFunction(1ull); // Compilation error. - * - * AnythingFunction('c'); // Works. - * AnythingFunction(1); // Works. - * AnythingFunction(1ull); // Works. - * @endcode - */ +#pragma once + +#include "typetraits.h" + +/** + * Helper type that can be used as one of the parameters in function declaration + * to limit the number of types this function can be called with. + * + * Example usage: + * @code + * void CharOnlyFunction(TExplicitType<char> value); + * void AnythingFunction(char value); + * + * CharOnlyFunction('c'); // Works. + * CharOnlyFunction(1); // Compilation error. + * CharOnlyFunction(1ull); // Compilation error. + * + * AnythingFunction('c'); // Works. + * AnythingFunction(1); // Works. + * AnythingFunction(1ull); // Works. + * @endcode + */ template <class T> -class TExplicitType { -public: +class TExplicitType { +public: template <class OtherT> TExplicitType(const OtherT& value, std::enable_if_t<std::is_same<OtherT, T>::value>* = nullptr) noexcept - : Value_(value) - { - } - - const T& Value() const noexcept { - return Value_; - } - - operator const T&() const noexcept { - return Value_; - } - -private: - const T& Value_; -}; + : Value_(value) + { + } + + const T& Value() const noexcept { + return Value_; + } + + operator const T&() const noexcept { + return Value_; + } + +private: + const T& Value_; +}; diff --git a/util/generic/explicit_type_ut.cpp b/util/generic/explicit_type_ut.cpp index 50a745f090..ef40fbbce6 100644 --- a/util/generic/explicit_type_ut.cpp +++ b/util/generic/explicit_type_ut.cpp @@ -1,43 +1,43 @@ -#include "explicit_type.h" - +#include "explicit_type.h" + #include <library/cpp/testing/unittest/registar.h> - -struct TCallableBase { -public: - using TYes = char; - using TNo = struct { TYes dummy[32]; }; - + +struct TCallableBase { +public: + using TYes = char; + using TNo = struct { TYes dummy[32]; }; + template <class T, class Arg> - static TNo Test(const T&, const Arg&, ...); - + static TNo Test(const T&, const Arg&, ...); + template <class T, class Arg> static TYes Test(const T&, const Arg&, int, decltype(std::declval<T>()(std::declval<Arg>()))* = nullptr); -}; - +}; + template <class T, class Arg> -struct TCallable: public TCallableBase { - enum { +struct TCallable: public TCallableBase { + enum { Result = sizeof(Test(std::declval<T>(), std::declval<Arg>(), 1)) == sizeof(TYes) - }; -}; - + }; +}; + template <class T> -struct TExplicitlyCallable { +struct TExplicitlyCallable { void operator()(TExplicitType<T>) { } -}; - -struct IntConvertible { +}; + +struct IntConvertible { operator int() { return 1; } -}; - -struct IntConstructible { +}; + +struct IntConstructible { IntConstructible(const int&) { } -}; - +}; + Y_UNIT_TEST_SUITE(TestExplicitType) { Y_UNIT_TEST(Test1) { UNIT_ASSERT_VALUES_EQUAL(static_cast<bool>(TCallable<TExplicitlyCallable<char>, char>::Result), true); @@ -48,5 +48,5 @@ Y_UNIT_TEST_SUITE(TestExplicitType) { UNIT_ASSERT_VALUES_EQUAL(static_cast<bool>(TCallable<TExplicitlyCallable<IntConstructible>, IntConstructible>::Result), true); UNIT_ASSERT_VALUES_EQUAL(static_cast<bool>(TCallable<TExplicitlyCallable<IntConstructible>, IntConvertible>::Result), false); UNIT_ASSERT_VALUES_EQUAL(static_cast<bool>(TCallable<TExplicitlyCallable<IntConstructible>, int>::Result), false); - } -} + } +} diff --git a/util/generic/flags.cpp b/util/generic/flags.cpp index 2ee52188a0..3fd1f2b272 100644 --- a/util/generic/flags.cpp +++ b/util/generic/flags.cpp @@ -1,14 +1,14 @@ -#include "flags.h" - -#include <util/stream/format.h> -#include <util/system/yassert.h> - +#include "flags.h" + +#include <util/stream/format.h> +#include <util/system/yassert.h> + void ::NPrivate::PrintFlags(IOutputStream& stream, ui64 value, size_t size) { - /* Note that this function is in cpp because we need to break circular - * dependency between TFlags and ENumberFormat. */ - stream << "TFlags("; - - switch (size) { + /* Note that this function is in cpp because we need to break circular + * dependency between TFlags and ENumberFormat. */ + stream << "TFlags("; + + switch (size) { case 1: stream << Bin(static_cast<ui8>(value), HF_FULL); break; @@ -23,7 +23,7 @@ void ::NPrivate::PrintFlags(IOutputStream& stream, ui64 value, size_t size) { break; default: Y_VERIFY(false); - } - - stream << ")"; -} + } + + stream << ")"; +} diff --git a/util/generic/flags.h b/util/generic/flags.h index a1f5921d42..1d9bda5855 100644 --- a/util/generic/flags.h +++ b/util/generic/flags.h @@ -1,173 +1,173 @@ -#pragma once - -#include <type_traits> - -#include <util/system/types.h> +#pragma once + +#include <type_traits> + +#include <util/system/types.h> #include <util/generic/typetraits.h> -#include <util/generic/fwd.h> - +#include <util/generic/fwd.h> + class IOutputStream; -namespace NPrivate { +namespace NPrivate { void PrintFlags(IOutputStream& stream, ui64 value, size_t size); -} - -/** - * `TFlags` wrapper provides a type-safe mechanism for storing OR combinations - * of enumeration values. - * - * This class is intended to be used mainly via helper macros. For example: - * @code - * class TAligner { - * public: - * enum EOrientation { - * Vertical = 1, - * Horizontal = 2 - * }; - * Y_DECLARE_FLAGS(EOrientations, EOrientation) - * - * // ... - * }; - * - * Y_DECLARE_OPERATORS_FOR_FLAGS(TAligner::EOrientations) - * @endcode - */ -template <class Enum> -class TFlags { - static_assert(std::is_enum<Enum>::value, "Expecting an enumeration here."); - -public: - using TEnum = Enum; +} + +/** + * `TFlags` wrapper provides a type-safe mechanism for storing OR combinations + * of enumeration values. + * + * This class is intended to be used mainly via helper macros. For example: + * @code + * class TAligner { + * public: + * enum EOrientation { + * Vertical = 1, + * Horizontal = 2 + * }; + * Y_DECLARE_FLAGS(EOrientations, EOrientation) + * + * // ... + * }; + * + * Y_DECLARE_OPERATORS_FOR_FLAGS(TAligner::EOrientations) + * @endcode + */ +template <class Enum> +class TFlags { + static_assert(std::is_enum<Enum>::value, "Expecting an enumeration here."); + +public: + using TEnum = Enum; using TInt = std::underlying_type_t<Enum>; - + constexpr TFlags(std::nullptr_t = 0) - : Value_(0) - { - } - - constexpr TFlags(Enum value) - : Value_(static_cast<TInt>(value)) - { - } - - /* Generated copy/move ctor/assignment are OK. */ - - constexpr operator TInt() const { - return Value_; - } - + : Value_(0) + { + } + + constexpr TFlags(Enum value) + : Value_(static_cast<TInt>(value)) + { + } + + /* Generated copy/move ctor/assignment are OK. */ + + constexpr operator TInt() const { + return Value_; + } + constexpr TInt ToBaseType() const { return Value_; } constexpr static TFlags FromBaseType(TInt value) { - return TFlags(TFlag(value)); - } - - constexpr friend TFlags operator|(TFlags l, TFlags r) { - return TFlags(TFlag(l.Value_ | r.Value_)); - } - - constexpr friend TFlags operator|(TEnum l, TFlags r) { - return TFlags(TFlag(static_cast<TInt>(l) | r.Value_)); - } - - constexpr friend TFlags operator|(TFlags l, TEnum r) { - return TFlags(TFlag(l.Value_ | static_cast<TInt>(r))); - } - - constexpr friend TFlags operator^(TFlags l, TFlags r) { - return TFlags(TFlag(l.Value_ ^ r.Value_)); - } - + return TFlags(TFlag(value)); + } + + constexpr friend TFlags operator|(TFlags l, TFlags r) { + return TFlags(TFlag(l.Value_ | r.Value_)); + } + + constexpr friend TFlags operator|(TEnum l, TFlags r) { + return TFlags(TFlag(static_cast<TInt>(l) | r.Value_)); + } + + constexpr friend TFlags operator|(TFlags l, TEnum r) { + return TFlags(TFlag(l.Value_ | static_cast<TInt>(r))); + } + + constexpr friend TFlags operator^(TFlags l, TFlags r) { + return TFlags(TFlag(l.Value_ ^ r.Value_)); + } + constexpr friend TFlags operator^(TEnum l, TFlags r) { - return TFlags(TFlag(static_cast<TInt>(l) ^ r.Value_)); - } - + return TFlags(TFlag(static_cast<TInt>(l) ^ r.Value_)); + } + constexpr friend TFlags operator^(TFlags l, TEnum r) { - return TFlags(TFlag(l.Value_ ^ static_cast<TInt>(r))); - } - + return TFlags(TFlag(l.Value_ ^ static_cast<TInt>(r))); + } + constexpr friend TFlags operator&(TFlags l, TFlags r) { - return TFlags(TFlag(l.Value_ & r.Value_)); - } - - constexpr friend TFlags operator&(TEnum l, TFlags r) { - return TFlags(TFlag(static_cast<TInt>(l) & r.Value_)); - } - - constexpr friend TFlags operator&(TFlags l, TEnum r) { - return TFlags(TFlag(l.Value_ & static_cast<TInt>(r))); - } - - constexpr friend bool operator==(TFlags l, TFlags r) { - return l.Value_ == r.Value_; - } - - constexpr friend bool operator==(TEnum l, TFlags r) { + return TFlags(TFlag(l.Value_ & r.Value_)); + } + + constexpr friend TFlags operator&(TEnum l, TFlags r) { + return TFlags(TFlag(static_cast<TInt>(l) & r.Value_)); + } + + constexpr friend TFlags operator&(TFlags l, TEnum r) { + return TFlags(TFlag(l.Value_ & static_cast<TInt>(r))); + } + + constexpr friend bool operator==(TFlags l, TFlags r) { + return l.Value_ == r.Value_; + } + + constexpr friend bool operator==(TEnum l, TFlags r) { return static_cast<TInt>(l) == r.Value_; - } - - constexpr friend bool operator==(TFlags l, TEnum r) { + } + + constexpr friend bool operator==(TFlags l, TEnum r) { return l.Value_ == static_cast<TInt>(r); - } - - constexpr friend bool operator!=(TFlags l, TFlags r) { - return l.Value_ != r.Value_; - } - - constexpr friend bool operator!=(TEnum l, TFlags r) { + } + + constexpr friend bool operator!=(TFlags l, TFlags r) { + return l.Value_ != r.Value_; + } + + constexpr friend bool operator!=(TEnum l, TFlags r) { return static_cast<TInt>(l) != r.Value_; - } - - constexpr friend bool operator!=(TFlags l, TEnum r) { + } + + constexpr friend bool operator!=(TFlags l, TEnum r) { return l.Value_ != static_cast<TInt>(r); - } - - TFlags& operator&=(TFlags mask) { + } + + TFlags& operator&=(TFlags mask) { *this = *this & mask; - return *this; - } - - TFlags& operator&=(Enum mask) { + return *this; + } + + TFlags& operator&=(Enum mask) { *this = *this & mask; - return *this; - } - - TFlags& operator|=(TFlags flags) { - *this = *this | flags; - return *this; - } - - TFlags& operator|=(Enum flags) { - *this = *this | flags; - return *this; - } - - TFlags& operator^=(TFlags flags) { - *this = *this ^ flags; - return *this; - } - - TFlags& operator^=(Enum flags) { - *this = *this ^ flags; - return *this; - } - - constexpr TFlags operator~() const { - return TFlags(TFlag(~Value_)); - } - - constexpr bool operator!() const { - return !Value_; - } - - constexpr explicit operator bool() const { - return Value_; - } - + return *this; + } + + TFlags& operator|=(TFlags flags) { + *this = *this | flags; + return *this; + } + + TFlags& operator|=(Enum flags) { + *this = *this | flags; + return *this; + } + + TFlags& operator^=(TFlags flags) { + *this = *this ^ flags; + return *this; + } + + TFlags& operator^=(Enum flags) { + *this = *this ^ flags; + return *this; + } + + constexpr TFlags operator~() const { + return TFlags(TFlag(~Value_)); + } + + constexpr bool operator!() const { + return !Value_; + } + + constexpr explicit operator bool() const { + return Value_; + } + constexpr bool HasFlags(TFlags flags) const { return (Value_ & flags.Value_) == flags.Value_; } @@ -178,31 +178,31 @@ public: } friend IOutputStream& operator<<(IOutputStream& stream, const TFlags& flags) { - ::NPrivate::PrintFlags(stream, static_cast<ui64>(flags.Value_), sizeof(TInt)); - return stream; - } - -private: - struct TFlag { + ::NPrivate::PrintFlags(stream, static_cast<ui64>(flags.Value_), sizeof(TInt)); + return stream; + } + +private: + struct TFlag { constexpr TFlag() { } constexpr explicit TFlag(TInt value) : Value(value) { } - - TInt Value = 0; - }; - - constexpr explicit TFlags(TFlag value) - : Value_(value.Value) - { - } - -private: - TInt Value_; -}; - + + TInt Value = 0; + }; + + constexpr explicit TFlags(TFlag value) + : Value_(value.Value) + { + } + +private: + TInt Value_; +}; + template <class T> struct TPodTraits<TFlags<T>> { enum { @@ -210,35 +210,35 @@ struct TPodTraits<TFlags<T>> { }; }; -template <class Enum> -struct THash<TFlags<Enum>> { - size_t operator()(const TFlags<Enum>& flags) const noexcept { - return THash<typename TFlags<Enum>::TInt>()(flags); - } -}; - -/** - * This macro defines a flags type for the provided enum. - * - * @param FLAGS Name of the flags type to declare. - * @param ENUM Name of the base enum type to use. - */ -#define Y_DECLARE_FLAGS(FLAGS, ENUM) \ - using FLAGS = TFlags<ENUM>; - -/** - * This macro declares global operator functions for enum base of `FLAGS` type. - * This way operations on individual enum values will provide a type-safe - * `TFlags` object. - * - * @param FLAGS Flags type to declare operator for. - */ -#define Y_DECLARE_OPERATORS_FOR_FLAGS(FLAGS) \ +template <class Enum> +struct THash<TFlags<Enum>> { + size_t operator()(const TFlags<Enum>& flags) const noexcept { + return THash<typename TFlags<Enum>::TInt>()(flags); + } +}; + +/** + * This macro defines a flags type for the provided enum. + * + * @param FLAGS Name of the flags type to declare. + * @param ENUM Name of the base enum type to use. + */ +#define Y_DECLARE_FLAGS(FLAGS, ENUM) \ + using FLAGS = TFlags<ENUM>; + +/** + * This macro declares global operator functions for enum base of `FLAGS` type. + * This way operations on individual enum values will provide a type-safe + * `TFlags` object. + * + * @param FLAGS Flags type to declare operator for. + */ +#define Y_DECLARE_OPERATORS_FOR_FLAGS(FLAGS) \ Y_DECLARE_UNUSED \ - constexpr inline FLAGS operator|(FLAGS::TEnum l, FLAGS::TEnum r) { \ - return FLAGS(l) | r; \ - } \ + constexpr inline FLAGS operator|(FLAGS::TEnum l, FLAGS::TEnum r) { \ + return FLAGS(l) | r; \ + } \ Y_DECLARE_UNUSED \ - constexpr inline FLAGS operator~(FLAGS::TEnum value) { \ - return ~FLAGS(value); \ - } + constexpr inline FLAGS operator~(FLAGS::TEnum value) { \ + return ~FLAGS(value); \ + } diff --git a/util/generic/flags_ut.cpp b/util/generic/flags_ut.cpp index 5377c6a058..109ead7090 100644 --- a/util/generic/flags_ut.cpp +++ b/util/generic/flags_ut.cpp @@ -1,27 +1,27 @@ #include <library/cpp/testing/unittest/registar.h> - -#include "flags.h" - + +#include "flags.h" + enum ETestFlag1: ui16 { - Test1 = 1, - Test2 = 2, - Test4 = 4, - Test8 = 8 -}; -Y_DECLARE_FLAGS(ETest1, ETestFlag1) -Y_DECLARE_OPERATORS_FOR_FLAGS(ETest1) - + Test1 = 1, + Test2 = 2, + Test4 = 4, + Test8 = 8 +}; +Y_DECLARE_FLAGS(ETest1, ETestFlag1) +Y_DECLARE_OPERATORS_FOR_FLAGS(ETest1) + static_assert(TTypeTraits<ETest1>::IsPod, "flags should be POD type"); -enum class ETestFlag2 { - Test1 = 1, - Test2 = 2, - Test4 = 4, - Test8 = 8 -}; -Y_DECLARE_FLAGS(ETest2, ETestFlag2) -Y_DECLARE_OPERATORS_FOR_FLAGS(ETest2) - +enum class ETestFlag2 { + Test1 = 1, + Test2 = 2, + Test4 = 4, + Test8 = 8 +}; +Y_DECLARE_FLAGS(ETest2, ETestFlag2) +Y_DECLARE_OPERATORS_FOR_FLAGS(ETest2) + namespace { // won't compile without Y_DECLARE_UNUSED enum class ETestFlag3 { One = 1, @@ -32,16 +32,16 @@ namespace { } Y_UNIT_TEST_SUITE(TFlagsTest) { - template <class Enum> - void TestEnum() { + template <class Enum> + void TestEnum() { { auto i = Enum::Test1 | Enum::Test2; - + UNIT_ASSERT((std::is_same<decltype(i), TFlags<Enum>>::value)); UNIT_ASSERT((std::is_same<decltype(~i), TFlags<Enum>>::value)); UNIT_ASSERT(!(std::is_same<decltype(i), int>::value)); UNIT_ASSERT_VALUES_EQUAL(sizeof(Enum), sizeof(TFlags<Enum>)); - + UNIT_ASSERT(i.HasFlags(Enum::Test1)); UNIT_ASSERT(i.HasFlags(Enum::Test4) == false); UNIT_ASSERT(i.HasFlags(Enum::Test1 | Enum::Test4) == false); @@ -64,42 +64,42 @@ Y_UNIT_TEST_SUITE(TFlagsTest) { i.RemoveFlags(Enum::Test1 | Enum::Test2); UNIT_ASSERT_EQUAL(i, TFlags<Enum>()); } - } - + } + Y_UNIT_TEST(TestFlags) { - TestEnum<ETestFlag1>(); - TestEnum<ETestFlag2>(); - } - + TestEnum<ETestFlag1>(); + TestEnum<ETestFlag2>(); + } + Y_UNIT_TEST(TestZero) { - /* This code should simply compile. */ - - ETest1 f = 0; - f = 0; - f = ETest1(0); - - ETest1 ff(0); + /* This code should simply compile. */ + + ETest1 f = 0; + f = 0; + f = ETest1(0); + + ETest1 ff(0); ff = 0; - } - + } + Y_UNIT_TEST(TestOutput) { ETest1 value0 = nullptr, value1 = Test1, value7 = Test1 | Test2 | Test4; - - UNIT_ASSERT_VALUES_EQUAL(ToString(value0), "TFlags(0000000000000000)"); - UNIT_ASSERT_VALUES_EQUAL(ToString(value1), "TFlags(0000000000000001)"); - UNIT_ASSERT_VALUES_EQUAL(ToString(value7), "TFlags(0000000000000111)"); - } - + + UNIT_ASSERT_VALUES_EQUAL(ToString(value0), "TFlags(0000000000000000)"); + UNIT_ASSERT_VALUES_EQUAL(ToString(value1), "TFlags(0000000000000001)"); + UNIT_ASSERT_VALUES_EQUAL(ToString(value7), "TFlags(0000000000000111)"); + } + Y_UNIT_TEST(TestHash) { ETest1 value0 = nullptr, value1 = Test1; - + THashMap<ETest1, int> hash; - hash[value0] = 0; - hash[value1] = 1; - - UNIT_ASSERT_VALUES_EQUAL(hash[value0], 0); - UNIT_ASSERT_VALUES_EQUAL(hash[value1], 1); - } + hash[value0] = 0; + hash[value1] = 1; + + UNIT_ASSERT_VALUES_EQUAL(hash[value0], 0); + UNIT_ASSERT_VALUES_EQUAL(hash[value1], 1); + } Y_UNIT_TEST(TestBaseType) { ui16 goodValue = 7; @@ -114,4 +114,4 @@ Y_UNIT_TEST_SUITE(TFlagsTest) { auto badFlags = ETest1::FromBaseType(badValue); UNIT_ASSERT_VALUES_EQUAL(badValue, badFlags.ToBaseType()); } -} +} diff --git a/util/generic/fwd.h b/util/generic/fwd.h index 5cc2da40e5..3e3b8d8122 100644 --- a/util/generic/fwd.h +++ b/util/generic/fwd.h @@ -21,19 +21,19 @@ using TUtf32StringBuf = TBasicStringBuf<wchar32>; //misc class TBuffer; -//functors -template <class T = void> -struct TLess; - -template <class T = void> +//functors +template <class T = void> +struct TLess; + +template <class T = void> struct TGreater; -template <class T = void> -struct TEqualTo; - -template <class T> -struct THash; - +template <class T = void> +struct TEqualTo; + +template <class T> +struct THash; + //intrusive containers struct TIntrusiveListDefaultTag; template <class T, class Tag = TIntrusiveListDefaultTag> @@ -54,7 +54,7 @@ class TRbTree; //containers template <class T, class A = std::allocator<T>> class TVector; - + template <class T, class A = std::allocator<T>> class TDeque; @@ -94,22 +94,22 @@ class TMultiSet; template <class T, class S = TDeque<T>> class TStack; -template <size_t BitCount, typename TChunkType = ui64> +template <size_t BitCount, typename TChunkType = ui64> class TBitMap; //autopointers -class TDelete; +class TDelete; class TDeleteArray; class TFree; -class TCopyNew; - -template <class T, class D = TDelete> +class TCopyNew; + +template <class T, class D = TDelete> class TAutoPtr; -template <class T, class D = TDelete> +template <class T, class D = TDelete> class THolder; -template <class T, class C, class D = TDelete> +template <class T, class C, class D = TDelete> class TRefCounted; template <class T> @@ -127,10 +127,10 @@ class TIntrusiveConstPtr; template <class T, class Ops = TDefaultIntrusivePtrOps<T>> using TSimpleIntrusivePtr = TIntrusivePtr<T, TSimpleIntrusiveOps<T, Ops>>; -template <class T, class C, class D = TDelete> +template <class T, class C, class D = TDelete> class TSharedPtr; -template <class T, class C = TCopyNew, class D = TDelete> +template <class T, class C = TCopyNew, class D = TDelete> class TCopyPtr; template <class TPtr, class TCopy = TCopyNew> diff --git a/util/generic/guid.h b/util/generic/guid.h index 2bf6c8ad99..247819f88e 100644 --- a/util/generic/guid.h +++ b/util/generic/guid.h @@ -13,7 +13,7 @@ * * See https://clubs.at.yandex-team.ru/stackoverflow/10238/10240 * and https://st.yandex-team.ru/IGNIETFERRO-768 for details. - */ + */ struct TGUID { ui32 dw[4] = {}; diff --git a/util/generic/hash.cpp b/util/generic/hash.cpp index a674ee4538..07c8daf1c0 100644 --- a/util/generic/hash.cpp +++ b/util/generic/hash.cpp @@ -1,8 +1,8 @@ -#include "hash.h" - +#include "hash.h" + #include <util/string/escape.h> -#include <util/string/cast.h> - +#include <util/string/cast.h> + const void* const _yhashtable_empty_data[] = {(void*)3, nullptr, (void*)1}; TString NPrivate::MapKeyToString(TStringBuf key) { @@ -10,41 +10,41 @@ TString NPrivate::MapKeyToString(TStringBuf key) { try { return EscapeC(key.substr(0, HASH_KEY_MAX_LENGTH)); } catch (...) { - return "TStringBuf"; + return "TStringBuf"; } } - + TString NPrivate::MapKeyToString(unsigned short key) { - return ToString(key); -} - + return ToString(key); +} + TString NPrivate::MapKeyToString(short key) { - return ToString(key); -} - + return ToString(key); +} + TString NPrivate::MapKeyToString(unsigned int key) { - return ToString(key); -} - + return ToString(key); +} + TString NPrivate::MapKeyToString(int key) { - return ToString(key); -} - + return ToString(key); +} + TString NPrivate::MapKeyToString(unsigned long key) { - return ToString(key); -} - + return ToString(key); +} + TString NPrivate::MapKeyToString(long key) { - return ToString(key); -} - + return ToString(key); +} + TString NPrivate::MapKeyToString(unsigned long long key) { - return ToString(key); -} - + return ToString(key); +} + TString NPrivate::MapKeyToString(long long key) { - return ToString(key); -} + return ToString(key); +} void NPrivate::ThrowKeyNotFoundInHashTableException(const TStringBuf keyRepresentation) { ythrow yexception() << "Key not found in hashtable: " << keyRepresentation; diff --git a/util/generic/hash.h b/util/generic/hash.h index e46db21fa9..78572070a9 100644 --- a/util/generic/hash.h +++ b/util/generic/hash.h @@ -1,6 +1,6 @@ #pragma once -#include "fwd.h" +#include "fwd.h" #include "mapfindptr.h" #include <util/memory/alloc.h> @@ -19,8 +19,8 @@ #include <cstdlib> -#include "hash_primes.h" - +#include "hash_primes.h" + struct TSelect1st { template <class TPair> inline const typename TPair::first_type& operator()(const TPair& x) const { @@ -37,10 +37,10 @@ struct __yhashtable_node { * This trick makes it possible to use only one node pointer in a hash table * iterator. */ __yhashtable_node* next; - + /** Value stored in a node. */ Value val; - + __yhashtable_node& operator=(const __yhashtable_node&) = delete; }; @@ -148,19 +148,19 @@ struct __yhashtable_const_iterator { } }; -/** - * This class saves some space in allocator-based containers for the most common - * use case of empty allocators. This is achieved thanks to the application of - * empty base class optimization (aka EBCO). - */ +/** + * This class saves some space in allocator-based containers for the most common + * use case of empty allocators. This is achieved thanks to the application of + * empty base class optimization (aka EBCO). + */ template <class Alloc> -class _allocator_base: private Alloc { -public: +class _allocator_base: private Alloc { +public: _allocator_base(const Alloc& other) : Alloc(other) { } - + Alloc& _get_alloc() { return static_cast<Alloc&>(*this); } @@ -170,45 +170,45 @@ public: void _set_alloc(const Alloc& allocator) { _get_alloc() = allocator; } - + void swap(_allocator_base& other) { DoSwap(_get_alloc(), other._get_alloc()); } -}; - -/** +}; + +/** * Wrapper for an array of THashTable buckets. - * - * Is better than vector for this particular use case. Main differences: - * - Occupies one less word on stack. + * + * Is better than vector for this particular use case. Main differences: + * - Occupies one less word on stack. * - Doesn't even try to initialize its elements. It is THashTable's responsibility. * - Presents a better interface in relation to THashTable's marker element trick. - * - * Internally this class is just a pointer-size pair, and the data on the heap - * has the following structure: - * - * +----------+----------------------+----------+-------------------------+ - * | raw_size | elements ... | marker | unused space [optional] | - * +----------+----------------------+----------+-------------------------+ - * ^ ^ - * | | - * Data points here end() points here - * - * `raw_size` stores the size of the allocated memory block. It is used to - * support resizing without reallocation. - * + * + * Internally this class is just a pointer-size pair, and the data on the heap + * has the following structure: + * + * +----------+----------------------+----------+-------------------------+ + * | raw_size | elements ... | marker | unused space [optional] | + * +----------+----------------------+----------+-------------------------+ + * ^ ^ + * | | + * Data points here end() points here + * + * `raw_size` stores the size of the allocated memory block. It is used to + * support resizing without reallocation. + * * `marker` is a special marker element that is set by the THashTable that is - * then used in iterator implementation to know when the end is reached. - * - * Unused space at the end of the memory block may not be present. - */ + * then used in iterator implementation to know when the end is reached. + * + * Unused space at the end of the memory block may not be present. + */ template <class T, class Alloc> class _yhashtable_buckets: private _allocator_base<Alloc> { using base_type = _allocator_base<Alloc>; - + static_assert(sizeof(T) == sizeof(size_t), "T is expected to be the same size as size_t."); - -public: + +public: using allocator_type = Alloc; using value_type = T; using pointer = T*; @@ -220,55 +220,55 @@ public: using size_type = size_t; using difference_type = ptrdiff_t; using TBucketDivisor = ::NPrivate::THashDivisor; - + _yhashtable_buckets(const Alloc& other) : base_type(other) , Data(nullptr) , Size() { } - + ~_yhashtable_buckets() { Y_ASSERT(!Data); } - + void initialize_dynamic(TBucketDivisor size) { Y_ASSERT(!Data); - + Data = this->_get_alloc().allocate(size() + 2) + 1; Size = size; - + *reinterpret_cast<size_type*>(Data - 1) = size() + 2; } - + void deinitialize_dynamic() { Y_ASSERT(Data); - + this->_get_alloc().deallocate(Data - 1, *reinterpret_cast<size_type*>(Data - 1)); Data = pointer(); Size = TBucketDivisor(); } - + void initialize_static(pointer data, TBucketDivisor size) { Y_ASSERT(!Data && data && size() >= 1); - + Data = data; Size = size; } - + void deinitialize_static() { Y_ASSERT(Data); - + Data = pointer(); Size = TBucketDivisor(); } - + void resize_noallocate(TBucketDivisor size) { Y_ASSERT(size() <= capacity()); - + Size = size; } - + iterator begin() { return Data; } @@ -281,14 +281,14 @@ public: const_iterator end() const { return Data + Size(); } - + pointer data() { return Data; } const_pointer data() const { return Data; } - + size_type size() const { return Size(); } @@ -301,57 +301,57 @@ public: int BucketDivisorHint() const { return +Size.Hint; } - + allocator_type get_allocator() const { return this->_get_alloc(); } - + const_reference operator[](size_type index) const { Y_ASSERT(index <= Size()); - + return *(Data + index); } - + reference operator[](size_type index) { Y_ASSERT(index <= Size()); - + return *(Data + index); } - + void swap(_yhashtable_buckets& other) { base_type::swap(other); DoSwap(Data, other.Data); DoSwap(Size, other.Size); } - -private: + +private: /** Pointer to the first element of the buckets array. */ pointer Data; - + /** Size of the buckets array. Doesn't take the marker element at the end into account. */ TBucketDivisor Size; -}; - -/** +}; + +/** * This class saves one word in THashTable for the most common use case of empty - * functors. The exact implementation picks a specialization with storage allocated - * for the functors if those are non-empty, and another specialization that creates - * functors on the fly if they are empty. It is expected that empty functors have - * trivial constructors. - * - * Note that this is basically the only way to do it portably. Another option is - * multiple inheritance from empty functors, but MSVC's empty base class - * optimization chokes up on multiple empty bases, and we're already using - * EBCO in _allocator_base. - * - * Note that there are no specializations for the case when only one or two - * of the functors are empty as this is a case that's just way too rare. - */ + * functors. The exact implementation picks a specialization with storage allocated + * for the functors if those are non-empty, and another specialization that creates + * functors on the fly if they are empty. It is expected that empty functors have + * trivial constructors. + * + * Note that this is basically the only way to do it portably. Another option is + * multiple inheritance from empty functors, but MSVC's empty base class + * optimization chokes up on multiple empty bases, and we're already using + * EBCO in _allocator_base. + * + * Note that there are no specializations for the case when only one or two + * of the functors are empty as this is a case that's just way too rare. + */ template <class HashFcn, class ExtractKey, class EqualKey, class Alloc, bool IsEmpty = std::is_empty<HashFcn>::value&& std::is_empty<ExtractKey>::value&& std::is_empty<EqualKey>::value> class _yhashtable_base: public _allocator_base<Alloc> { using base_type = _allocator_base<Alloc>; -public: +public: _yhashtable_base(const HashFcn& hash, const ExtractKey& extract, const EqualKey& equals, const Alloc& alloc) : base_type(alloc) , hash_(hash) @@ -359,7 +359,7 @@ public: , equals_(equals) { } - + const EqualKey& _get_key_eq() const { return equals_; } @@ -369,7 +369,7 @@ public: void _set_key_eq(const EqualKey& equals) { this->equals_ = equals; } - + const ExtractKey& _get_key_extract() const { return extract_; } @@ -379,7 +379,7 @@ public: void _set_key_extract(const ExtractKey& extract) { this->extract_ = extract; } - + const HashFcn& _get_hash_fun() const { return hash_; } @@ -389,63 +389,63 @@ public: void _set_hash_fun(const HashFcn& hash) { this->hash_ = hash; } - + void swap(_yhashtable_base& other) { base_type::swap(other); DoSwap(equals_, other.equals_); DoSwap(extract_, other.extract_); DoSwap(hash_, other.hash_); } - -private: + +private: HashFcn hash_; ExtractKey extract_; EqualKey equals_; -}; - +}; + template <class HashFcn, class ExtractKey, class EqualKey, class Alloc> class _yhashtable_base<HashFcn, ExtractKey, EqualKey, Alloc, true>: public _allocator_base<Alloc> { using base_type = _allocator_base<Alloc>; - -public: + +public: _yhashtable_base(const HashFcn&, const ExtractKey&, const EqualKey&, const Alloc& alloc) : base_type(alloc) { } - + EqualKey _get_key_eq() const { return EqualKey(); } void _set_key_eq(const EqualKey&) { } - + ExtractKey _get_key_extract() const { return ExtractKey(); } void _set_key_extract(const ExtractKey&) { } - + HashFcn _get_hash_fun() const { return HashFcn(); } void _set_hash_fun(const HashFcn&) { } - + void swap(_yhashtable_base& other) { base_type::swap(other); } -}; - -template <class Value, class Key, class HashFcn, class ExtractKey, class EqualKey, class Alloc> +}; + +template <class Value, class Key, class HashFcn, class ExtractKey, class EqualKey, class Alloc> struct _yhashtable_traits { using node = __yhashtable_node<Value>; - + using node_allocator_type = TReboundAllocator<Alloc, node>; using nodep_allocator_type = TReboundAllocator<Alloc, node*>; - + using base_type = _yhashtable_base<HashFcn, ExtractKey, EqualKey, node_allocator_type>; -}; - +}; + extern const void* const _yhashtable_empty_data[]; template <class Value, class Key, class HashFcn, class ExtractKey, class EqualKey, class Alloc> @@ -456,7 +456,7 @@ class THashTable: private _yhashtable_traits<Value, Key, HashFcn, ExtractKey, Eq using nodep_allocator_type = typename traits_type::nodep_allocator_type; using buckets_type = _yhashtable_buckets<node*, nodep_allocator_type>; using TBucketDivisor = ::NPrivate::THashDivisor; - + public: using key_type = Key; using value_type = Value; @@ -497,16 +497,16 @@ private: auto get_key(const ValueL& value) const -> decltype(ExtractKey()(value)) { return this->_get_key_extract()(value); } - + node* get_node() { - node* result = this->_get_alloc().allocate(1); + node* result = this->_get_alloc().allocate(1); Y_ASSERT((reinterpret_cast<uintptr_t>(result) & 1) == 0); /* We're using the last bit of the node pointer. */ - return result; + return result; } void put_node(node* p) { this->_get_alloc().deallocate(p, 1); } - + buckets_type buckets; size_type num_elements; @@ -526,7 +526,7 @@ public: { initialize_buckets(buckets, 0); } - + THashTable(size_type n, const HashFcn& hf, const EqualKey& eql, const ExtractKey& ext) : base_type(hf, ext, eql, node_allocator_type()) , buckets(nodep_allocator_type()) @@ -550,7 +550,7 @@ public: , num_elements(0) { initialize_buckets(buckets, n); - } + } THashTable(const THashTable& ht) : base_type(ht._get_hash_fun(), ht._get_key_extract(), ht._get_key_eq(), ht._get_alloc()) @@ -559,12 +559,12 @@ public: { if (ht.empty()) { initialize_buckets(buckets, 0); - } else { + } else { initialize_buckets_dynamic(buckets, ht.buckets.ExtSize()); copy_from_dynamic(ht); - } + } } - + THashTable(THashTable&& ht) noexcept : base_type(ht._get_hash_fun(), ht._get_key_extract(), ht._get_key_eq(), ht._get_alloc()) , buckets(ht.buckets.get_allocator()) @@ -580,16 +580,16 @@ public: this->_set_hash_fun(ht._get_hash_fun()); this->_set_key_eq(ht._get_key_eq()); this->_set_key_extract(ht._get_key_extract()); - /* We don't copy allocator for a reason. */ - - if (ht.empty()) { - /* Some of the old code in Arcadia works around the behavior in - * clear() by invoking operator= with empty hash as an argument. - * It's expected that this will deallocate the buckets array, so - * this is what we have to do here. */ - deinitialize_buckets(buckets); - initialize_buckets(buckets, 0); - } else { + /* We don't copy allocator for a reason. */ + + if (ht.empty()) { + /* Some of the old code in Arcadia works around the behavior in + * clear() by invoking operator= with empty hash as an argument. + * It's expected that this will deallocate the buckets array, so + * this is what we have to do here. */ + deinitialize_buckets(buckets); + initialize_buckets(buckets, 0); + } else { if (buckets.capacity() > ht.buckets.size()) { buckets.resize_noallocate(ht.buckets.ExtSize()); } else { @@ -604,8 +604,8 @@ public: } THashTable& operator=(THashTable&& ht) noexcept { - basic_clear(); - swap(ht); + basic_clear(); + swap(ht); return *this; } @@ -668,13 +668,13 @@ public: } template <class OtherValue> - std::pair<iterator, bool> insert_unique(const OtherValue& obj) { + std::pair<iterator, bool> insert_unique(const OtherValue& obj) { reserve(num_elements + 1); return insert_unique_noresize(obj); } template <class OtherValue> - iterator insert_equal(const OtherValue& obj) { + iterator insert_equal(const OtherValue& obj) { reserve(num_elements + 1); return emplace_equal_noresize(obj); } @@ -686,7 +686,7 @@ public: } template <class OtherValue> - iterator insert_direct(const OtherValue& obj, insert_ctx ins) { + iterator insert_direct(const OtherValue& obj, insert_ctx ins) { return emplace_direct(ins, obj); } @@ -712,9 +712,9 @@ public: template <typename... Args> std::pair<iterator, bool> emplace_unique_noresize(Args&&... args); - template <class OtherValue> - std::pair<iterator, bool> insert_unique_noresize(const OtherValue& obj); - + template <class OtherValue> + std::pair<iterator, bool> insert_unique_noresize(const OtherValue& obj); + template <typename... Args> iterator emplace_equal_noresize(Args&&... args); @@ -759,10 +759,10 @@ public: } template <class OtherValue> - reference find_or_insert(const OtherValue& v); + reference find_or_insert(const OtherValue& v); template <class OtherKey> - iterator find(const OtherKey& key) { + iterator find(const OtherKey& key) { size_type n = bkt_num_key(key); node* first; for (first = buckets[n]; @@ -774,7 +774,7 @@ public: } template <class OtherKey> - const_iterator find(const OtherKey& key) const { + const_iterator find(const OtherKey& key) const { size_type n = bkt_num_key(key); const node* first; for (first = buckets[n]; @@ -786,10 +786,10 @@ public: } template <class OtherKey> - iterator find_i(const OtherKey& key, insert_ctx& ins); + iterator find_i(const OtherKey& key, insert_ctx& ins); template <class OtherKey> - size_type count(const OtherKey& key) const { + size_type count(const OtherKey& key) const { const size_type n = bkt_num_key(key); size_type result = 0; @@ -801,17 +801,17 @@ public: } template <class OtherKey> - std::pair<iterator, iterator> equal_range(const OtherKey& key); + std::pair<iterator, iterator> equal_range(const OtherKey& key); template <class OtherKey> - std::pair<const_iterator, const_iterator> equal_range(const OtherKey& key) const; - + std::pair<const_iterator, const_iterator> equal_range(const OtherKey& key) const; + template <class OtherKey> - size_type erase(const OtherKey& key); - + size_type erase(const OtherKey& key); + template <class OtherKey> - size_type erase_one(const OtherKey& key); - + size_type erase_one(const OtherKey& key); + // void (instead of iterator) is intended, see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2023.pdf void erase(const iterator& it); void erase(iterator first, iterator last); @@ -822,21 +822,21 @@ public: bool reserve(size_type num_elements_hint); void basic_clear(); - /** - * Clears the hashtable without deallocating the nodes. - * - * This might come in handy with non-standard allocators, e.g. a pool - * allocator with a pool that is then cleared manually, thus releasing all - * the nodes at once. - */ - void release_nodes() { - if (empty()) - return; /* Need this check because empty buckets may reside in read-only memory. */ - - clear_buckets(buckets); - num_elements = 0; - } - + /** + * Clears the hashtable without deallocating the nodes. + * + * This might come in handy with non-standard allocators, e.g. a pool + * allocator with a pool that is then cleared manually, thus releasing all + * the nodes at once. + */ + void release_nodes() { + if (empty()) + return; /* Need this check because empty buckets may reside in read-only memory. */ + + clear_buckets(buckets); + num_elements = 0; + } + // implemented in save_stl.h template <class KeySaver> int save_for_st(IOutputStream* stream, KeySaver& ks, sthash<int, int, THash<int>, TEqualTo<int>, typename KeySaver::TSizeType>* stHash = nullptr) const; @@ -852,7 +852,7 @@ public: } } } - + /** * Clears the hashtable and tries to reasonably downsize it. Note that * downsizing is mainly for the following use case: @@ -884,36 +884,36 @@ private: } else { TBucketDivisor size = HashBucketCountExt(sizeHint); Y_ASSERT(size() >= 7); - + initialize_buckets_dynamic(buckets, size); } - } + } static void initialize_buckets_dynamic(buckets_type& buckets, TBucketDivisor size) { buckets.initialize_dynamic(size); memset(buckets.data(), 0, size() * sizeof(*buckets.data())); buckets[size()] = (node*)1; } - + static void deinitialize_buckets(buckets_type& buckets) { if (buckets.size() == 1) { buckets.deinitialize_static(); } else { buckets.deinitialize_dynamic(); } - } - + } + static void clear_buckets(buckets_type& buckets) { memset(buckets.data(), 0, buckets.size() * sizeof(*buckets.data())); } - + template <class OtherKey> - size_type bkt_num_key(const OtherKey& key) const { + size_type bkt_num_key(const OtherKey& key) const { return bkt_num_key(key, buckets.ExtSize()); } template <class OtherValue> - size_type bkt_num(const OtherValue& obj) const { + size_type bkt_num(const OtherValue& obj) const { return bkt_num_key(get_key(obj)); } @@ -1019,7 +1019,7 @@ std::pair<typename THashTable<V, K, HF, Ex, Eq, A>::iterator, bool> THashTable<V } template <class V, class K, class HF, class Ex, class Eq, class A> -template <class OtherValue> +template <class OtherValue> std::pair<typename THashTable<V, K, HF, Ex, Eq, A>::iterator, bool> THashTable<V, K, HF, Ex, Eq, A>::insert_unique_noresize(const OtherValue& obj) { const size_type n = bkt_num(obj); node* first = buckets[n]; @@ -1063,7 +1063,7 @@ __yhashtable_iterator<V> THashTable<V, K, HF, Ex, Eq, A>::emplace_equal_noresize } template <class V, class K, class HF, class Ex, class Eq, class A> -template <class OtherValue> +template <class OtherValue> typename THashTable<V, K, HF, Ex, Eq, A>::reference THashTable<V, K, HF, Ex, Eq, A>::find_or_insert(const OtherValue& v) { reserve(num_elements + 1); @@ -1083,7 +1083,7 @@ typename THashTable<V, K, HF, Ex, Eq, A>::reference THashTable<V, K, HF, Ex, Eq, } template <class V, class K, class HF, class Ex, class Eq, class A> -template <class OtherKey> +template <class OtherKey> __yhashtable_iterator<V> THashTable<V, K, HF, Ex, Eq, A>::find_i(const OtherKey& key, insert_ctx& ins) { size_type n = bkt_num_key(key); ins = &buckets[n]; @@ -1097,7 +1097,7 @@ __yhashtable_iterator<V> THashTable<V, K, HF, Ex, Eq, A>::find_i(const OtherKey& } template <class V, class K, class HF, class Ex, class Eq, class A> -template <class OtherKey> +template <class OtherKey> std::pair<__yhashtable_iterator<V>, __yhashtable_iterator<V>> THashTable<V, K, HF, Ex, Eq, A>::equal_range(const OtherKey& key) { using pii = std::pair<iterator, iterator>; const size_type n = bkt_num_key(key); @@ -1120,7 +1120,7 @@ std::pair<__yhashtable_iterator<V>, __yhashtable_iterator<V>> THashTable<V, K, H } template <class V, class K, class HF, class Ex, class Eq, class A> -template <class OtherKey> +template <class OtherKey> std::pair<__yhashtable_const_iterator<V>, __yhashtable_const_iterator<V>> THashTable<V, K, HF, Ex, Eq, A>::equal_range(const OtherKey& key) const { using pii = std::pair<const_iterator, const_iterator>; const size_type n = bkt_num_key(key); @@ -1144,7 +1144,7 @@ std::pair<__yhashtable_const_iterator<V>, __yhashtable_const_iterator<V>> THashT } template <class V, class K, class HF, class Ex, class Eq, class A> -template <class OtherKey> +template <class OtherKey> typename THashTable<V, K, HF, Ex, Eq, A>::size_type THashTable<V, K, HF, Ex, Eq, A>::erase(const OtherKey& key) { const size_type n = bkt_num_key(key); node* first = buckets[n]; @@ -1176,7 +1176,7 @@ typename THashTable<V, K, HF, Ex, Eq, A>::size_type THashTable<V, K, HF, Ex, Eq, } template <class V, class K, class HF, class Ex, class Eq, class A> -template <class OtherKey> +template <class OtherKey> typename THashTable<V, K, HF, Ex, Eq, A>::size_type THashTable<V, K, HF, Ex, Eq, A>::erase_one(const OtherKey& key) { const size_type n = bkt_num_key(key); node* first = buckets[n]; @@ -1287,7 +1287,7 @@ bool THashTable<V, K, HF, Ex, Eq, A>::reserve(size_type num_elements_hint) { first = buckets[bucket]; } } - + buckets.swap(tmp); deinitialize_buckets(tmp); @@ -1365,7 +1365,7 @@ void THashTable<V, K, HF, Ex, Eq, A>::basic_clear() { template <class V, class K, class HF, class Ex, class Eq, class A> void THashTable<V, K, HF, Ex, Eq, A>::copy_from_dynamic(const THashTable& ht) { Y_ASSERT(buckets.size() == ht.buckets.size() && !ht.empty()); - + #ifdef __STL_USE_EXCEPTIONS try { #endif /* __STL_USE_EXCEPTIONS */ @@ -1391,9 +1391,9 @@ void THashTable<V, K, HF, Ex, Eq, A>::copy_from_dynamic(const THashTable& ht) { } namespace NPrivate { - template <class Key> + template <class Key> inline TString MapKeyToString(const Key&) { - return TypeName<Key>(); + return TypeName<Key>(); } TString MapKeyToString(TStringBuf key); @@ -1649,7 +1649,7 @@ public: template <class TheKey> const T& at(const TheKey& key) const { - using namespace ::NPrivate; + using namespace ::NPrivate; const_iterator it = find(key); if (Y_UNLIKELY(it == end())) { @@ -1661,7 +1661,7 @@ public: template <class TheKey> T& at(const TheKey& key) { - using namespace ::NPrivate; + using namespace ::NPrivate; iterator it = find(key); if (Y_UNLIKELY(it == end())) { @@ -1706,14 +1706,14 @@ public: void basic_clear() { rep.basic_clear(); } - void release_nodes() { - rep.release_nodes(); - } + void release_nodes() { + rep.release_nodes(); + } // if (stHash != NULL) bucket_count() must be equal to stHash->bucket_count() template <class KeySaver> int save_for_st(IOutputStream* stream, KeySaver& ks, sthash<int, int, THash<int>, TEqualTo<int>, typename KeySaver::TSizeType>* stHash = nullptr) const { - return rep.template save_for_st<KeySaver>(stream, ks, stHash); + return rep.template save_for_st<KeySaver>(stream, ks, stHash); } public: @@ -1748,12 +1748,12 @@ inline bool operator==(const THashMap<Key, T, HashFcn, EqualKey, Alloc>& hm1, co return true; } -template <class Key, class T, class HashFcn, class EqualKey, class Alloc> +template <class Key, class T, class HashFcn, class EqualKey, class Alloc> inline bool operator!=(const THashMap<Key, T, HashFcn, EqualKey, Alloc>& hm1, const THashMap<Key, T, HashFcn, EqualKey, Alloc>& hm2) { - return !(hm1 == hm2); -} - -template <class Key, class T, class HashFcn, class EqualKey, class Alloc> + return !(hm1 == hm2); +} + +template <class Key, class T, class HashFcn, class EqualKey, class Alloc> class THashMultiMap { private: using ht = THashTable<std::pair<const Key, T>, Key, HashFcn, TSelect1st, EqualKey, Alloc>; @@ -1972,9 +1972,9 @@ public: void basic_clear() { rep.basic_clear(); } - void release_nodes() { - rep.release_nodes(); - } + void release_nodes() { + rep.release_nodes(); + } // if (stHash != NULL) bucket_count() must be equal to stHash->bucket_count() template <class KeySaver> @@ -2017,10 +2017,10 @@ inline bool operator==(const THashMultiMap<Key, T, HF, EqKey, Alloc>& hm1, const return true; } -template <class Key, class T, class HF, class EqKey, class Alloc> +template <class Key, class T, class HF, class EqKey, class Alloc> inline bool operator!=(const THashMultiMap<Key, T, HF, EqKey, Alloc>& hm1, const THashMultiMap<Key, T, HF, EqKey, Alloc>& hm2) { - return !(hm1 == hm2); -} + return !(hm1 == hm2); +} // Cannot name it just 'Hash' because it clashes with too many class members in the code. template <class T> diff --git a/util/generic/hash_primes.cpp b/util/generic/hash_primes.cpp index 656d31e046..1c3594eb46 100644 --- a/util/generic/hash_primes.cpp +++ b/util/generic/hash_primes.cpp @@ -1,7 +1,7 @@ -#include "hash_primes.h" -#include "array_size.h" +#include "hash_primes.h" +#include "array_size.h" #include "algorithm.h" - + /// Order of fields: reciprocal, reciprocal shift, adjacent hint, divisor #if defined(_32_) static constexpr ::NPrivate::THashDivisor PRIME_DIVISORS_HOLDER[]{ diff --git a/util/generic/hash_primes.h b/util/generic/hash_primes.h index 4dc2da0b8f..a7750716b7 100644 --- a/util/generic/hash_primes.h +++ b/util/generic/hash_primes.h @@ -1,5 +1,5 @@ -#pragma once - +#pragma once + #include <util/system/compiler.h> #include <util/system/types.h> @@ -7,14 +7,14 @@ #include <intrin.h> #endif -/** - * Calculates the number of buckets for the hash table that will hold the given - * number of elements. - * - * @param elementCount Number of elements that the hash table will hold. - * @returns Number of buckets, a prime number that is - * greater or equal to `elementCount`. - */ +/** + * Calculates the number of buckets for the hash table that will hold the given + * number of elements. + * + * @param elementCount Number of elements that the hash table will hold. + * @returns Number of buckets, a prime number that is + * greater or equal to `elementCount`. + */ Y_CONST_FUNCTION unsigned long HashBucketCount(unsigned long elementCount); diff --git a/util/generic/hash_set.h b/util/generic/hash_set.h index e8088cf23b..24333e16c4 100644 --- a/util/generic/hash_set.h +++ b/util/generic/hash_set.h @@ -1,11 +1,11 @@ #pragma once -#include "fwd.h" +#include "fwd.h" #include "hash.h" -#include <initializer_list> +#include <initializer_list> #include <utility> - + #undef value_type template <class Value, class HashFcn, class EqualKey, class Alloc> @@ -65,25 +65,25 @@ public: THashSet(std::initializer_list<value_type> list) : rep(list.size(), hasher(), key_equal()) - { - rep.insert_unique(list.begin(), list.end()); - } + { + rep.insert_unique(list.begin(), list.end()); + } THashSet(std::initializer_list<value_type> list, size_type n) - : rep(n, hasher(), key_equal()) - { - rep.insert_unique(list.begin(), list.end()); - } + : rep(n, hasher(), key_equal()) + { + rep.insert_unique(list.begin(), list.end()); + } THashSet(std::initializer_list<value_type> list, size_type n, const hasher& hf) - : rep(n, hf, key_equal()) - { - rep.insert_unique(list.begin(), list.end()); - } + : rep(n, hf, key_equal()) + { + rep.insert_unique(list.begin(), list.end()); + } THashSet(std::initializer_list<value_type> list, size_type n, const hasher& hf, const key_equal& eql) - : rep(n, hf, eql) - { - rep.insert_unique(list.begin(), list.end()); - } - + : rep(n, hf, eql) + { + rep.insert_unique(list.begin(), list.end()); + } + template <class InputIterator> THashSet(InputIterator f, InputIterator l) : rep(0, hasher(), key_equal()) @@ -168,9 +168,9 @@ public: iterator insert(const_iterator, const value_type& obj) { // insert_hint std::pair<mutable_iterator, bool> p = rep.insert_unique(obj); - return p.first; - } - + return p.first; + } + std::pair<iterator, bool> insert_noresize(const value_type& obj) { std::pair<mutable_iterator, bool> p = rep.insert_unique_noresize(obj); return std::pair<iterator, bool>(p.first, p.second); @@ -236,13 +236,13 @@ public: void basic_clear() { rep.basic_clear(); } - void release_nodes() { - rep.release_nodes(); - } + void release_nodes() { + rep.release_nodes(); + } template <class KeySaver> int save_for_st(IOutputStream* stream, KeySaver& ks) const { - return rep.template save_for_st<KeySaver>(stream, ks); + return rep.template save_for_st<KeySaver>(stream, ks); } public: @@ -273,12 +273,12 @@ inline bool operator==(const THashSet<Value, HashFcn, EqualKey, Alloc>& hs1, con return true; } -template <class Value, class HashFcn, class EqualKey, class Alloc> +template <class Value, class HashFcn, class EqualKey, class Alloc> inline bool operator!=(const THashSet<Value, HashFcn, EqualKey, Alloc>& hs1, const THashSet<Value, HashFcn, EqualKey, Alloc>& hs2) { - return !(hs1 == hs2); -} - -template <class Value, class HashFcn, class EqualKey, class Alloc> + return !(hs1 == hs2); +} + +template <class Value, class HashFcn, class EqualKey, class Alloc> class THashMultiSet { private: using ht = THashTable<Value, Value, HashFcn, ::TIdentity, EqualKey, Alloc>; @@ -444,9 +444,9 @@ public: void basic_clear() { rep.basic_clear(); } - void release_nodes() { - rep.release_nodes(); - } + void release_nodes() { + rep.release_nodes(); + } public: void reserve(size_type hint) { @@ -481,8 +481,8 @@ inline bool operator==(const THashMultiSet<Val, HashFcn, EqualKey, Alloc>& hs1, } return true; } - -template <class Val, class HashFcn, class EqualKey, class Alloc> + +template <class Val, class HashFcn, class EqualKey, class Alloc> inline bool operator!=(const THashMultiSet<Val, HashFcn, EqualKey, Alloc>& hs1, const THashMultiSet<Val, HashFcn, EqualKey, Alloc>& hs2) { - return !(hs1 == hs2); -} + return !(hs1 == hs2); +} diff --git a/util/generic/hash_ut.cpp b/util/generic/hash_ut.cpp index 0551d58770..7153d7a742 100644 --- a/util/generic/hash_ut.cpp +++ b/util/generic/hash_ut.cpp @@ -36,7 +36,7 @@ class THashTest: public TTestBase { UNIT_TEST(TestSizeOf); UNIT_TEST(TestInvariants); UNIT_TEST(TestAllocation); - UNIT_TEST(TestInsertCopy); + UNIT_TEST(TestInsertCopy); UNIT_TEST(TestEmplace); UNIT_TEST(TestEmplaceNoresize); UNIT_TEST(TestEmplaceDirect); @@ -49,9 +49,9 @@ class THashTest: public TTestBase { UNIT_TEST(TestHSetEmplaceNoresize); UNIT_TEST(TestHSetEmplaceDirect); UNIT_TEST(TestNonCopyable); - UNIT_TEST(TestValueInitialization); - UNIT_TEST(TestAssignmentClear); - UNIT_TEST(TestReleaseNodes); + UNIT_TEST(TestValueInitialization); + UNIT_TEST(TestAssignmentClear); + UNIT_TEST(TestReleaseNodes); UNIT_TEST(TestAt); UNIT_TEST(TestHMapInitializerList); UNIT_TEST(TestHMMapInitializerList); @@ -87,7 +87,7 @@ protected: void TestSizeOf(); void TestInvariants(); void TestAllocation(); - void TestInsertCopy(); + void TestInsertCopy(); void TestEmplace(); void TestEmplaceNoresize(); void TestEmplaceDirect(); @@ -100,9 +100,9 @@ protected: void TestHMMapEmplaceNoresize(); void TestHMMapEmplaceDirect(); void TestNonCopyable(); - void TestValueInitialization(); - void TestAssignmentClear(); - void TestReleaseNodes(); + void TestValueInitialization(); + void TestAssignmentClear(); + void TestReleaseNodes(); void TestAt(); void TestHMapInitializerList(); void TestHMMapInitializerList(); @@ -218,11 +218,11 @@ void THashTest::TestHMapEqualityOperator() { UNIT_ASSERT(c2 == base); c2["three"] = 3; - UNIT_ASSERT(c2 != base); + UNIT_ASSERT(c2 != base); container c3(base); c3["one"] = 0; - UNIT_ASSERT(c3 != base); + UNIT_ASSERT(c3 != base); } void THashTest::TestHMMapEqualityOperator() { @@ -399,9 +399,9 @@ void THashTest::TestHSetConstructorsAndAssignments() { UNIT_ASSERT_VALUES_EQUAL(0, c2.size()); UNIT_ASSERT_VALUES_EQUAL(4, c3.size()); UNIT_ASSERT(c3.contains(400)); - - container c4 = {1, 2, 3}; - UNIT_ASSERT_VALUES_EQUAL(c4.size(), 3); + + container c4 = {1, 2, 3}; + UNIT_ASSERT_VALUES_EQUAL(c4.size(), 3); UNIT_ASSERT(c4.contains(1)); UNIT_ASSERT(c4.contains(2)); UNIT_ASSERT(c4.contains(3)); @@ -423,7 +423,7 @@ void THashTest::TestHSetSize() { void THashTest::TestHSet2() { THashSet<int, THash<int>, TEqualTo<int>> s; - auto p = s.insert(42); + auto p = s.insert(42); UNIT_ASSERT(p.second); UNIT_ASSERT(*(p.first) == 42); @@ -445,7 +445,7 @@ void THashTest::TestHSetEqualityOperator() { UNIT_ASSERT(c1 == base); c1.insert(3); - UNIT_ASSERT(c1 != base); + UNIT_ASSERT(c1 != base); container c2; c2.insert(2); @@ -454,7 +454,7 @@ void THashTest::TestHSetEqualityOperator() { container c3; c3.insert(1); - UNIT_ASSERT(c3 != base); + UNIT_ASSERT(c3 != base); } void THashTest::TestHMSetConstructorsAndAssignments() { @@ -513,7 +513,7 @@ void THashTest::TestHMSet1() { UNIT_ASSERT(s.count(star) == 1); s.insert(star); UNIT_ASSERT(s.count(star) == 2); - auto i = s.find(char(40)); + auto i = s.find(char(40)); UNIT_ASSERT(i == s.end()); i = s.find(star); @@ -659,7 +659,7 @@ namespace { THash<TString>, TSelectKey, TEqualTo<TString>, - std::allocator<TItemPtr>>; + std::allocator<TItemPtr>>; struct TItemMap: public TItemMapBase { TItemMap() @@ -705,188 +705,188 @@ void THashTest::TestEmpty() { EmptyAndInsertTest<THashMap<int, int>>(std::pair<int, int>(1, 2)); EmptyAndInsertTest<THashMultiMap<int, int>>(std::pair<int, int>(1, 2)); } - -void THashTest::TestDefaultConstructor() { + +void THashTest::TestDefaultConstructor() { THashSet<int> set; - - UNIT_ASSERT(set.begin() == set.end()); - - UNIT_ASSERT(set.find(0) == set.end()); - - auto range = set.equal_range(0); - UNIT_ASSERT(range.first == range.second); -} - -void THashTest::TestSizeOf() { - /* This test checks that we don't waste memory when all functors passed to + + UNIT_ASSERT(set.begin() == set.end()); + + UNIT_ASSERT(set.find(0) == set.end()); + + auto range = set.equal_range(0); + UNIT_ASSERT(range.first == range.second); +} + +void THashTest::TestSizeOf() { + /* This test checks that we don't waste memory when all functors passed to * THashTable are empty. It does rely on knowledge of THashTable internals, - * so if those change, the test will have to be adjusted accordingly. */ - + * so if those change, the test will have to be adjusted accordingly. */ + size_t expectedSize = sizeof(uintptr_t) + 3 * sizeof(size_t); - + UNIT_ASSERT_VALUES_EQUAL(sizeof(THashMap<int, int>), expectedSize); UNIT_ASSERT_VALUES_EQUAL(sizeof(THashMap<std::pair<int, int>, std::pair<int, int>>), expectedSize); -} - -void THashTest::TestInvariants() { +} + +void THashTest::TestInvariants() { std::set<int> reference_set; THashSet<int> set; - - for (int i = 0; i < 1000; i++) { - set.insert(i); - reference_set.insert(i); - } - UNIT_ASSERT_VALUES_EQUAL(set.size(), 1000); - - int count0 = 0; + + for (int i = 0; i < 1000; i++) { + set.insert(i); + reference_set.insert(i); + } + UNIT_ASSERT_VALUES_EQUAL(set.size(), 1000); + + int count0 = 0; for (int i = 0; i < 1000; i++) { - count0 += (set.find(i) != set.end()) ? 1 : 0; + count0 += (set.find(i) != set.end()) ? 1 : 0; } - UNIT_ASSERT_VALUES_EQUAL(count0, 1000); - - int count1 = 0; + UNIT_ASSERT_VALUES_EQUAL(count0, 1000); + + int count1 = 0; for (auto pos = set.begin(); pos != set.end(); pos++) { ++count1; } - UNIT_ASSERT_VALUES_EQUAL(count1, 1000); - - int count2 = 0; + UNIT_ASSERT_VALUES_EQUAL(count1, 1000); + + int count2 = 0; for (const int& value : set) { - count2 += (reference_set.find(value) != reference_set.end()) ? 1 : 0; + count2 += (reference_set.find(value) != reference_set.end()) ? 1 : 0; } - UNIT_ASSERT_VALUES_EQUAL(count2, 1000); -} - -struct TAllocatorCounters { - TAllocatorCounters() - : Allocations(0) - , Deallocations(0) + UNIT_ASSERT_VALUES_EQUAL(count2, 1000); +} + +struct TAllocatorCounters { + TAllocatorCounters() + : Allocations(0) + , Deallocations(0) { } - - ~TAllocatorCounters() { + + ~TAllocatorCounters() { std::allocator<char> allocator; - - /* Release whatever was (intentionally) leaked. */ + + /* Release whatever was (intentionally) leaked. */ for (const auto& chunk : Chunks) { - allocator.deallocate(static_cast<char*>(chunk.first), chunk.second); + allocator.deallocate(static_cast<char*>(chunk.first), chunk.second); } - } - - size_t Allocations; - size_t Deallocations; + } + + size_t Allocations; + size_t Deallocations; TSet<std::pair<void*, size_t>> Chunks; -}; - +}; + template <class T> class TCountingAllocator: public std::allocator<T> { using base_type = std::allocator<T>; -public: +public: using size_type = typename base_type::size_type; - + template <class Other> - struct rebind { - using other = TCountingAllocator<Other>; - }; - - TCountingAllocator() + struct rebind { + using other = TCountingAllocator<Other>; + }; + + TCountingAllocator() : Counters_(nullptr) { } - - TCountingAllocator(TAllocatorCounters* counters) + + TCountingAllocator(TAllocatorCounters* counters) : Counters_(counters) { Y_ASSERT(counters); - } - + } + template <class Other> - TCountingAllocator(const TCountingAllocator<Other>& other) + TCountingAllocator(const TCountingAllocator<Other>& other) : Counters_(other.Counters) { } - + T* allocate(size_type n) { auto result = base_type::allocate(n); - + if (Counters_) { ++Counters_->Allocations; Counters_->Chunks.emplace(result, n * sizeof(T)); - } - - return result; - } - + } + + return result; + } + void deallocate(T* p, size_type n) { if (Counters_) { ++Counters_->Deallocations; Counters_->Chunks.erase(std::make_pair(p, n * sizeof(T))); - } - - base_type::deallocate(p, n); - } - -private: + } + + base_type::deallocate(p, n); + } + +private: TAllocatorCounters* Counters_; -}; - -void THashTest::TestAllocation() { - TAllocatorCounters counters; - +}; + +void THashTest::TestAllocation() { + TAllocatorCounters counters; + using int_set = THashSet<int, THash<int>, TEqualTo<int>, TCountingAllocator<int>>; - - { - int_set set0(&counters); - int_set set1(set0); - set0.clear(); - int_set set2(&counters); - set2 = set1; - UNIT_ASSERT_VALUES_EQUAL(counters.Allocations, 0); /* Copying around null sets should not trigger allocations. */ - - set0.insert(0); - UNIT_ASSERT_VALUES_EQUAL(counters.Allocations, 2); /* One for buckets array, one for a new node. */ - - set0.clear(); - set1 = set0; - int_set set3(set0); - UNIT_ASSERT_VALUES_EQUAL(counters.Allocations, 2); /* Copying from an empty set with allocated buckets should not trigger allocations. */ - + + { + int_set set0(&counters); + int_set set1(set0); + set0.clear(); + int_set set2(&counters); + set2 = set1; + UNIT_ASSERT_VALUES_EQUAL(counters.Allocations, 0); /* Copying around null sets should not trigger allocations. */ + + set0.insert(0); + UNIT_ASSERT_VALUES_EQUAL(counters.Allocations, 2); /* One for buckets array, one for a new node. */ + + set0.clear(); + set1 = set0; + int_set set3(set0); + UNIT_ASSERT_VALUES_EQUAL(counters.Allocations, 2); /* Copying from an empty set with allocated buckets should not trigger allocations. */ + for (int i = 0; i < 1000; i++) { - set0.insert(i); + set0.insert(i); } - size_t allocations = counters.Allocations; - set0.clear(); - UNIT_ASSERT_VALUES_EQUAL(counters.Allocations, allocations); /* clear() should not trigger allocations. */ - } - - UNIT_ASSERT_VALUES_EQUAL(counters.Allocations, counters.Deallocations); -} - + size_t allocations = counters.Allocations; + set0.clear(); + UNIT_ASSERT_VALUES_EQUAL(counters.Allocations, allocations); /* clear() should not trigger allocations. */ + } + + UNIT_ASSERT_VALUES_EQUAL(counters.Allocations, counters.Deallocations); +} + template <int Value> -class TNonCopyableInt { -public: +class TNonCopyableInt { +public: explicit TNonCopyableInt(int) { } - - TNonCopyableInt() = delete; - TNonCopyableInt(const TNonCopyableInt&) = delete; - TNonCopyableInt(TNonCopyable&&) = delete; + + TNonCopyableInt() = delete; + TNonCopyableInt(const TNonCopyableInt&) = delete; + TNonCopyableInt(TNonCopyable&&) = delete; TNonCopyableInt& operator=(const TNonCopyable&) = delete; TNonCopyableInt& operator=(TNonCopyable&&) = delete; - - operator int() const { - return Value; - } -}; - -void THashTest::TestInsertCopy() { + + operator int() const { + return Value; + } +}; + +void THashTest::TestInsertCopy() { THashMap<int, int> hash; - - /* Insertion should not make copies of the provided key. */ - hash[TNonCopyableInt<0>(0)] = 0; -} - + + /* Insertion should not make copies of the provided key. */ + hash[TNonCopyableInt<0>(0)] = 0; +} + void THashTest::TestEmplace() { using hash_t = THashMap<int, TNonCopyableInt<0>>; hash_t hash; @@ -1057,60 +1057,60 @@ void THashTest::TestNonCopyable() { UNIT_ASSERT_VALUES_EQUAL(static_cast<int>(not_inserted), 0); } -void THashTest::TestValueInitialization() { +void THashTest::TestValueInitialization() { THashMap<int, int> hash; - + int& value = hash[0]; - - /* Implicitly inserted values should be value-initialized. */ - UNIT_ASSERT_VALUES_EQUAL(value, 0); -} - -void THashTest::TestAssignmentClear() { - /* This one tests that assigning an empty hash resets the buckets array. - * See operator= for details. */ - + + /* Implicitly inserted values should be value-initialized. */ + UNIT_ASSERT_VALUES_EQUAL(value, 0); +} + +void THashTest::TestAssignmentClear() { + /* This one tests that assigning an empty hash resets the buckets array. + * See operator= for details. */ + THashMap<int, int> hash; - size_t emptyBucketCount = hash.bucket_count(); - - for (int i = 0; i < 100; i++) { - hash[i] = i; - } - + size_t emptyBucketCount = hash.bucket_count(); + + for (int i = 0; i < 100; i++) { + hash[i] = i; + } + hash = THashMap<int, int>(); - - UNIT_ASSERT_VALUES_EQUAL(hash.bucket_count(), emptyBucketCount); -} - -void THashTest::TestReleaseNodes() { - TAllocatorCounters counters; + + UNIT_ASSERT_VALUES_EQUAL(hash.bucket_count(), emptyBucketCount); +} + +void THashTest::TestReleaseNodes() { + TAllocatorCounters counters; using TIntSet = THashSet<int, THash<int>, TEqualTo<int>, TCountingAllocator<int>>; - - TIntSet set(&counters); + + TIntSet set(&counters); for (int i = 0; i < 3; i++) { - set.insert(i); + set.insert(i); } - UNIT_ASSERT_VALUES_EQUAL(counters.Allocations, 4); - - set.release_nodes(); - UNIT_ASSERT_VALUES_EQUAL(counters.Allocations, 4); - UNIT_ASSERT_VALUES_EQUAL(set.size(), 0); - + UNIT_ASSERT_VALUES_EQUAL(counters.Allocations, 4); + + set.release_nodes(); + UNIT_ASSERT_VALUES_EQUAL(counters.Allocations, 4); + UNIT_ASSERT_VALUES_EQUAL(set.size(), 0); + for (int i = 10; i < 13; i++) { - set.insert(i); + set.insert(i); } - UNIT_ASSERT_VALUES_EQUAL(counters.Allocations, 7); + UNIT_ASSERT_VALUES_EQUAL(counters.Allocations, 7); UNIT_ASSERT(set.contains(10)); UNIT_ASSERT(!set.contains(0)); - - set.basic_clear(); - UNIT_ASSERT_VALUES_EQUAL(counters.Deallocations, 3); - - TIntSet set2; - set2.release_nodes(); - set2.insert(1); - UNIT_ASSERT_VALUES_EQUAL(set2.size(), 1); -} + + set.basic_clear(); + UNIT_ASSERT_VALUES_EQUAL(counters.Deallocations, 3); + + TIntSet set2; + set2.release_nodes(); + set2.insert(1); + UNIT_ASSERT_VALUES_EQUAL(set2.size(), 1); +} void THashTest::TestAt() { #define TEST_AT_THROWN_EXCEPTION(SRC_TYPE, DST_TYPE, KEY_TYPE, KEY, MESSAGE) \ @@ -1121,7 +1121,7 @@ void THashTest::TestAt() { testMap.at(testKey); \ UNIT_ASSERT_C(false, "THashMap::at(\"" << KEY << "\") should throw"); \ } catch (const yexception& e) { \ - UNIT_ASSERT_C(e.AsStrBuf().Contains(MESSAGE), "Incorrect exception description: got \"" << e.what() << "\", expected: \"" << MESSAGE << "\""); \ + UNIT_ASSERT_C(e.AsStrBuf().Contains(MESSAGE), "Incorrect exception description: got \"" << e.what() << "\", expected: \"" << MESSAGE << "\""); \ } catch (...) { \ UNIT_ASSERT_C(false, "THashMap::at(\"" << KEY << "\") should throw yexception"); \ } \ @@ -1132,14 +1132,14 @@ void THashTest::TestAt() { TEST_AT_THROWN_EXCEPTION(TString, TString, TStringBuf, "111", "111"); TEST_AT_THROWN_EXCEPTION(TString, TString, const TStringBuf, "111", "111"); TEST_AT_THROWN_EXCEPTION(TStringBuf, TStringBuf, const char*, "111", "111"); - TEST_AT_THROWN_EXCEPTION(int, int, short, 11, "11"); - TEST_AT_THROWN_EXCEPTION(int, int, int, -1, "-1"); - TEST_AT_THROWN_EXCEPTION(int, int, long, 111, "111"); - TEST_AT_THROWN_EXCEPTION(int, int, long long, -1000000000000ll, "-1000000000000"); - TEST_AT_THROWN_EXCEPTION(int, int, unsigned short, 11, "11"); - TEST_AT_THROWN_EXCEPTION(int, int, unsigned int, 2, "2"); - TEST_AT_THROWN_EXCEPTION(int, int, unsigned long, 131, "131"); - TEST_AT_THROWN_EXCEPTION(int, int, unsigned long long, 1000000000000ll, "1000000000000"); + TEST_AT_THROWN_EXCEPTION(int, int, short, 11, "11"); + TEST_AT_THROWN_EXCEPTION(int, int, int, -1, "-1"); + TEST_AT_THROWN_EXCEPTION(int, int, long, 111, "111"); + TEST_AT_THROWN_EXCEPTION(int, int, long long, -1000000000000ll, "-1000000000000"); + TEST_AT_THROWN_EXCEPTION(int, int, unsigned short, 11, "11"); + TEST_AT_THROWN_EXCEPTION(int, int, unsigned int, 2, "2"); + TEST_AT_THROWN_EXCEPTION(int, int, unsigned long, 131, "131"); + TEST_AT_THROWN_EXCEPTION(int, int, unsigned long long, 1000000000000ll, "1000000000000"); char key[] = {11, 12, 0, 1, 2, 11, 0}; TEST_AT_THROWN_EXCEPTION(TString, TString, char*, key, "\\x0B\\x0C"); diff --git a/util/generic/is_in_ut.cpp b/util/generic/is_in_ut.cpp index c668bce807..2cb9d7c6c4 100644 --- a/util/generic/is_in_ut.cpp +++ b/util/generic/is_in_ut.cpp @@ -72,19 +72,19 @@ Y_UNIT_TEST_SUITE(TIsIn) { } Y_UNIT_TEST(IsInInitListTest) { - const char* abc = "abc"; - const char* def = "def"; - + const char* abc = "abc"; + const char* def = "def"; + UNIT_ASSERT(IsIn({6, 2, 12}, 6)); UNIT_ASSERT(IsIn({6, 2, 12}, 2)); UNIT_ASSERT(!IsIn({6, 2, 12}, 7)); UNIT_ASSERT(IsIn({6}, 6)); UNIT_ASSERT(!IsIn({6}, 7)); - UNIT_ASSERT(!IsIn(std::initializer_list<int>(), 6)); + UNIT_ASSERT(!IsIn(std::initializer_list<int>(), 6)); UNIT_ASSERT(IsIn({TStringBuf("abc"), TStringBuf("def")}, TStringBuf("abc"))); UNIT_ASSERT(IsIn({TStringBuf("abc"), TStringBuf("def")}, TStringBuf("def"))); UNIT_ASSERT(IsIn({"abc", "def"}, TStringBuf("def"))); - UNIT_ASSERT(IsIn({abc, def}, def)); // direct pointer comparison + UNIT_ASSERT(IsIn({abc, def}, def)); // direct pointer comparison UNIT_ASSERT(!IsIn({TStringBuf("abc"), TStringBuf("def")}, TStringBuf("ghi"))); UNIT_ASSERT(!IsIn({"abc", "def"}, TStringBuf("ghi"))); UNIT_ASSERT(!IsIn({"abc", "def"}, TString("ghi"))); diff --git a/util/generic/iterator.h b/util/generic/iterator.h index 19e9d20976..0d5587691c 100644 --- a/util/generic/iterator.h +++ b/util/generic/iterator.h @@ -30,42 +30,42 @@ namespace NStlIterator { }; } // namespace NStlIterator -/** - * Range adaptor that turns a derived class with a Java-style iteration - * interface into an STL range. - * - * Derived class is expected to define: - * \code - * TSomething* Next(); - * \endcode - * - * `Next()` returning `nullptr` signals end of range. Note that you can also use - * pointer-like types instead of actual pointers (e.g. `TAtomicSharedPtr`). - * - * Since iteration state is stored inside the derived class, the resulting range - * is an input range (works for single pass algorithms only). Technically speaking, - * if you're returning a non-const pointer from `Next`, it can also work as an output range. - * - * Example usage: - * \code - * class TSquaresGenerator: public TInputRangeAdaptor<TSquaresGenerator> { - * public: - * const double* Next() { - * Current_ = State_ * State_; - * State_ += 1.0; - * // Never return nullptr => we have infinite range! - * return &Current_; - * } - * - * private: - * double State_ = 0.0; - * double Current_ = 0.0; - * } - * \endcode - */ +/** + * Range adaptor that turns a derived class with a Java-style iteration + * interface into an STL range. + * + * Derived class is expected to define: + * \code + * TSomething* Next(); + * \endcode + * + * `Next()` returning `nullptr` signals end of range. Note that you can also use + * pointer-like types instead of actual pointers (e.g. `TAtomicSharedPtr`). + * + * Since iteration state is stored inside the derived class, the resulting range + * is an input range (works for single pass algorithms only). Technically speaking, + * if you're returning a non-const pointer from `Next`, it can also work as an output range. + * + * Example usage: + * \code + * class TSquaresGenerator: public TInputRangeAdaptor<TSquaresGenerator> { + * public: + * const double* Next() { + * Current_ = State_ * State_; + * State_ += 1.0; + * // Never return nullptr => we have infinite range! + * return &Current_; + * } + * + * private: + * double State_ = 0.0; + * double Current_ = 0.0; + * } + * \endcode + */ template <class TSlave> -class TInputRangeAdaptor { -public: // TODO: private +class TInputRangeAdaptor { +public: // TODO: private class TIterator { public: static constexpr bool IsNoexceptNext = noexcept(std::declval<TSlave>().Next()); @@ -74,7 +74,7 @@ public: // TODO: private using pointer = decltype(std::declval<TSlave>().Next()); using reference = decltype(*std::declval<TSlave>().Next()); using value_type = std::remove_cv_t<std::remove_reference_t<reference>>; - using iterator_category = std::input_iterator_tag; + using iterator_category = std::input_iterator_tag; inline TIterator() noexcept : Slave_(nullptr) @@ -96,11 +96,11 @@ public: // TODO: private return !(*this == it); } - inline pointer operator->() const noexcept { + inline pointer operator->() const noexcept { return Cur_; } - inline reference operator*() const noexcept { + inline reference operator*() const noexcept { return *Cur_; } @@ -128,12 +128,12 @@ public: } }; -/** - * Transform given reverse iterator into forward iterator pointing to the same element. +/** + * Transform given reverse iterator into forward iterator pointing to the same element. * - * @see http://stackoverflow.com/a/1830240 + * @see http://stackoverflow.com/a/1830240 */ template <class TIterator> -auto ToForwardIterator(TIterator iter) { +auto ToForwardIterator(TIterator iter) { return std::next(iter).base(); } diff --git a/util/generic/iterator_range.h b/util/generic/iterator_range.h index 9f4d02da29..1011553e37 100644 --- a/util/generic/iterator_range.h +++ b/util/generic/iterator_range.h @@ -1,10 +1,10 @@ -#pragma once - +#pragma once + #include <util/system/yassert.h> -#include <iterator> -#include <utility> - +#include <iterator> +#include <utility> + template <typename TBegin, typename TEnd = TBegin> struct TIteratorRange { using TElement = std::remove_reference_t<decltype(*std::declval<TBegin>())>; @@ -40,35 +40,35 @@ private: template <class TIterator> class TIteratorRange<TIterator, TIterator> { -public: +public: using iterator = TIterator; using const_iterator = TIterator; using value_type = typename std::iterator_traits<iterator>::value_type; using reference = typename std::iterator_traits<iterator>::reference; using const_reference = typename std::iterator_traits<const_iterator>::reference; using difference_type = typename std::iterator_traits<iterator>::difference_type; - using size_type = std::size_t; - + using size_type = std::size_t; + TIteratorRange() : Begin_() , End_() { } - + TIteratorRange(TIterator begin, TIterator end) - : Begin_(begin) - , End_(end) + : Begin_(begin) + , End_(end) { } - + TIterator begin() const { - return Begin_; - } - + return Begin_; + } + TIterator end() const { - return End_; - } - + return End_; + } + Y_PURE_FUNCTION bool empty() const { return Begin_ == End_; } @@ -83,20 +83,20 @@ public: return *(Begin_ + at); } -private: +private: TIterator Begin_; TIterator End_; -}; - +}; + template <class TIterator> TIteratorRange<TIterator> MakeIteratorRange(TIterator begin, TIterator end) { return TIteratorRange<TIterator>(begin, end); -} - +} + template <class TIterator> TIteratorRange<TIterator> MakeIteratorRange(const std::pair<TIterator, TIterator>& range) { return TIteratorRange<TIterator>(range.first, range.second); -} +} template <class TBegin, class TEnd> TIteratorRange<TBegin, TEnd> MakeIteratorRange(TBegin begin, TEnd end) { diff --git a/util/generic/list.h b/util/generic/list.h index 7b0b8ffc72..56d097af80 100644 --- a/util/generic/list.h +++ b/util/generic/list.h @@ -1,17 +1,17 @@ #pragma once -#include "fwd.h" - +#include "fwd.h" + #include <util/memory/alloc.h> #include <initializer_list> #include <list> -#include <memory> +#include <memory> #include <utility> template <class T, class A> class TList: public std::list<T, TReboundAllocator<A, T>> { - using TBase = std::list<T, TReboundAllocator<A, T>>; + using TBase = std::list<T, TReboundAllocator<A, T>>; public: using TBase::TBase; diff --git a/util/generic/map.h b/util/generic/map.h index b5001b56c0..0a4321383b 100644 --- a/util/generic/map.h +++ b/util/generic/map.h @@ -1,15 +1,15 @@ #pragma once -#include "fwd.h" +#include "fwd.h" #include "mapfindptr.h" -#include <util/str_stl.h> +#include <util/str_stl.h> #include <util/memory/alloc.h> - + #include <utility> #include <initializer_list> #include <map> -#include <memory> +#include <memory> template <class K, class V, class Less, class A> class TMap: public std::map<K, V, Less, TReboundAllocator<A, std::pair<const K, V>>>, public TMapOps<TMap<K, V, Less, A>> { diff --git a/util/generic/mapfindptr.h b/util/generic/mapfindptr.h index bc10cac60f..b44585af48 100644 --- a/util/generic/mapfindptr.h +++ b/util/generic/mapfindptr.h @@ -1,7 +1,7 @@ #pragma once -#include <type_traits> - +#include <type_traits> + /** MapFindPtr usage: if (T* value = MapFindPtr(myMap, someKey) { @@ -11,50 +11,50 @@ if (T* value = MapFindPtr(myMap, someKey) { */ template <class Map, class K> -inline auto MapFindPtr(Map& map, const K& key) { +inline auto MapFindPtr(Map& map, const K& key) { auto i = map.find(key); return (i == map.end() ? nullptr : &i->second); } template <class Map, class K> -inline auto MapFindPtr(const Map& map, const K& key) { +inline auto MapFindPtr(const Map& map, const K& key) { auto i = map.find(key); return (i == map.end() ? nullptr : &i->second); } /** helper for THashMap/TMap */ -template <class Derived> +template <class Derived> struct TMapOps { template <class K> - inline auto FindPtr(const K& key) { + inline auto FindPtr(const K& key) { return MapFindPtr(static_cast<Derived&>(*this), key); } template <class K> - inline auto FindPtr(const K& key) const { + inline auto FindPtr(const K& key) const { return MapFindPtr(static_cast<const Derived&>(*this), key); } - template <class K, class DefaultValue> - inline auto Value(const K& key, const DefaultValue& defaultValue) const -> std::remove_reference_t<decltype(*this->FindPtr(key))> { - if (auto found = FindPtr(key)) { + template <class K, class DefaultValue> + inline auto Value(const K& key, const DefaultValue& defaultValue) const -> std::remove_reference_t<decltype(*this->FindPtr(key))> { + if (auto found = FindPtr(key)) { return *found; } - return defaultValue; + return defaultValue; } - - template <class K, class V> - inline const V& ValueRef(const K& key, V& defaultValue) const { - static_assert(std::is_same<std::remove_const_t<V>, typename Derived::mapped_type>::value, "Passed default value must have the same type as the underlying map's mapped_type."); - - if (auto found = FindPtr(key)) { - return *found; - } - return defaultValue; - } - - template <class K, class V> - inline const V& ValueRef(const K& key, V&& defaultValue) const = delete; + + template <class K, class V> + inline const V& ValueRef(const K& key, V& defaultValue) const { + static_assert(std::is_same<std::remove_const_t<V>, typename Derived::mapped_type>::value, "Passed default value must have the same type as the underlying map's mapped_type."); + + if (auto found = FindPtr(key)) { + return *found; + } + return defaultValue; + } + + template <class K, class V> + inline const V& ValueRef(const K& key, V&& defaultValue) const = delete; }; diff --git a/util/generic/mapfindptr_ut.cpp b/util/generic/mapfindptr_ut.cpp index 613da7a96b..75443e8b62 100644 --- a/util/generic/mapfindptr_ut.cpp +++ b/util/generic/mapfindptr_ut.cpp @@ -14,7 +14,7 @@ Y_UNIT_TEST_SUITE(TMapFindPtrTest) { TTestMap a; a[42] = "cat"; - UNIT_ASSERT(a.FindPtr(42)); + UNIT_ASSERT(a.FindPtr(42)); UNIT_ASSERT_EQUAL(*a.FindPtr(42), "cat"); UNIT_ASSERT_EQUAL(a.FindPtr(0), nullptr); @@ -22,7 +22,7 @@ Y_UNIT_TEST_SUITE(TMapFindPtrTest) { if (TString* p = a.FindPtr(42)) { *p = "dog"; } - UNIT_ASSERT(a.FindPtr(42)); + UNIT_ASSERT(a.FindPtr(42)); UNIT_ASSERT_EQUAL(*a.FindPtr(42), "dog"); //test const-overloaded functions too @@ -42,26 +42,26 @@ Y_UNIT_TEST_SUITE(TMapFindPtrTest) { UNIT_ASSERT(m.FindPtr(TStringBuf("x"))); UNIT_ASSERT_EQUAL(*m.FindPtr(TStringBuf("x")), 2); } - + Y_UNIT_TEST(TestValue) { - TTestMap a; - - a[1] = "lol"; - - UNIT_ASSERT_VALUES_EQUAL(a.Value(1, "123"), "lol"); - UNIT_ASSERT_VALUES_EQUAL(a.Value(2, "123"), "123"); - } - + TTestMap a; + + a[1] = "lol"; + + UNIT_ASSERT_VALUES_EQUAL(a.Value(1, "123"), "lol"); + UNIT_ASSERT_VALUES_EQUAL(a.Value(2, "123"), "123"); + } + Y_UNIT_TEST(TestValueRef) { - TTestMap a; - - a[1] = "lol"; - + TTestMap a; + + a[1] = "lol"; + const TString str123 = "123"; TString str1234 = "1234"; - - UNIT_ASSERT_VALUES_EQUAL(a.ValueRef(1, str123), "lol"); - UNIT_ASSERT_VALUES_EQUAL(a.ValueRef(2, str123), "123"); - UNIT_ASSERT_VALUES_EQUAL(a.ValueRef(3, str1234), "1234"); - } -} + + UNIT_ASSERT_VALUES_EQUAL(a.ValueRef(1, str123), "lol"); + UNIT_ASSERT_VALUES_EQUAL(a.ValueRef(2, str123), "123"); + UNIT_ASSERT_VALUES_EQUAL(a.ValueRef(3, str1234), "1234"); + } +} diff --git a/util/generic/mem_copy_ut.cpp b/util/generic/mem_copy_ut.cpp index 8b55a11cf6..9d69ec108f 100644 --- a/util/generic/mem_copy_ut.cpp +++ b/util/generic/mem_copy_ut.cpp @@ -74,10 +74,10 @@ Y_UNIT_TEST_SUITE(TestMemCopy) { template <class T> inline void FillX(T* b, T* e) { - int tmp = 0; + int tmp = 0; while (b != e) { - (b++)->X = ++tmp; + (b++)->X = ++tmp; } } @@ -105,9 +105,9 @@ Y_UNIT_TEST_SUITE(TestMemCopy) { } Y_UNIT_TEST(TestEmpty) { - char* tmp = nullptr; + char* tmp = nullptr; - UNIT_ASSERT(MemCopy(tmp, tmp, 0) == nullptr); - UNIT_ASSERT(MemMove(tmp, tmp, 0) == nullptr); + UNIT_ASSERT(MemCopy(tmp, tmp, 0) == nullptr); + UNIT_ASSERT(MemMove(tmp, tmp, 0) == nullptr); } } diff --git a/util/generic/noncopyable.h b/util/generic/noncopyable.h index c007934133..655436a70d 100644 --- a/util/generic/noncopyable.h +++ b/util/generic/noncopyable.h @@ -1,20 +1,20 @@ #pragma once -/** - * @class TNonCopyable +/** + * @class TNonCopyable * - * Inherit your class from `TNonCopyable` if you want to make it noncopyable. + * Inherit your class from `TNonCopyable` if you want to make it noncopyable. * - * Example usage: - * @code - * class Foo: private TNonCopyable { - * // ... - * }; - * @endcode + * Example usage: + * @code + * class Foo: private TNonCopyable { + * // ... + * }; + * @endcode */ namespace NNonCopyable { // protection from unintended ADL - struct TNonCopyable { + struct TNonCopyable { TNonCopyable(const TNonCopyable&) = delete; TNonCopyable& operator=(const TNonCopyable&) = delete; @@ -22,7 +22,7 @@ namespace NNonCopyable { // protection from unintended ADL ~TNonCopyable() = default; }; - struct TMoveOnly { + struct TMoveOnly { TMoveOnly(TMoveOnly&&) noexcept = default; TMoveOnly& operator=(TMoveOnly&&) noexcept = default; diff --git a/util/generic/object_counter.h b/util/generic/object_counter.h index 5257afa2e6..9b0710e971 100644 --- a/util/generic/object_counter.h +++ b/util/generic/object_counter.h @@ -2,20 +2,20 @@ #include <util/system/atomic.h> -/** - * Simple thread-safe per-class counter that can be used to make sure you don't - * have any leaks in your code, or for statistical purposes. - * - * Example usage: - * \code - * class TMyClass: public TObjectCounter<TMyClass> { - * // ... - * }; - * - * // In your code: - * Cerr << "TMyClass instances in use: " << TMyClass::ObjectCount() << Endl; - * \endcode - */ +/** + * Simple thread-safe per-class counter that can be used to make sure you don't + * have any leaks in your code, or for statistical purposes. + * + * Example usage: + * \code + * class TMyClass: public TObjectCounter<TMyClass> { + * // ... + * }; + * + * // In your code: + * Cerr << "TMyClass instances in use: " << TMyClass::ObjectCount() << Endl; + * \endcode + */ template <class T> class TObjectCounter { public: @@ -35,12 +35,12 @@ public: return AtomicGet(Count_); } - /** - * Resets object count. Mainly for tests, as you don't want to do this in - * your code and then end up with negative counts. - * - * \returns Current object count. - */ + /** + * Resets object count. Mainly for tests, as you don't want to do this in + * your code and then end up with negative counts. + * + * \returns Current object count. + */ static inline long ResetObjectCount() noexcept { return AtomicSwap(&Count_, 0); } diff --git a/util/generic/ptr.h b/util/generic/ptr.h index 19db0e3ec5..8483d382b6 100644 --- a/util/generic/ptr.h +++ b/util/generic/ptr.h @@ -1,6 +1,6 @@ #pragma once -#include "fwd.h" +#include "fwd.h" #include "utility.h" #include "intrlist.h" #include "refcount.h" @@ -160,7 +160,7 @@ template <class Base> class TPointerBase<Base, void>: public TPointerCommon<Base, void> { }; -template <class T, class D> +template <class T, class D> class TAutoPtr: public TPointerBase<TAutoPtr<T, D>, T> { public: inline TAutoPtr(T* t = nullptr) noexcept @@ -197,9 +197,9 @@ public: } inline void Reset() noexcept { - Destroy(); - } - + Destroy(); + } + inline void Destroy() noexcept { Reset(nullptr); } @@ -229,7 +229,7 @@ private: mutable T* T_; }; -template <class T, class D> +template <class T, class D> class THolder: public TPointerBase<THolder<T, D>, T> { public: constexpr THolder() noexcept @@ -296,9 +296,9 @@ public: } inline void Reset() noexcept { - Destroy(); - } - + Destroy(); + } + inline void Swap(THolder& r) noexcept { DoSwap(T_, r.T_); } @@ -355,7 +355,7 @@ template <typename T, typename... Args> * and we get methods Ref() && UnRef() with * proper destruction of last UnRef() */ -template <class T, class C, class D> +template <class T, class C, class D> class TRefCounted { public: inline TRefCounted(long initval = 0) noexcept @@ -551,9 +551,9 @@ public: } inline void Reset() noexcept { - Drop(); - } - + Drop(); + } + inline T* Get() const noexcept { return T_; } @@ -671,9 +671,9 @@ public: } inline void Reset() noexcept { - Drop(); - } - + Drop(); + } + inline const T* Get() const noexcept { return T_; } @@ -788,7 +788,7 @@ template <typename T, class Ops = TDefaultIntrusivePtrOps<T>, typename... Args> return new T{std::forward<Args>(args)...}; } -template <class T, class C, class D> +template <class T, class C, class D> class TSharedPtr: public TPointerBase<TSharedPtr<T, C, D>, T> { template <class TT, class CC, class DD> friend class TSharedPtr; @@ -870,9 +870,9 @@ public: } inline void Reset() noexcept { - Drop(); - } - + Drop(); + } + inline void Drop() noexcept { TSharedPtr().Swap(*this); } @@ -982,7 +982,7 @@ public: } }; -template <class T, class C, class D> +template <class T, class C, class D> class TCopyPtr: public TPointerBase<TCopyPtr<T, C, D>, T> { public: inline TCopyPtr(T* t = nullptr) noexcept @@ -1023,9 +1023,9 @@ public: } inline void Reset() noexcept { - Destroy(); - } - + Destroy(); + } + inline void Destroy() noexcept { Reset(nullptr); } @@ -1098,10 +1098,10 @@ public: p.Swap(*this); } - inline void Reset() { - T_.Reset(); - } - + inline void Reset() { + T_.Reset(); + } + #ifdef __cpp_impl_three_way_comparison template <class Other> inline bool operator==(const Other& p) const noexcept { diff --git a/util/generic/queue.h b/util/generic/queue.h index f5959f68f2..992645e004 100644 --- a/util/generic/queue.h +++ b/util/generic/queue.h @@ -1,6 +1,6 @@ #pragma once -#include "fwd.h" +#include "fwd.h" #include "deque.h" #include "vector.h" #include "utility.h" @@ -9,7 +9,7 @@ #include <queue> -template <class T, class S> +template <class T, class S> class TQueue: public std::queue<T, S> { using TBase = std::queue<T, S>; @@ -33,7 +33,7 @@ public: } }; -template <class T, class S, class C> +template <class T, class S, class C> class TPriorityQueue: public std::priority_queue<T, S, C> { using TBase = std::priority_queue<T, S, C>; diff --git a/util/generic/set.h b/util/generic/set.h index 4c437ca26f..43325e7bd5 100644 --- a/util/generic/set.h +++ b/util/generic/set.h @@ -1,18 +1,18 @@ #pragma once -#include "fwd.h" - -#include <util/str_stl.h> +#include "fwd.h" + +#include <util/str_stl.h> #include <util/memory/alloc.h> #include <initializer_list> -#include <memory> +#include <memory> #include <set> template <class K, class L, class A> class TSet: public std::set<K, L, TReboundAllocator<A, K>> { public: - using TBase = std::set<K, L, TReboundAllocator<A, K>>; + using TBase = std::set<K, L, TReboundAllocator<A, K>>; using TBase::TBase; inline explicit operator bool() const noexcept { @@ -25,10 +25,10 @@ public: } }; -template <class K, class L, class A> +template <class K, class L, class A> class TMultiSet: public std::multiset<K, L, TReboundAllocator<A, K>> { public: - using TBase = std::multiset<K, L, TReboundAllocator<A, K>>; + using TBase = std::multiset<K, L, TReboundAllocator<A, K>>; using TBase::TBase; inline explicit operator bool() const noexcept { diff --git a/util/generic/stack.h b/util/generic/stack.h index dbcbf2b5c9..6bfa5f90a4 100644 --- a/util/generic/stack.h +++ b/util/generic/stack.h @@ -1,11 +1,11 @@ #pragma once -#include "fwd.h" +#include "fwd.h" #include "deque.h" #include <stack> -template <class T, class S> +template <class T, class S> class TStack: public std::stack<T, S> { using TBase = std::stack<T, S>; diff --git a/util/generic/store_policy.h b/util/generic/store_policy.h index 148821c70c..d8a4b530ae 100644 --- a/util/generic/store_policy.h +++ b/util/generic/store_policy.h @@ -71,10 +71,10 @@ struct TRefPolicy { }; /** - * Storage class that can be handy for implementing proxies / adaptors that can - * accept both lvalues and rvalues. In the latter case it's often required to - * extend the lifetime of the passed rvalue, and the only option is to store it - * in your proxy / adaptor. + * Storage class that can be handy for implementing proxies / adaptors that can + * accept both lvalues and rvalues. In the latter case it's often required to + * extend the lifetime of the passed rvalue, and the only option is to store it + * in your proxy / adaptor. * * Example usage: * \code @@ -87,28 +87,28 @@ struct TRefPolicy { * template<class T> * TProxy<T> MakeProxy(T&& value) { * // Rvalues are automagically moved-from, and stored inside the proxy. - * return {std::forward<T>(value)}; + * return {std::forward<T>(value)}; * } * \endcode * * Look at `Reversed` in `adaptor.h` for real example. */ -template <class T, bool IsReference = std::is_reference<T>::value> +template <class T, bool IsReference = std::is_reference<T>::value> struct TAutoEmbedOrPtrPolicy: TPtrPolicy<std::remove_reference_t<T>> { - using TBase = TPtrPolicy<std::remove_reference_t<T>>; + using TBase = TPtrPolicy<std::remove_reference_t<T>>; - TAutoEmbedOrPtrPolicy(T& reference) - : TBase(&reference) + TAutoEmbedOrPtrPolicy(T& reference) + : TBase(&reference) { } }; -template <class T> +template <class T> struct TAutoEmbedOrPtrPolicy<T, false>: TEmbedPolicy<T> { - using TBase = TEmbedPolicy<T>; + using TBase = TEmbedPolicy<T>; - TAutoEmbedOrPtrPolicy(T&& object) - : TBase(std::move(object)) + TAutoEmbedOrPtrPolicy(T&& object) + : TBase(std::move(object)) { } }; diff --git a/util/generic/store_policy_ut.cpp b/util/generic/store_policy_ut.cpp index c9722203aa..5d78823bac 100644 --- a/util/generic/store_policy_ut.cpp +++ b/util/generic/store_policy_ut.cpp @@ -46,7 +46,7 @@ Y_UNIT_TEST_SUITE(StorePolicy) { static_assert(std::is_const<decltype(a)>::value); FunctionTakingRefDefaultIsObject(a, [](auto& holder) { - static_assert(std::is_const<std::remove_reference_t<decltype(*holder.Ptr())>>::value); + static_assert(std::is_const<std::remove_reference_t<decltype(*holder.Ptr())>>::value); UNIT_ASSERT_VALUES_EQUAL(holder.Ptr()->size(), 3); }); } @@ -54,7 +54,7 @@ Y_UNIT_TEST_SUITE(StorePolicy) { template <typename T, typename TFunc> void FunctionTakingObjectDefaultObject(T&& a, TFunc func) { - TAutoEmbedOrPtrPolicy<T> objectHolder(std::forward<T>(a)); + TAutoEmbedOrPtrPolicy<T> objectHolder(std::forward<T>(a)); func(objectHolder); } diff --git a/util/generic/strbase.h b/util/generic/strbase.h index ab39fc7537..263ac52bb2 100644 --- a/util/generic/strbase.h +++ b/util/generic/strbase.h @@ -124,9 +124,9 @@ public: } /** - * @param Pointer to character inside the string, or nullptr. - * @return Offset from string beginning (in chars), or npos on nullptr. - */ + * @param Pointer to character inside the string, or nullptr. + * @return Offset from string beginning (in chars), or npos on nullptr. + */ inline size_t off(const TCharType* ret) const noexcept { return ret ? (size_t)(ret - Ptr()) : npos; } @@ -168,7 +168,7 @@ public: } inline TCharType back() const noexcept { - Y_ASSERT(!this->empty()); + Y_ASSERT(!this->empty()); return Ptr()[Len() - 1]; } @@ -177,10 +177,10 @@ public: return Ptr()[0]; } - constexpr const TCharType* data() const noexcept { - return Ptr(); - } - + constexpr const TCharType* data() const noexcept { + return Ptr(); + } + constexpr inline size_t size() const noexcept { return Len(); } @@ -253,10 +253,10 @@ public: return compare(LegacySubString(*this, p, n), LegacySubString(t, 0, n1)); } - inline int compare(const TCharType* p, size_t len) const noexcept { + inline int compare(const TCharType* p, size_t len) const noexcept { return compare(*this, TStringView(p, len)); - } - + } + static bool equal(const TSelf& s1, const TSelf& s2) noexcept { return s1.AsStringView() == s2.AsStringView(); } @@ -441,13 +441,13 @@ public: inline TCharType operator[](size_t pos) const noexcept { Y_ASSERT(pos < this->size()); - return Ptr()[pos]; + return Ptr()[pos]; } //~~~~Search~~~~ - /** - * @return Position of the substring inside this string, or `npos` if not found. - */ + /** + * @return Position of the substring inside this string, or `npos` if not found. + */ inline size_t find(const TStringView s, size_t pos = 0) const noexcept { return find(s.data(), pos, s.size()); } @@ -476,9 +476,9 @@ public: } //~~~~Contains~~~~ - /** - * @returns Whether this string contains the provided substring. - */ + /** + * @returns Whether this string contains the provided substring. + */ inline bool Contains(const TStringView s, size_t pos = 0) const noexcept { return !s.length() || find(s, pos) != npos; } @@ -493,33 +493,33 @@ public: //~~~~Character Set Search~~~ inline size_t find_first_of(TCharType c) const noexcept { - return find_first_of(c, 0); + return find_first_of(c, 0); } inline size_t find_first_of(TCharType c, size_t pos) const noexcept { - return find(c, pos); - } - + return find(c, pos); + } + inline size_t find_first_of(const TStringView set) const noexcept { - return find_first_of(set, 0); - } - + return find_first_of(set, 0); + } + inline size_t find_first_of(const TStringView set, size_t pos) const noexcept { return AsStringView().find_first_of(set.data(), pos, set.size()); } inline size_t find_first_not_of(TCharType c) const noexcept { - return find_first_not_of(c, 0); - } - + return find_first_not_of(c, 0); + } + inline size_t find_first_not_of(TCharType c, size_t pos) const noexcept { return find_first_not_of(TStringView(&c, 1), pos); - } - + } + inline size_t find_first_not_of(const TStringView set) const noexcept { - return find_first_not_of(set, 0); - } - + return find_first_not_of(set, 0); + } + inline size_t find_first_not_of(const TStringView set, size_t pos) const noexcept { return AsStringView().find_first_not_of(set.data(), pos, set.size()); } diff --git a/util/generic/strbuf.h b/util/generic/strbuf.h index 70b9360d58..7cbc94cc38 100644 --- a/util/generic/strbuf.h +++ b/util/generic/strbuf.h @@ -1,6 +1,6 @@ #pragma once -#include "fwd.h" +#include "fwd.h" #include "strbase.h" #include "utility.h" #include "typetraits.h" @@ -13,7 +13,7 @@ template <typename TCharType, typename TTraits> class TBasicStringBuf: public std::basic_string_view<TCharType>, public TStringBase<TBasicStringBuf<TCharType, TTraits>, TCharType, TTraits> { private: - using TdSelf = TBasicStringBuf; + using TdSelf = TBasicStringBuf; using TBase = TStringBase<TdSelf, TCharType, TTraits>; using TStringView = std::basic_string_view<TCharType>; @@ -152,7 +152,7 @@ public: } inline TBasicStringBuf(const TBasicStringBuf& src, size_t pos) noexcept - : TBasicStringBuf(src, pos, TBase::npos) + : TBasicStringBuf(src, pos, TBase::npos) { } diff --git a/util/generic/string.h b/util/generic/string.h index 8cd8aa6917..210d1b6d43 100644 --- a/util/generic/string.h +++ b/util/generic/string.h @@ -13,7 +13,7 @@ #include "ptr.h" #include "utility.h" #include "bitops.h" -#include "explicit_type.h" +#include "explicit_type.h" #include "reserve.h" #include "singleton.h" #include "strbase.h" @@ -158,7 +158,7 @@ private: template <typename TCharType, typename TTraits> class TBasicString: public TStringBase<TBasicString<TCharType, TTraits>, TCharType, TTraits> { public: - // TODO: Move to private section + // TODO: Move to private section using TBase = TStringBase<TBasicString, TCharType, TTraits>; using TStringType = std::basic_string<TCharType, TTraits>; #ifdef TSTRING_IS_STD_STRING @@ -169,7 +169,7 @@ public: using TStorage = TIntrusivePtr<TStdStr, TStringPtrOps<TStdStr>>; using reference = TBasicCharRef<TBasicString>; #endif - using char_type = TCharType; // TODO: DROP + using char_type = TCharType; // TODO: DROP using value_type = TCharType; using traits_type = TTraits; @@ -179,15 +179,15 @@ public: using typename TBase::const_reference; using typename TBase::const_reverse_iterator; - struct TUninitialized { + struct TUninitialized { explicit TUninitialized(size_t size) : Size(size) { } - - size_t Size; - }; - + + size_t Size; + }; + static size_t max_size() noexcept { static size_t res = TStringType().max_size(); @@ -217,11 +217,11 @@ protected: return *S_; } - /** - * Makes a distinct copy of this string. `IsDetached()` is always true after this call. - * - * @throw std::length_error - */ + /** + * Makes a distinct copy of this string. `IsDetached()` is always true after this call. + * + * @throw std::length_error + */ void Clone() { Construct(StdStr()).Swap(S_); } @@ -253,7 +253,7 @@ public: inline const_reference operator[](size_t pos) const noexcept { Y_ASSERT(pos <= length()); - return this->data()[pos]; + return this->data()[pos]; } inline reference operator[](size_t pos) noexcept { @@ -269,8 +269,8 @@ public: using TBase::back; inline reference back() noexcept { - Y_ASSERT(!this->empty()); - + Y_ASSERT(!this->empty()); + #ifdef TSTRING_IS_STD_STRING return Storage_.back(); #else @@ -302,10 +302,10 @@ public: return ConstRef().data(); } - inline const TCharType* c_str() const noexcept { + inline const TCharType* c_str() const noexcept { return ConstRef().c_str(); - } - + } + // ~~~ STL compatible method to obtain data pointer ~~~ iterator begin() { return &*MutRef().begin(); @@ -344,19 +344,19 @@ public: #endif } - TCharType* Detach() { + TCharType* Detach() { #ifdef TSTRING_IS_STD_STRING return Storage_.data(); #else if (Y_UNLIKELY(!IsDetached())) { Clone(); - } - + } + return (TCharType*)S_->data(); #endif - } - - bool IsDetached() const { + } + + bool IsDetached() const { #ifdef TSTRING_IS_STD_STRING return true; #else @@ -371,7 +371,7 @@ public: return *this; } - // ~~~ Constructor ~~~ : FAMILY0(,TBasicString) + // ~~~ Constructor ~~~ : FAMILY0(,TBasicString) TBasicString() noexcept #ifndef TSTRING_IS_STD_STRING : S_(Construct()) @@ -410,7 +410,7 @@ public: } template <typename T, typename A> - explicit inline TBasicString(const std::basic_string<TCharType, T, A>& s) + explicit inline TBasicString(const std::basic_string<TCharType, T, A>& s) : TBasicString(s.data(), s.size()) { } @@ -457,7 +457,7 @@ public: } #ifdef TSTRING_IS_STD_STRING - explicit TBasicString(TExplicitType<TCharType> c) { + explicit TBasicString(TExplicitType<TCharType> c) { Storage_.push_back(c); } #else @@ -480,13 +480,13 @@ public: { } - /** - * Constructs an uninitialized string of size `uninitialized.Size`. The proper - * way to use this ctor is via `TBasicString::Uninitialized` factory function. - * - * @throw std::length_error - */ - TBasicString(TUninitialized uninitialized) { + /** + * Constructs an uninitialized string of size `uninitialized.Size`. The proper + * way to use this ctor is via `TBasicString::Uninitialized` factory function. + * + * @throw std::length_error + */ + TBasicString(TUninitialized uninitialized) { #if !defined(TSTRING_IS_STD_STRING) S_ = Construct(); #endif @@ -503,12 +503,12 @@ public: { } - template <typename Traits> + template <typename Traits> explicit inline TBasicString(const std::basic_string_view<TCharType, Traits>& s) : TBasicString(s.data(), s.size()) { - } - + } + /** * WARN: * Certain invocations of this method will result in link-time error. @@ -528,8 +528,8 @@ public: static TBasicString Uninitialized(size_t n) { return TBasicString(TUninitialized(n)); - } - + } + private: template <typename... R> static size_t SumLength(const TBasicStringBuf<TCharType, TTraits> s1, const R&... r) noexcept { @@ -565,7 +565,7 @@ public: #ifdef TSTRING_IS_STD_STRING Storage_.clear(); #else - if (IsDetached()) { + if (IsDetached()) { S_->clear(); return; @@ -584,7 +584,7 @@ public: return s; } - // ~~~ Assignment ~~~ : FAMILY0(TBasicString&, assign); + // ~~~ Assignment ~~~ : FAMILY0(TBasicString&, assign); TBasicString& assign(size_t size, TCharType ch) { ReserveAndResize(size); std::fill(begin(), vend(), ch); @@ -711,7 +711,7 @@ public: MutRef().reserve(len); } - // ~~~ Appending ~~~ : FAMILY0(TBasicString&, append); + // ~~~ Appending ~~~ : FAMILY0(TBasicString&, append); inline TBasicString& append(size_t count, TCharType ch) { MutRef().append(count, ch); @@ -1011,7 +1011,7 @@ public: return *this; #else - return insert(this->off(pos), b, e - b); + return insert(this->off(pos), b, e - b); #endif } @@ -1022,11 +1022,11 @@ public: } TBasicString& insert(const_iterator pos, size_t len, TCharType ch) { - return this->insert(this->off(pos), len, ch); + return this->insert(this->off(pos), len, ch); } TBasicString& insert(const_iterator pos, TCharType ch) { - return this->insert(pos, 1, ch); + return this->insert(pos, 1, ch); } TBasicString& insert(size_t pos, const TBasicStringBuf<TCharType, TTraits> s, size_t spos = 0, size_t sn = TBase::npos) { @@ -1125,10 +1125,10 @@ public: #endif } - /** - * @returns String suitable for debug printing (like Python's `repr()`). - * Format of the string is unspecified and may be changed over time. - */ + /** + * @returns String suitable for debug printing (like Python's `repr()`). + * Format of the string is unspecified and may be changed over time. + */ TBasicString Quote() const { extern TBasicString EscapeC(const TBasicString&); diff --git a/util/generic/string_ut.cpp b/util/generic/string_ut.cpp index ac82e9091d..2ec5963903 100644 --- a/util/generic/string_ut.cpp +++ b/util/generic/string_ut.cpp @@ -136,7 +136,7 @@ protected: s.reserve(128); UNIT_ASSERT(s.capacity() >= 128 && s.capacity() < 256 && s.data() != data); - UNIT_ASSERT(s.IsDetached()); + UNIT_ASSERT(s.IsDetached()); s.resize(64, 'x'); data = s.data(); @@ -145,7 +145,7 @@ protected: s.reserve(10); UNIT_ASSERT(s.capacity() >= 64 && s.capacity() < 128 && s.data() != data); - UNIT_ASSERT(s.IsDetached()); + UNIT_ASSERT(s.IsDetached()); #endif #endif } @@ -746,8 +746,8 @@ public: #endif UNIT_TEST(TestBack) UNIT_TEST(TestFront) - UNIT_TEST(TestIterators); - UNIT_TEST(TestReverseIterators); + UNIT_TEST(TestIterators); + UNIT_TEST(TestReverseIterators); UNIT_TEST(TestAppendUtf16) UNIT_TEST(TestFillingAssign) UNIT_TEST(TestStdStreamApi) @@ -809,8 +809,8 @@ public: UNIT_TEST(TestBack); UNIT_TEST(TestFront) UNIT_TEST(TestDecodingMethods); - UNIT_TEST(TestIterators); - UNIT_TEST(TestReverseIterators); + UNIT_TEST(TestIterators); + UNIT_TEST(TestReverseIterators); UNIT_TEST(TestStringLiterals); UNIT_TEST_SUITE_END(); @@ -934,8 +934,8 @@ public: UNIT_TEST(TestFront) UNIT_TEST(TestDecodingMethods); UNIT_TEST(TestDecodingMethodsMixedStr); - UNIT_TEST(TestIterators); - UNIT_TEST(TestReverseIterators); + UNIT_TEST(TestIterators); + UNIT_TEST(TestReverseIterators); UNIT_TEST(TestStringLiterals); UNIT_TEST_SUITE_END(); diff --git a/util/generic/string_ut.h b/util/generic/string_ut.h index 44bb10bdeb..0ed2c4e979 100644 --- a/util/generic/string_ut.h +++ b/util/generic/string_ut.h @@ -543,7 +543,7 @@ public: #ifndef TSTRING_IS_STD_STRING TStringType s3 = TStringType::Uninitialized(10); - UNIT_ASSERT(s3.size() == 10); + UNIT_ASSERT(s3.size() == 10); #endif TStringType s4(Data._0123456(), 1, 3); @@ -562,9 +562,9 @@ public: TStringType s8(s7, 1, 3); UNIT_ASSERT(s8 == Data._123()); - + TStringType s9(*Data._1()); - UNIT_ASSERT(s9 == Data._1()); + UNIT_ASSERT(s9 == Data._1()); TStringType s10(Reserve(100)); UNIT_ASSERT(s10.empty()); @@ -692,10 +692,10 @@ public: UNIT_ASSERT(s.find_first_of(Data._389()) == 3); UNIT_ASSERT(s.find_first_of(Data._389(), s.size()) == TStringType::npos); UNIT_ASSERT(s.find_first_not_of(Data._123()) == 0); - UNIT_ASSERT(s.find_first_of('6') == 6); - UNIT_ASSERT(s.find_first_of('1', 2) == 8); - UNIT_ASSERT(s.find_first_not_of('0') == 1); - UNIT_ASSERT(s.find_first_not_of('1', 1) == 2); + UNIT_ASSERT(s.find_first_of('6') == 6); + UNIT_ASSERT(s.find_first_of('1', 2) == 8); + UNIT_ASSERT(s.find_first_not_of('0') == 1); + UNIT_ASSERT(s.find_first_not_of('1', 1) == 2); const TStringType rs = Data._0123401234(); UNIT_ASSERT(rs.rfind(*Data._3()) == 8); @@ -1075,7 +1075,7 @@ public: UNIT_ASSERT_VALUES_EQUAL(str.front(), (ui8)'r'); } - void TestIterators() { + void TestIterators() { const char_type chars[] = {'f', 'o', 0}; TStringType str = chars; @@ -1113,7 +1113,7 @@ public: UNIT_ASSERT_VALUES_EQUAL(*citBegin, (ui8)'f'); } - void TestReverseIterators() { + void TestReverseIterators() { const char_type chars[] = {'f', 'o', 0}; TStringType str = chars; diff --git a/util/generic/typetraits.h b/util/generic/typetraits.h index d165bd1a06..ca0a56b276 100644 --- a/util/generic/typetraits.h +++ b/util/generic/typetraits.h @@ -7,7 +7,7 @@ #include <iterator> #include <type_traits> #include <stlfwd> - + #if _LIBCPP_STD_VER >= 17 template <bool B> using TBoolConstant = std::bool_constant<B>; diff --git a/util/generic/typetraits_ut.cpp b/util/generic/typetraits_ut.cpp index e7571c75ec..e373acc322 100644 --- a/util/generic/typetraits_ut.cpp +++ b/util/generic/typetraits_ut.cpp @@ -16,29 +16,29 @@ namespace { TNonPodClass() { } }; - - class TEmptyClass { + + class TEmptyClass { void operator()() const { } - }; - - class TAnotherEmptyClass { - }; - + }; + + class TAnotherEmptyClass { + }; + class TEmptyDerivedClass: public TEmptyClass { - }; - + }; + class TEmptyMultiDerivedClass: public TEmptyDerivedClass, public TAnotherEmptyClass { - /* Not empty under MSVC. - * MSVC's EBCO implementation can handle only one empty base class. */ - }; - + /* Not empty under MSVC. + * MSVC's EBCO implementation can handle only one empty base class. */ + }; + struct TNonEmptyClass { - TEmptyClass member; - }; - + TEmptyClass member; + }; + class TNonEmptyDerivedClass: public TNonEmptyClass { - }; + }; class TStdLayoutClass1: public TEmptyClass { public: @@ -80,10 +80,10 @@ namespace { Y_UNIT_TEST_SUITE(TTypeTraitsTest) { Y_UNIT_TEST(TestIsSame) { - UNIT_ASSERT((std::is_same<int, int>::value)); - UNIT_ASSERT(!(std::is_same<signed int, unsigned int>::value)); - } - + UNIT_ASSERT((std::is_same<int, int>::value)); + UNIT_ASSERT(!(std::is_same<signed int, unsigned int>::value)); + } + Y_UNIT_TEST(TestRemoveReference) { ASSERT_SAME_TYPE(std::remove_reference_t<int>, int); ASSERT_SAME_TYPE(std::remove_reference_t<const int>, const int); @@ -137,7 +137,7 @@ Y_UNIT_TEST_SUITE(TTypeTraitsTest) { a = std::is_same<typename TTypeTraits<const volatile T>::TFuncParam, const volatile T>::value; UNIT_ASSERT(a); } - + template <class T> inline void TestUnsignedIntType() { UNIT_ASSERT(std::is_unsigned<T>::value); @@ -174,28 +174,28 @@ Y_UNIT_TEST_SUITE(TTypeTraitsTest) { } Y_UNIT_TEST(TestUnsignedChar) { - TestArithmeticType<unsigned char>(); + TestArithmeticType<unsigned char>(); TestUnsignedIntType<unsigned char>(); - } - + } + Y_UNIT_TEST(TestSizeT) { - TestArithmeticType<size_t>(); + TestArithmeticType<size_t>(); TestUnsignedIntType<size_t>(); - } - + } + Y_UNIT_TEST(TestInt) { TestArithmeticType<int>(); TestSignedIntType<int>(); } Y_UNIT_TEST(TestDouble) { - TestArithmeticType<double>(); - } - + TestArithmeticType<double>(); + } + Y_UNIT_TEST(TestLongDouble) { - TestArithmeticType<long double>(); - } - + TestArithmeticType<long double>(); + } + Y_UNIT_TEST(TestAddRValueReference) { ASSERT_SAME_TYPE(std::add_rvalue_reference_t<int>, int&&); ASSERT_SAME_TYPE(std::add_rvalue_reference_t<int const&>, int const&); @@ -209,11 +209,11 @@ Y_UNIT_TEST_SUITE(TTypeTraitsTest) { UNIT_ASSERT(std::is_empty<TEmptyClass>::value); UNIT_ASSERT(std::is_empty<TEmptyDerivedClass>::value); UNIT_ASSERT(std::is_empty<TAnotherEmptyClass>::value); -#ifdef _MSC_VER - UNIT_ASSERT(!std::is_empty<TEmptyMultiDerivedClass>::value); -#else +#ifdef _MSC_VER + UNIT_ASSERT(!std::is_empty<TEmptyMultiDerivedClass>::value); +#else UNIT_ASSERT(std::is_empty<TEmptyMultiDerivedClass>::value); -#endif +#endif UNIT_ASSERT(!std::is_empty<TNonEmptyClass>::value); UNIT_ASSERT(!std::is_empty<TNonEmptyDerivedClass>::value); } @@ -234,29 +234,29 @@ Y_UNIT_TEST_SUITE(TTypeTraitsTest) { struct TPod { int value; }; - - struct TNontriviallyCopyAssignable { - TNontriviallyCopyAssignable(const TNontriviallyCopyAssignable&) = default; - TNontriviallyCopyAssignable& operator=(const TNontriviallyCopyAssignable&); - }; - - struct TNonTriviallyCopyConstructible { - TNonTriviallyCopyConstructible(const TNonTriviallyCopyConstructible&); - TNonTriviallyCopyConstructible& operator=(const TNonTriviallyCopyConstructible&) = default; - }; - - struct TNonTriviallyDestructible { - TNonTriviallyDestructible(const TNonTriviallyDestructible&) = default; - TNonTriviallyDestructible& operator=(const TNonTriviallyDestructible&) = default; - ~TNonTriviallyDestructible(); - }; - - UNIT_ASSERT(std::is_trivially_copyable<int>::value); - UNIT_ASSERT(std::is_trivially_copyable<TPod>::value); - UNIT_ASSERT(!std::is_trivially_copyable<TNontriviallyCopyAssignable>::value); - UNIT_ASSERT(!std::is_trivially_copyable<TNonTriviallyCopyConstructible>::value); - UNIT_ASSERT(!std::is_trivially_copyable<TNonTriviallyDestructible>::value); - } + + struct TNontriviallyCopyAssignable { + TNontriviallyCopyAssignable(const TNontriviallyCopyAssignable&) = default; + TNontriviallyCopyAssignable& operator=(const TNontriviallyCopyAssignable&); + }; + + struct TNonTriviallyCopyConstructible { + TNonTriviallyCopyConstructible(const TNonTriviallyCopyConstructible&); + TNonTriviallyCopyConstructible& operator=(const TNonTriviallyCopyConstructible&) = default; + }; + + struct TNonTriviallyDestructible { + TNonTriviallyDestructible(const TNonTriviallyDestructible&) = default; + TNonTriviallyDestructible& operator=(const TNonTriviallyDestructible&) = default; + ~TNonTriviallyDestructible(); + }; + + UNIT_ASSERT(std::is_trivially_copyable<int>::value); + UNIT_ASSERT(std::is_trivially_copyable<TPod>::value); + UNIT_ASSERT(!std::is_trivially_copyable<TNontriviallyCopyAssignable>::value); + UNIT_ASSERT(!std::is_trivially_copyable<TNonTriviallyCopyConstructible>::value); + UNIT_ASSERT(!std::is_trivially_copyable<TNonTriviallyDestructible>::value); + } }; namespace { diff --git a/util/generic/ut/ya.make b/util/generic/ut/ya.make index 6eaf24cc5f..49cd3fd345 100644 --- a/util/generic/ut/ya.make +++ b/util/generic/ut/ya.make @@ -16,7 +16,7 @@ SRCS( generic/cast_ut.cpp generic/deque_ut.cpp generic/explicit_type_ut.cpp - generic/flags_ut.cpp + generic/flags_ut.cpp generic/function_ut.cpp generic/guid_ut.cpp generic/hash_primes_ut.cpp diff --git a/util/generic/vector.h b/util/generic/vector.h index a5b258955a..b794ad038d 100644 --- a/util/generic/vector.h +++ b/util/generic/vector.h @@ -1,8 +1,8 @@ #pragma once -#include "fwd.h" +#include "fwd.h" #include "reserve.h" - + #include <util/memory/alloc.h> #include <vector> @@ -11,7 +11,7 @@ template <class T, class A> class TVector: public std::vector<T, TReboundAllocator<A, T>> { public: - using TBase = std::vector<T, TReboundAllocator<A, T>>; + using TBase = std::vector<T, TReboundAllocator<A, T>>; using TSelf = TVector<T, A>; using size_type = typename TBase::size_type; diff --git a/util/generic/vector_ut.cpp b/util/generic/vector_ut.cpp index 0f6b4037a0..e210f2f7f1 100644 --- a/util/generic/vector_ut.cpp +++ b/util/generic/vector_ut.cpp @@ -565,7 +565,7 @@ private: } void CheckInitializeList(const TVector<int>& v) { - for (size_t i = 0; i < v.size(); ++i) { + for (size_t i = 0; i < v.size(); ++i) { UNIT_ASSERT_EQUAL(v[i], static_cast<int>(i)); } } diff --git a/util/generic/yexception.h b/util/generic/yexception.h index b0c604e8c4..416adcab2d 100644 --- a/util/generic/yexception.h +++ b/util/generic/yexception.h @@ -119,8 +119,8 @@ class TIoSystemError: public TIoException { class TFileError: public TIoSystemError { }; -/** - * TBadArgumentException should be thrown when an argument supplied to some function (or constructor) +/** + * TBadArgumentException should be thrown when an argument supplied to some function (or constructor) * is invalid or incorrect. * * \note @@ -131,8 +131,8 @@ class TFileError: public TIoSystemError { struct TBadArgumentException: public virtual yexception { }; -/** - * TBadCastException should be thrown to indicate the failure of some type casting procedure +/** + * TBadCastException should be thrown to indicate the failure of some type casting procedure * (e.g. reading an integer parameter from string). */ struct TBadCastException: public virtual TBadArgumentException { diff --git a/util/generic/ymath.cpp b/util/generic/ymath.cpp index 31270728f4..d6a0038b5d 100644 --- a/util/generic/ymath.cpp +++ b/util/generic/ymath.cpp @@ -26,9 +26,9 @@ double Erf(double x) { return f; } -#endif // _MSC_VER +#endif // _MSC_VER -double LogGammaImpl(double x) { +double LogGammaImpl(double x) { static constexpr double lnSqrt2Pi = 0.91893853320467274178; // log(sqrt(2.0 * PI)) static constexpr double coeff9 = 1.0 / 1188.0; static constexpr double coeff7 = -1.0 / 1680.0; @@ -37,20 +37,20 @@ double LogGammaImpl(double x) { static constexpr double coeff1 = 1.0 / 12.0; if ((x == 1.0) || (x == 2.0)) { - return 0.0; // 0! = 1 + return 0.0; // 0! = 1 } - double bonus = 0.0; - while (x < 3.0) { - bonus -= log(x); - x += 1.0; - } - double lnX = log(x); - double sqrXInv = 1.0 / (x * x); - double res = coeff9 * sqrXInv + coeff7; - res = res * sqrXInv + coeff5; - res = res * sqrXInv + coeff3; - res = res * sqrXInv + coeff1; - res /= x; - res += x * lnX - x + lnSqrt2Pi - 0.5 * lnX; - return res + bonus; + double bonus = 0.0; + while (x < 3.0) { + bonus -= log(x); + x += 1.0; + } + double lnX = log(x); + double sqrXInv = 1.0 / (x * x); + double res = coeff9 * sqrXInv + coeff7; + res = res * sqrXInv + coeff5; + res = res * sqrXInv + coeff3; + res = res * sqrXInv + coeff1; + res /= x; + res += x * lnX - x + lnSqrt2Pi - 0.5 * lnX; + return res + bonus; } diff --git a/util/generic/ymath.h b/util/generic/ymath.h index 9ff9ae2abe..aabb1b0295 100644 --- a/util/generic/ymath.h +++ b/util/generic/ymath.h @@ -7,46 +7,46 @@ #include <cfloat> #include <cstdlib> -#include "typetraits.h" -#include "utility.h" - +#include "typetraits.h" +#include "utility.h" + constexpr double PI = M_PI; constexpr double M_LOG2_10 = 3.32192809488736234787; // log2(10) constexpr double M_LN2_INV = M_LOG2E; // 1 / ln(2) == log2(e) -/** - * \returns Absolute value of the provided argument. - */ -template <class T> +/** + * \returns Absolute value of the provided argument. + */ +template <class T> constexpr T Abs(T value) { return std::abs(value); -} - -/** - * @returns Base 2 logarithm of the provided double - * precision floating point value. - */ -inline double Log2(double value) { - return log(value) * M_LN2_INV; -} - -/** - * @returns Base 2 logarithm of the provided - * floating point value. - */ -inline float Log2(float value) { - return logf(value) * static_cast<float>(M_LN2_INV); -} - -/** - * @returns Base 2 logarithm of the provided integral value. - */ +} + +/** + * @returns Base 2 logarithm of the provided double + * precision floating point value. + */ +inline double Log2(double value) { + return log(value) * M_LN2_INV; +} + +/** + * @returns Base 2 logarithm of the provided + * floating point value. + */ +inline float Log2(float value) { + return logf(value) * static_cast<float>(M_LN2_INV); +} + +/** + * @returns Base 2 logarithm of the provided integral value. + */ template <class T> inline std::enable_if_t<std::is_integral<T>::value, double> -Log2(T value) { - return Log2(static_cast<double>(value)); -} - +Log2(T value) { + return Log2(static_cast<double>(value)); +} + /** Returns 2^x */ double Exp2(double); float Exp2f(float); @@ -100,10 +100,10 @@ inline double Erf(double x) { } #endif -/** - * @returns Natural logarithm of the absolute value - * of the gamma function of provided argument. - */ +/** + * @returns Natural logarithm of the absolute value + * of the gamma function of provided argument. + */ inline double LogGamma(double x) noexcept { #if defined(_glibc_) int sign; @@ -118,7 +118,7 @@ inline double LogGamma(double x) noexcept { #else extern double LogGammaImpl(double); return LogGammaImpl(x); -#endif +#endif } /** @@ -146,32 +146,32 @@ T Power(T x, Int n) { } return result; }; - -/** - * Compares two floating point values and returns true if they are considered equal. - * The two numbers are compared in a relative way, where the exactness is stronger - * the smaller the numbers are. - * - * Note that comparing values where either one is 0.0 will not work. - * The solution to this is to compare against values greater than or equal to 1.0. - * - * @code - * // Instead of comparing with 0.0 - * FuzzyEquals(0.0, 1.0e-200); // This will return false - * // Compare adding 1 to both values will fix the problem - * FuzzyEquals(1 + 0.0, 1 + 1.0e-200); // This will return true - * @endcode - */ -inline bool FuzzyEquals(double p1, double p2, double eps = 1.0e-13) { - return (Abs(p1 - p2) <= eps * Min(Abs(p1), Abs(p2))); -} - -/** - * @see FuzzyEquals(double, double, double) - */ -inline bool FuzzyEquals(float p1, float p2, float eps = 1.0e-6) { - return (Abs(p1 - p2) <= eps * Min(Abs(p1), Abs(p2))); -} + +/** + * Compares two floating point values and returns true if they are considered equal. + * The two numbers are compared in a relative way, where the exactness is stronger + * the smaller the numbers are. + * + * Note that comparing values where either one is 0.0 will not work. + * The solution to this is to compare against values greater than or equal to 1.0. + * + * @code + * // Instead of comparing with 0.0 + * FuzzyEquals(0.0, 1.0e-200); // This will return false + * // Compare adding 1 to both values will fix the problem + * FuzzyEquals(1 + 0.0, 1 + 1.0e-200); // This will return true + * @endcode + */ +inline bool FuzzyEquals(double p1, double p2, double eps = 1.0e-13) { + return (Abs(p1 - p2) <= eps * Min(Abs(p1), Abs(p2))); +} + +/** + * @see FuzzyEquals(double, double, double) + */ +inline bool FuzzyEquals(float p1, float p2, float eps = 1.0e-6) { + return (Abs(p1 - p2) <= eps * Min(Abs(p1), Abs(p2))); +} namespace NUtilMathPrivate { template <bool IsSigned> diff --git a/util/generic/ymath_ut.cpp b/util/generic/ymath_ut.cpp index 29190b55eb..3baea7928f 100644 --- a/util/generic/ymath_ut.cpp +++ b/util/generic/ymath_ut.cpp @@ -31,7 +31,7 @@ class TMathTest: public TTestBase { UNIT_TEST(TestErf); UNIT_TEST(TestLogGamma); UNIT_TEST(TestIsValidFloat); - UNIT_TEST(TestAbs); + UNIT_TEST(TestAbs); UNIT_TEST(TestPower); UNIT_TEST(TestSigmoid); UNIT_TEST(TestCeilDiv); @@ -42,7 +42,7 @@ private: void TestSqr(); void TestErf(); void TestLogGamma(); - void TestAbs(); + void TestAbs(); void TestPower(); void TestSigmoid(); void TestCeilDiv(); @@ -63,8 +63,8 @@ private: inline void TestLog2() { UNIT_ASSERT_DOUBLES_EQUAL(Log2(2.0), 1.0, 1e-10); - UNIT_ASSERT_DOUBLES_EQUAL(Log2(2ull), 1.0, 1e-10); - UNIT_ASSERT_DOUBLES_EQUAL(Log2(2.0f), 1.0f, 1e-7f); + UNIT_ASSERT_DOUBLES_EQUAL(Log2(2ull), 1.0, 1e-10); + UNIT_ASSERT_DOUBLES_EQUAL(Log2(2.0f), 1.0f, 1e-7f); } inline void ValueBitCount() { @@ -162,16 +162,16 @@ void TMathTest::TestLogGamma() { curVal += log(i - 0.5); } } - -void TMathTest::TestAbs() { - UNIT_ASSERT_VALUES_EQUAL(Abs(1), 1); - UNIT_ASSERT_VALUES_EQUAL(Abs(-1), 1); - UNIT_ASSERT_VALUES_EQUAL(Abs(-1000000000000ll), 1000000000000ll); - UNIT_ASSERT_VALUES_EQUAL(Abs(0), 0); - UNIT_ASSERT_VALUES_EQUAL(Abs(1.0), 1.0); - UNIT_ASSERT_VALUES_EQUAL(Abs(-1.0), 1.0); - UNIT_ASSERT_VALUES_EQUAL(Abs(0.0), 0.0); -} + +void TMathTest::TestAbs() { + UNIT_ASSERT_VALUES_EQUAL(Abs(1), 1); + UNIT_ASSERT_VALUES_EQUAL(Abs(-1), 1); + UNIT_ASSERT_VALUES_EQUAL(Abs(-1000000000000ll), 1000000000000ll); + UNIT_ASSERT_VALUES_EQUAL(Abs(0), 0); + UNIT_ASSERT_VALUES_EQUAL(Abs(1.0), 1.0); + UNIT_ASSERT_VALUES_EQUAL(Abs(-1.0), 1.0); + UNIT_ASSERT_VALUES_EQUAL(Abs(0.0), 0.0); +} void TMathTest::TestPower() { UNIT_ASSERT_VALUES_EQUAL(Power(0, 0), 1); diff --git a/util/memory/blob.cpp b/util/memory/blob.cpp index 91da5cadca..f613639d2a 100644 --- a/util/memory/blob.cpp +++ b/util/memory/blob.cpp @@ -3,7 +3,7 @@ #include <util/system/yassert.h> #include <util/system/filemap.h> -#include <util/system/mlock.h> +#include <util/system/mlock.h> #include <util/stream/buffer.h> #include <util/generic/ptr.h> #include <util/generic/string.h> @@ -105,9 +105,9 @@ class TMappedBlobBase: public TBlob::TBase, public TRefCounted<TMappedBlobBase<T using TRefBase = TRefCounted<TMappedBlobBase<TCounter>, TCounter>; public: - inline TMappedBlobBase(const TMemoryMap& map, ui64 offset, size_t len, EMappingMode mode) + inline TMappedBlobBase(const TMemoryMap& map, ui64 offset, size_t len, EMappingMode mode) : Map_(map) - , Mode_(mode) + , Mode_(mode) { Y_ENSURE(Map_.IsOpen(), TStringBuf("memory map not open")); @@ -116,17 +116,17 @@ public: if (len && !Map_.Ptr()) { // Ptr is 0 for blob of size 0 ythrow yexception() << "can not map(" << offset << ", " << len << ")"; } - + if (Mode_ == EMappingMode::Locked) { - LockMemory(Data(), Length()); + LockMemory(Data(), Length()); } } ~TMappedBlobBase() override { if (Mode_ == EMappingMode::Locked && Length()) { - UnlockMemory(Data(), Length()); + UnlockMemory(Data(), Length()); } - } + } void Ref() noexcept override { TRefBase::Ref(); @@ -146,7 +146,7 @@ public: private: TFileMap Map_; - EMappingMode Mode_; + EMappingMode Mode_; }; TBlob TBlob::SubBlob(size_t len) const { @@ -197,27 +197,27 @@ TBlob TBlob::NoCopy(const void* data, size_t length) { } template <class TCounter> -static inline TBlob ConstructFromMap(const TMemoryMap& map, ui64 offset, size_t length, EMappingMode mode) { +static inline TBlob ConstructFromMap(const TMemoryMap& map, ui64 offset, size_t length, EMappingMode mode) { using TBase = TMappedBlobBase<TCounter>; - THolder<TBase> base(new TBase(map, offset, length, mode)); + THolder<TBase> base(new TBase(map, offset, length, mode)); TBlob ret(base->Data(), base->Length(), base.Get()); Y_UNUSED(base.Release()); return ret; } -template <class TCounter, class T> -static inline TBlob ConstructAsMap(const T& t, EMappingMode mode) { +template <class TCounter, class T> +static inline TBlob ConstructAsMap(const T& t, EMappingMode mode) { TMemoryMap::EOpenMode openMode = (mode == EMappingMode::Precharged) ? (TMemoryMap::oRdOnly | TMemoryMap::oPrecharge) : TMemoryMap::oRdOnly; - TMemoryMap map(t, openMode); + TMemoryMap map(t, openMode); const ui64 toMap = map.Length(); if (toMap > Max<size_t>()) { ythrow yexception() << "can not map whole file(length = " << toMap << ")"; } - return ConstructFromMap<TCounter>(map, 0, static_cast<size_t>(toMap), mode); + return ConstructFromMap<TCounter>(map, 0, static_cast<size_t>(toMap), mode); } TBlob TBlob::FromFileSingleThreaded(const TString& path, EMappingMode mode) { @@ -236,54 +236,54 @@ TBlob TBlob::FromFile(const TFile& file, EMappingMode mode) { return ConstructAsMap<TAtomicCounter>(file, mode); } -TBlob TBlob::FromFileSingleThreaded(const TString& path) { +TBlob TBlob::FromFileSingleThreaded(const TString& path) { return ConstructAsMap<TSimpleCounter>(path, EMappingMode::Standard); } -TBlob TBlob::FromFile(const TString& path) { +TBlob TBlob::FromFile(const TString& path) { return ConstructAsMap<TAtomicCounter>(path, EMappingMode::Standard); } -TBlob TBlob::FromFileSingleThreaded(const TFile& file) { +TBlob TBlob::FromFileSingleThreaded(const TFile& file) { return ConstructAsMap<TSimpleCounter>(file, EMappingMode::Standard); } -TBlob TBlob::FromFile(const TFile& file) { +TBlob TBlob::FromFile(const TFile& file) { return ConstructAsMap<TAtomicCounter>(file, EMappingMode::Standard); } -TBlob TBlob::PrechargedFromFileSingleThreaded(const TString& path) { +TBlob TBlob::PrechargedFromFileSingleThreaded(const TString& path) { return ConstructAsMap<TSimpleCounter>(path, EMappingMode::Precharged); -} - -TBlob TBlob::PrechargedFromFile(const TString& path) { +} + +TBlob TBlob::PrechargedFromFile(const TString& path) { return ConstructAsMap<TAtomicCounter>(path, EMappingMode::Precharged); -} - -TBlob TBlob::PrechargedFromFileSingleThreaded(const TFile& file) { +} + +TBlob TBlob::PrechargedFromFileSingleThreaded(const TFile& file) { return ConstructAsMap<TSimpleCounter>(file, EMappingMode::Precharged); -} - -TBlob TBlob::PrechargedFromFile(const TFile& file) { +} + +TBlob TBlob::PrechargedFromFile(const TFile& file) { return ConstructAsMap<TAtomicCounter>(file, EMappingMode::Precharged); -} - -TBlob TBlob::LockedFromFileSingleThreaded(const TString& path) { +} + +TBlob TBlob::LockedFromFileSingleThreaded(const TString& path) { return ConstructAsMap<TSimpleCounter>(path, EMappingMode::Locked); -} - -TBlob TBlob::LockedFromFile(const TString& path) { +} + +TBlob TBlob::LockedFromFile(const TString& path) { return ConstructAsMap<TAtomicCounter>(path, EMappingMode::Locked); -} - -TBlob TBlob::LockedFromFileSingleThreaded(const TFile& file) { +} + +TBlob TBlob::LockedFromFileSingleThreaded(const TFile& file) { return ConstructAsMap<TSimpleCounter>(file, EMappingMode::Locked); -} - -TBlob TBlob::LockedFromFile(const TFile& file) { +} + +TBlob TBlob::LockedFromFile(const TFile& file) { return ConstructAsMap<TAtomicCounter>(file, EMappingMode::Locked); -} - +} + TBlob TBlob::LockedFromMemoryMapSingleThreaded(const TMemoryMap& map, ui64 offset, size_t length) { return ConstructFromMap<TSimpleCounter>(map, offset, length, EMappingMode::Locked); } diff --git a/util/memory/blob.h b/util/memory/blob.h index 20c02a68df..f0de873ea1 100644 --- a/util/memory/blob.h +++ b/util/memory/blob.h @@ -60,9 +60,9 @@ public: using const_pointer = const value_type*; using const_iterator = const_pointer; - /** + /** * Constructs a null blob (data array points to nullptr). - */ + */ TBlob() noexcept : S_(nullptr, 0, nullptr) { @@ -213,43 +213,43 @@ public: static TBlob FromFile(const TFile& file, EMappingMode); /// Creates a blob with a single-threaded (non atomic) refcounter. It maps the file on the path as data. - static TBlob FromFileSingleThreaded(const TString& path); + static TBlob FromFileSingleThreaded(const TString& path); /// Creates a blob with a multi-threaded (atomic) refcounter. It maps the file on the path as data. - static TBlob FromFile(const TString& path); + static TBlob FromFile(const TString& path); /// Creates a blob with a single-threaded (non atomic) refcounter. It maps the file on the path as data. - static TBlob FromFileSingleThreaded(const TFile& file); + static TBlob FromFileSingleThreaded(const TFile& file); /// Creates a blob with a multi-threaded (atomic) refcounter. It maps the file on the path as data. - static TBlob FromFile(const TFile& file); - - // TODO: drop Precharged* functions. + static TBlob FromFile(const TFile& file); + // TODO: drop Precharged* functions. + /// Creates a precharged blob with a single-threaded (non atomic) refcounter. It maps the file on the path as data. - static TBlob PrechargedFromFileSingleThreaded(const TString& path); + static TBlob PrechargedFromFileSingleThreaded(const TString& path); /// Creates a precharged blob with a multi-threaded (atomic) refcounter. It maps the file on the path as data. - static TBlob PrechargedFromFile(const TString& path); + static TBlob PrechargedFromFile(const TString& path); /// Creates a precharged blob with a single-threaded (non atomic) refcounter. It maps the file content as data. - static TBlob PrechargedFromFileSingleThreaded(const TFile& file); + static TBlob PrechargedFromFileSingleThreaded(const TFile& file); /// Creates a precharged blob with a multi-threaded (atomic) refcounter. It maps the file content as data. - static TBlob PrechargedFromFile(const TFile& file); - - /// Creates a locked blob with a single-threaded (non atomic) refcounter. It maps the file on the path as data. - static TBlob LockedFromFileSingleThreaded(const TString& path); - - /// Creates a locked blob with a multi-threaded (atomic) refcounter. It maps the file on the path as data. - static TBlob LockedFromFile(const TString& path); - - /// Creates a locked blob with a single-threaded (non atomic) refcounter. It maps the file content as data. - static TBlob LockedFromFileSingleThreaded(const TFile& file); - - /// Creates a locked blob with a multi-threaded (atomic) refcounter. It maps the file content as data. - static TBlob LockedFromFile(const TFile& file); - + static TBlob PrechargedFromFile(const TFile& file); + + /// Creates a locked blob with a single-threaded (non atomic) refcounter. It maps the file on the path as data. + static TBlob LockedFromFileSingleThreaded(const TString& path); + + /// Creates a locked blob with a multi-threaded (atomic) refcounter. It maps the file on the path as data. + static TBlob LockedFromFile(const TString& path); + + /// Creates a locked blob with a single-threaded (non atomic) refcounter. It maps the file content as data. + static TBlob LockedFromFileSingleThreaded(const TFile& file); + + /// Creates a locked blob with a multi-threaded (atomic) refcounter. It maps the file content as data. + static TBlob LockedFromFile(const TFile& file); + /// Creates a locked blob with a single-threaded (non atomic) refcounter from the mapped memory. static TBlob LockedFromMemoryMapSingleThreaded(const TMemoryMap& map, ui64 offset, size_t length); diff --git a/util/memory/blob_ut.cpp b/util/memory/blob_ut.cpp index 023f9a0487..cef9bfc38b 100644 --- a/util/memory/blob_ut.cpp +++ b/util/memory/blob_ut.cpp @@ -5,7 +5,7 @@ #include <util/system/tempfile.h> #include <util/folder/path.h> #include <util/stream/output.h> -#include <util/stream/file.h> +#include <util/stream/file.h> #include <util/generic/buffer.h> #include <util/generic/array_ref.h> @@ -53,19 +53,19 @@ Y_UNIT_TEST_SUITE(TBlobTest) { UNIT_ASSERT_EQUAL(buf.Size(), 0u); UNIT_ASSERT_EQUAL(b.Size(), sz); } - + Y_UNIT_TEST(TestFromFile) { TString path = "testfile"; - + TOFStream stream(path); stream.Write("1234", 4); stream.Finish(); - + auto testMode = [](TBlob blob) { UNIT_ASSERT_EQUAL(blob.Size(), 4); UNIT_ASSERT_EQUAL(TStringBuf(static_cast<const char*>(blob.Data()), 4), "1234"); }; - + testMode(TBlob::FromFile(path)); testMode(TBlob::PrechargedFromFile(path)); testMode(TBlob::LockedFromFile(path)); diff --git a/util/memory/pool.h b/util/memory/pool.h index 13c8b6b9ed..37c64e9731 100644 --- a/util/memory/pool.h +++ b/util/memory/pool.h @@ -14,15 +14,15 @@ #include <string> #include <utility> -/** - * Memory pool implements a memory allocation scheme that is very fast, but - * limited in its usage. - * - * A common use case is when you want to allocate a bunch of small objects, and - * then release them all at some point of your program. Using memory pool, you - * can just drop them off into oblivion without calling any destructors, - * provided that all associated memory was allocated on the pool. - */ +/** + * Memory pool implements a memory allocation scheme that is very fast, but + * limited in its usage. + * + * A common use case is when you want to allocate a bunch of small objects, and + * then release them all at some point of your program. Using memory pool, you + * can just drop them off into oblivion without calling any destructors, + * provided that all associated memory was allocated on the pool. + */ class TMemoryPool { private: using TBlock = IAllocator::TBlock; @@ -50,16 +50,16 @@ private: } inline void* Allocate(size_t len, size_t align) noexcept { - size_t pad = AlignUp(Cur_, align) - Cur_; - - void* ret = Allocate(pad + len); - if (ret) { - return static_cast<char*>(ret) + pad; - } - + size_t pad = AlignUp(Cur_, align) - Cur_; + + void* ret = Allocate(pad + len); + if (ret) { + return static_cast<char*>(ret) + pad; + } + return nullptr; - } - + } + inline size_t BlockLength() const noexcept { return (Cur_ + Left_) - (char*)this; } @@ -151,30 +151,30 @@ public: return RawAllocate(AlignUp<size_t>(len, PLATFORM_DATA_ALIGN)); } - inline void* Allocate(size_t len, size_t align) { - return RawAllocate(AlignUp<size_t>(len, PLATFORM_DATA_ALIGN), align); - } - + inline void* Allocate(size_t len, size_t align) { + return RawAllocate(AlignUp<size_t>(len, PLATFORM_DATA_ALIGN), align); + } + template <typename T> inline T* Allocate() { return (T*)this->Allocate(sizeof(T), alignof(T)); } template <typename T> - inline T* Allocate(size_t align) { + inline T* Allocate(size_t align) { return (T*)this->Allocate(sizeof(T), Max(align, alignof(T))); - } - - template <typename T> + } + + template <typename T> inline T* AllocateArray(size_t count) { return (T*)this->Allocate(sizeof(T) * count, alignof(T)); } - template <typename T> - inline T* AllocateArray(size_t count, size_t align) { + template <typename T> + inline T* AllocateArray(size_t count, size_t align) { return (T*)this->Allocate(sizeof(T) * count, Max(align, alignof(T))); - } - + } + template <typename T> inline T* AllocateZeroArray(size_t count) { T* ptr = AllocateArray<T>(count); @@ -219,17 +219,17 @@ public: } template <typename TChar> - inline TBasicStringBuf<TChar> AppendString(const TBasicStringBuf<TChar>& buf) { - return TBasicStringBuf<TChar>(Append(buf.data(), buf.size()), buf.size()); + inline TBasicStringBuf<TChar> AppendString(const TBasicStringBuf<TChar>& buf) { + return TBasicStringBuf<TChar>(Append(buf.data(), buf.size()), buf.size()); } template <typename TChar> - inline TBasicStringBuf<TChar> AppendCString(const TBasicStringBuf<TChar>& buf) { + inline TBasicStringBuf<TChar> AppendCString(const TBasicStringBuf<TChar>& buf) { TChar* ret = static_cast<TChar*>(Allocate((buf.size() + 1) * sizeof(TChar))); std::char_traits<TChar>::copy(ret, buf.data(), buf.size()); *(ret + buf.size()) = 0; - return TBasicStringBuf<TChar>(ret, buf.size()); + return TBasicStringBuf<TChar>(ret, buf.size()); } inline size_t Available() const noexcept { @@ -276,19 +276,19 @@ protected: return Current_->Allocate(len); } - inline void* RawAllocate(size_t len, size_t align) { + inline void* RawAllocate(size_t len, size_t align) { Y_ASSERT(align > 0); - void* ret = Current_->Allocate(len, align); - - if (ret) { - return ret; - } - + void* ret = Current_->Allocate(len, align); + + if (ret) { + return ret; + } + AddChunk(len + align - 1); - - return Current_->Allocate(len, align); - } - + + return Current_->Allocate(len, align); + } + private: void AddChunk(size_t hint); void DoClear(bool keepfirst) noexcept; @@ -373,7 +373,7 @@ public: } inline T* allocate(size_t n) { - return (T*)Pool_->Allocate(n * sizeof(T), alignof(T)); + return (T*)Pool_->Allocate(n * sizeof(T), alignof(T)); } inline void deallocate(pointer /*p*/, size_t /*n*/) { @@ -402,14 +402,14 @@ public: return Pool_; } - inline friend bool operator==(const TPoolAllocBase& l, const TPoolAllocBase& r) { - return l.Pool_ == r.Pool_; - } - - inline friend bool operator!=(const TPoolAllocBase& l, const TPoolAllocBase& r) { - return !(l == r); - } - + inline friend bool operator==(const TPoolAllocBase& l, const TPoolAllocBase& r) { + return l.Pool_ == r.Pool_; + } + + inline friend bool operator!=(const TPoolAllocBase& l, const TPoolAllocBase& r) { + return !(l == r); + } + private: TPool* Pool_; }; diff --git a/util/memory/pool_ut.cpp b/util/memory/pool_ut.cpp index 1158a8ca42..58bb627b9a 100644 --- a/util/memory/pool_ut.cpp +++ b/util/memory/pool_ut.cpp @@ -4,51 +4,51 @@ #include <util/stream/output.h> -class TCheckedAllocator: public TDefaultAllocator { -public: - inline TCheckedAllocator() - : Alloced_(0) - , Released_(0) - , Allocs_(0) +class TCheckedAllocator: public TDefaultAllocator { +public: + inline TCheckedAllocator() + : Alloced_(0) + , Released_(0) + , Allocs_(0) , Frees_(0) { - } + } TBlock Allocate(size_t len) override { - Check(); + Check(); - Alloced_ += len; - ++Allocs_; + Alloced_ += len; + ++Allocs_; - return TDefaultAllocator::Allocate(len); - } + return TDefaultAllocator::Allocate(len); + } void Release(const TBlock& block) override { - Released_ += block.Len; - ++Frees_; + Released_ += block.Len; + ++Frees_; - Check(); + Check(); - TDefaultAllocator::Release(block); - } + TDefaultAllocator::Release(block); + } - inline void CheckAtEnd() { - UNIT_ASSERT_EQUAL(Alloced_, Released_); - UNIT_ASSERT_EQUAL(Allocs_, Frees_); - } + inline void CheckAtEnd() { + UNIT_ASSERT_EQUAL(Alloced_, Released_); + UNIT_ASSERT_EQUAL(Allocs_, Frees_); + } -private: - inline void Check() { - UNIT_ASSERT(Alloced_ >= Released_); - UNIT_ASSERT(Allocs_ >= Frees_); - } +private: + inline void Check() { + UNIT_ASSERT(Alloced_ >= Released_); + UNIT_ASSERT(Allocs_ >= Frees_); + } -private: - size_t Alloced_; - size_t Released_; - size_t Allocs_; - size_t Frees_; -}; +private: + size_t Alloced_; + size_t Released_; + size_t Allocs_; + size_t Frees_; +}; class TErrorOnCopy { public: @@ -76,20 +76,20 @@ public: TNoMove(TNoMove&&) = delete; }; -class TMemPoolTest: public TTestBase { - UNIT_TEST_SUITE(TMemPoolTest); - UNIT_TEST(TestMemPool) - UNIT_TEST(TestAlign) +class TMemPoolTest: public TTestBase { + UNIT_TEST_SUITE(TMemPoolTest); + UNIT_TEST(TestMemPool) + UNIT_TEST(TestAlign) UNIT_TEST(TestZeroArray) - UNIT_TEST(TestLargeStartingAlign) + UNIT_TEST(TestLargeStartingAlign) UNIT_TEST(TestMoveAlloc) UNIT_TEST(TestRoundUpToNextPowerOfTwoOption) - UNIT_TEST_SUITE_END(); - -private: - inline void TestMemPool() { - TCheckedAllocator alloc; + UNIT_TEST_SUITE_END(); +private: + inline void TestMemPool() { + TCheckedAllocator alloc; + { TMemoryPool pool(123, TMemoryPool::TExpGrow::Instance(), &alloc); @@ -168,18 +168,18 @@ private: alloc.CheckAtEnd(); } - - inline void TestAlign() { - TMemoryPool pool(1); - - void* aligned16 = pool.Allocate(3, 16); - void* aligned2 = pool.Allocate(3, 2); - void* aligned128 = pool.Allocate(3, 128); - void* aligned4 = pool.Allocate(3, 4); - void* aligned256 = pool.Allocate(3, 256); - void* aligned8 = pool.Allocate(3, 8); - void* aligned1024 = pool.Allocate(3, 1024); - + + inline void TestAlign() { + TMemoryPool pool(1); + + void* aligned16 = pool.Allocate(3, 16); + void* aligned2 = pool.Allocate(3, 2); + void* aligned128 = pool.Allocate(3, 128); + void* aligned4 = pool.Allocate(3, 4); + void* aligned256 = pool.Allocate(3, 256); + void* aligned8 = pool.Allocate(3, 8); + void* aligned1024 = pool.Allocate(3, 1024); + UNIT_ASSERT_VALUES_UNEQUAL(aligned16, nullptr); UNIT_ASSERT_VALUES_UNEQUAL(aligned2, nullptr); UNIT_ASSERT_VALUES_UNEQUAL(aligned128, nullptr); @@ -188,14 +188,14 @@ private: UNIT_ASSERT_VALUES_UNEQUAL(aligned8, nullptr); UNIT_ASSERT_VALUES_UNEQUAL(aligned1024, nullptr); - UNIT_ASSERT_VALUES_EQUAL(reinterpret_cast<uintptr_t>(aligned2) & 1, 0); - UNIT_ASSERT_VALUES_EQUAL(reinterpret_cast<uintptr_t>(aligned4) & 3, 0); - UNIT_ASSERT_VALUES_EQUAL(reinterpret_cast<uintptr_t>(aligned8) & 7, 0); - UNIT_ASSERT_VALUES_EQUAL(reinterpret_cast<uintptr_t>(aligned16) & 15, 0); - UNIT_ASSERT_VALUES_EQUAL(reinterpret_cast<uintptr_t>(aligned128) & 127, 0); - UNIT_ASSERT_VALUES_EQUAL(reinterpret_cast<uintptr_t>(aligned256) & 255, 0); - UNIT_ASSERT_VALUES_EQUAL(reinterpret_cast<uintptr_t>(aligned1024) & 1023, 0); - } + UNIT_ASSERT_VALUES_EQUAL(reinterpret_cast<uintptr_t>(aligned2) & 1, 0); + UNIT_ASSERT_VALUES_EQUAL(reinterpret_cast<uintptr_t>(aligned4) & 3, 0); + UNIT_ASSERT_VALUES_EQUAL(reinterpret_cast<uintptr_t>(aligned8) & 7, 0); + UNIT_ASSERT_VALUES_EQUAL(reinterpret_cast<uintptr_t>(aligned16) & 15, 0); + UNIT_ASSERT_VALUES_EQUAL(reinterpret_cast<uintptr_t>(aligned128) & 127, 0); + UNIT_ASSERT_VALUES_EQUAL(reinterpret_cast<uintptr_t>(aligned256) & 255, 0); + UNIT_ASSERT_VALUES_EQUAL(reinterpret_cast<uintptr_t>(aligned1024) & 1023, 0); + } void TestZeroArray() { TMemoryPool pool(1); @@ -205,24 +205,24 @@ private: UNIT_ASSERT(intArray[i] == 0); } - size_t align = 256; + size_t align = 256; ui8* byteArray = pool.AllocateZeroArray<ui8>(size, align); UNIT_ASSERT(size_t(byteArray) % align == 0); for (size_t i = 0; i < size; ++i) { UNIT_ASSERT(byteArray[i] == 0); - } - } - - void TestLargeStartingAlign() { - TMemoryPool pool(1); - - void* aligned4k1 = pool.Allocate(1, 4096); - void* aligned4k2 = pool.Allocate(1, 4096); - UNIT_ASSERT_VALUES_UNEQUAL(aligned4k1, nullptr); - UNIT_ASSERT_VALUES_UNEQUAL(aligned4k2, nullptr); - UNIT_ASSERT_VALUES_EQUAL(reinterpret_cast<uintptr_t>(aligned4k1) & 4095, 0); - UNIT_ASSERT_VALUES_EQUAL(reinterpret_cast<uintptr_t>(aligned4k2) & 4095, 0); + } } + + void TestLargeStartingAlign() { + TMemoryPool pool(1); + + void* aligned4k1 = pool.Allocate(1, 4096); + void* aligned4k2 = pool.Allocate(1, 4096); + UNIT_ASSERT_VALUES_UNEQUAL(aligned4k1, nullptr); + UNIT_ASSERT_VALUES_UNEQUAL(aligned4k2, nullptr); + UNIT_ASSERT_VALUES_EQUAL(reinterpret_cast<uintptr_t>(aligned4k1) & 4095, 0); + UNIT_ASSERT_VALUES_EQUAL(reinterpret_cast<uintptr_t>(aligned4k2) & 4095, 0); + } template <typename T> void CheckMoveAlloc() { diff --git a/util/memory/segmented_string_pool.h b/util/memory/segmented_string_pool.h index a40aa408f5..ed0b542757 100644 --- a/util/memory/segmented_string_pool.h +++ b/util/memory/segmented_string_pool.h @@ -7,14 +7,14 @@ #include <util/generic/vector.h> #include <util/generic/strbuf.h> -#include <memory> +#include <memory> #include <cstdio> #include <cstdlib> /* * Non-reallocated storage for the objects of POD type */ -template <class T, class Alloc = std::allocator<T>> +template <class T, class Alloc = std::allocator<T>> class segmented_pool: TNonCopyable { protected: Alloc seg_allocator; diff --git a/util/network/ip.h b/util/network/ip.h index dc7c2d24a0..2b644dd8dd 100644 --- a/util/network/ip.h +++ b/util/network/ip.h @@ -69,7 +69,7 @@ struct TIpAddress: public sockaddr_in { Set(ip, port); } - inline TIpAddress(TStringBuf ip, TIpPort port) { + inline TIpAddress(TStringBuf ip, TIpPort port) { Set(ResolveHost(ip.data(), ip.size()), port); } diff --git a/util/network/sock.h b/util/network/sock.h index b10be2f715..de4f272f18 100644 --- a/util/network/sock.h +++ b/util/network/sock.h @@ -380,14 +380,14 @@ protected: public: ssize_t SendTo(const void* msg, size_t len, const ISockAddr* toAddr) { ssize_t ret = toAddr->ResolveAddr(); - if (ret < 0) { - return -LastSystemError(); - } + if (ret < 0) { + return -LastSystemError(); + } ret = sendto((SOCKET) * this, (const char*)msg, (int)len, 0, toAddr->SockAddr(), toAddr->Len()); - if (ret < 0) { - return -LastSystemError(); - } + if (ret < 0) { + return -LastSystemError(); + } return ret; } @@ -396,7 +396,7 @@ public: socklen_t fromSize = fromAddr->Size(); const ssize_t ret = recvfrom((SOCKET) * this, (char*)buf, (int)len, 0, fromAddr->SockAddr(), &fromSize); if (ret < 0) { - return -LastSystemError(); + return -LastSystemError(); } return ret; diff --git a/util/network/socket.cpp b/util/network/socket.cpp index 4f6e804346..2d157bb53d 100644 --- a/util/network/socket.cpp +++ b/util/network/socket.cpp @@ -956,15 +956,15 @@ namespace { insert("::1"); } - inline bool IsLocalName(const char* name) const noexcept { + inline bool IsLocalName(const char* name) const noexcept { struct sockaddr_in sa; memset(&sa, 0, sizeof(sa)); - if (inet_pton(AF_INET, name, &(sa.sin_addr)) == 1) { + if (inet_pton(AF_INET, name, &(sa.sin_addr)) == 1) { return (InetToHost(sa.sin_addr.s_addr) >> 24) == 127; } - return contains(name); + return contains(name); } }; } diff --git a/util/random/init_atfork.cpp b/util/random/init_atfork.cpp index 0faa3d119a..3397425a29 100644 --- a/util/random/init_atfork.cpp +++ b/util/random/init_atfork.cpp @@ -12,7 +12,7 @@ namespace { struct TInit { inline TInit() noexcept { - (void)&AtFork; + (void)&AtFork; #if defined(_unix_) Y_VERIFY(pthread_atfork(nullptr, AtFork, nullptr) == 0, "it happens"); diff --git a/util/str_stl.h b/util/str_stl.h index f1e137181d..43df6e48bb 100644 --- a/util/str_stl.h +++ b/util/str_stl.h @@ -5,7 +5,7 @@ #include <util/generic/string.h> #include <util/generic/string_hash.h> #include <util/generic/strbuf.h> -#include <util/generic/typetraits.h> +#include <util/generic/typetraits.h> #include <functional> #include <typeindex> @@ -13,13 +13,13 @@ namespace std { template <> - struct less<const char*> { + struct less<const char*> { bool operator()(const char* x, const char* y) const { return strcmp(x, y) < 0; } }; template <> - struct equal_to<const char*> { + struct equal_to<const char*> { bool operator()(const char* x, const char* y) const { return strcmp(x, y) == 0; } @@ -145,42 +145,42 @@ template <class T> struct THash: public ::hash<T> { }; -namespace NHashPrivate { +namespace NHashPrivate { template <class TFirst, class TSecond, bool IsEmpty = std::is_empty<THash<TFirst>>::value&& std::is_empty<THash<TSecond>>::value> - struct TPairHash { - private: - THash<TFirst> FirstHash; - THash<TSecond> SecondHash; + struct TPairHash { + private: + THash<TFirst> FirstHash; + THash<TSecond> SecondHash; - public: + public: template <class T> inline size_t operator()(const T& pair) const { - return CombineHashes(FirstHash(pair.first), SecondHash(pair.second)); - } - }; - - /** - * Specialization for the case where both hash functors are empty. Basically the - * only one we care about. We don't introduce additional specializations for - * cases where only one of the functors is empty as the code bloat is just not worth it. - */ - template <class TFirst, class TSecond> - struct TPairHash<TFirst, TSecond, true> { + return CombineHashes(FirstHash(pair.first), SecondHash(pair.second)); + } + }; + + /** + * Specialization for the case where both hash functors are empty. Basically the + * only one we care about. We don't introduce additional specializations for + * cases where only one of the functors is empty as the code bloat is just not worth it. + */ + template <class TFirst, class TSecond> + struct TPairHash<TFirst, TSecond, true> { template <class T> inline size_t operator()(const T& pair) const { // maps have TFirst = const TFoo, which would make for an undefined specialization using TFirstClean = std::remove_cv_t<TFirst>; using TSecondClean = std::remove_cv_t<TSecond>; return CombineHashes(THash<TFirstClean>()(pair.first), THash<TSecondClean>()(pair.second)); - } - }; + } + }; } - -template <class TFirst, class TSecond> + +template <class TFirst, class TSecond> struct hash<std::pair<TFirst, TSecond>>: public NHashPrivate::TPairHash<TFirst, TSecond> { }; -template <class T> +template <class T> struct TEqualTo: public std::equal_to<T> { }; @@ -233,7 +233,7 @@ struct TCIEqualTo<TString> { } }; -template <class T> +template <class T> struct TLess: public std::less<T> { }; @@ -252,7 +252,7 @@ struct TLess<TUtf32String>: public TLess<TUtf32StringBuf> { using is_transparent = void; }; -template <class T> +template <class T> struct TGreater: public std::greater<T> { }; diff --git a/util/stream/aligned.cpp b/util/stream/aligned.cpp index 2fd12d15b7..9e280858a7 100644 --- a/util/stream/aligned.cpp +++ b/util/stream/aligned.cpp @@ -1,30 +1,30 @@ #include "aligned.h" - -size_t TAlignedInput::DoRead(void* ptr, size_t len) { - size_t ret = Stream_->Read(ptr, len); - Position_ += ret; - return ret; -} - -size_t TAlignedInput::DoSkip(size_t len) { - size_t ret = Stream_->Skip(len); - Position_ += ret; - return ret; -} - + +size_t TAlignedInput::DoRead(void* ptr, size_t len) { + size_t ret = Stream_->Read(ptr, len); + Position_ += ret; + return ret; +} + +size_t TAlignedInput::DoSkip(size_t len) { + size_t ret = Stream_->Skip(len); + Position_ += ret; + return ret; +} + size_t TAlignedInput::DoReadTo(TString& st, char ch) { - size_t ret = Stream_->ReadTo(st, ch); - Position_ += ret; - return ret; -} - + size_t ret = Stream_->ReadTo(st, ch); + Position_ += ret; + return ret; +} + ui64 TAlignedInput::DoReadAll(IOutputStream& out) { - ui64 ret = Stream_->ReadAll(out); - Position_ += ret; - return ret; -} - -void TAlignedOutput::DoWrite(const void* ptr, size_t len) { - Stream_->Write(ptr, len); - Position_ += len; -} + ui64 ret = Stream_->ReadAll(out); + Position_ += ret; + return ret; +} + +void TAlignedOutput::DoWrite(const void* ptr, size_t len) { + Stream_->Write(ptr, len); + Position_ += len; +} diff --git a/util/stream/aligned.h b/util/stream/aligned.h index 70e7be05a9..04b420c1db 100644 --- a/util/stream/aligned.h +++ b/util/stream/aligned.h @@ -1,99 +1,99 @@ #pragma once -#include "input.h" -#include "output.h" - -#include <util/system/yassert.h> -#include <util/generic/bitops.h> - -/** - * @addtogroup Streams - * @{ - */ - -/** - * Proxy input stream that provides additional functions that make reading - * aligned data easier. - */ +#include "input.h" +#include "output.h" + +#include <util/system/yassert.h> +#include <util/generic/bitops.h> + +/** + * @addtogroup Streams + * @{ + */ + +/** + * Proxy input stream that provides additional functions that make reading + * aligned data easier. + */ class TAlignedInput: public IInputStream { public: TAlignedInput(IInputStream* s) - : Stream_(s) - , Position_(0) + : Stream_(s) + , Position_(0) { } - /** - * Ensures alignment of the position in the input stream by skipping - * some input. - * - * @param alignment Alignment. Must be a power of 2. - */ - void Align(size_t alignment = sizeof(void*)) { + /** + * Ensures alignment of the position in the input stream by skipping + * some input. + * + * @param alignment Alignment. Must be a power of 2. + */ + void Align(size_t alignment = sizeof(void*)) { Y_ASSERT(IsPowerOf2(alignment)); - - if (Position_ & (alignment - 1)) { - size_t len = alignment - (Position_ & (alignment - 1)); - - do { - len -= DoSkip(len); - } while (len); - } + + if (Position_ & (alignment - 1)) { + size_t len = alignment - (Position_ & (alignment - 1)); + + do { + len -= DoSkip(len); + } while (len); + } } - -private: + +private: size_t DoRead(void* ptr, size_t len) override; size_t DoSkip(size_t len) override; size_t DoReadTo(TString& st, char ch) override; ui64 DoReadAll(IOutputStream& out) override; - -private: + +private: IInputStream* Stream_; - ui64 Position_; + ui64 Position_; }; -/** - * Proxy output stream that provides additional functions that make writing - * aligned data easier. - */ +/** + * Proxy output stream that provides additional functions that make writing + * aligned data easier. + */ class TAlignedOutput: public IOutputStream { public: TAlignedOutput(IOutputStream* s) - : Stream_(s) - , Position_(0) + : Stream_(s) + , Position_(0) { } TAlignedOutput(TAlignedOutput&&) noexcept = default; TAlignedOutput& operator=(TAlignedOutput&&) noexcept = default; - size_t GetCurrentOffset() const { - return Position_; + size_t GetCurrentOffset() const { + return Position_; } - /** - * Ensures alignment of the position in the output stream by writing - * some data. - * - * @param alignment Alignment. Must be a power of 2. - */ - void Align(size_t alignment = sizeof(void*)) { + /** + * Ensures alignment of the position in the output stream by writing + * some data. + * + * @param alignment Alignment. Must be a power of 2. + */ + void Align(size_t alignment = sizeof(void*)) { Y_ASSERT(IsPowerOf2(alignment)); - + static char unused[sizeof(void*) * 2]; Y_ASSERT(alignment <= sizeof(unused)); - + if (Position_ & (alignment - 1)) { - DoWrite(unused, alignment - (Position_ & (alignment - 1))); + DoWrite(unused, alignment - (Position_ & (alignment - 1))); } } - -private: + +private: void DoWrite(const void* ptr, size_t len) override; - -private: + +private: IOutputStream* Stream_; - ui64 Position_; + ui64 Position_; }; -/** @} */ +/** @} */ diff --git a/util/stream/aligned_ut.cpp b/util/stream/aligned_ut.cpp index e980d05cf7..cf2bf6f2a7 100644 --- a/util/stream/aligned_ut.cpp +++ b/util/stream/aligned_ut.cpp @@ -1,63 +1,63 @@ -#include "aligned.h" - +#include "aligned.h" + #include <library/cpp/testing/unittest/registar.h> - + class TNastyInputStream: public IInputStream { -public: +public: TNastyInputStream() : Pos_(0) { } - -protected: + +protected: size_t DoRead(void* buf, size_t len) override { if (len == 0) { - return 0; + return 0; } - - *static_cast<unsigned char*>(buf) = static_cast<unsigned char>(Pos_); + + *static_cast<unsigned char*>(buf) = static_cast<unsigned char>(Pos_); ++Pos_; - return 1; - } - + return 1; + } + size_t DoSkip(size_t len) override { if (len == 0) { - return 0; + return 0; } - + ++Pos_; - return 1; - } - -private: - size_t Pos_; -}; - + return 1; + } + +private: + size_t Pos_; +}; + Y_UNIT_TEST_SUITE(TAlignedTest) { Y_UNIT_TEST(AlignInput) { - TNastyInputStream input0; - TAlignedInput alignedInput(&input0); - - char c = '\1'; - - alignedInput.Align(2); - alignedInput.ReadChar(c); - UNIT_ASSERT_VALUES_EQUAL(c, '\x0'); - - alignedInput.Align(2); - alignedInput.ReadChar(c); - UNIT_ASSERT_VALUES_EQUAL(c, '\x2'); - - alignedInput.Align(4); - alignedInput.ReadChar(c); - UNIT_ASSERT_VALUES_EQUAL(c, '\x4'); - - alignedInput.Align(16); - alignedInput.ReadChar(c); - UNIT_ASSERT_VALUES_EQUAL(c, '\x10'); - - alignedInput.Align(128); - alignedInput.ReadChar(c); - UNIT_ASSERT_VALUES_EQUAL(c, '\x80'); - } -} + TNastyInputStream input0; + TAlignedInput alignedInput(&input0); + + char c = '\1'; + + alignedInput.Align(2); + alignedInput.ReadChar(c); + UNIT_ASSERT_VALUES_EQUAL(c, '\x0'); + + alignedInput.Align(2); + alignedInput.ReadChar(c); + UNIT_ASSERT_VALUES_EQUAL(c, '\x2'); + + alignedInput.Align(4); + alignedInput.ReadChar(c); + UNIT_ASSERT_VALUES_EQUAL(c, '\x4'); + + alignedInput.Align(16); + alignedInput.ReadChar(c); + UNIT_ASSERT_VALUES_EQUAL(c, '\x10'); + + alignedInput.Align(128); + alignedInput.ReadChar(c); + UNIT_ASSERT_VALUES_EQUAL(c, '\x80'); + } +} diff --git a/util/stream/buffer.cpp b/util/stream/buffer.cpp index 2facece4ea..9c353af400 100644 --- a/util/stream/buffer.cpp +++ b/util/stream/buffer.cpp @@ -66,15 +66,15 @@ TBufferOutput::TBufferOutput(TBuffer& buffer) { } -TBufferOutput::TBufferOutput(TBufferOutput&&) noexcept = default; -TBufferOutput& TBufferOutput::operator=(TBufferOutput&&) noexcept = default; - +TBufferOutput::TBufferOutput(TBufferOutput&&) noexcept = default; +TBufferOutput& TBufferOutput::operator=(TBufferOutput&&) noexcept = default; + TBufferOutput::~TBufferOutput() = default; TBuffer& TBufferOutput::Buffer() const noexcept { - return Impl_->Buffer(); -} - + return Impl_->Buffer(); +} + size_t TBufferOutput::DoNext(void** ptr) { return Impl_->DoNext(ptr); } @@ -91,28 +91,28 @@ void TBufferOutput::DoWriteC(char c) { Impl_->DoWriteC(c); } -TBufferInput::TBufferInput(const TBuffer& buffer) - : Buf_(buffer) - , Readed_(0) -{ +TBufferInput::TBufferInput(const TBuffer& buffer) + : Buf_(buffer) + , Readed_(0) +{ } TBufferInput::~TBufferInput() = default; - + const TBuffer& TBufferInput::Buffer() const noexcept { - return Buf_; -} - + return Buf_; +} + void TBufferInput::Rewind() noexcept { - Readed_ = 0; -} - -size_t TBufferInput::DoNext(const void** ptr, size_t len) { - len = Min(Buf_.Size() - Readed_, len); + Readed_ = 0; +} + +size_t TBufferInput::DoNext(const void** ptr, size_t len) { + len = Min(Buf_.Size() - Readed_, len); *ptr = Buf_.data() + Readed_; - Readed_ += len; - return len; -} + Readed_ += len; + return len; +} void TBufferInput::DoUndo(size_t len) { Y_VERIFY(len <= Readed_); diff --git a/util/stream/buffer.h b/util/stream/buffer.h index 9dc99dbe49..35d9db3018 100644 --- a/util/stream/buffer.h +++ b/util/stream/buffer.h @@ -1,48 +1,48 @@ #pragma once -#include "zerocopy.h" +#include "zerocopy.h" #include "zerocopy_output.h" #include <util/generic/ptr.h> class TBuffer; - -/** - * @addtogroup Streams_Buffers - * @{ - */ - -/** - * Output stream that writes into a `TBuffer`. - */ + +/** + * @addtogroup Streams_Buffers + * @{ + */ + +/** + * Output stream that writes into a `TBuffer`. + */ class TBufferOutput: public IZeroCopyOutput { public: class TImpl; - /** - * Constructs a stream that writes into an internal buffer. - * - * @param buflen Initial size of the internal buffer. + /** + * Constructs a stream that writes into an internal buffer. + * + * @param buflen Initial size of the internal buffer. */ TBufferOutput(size_t buflen = 1024); - /** - * Constructs a stream that writes into the provided buffer. It's up to the - * user to make sure that the buffer doesn't get destroyed while this stream - * is in use. - * - * @param buffer Buffer to write into. + /** + * Constructs a stream that writes into the provided buffer. It's up to the + * user to make sure that the buffer doesn't get destroyed while this stream + * is in use. + * + * @param buffer Buffer to write into. */ TBufferOutput(TBuffer& buffer); - TBufferOutput(TBufferOutput&&) noexcept; - TBufferOutput& operator=(TBufferOutput&&) noexcept; + TBufferOutput(TBufferOutput&&) noexcept; + TBufferOutput& operator=(TBufferOutput&&) noexcept; ~TBufferOutput() override; - /** - * @returns Buffer that this stream writes into. - */ + /** + * @returns Buffer that this stream writes into. + */ TBuffer& Buffer() const noexcept; private: @@ -55,65 +55,65 @@ private: THolder<TImpl> Impl_; }; -/** - * Input stream that reads from an external `TBuffer`. - */ +/** + * Input stream that reads from an external `TBuffer`. + */ class TBufferInput: public IZeroCopyInputFastReadTo { public: - /** - * Constructs a stream that reads from an external buffer. It's up to the - * user to make sure that the buffer doesn't get destroyed before this - * stream. - * - * @param buffer External buffer to read from. - */ - TBufferInput(const TBuffer& buffer); + /** + * Constructs a stream that reads from an external buffer. It's up to the + * user to make sure that the buffer doesn't get destroyed before this + * stream. + * + * @param buffer External buffer to read from. + */ + TBufferInput(const TBuffer& buffer); ~TBufferInput() override; const TBuffer& Buffer() const noexcept; - + void Rewind() noexcept; -protected: +protected: size_t DoNext(const void** ptr, size_t len) override; void DoUndo(size_t len) override; - + private: const TBuffer& Buf_; size_t Readed_; }; -/** - * Input/output stream that works with a `TBuffer`. - */ +/** + * Input/output stream that works with a `TBuffer`. + */ class TBufferStream: public TBufferOutput, public TBufferInput { public: - /** - * Constructs a stream that works with an internal buffer. - * - * @param buflen Initial size of the internal buffer. - */ + /** + * Constructs a stream that works with an internal buffer. + * + * @param buflen Initial size of the internal buffer. + */ inline TBufferStream(size_t buflen = 1024) : TBufferOutput(buflen) - , TBufferInput(TBufferOutput::Buffer()) + , TBufferInput(TBufferOutput::Buffer()) { } - /** - * Constructs a stream that works with the provided buffer. - * - * @param buffer Buffer to work with. - */ + /** + * Constructs a stream that works with the provided buffer. + * + * @param buffer Buffer to work with. + */ inline TBufferStream(TBuffer& buffer) : TBufferOutput(buffer) - , TBufferInput(TBufferOutput::Buffer()) + , TBufferInput(TBufferOutput::Buffer()) { } ~TBufferStream() override = default; - - using TBufferOutput::Buffer; + + using TBufferOutput::Buffer; }; -/** @} */ +/** @} */ diff --git a/util/stream/buffer_ut.cpp b/util/stream/buffer_ut.cpp index 3494696190..eea214142e 100644 --- a/util/stream/buffer_ut.cpp +++ b/util/stream/buffer_ut.cpp @@ -1,37 +1,37 @@ -#include "buffer.h" - +#include "buffer.h" + #include <library/cpp/testing/unittest/registar.h> - -#include <util/generic/buffer.h> - + +#include <util/generic/buffer.h> + #include <cstring> -#include "str.h" - +#include "str.h" + Y_UNIT_TEST_SUITE(TBufferTest) { Y_UNIT_TEST(Transfer) { - TBuffer buffer("razrazraz", 9); - TBufferInput input(buffer); - - input.Skip(3); - - TStringStream output; - TransferData(&input, &output); - - UNIT_ASSERT_VALUES_EQUAL(output.Str(), "razraz"); - } - + TBuffer buffer("razrazraz", 9); + TBufferInput input(buffer); + + input.Skip(3); + + TStringStream output; + TransferData(&input, &output); + + UNIT_ASSERT_VALUES_EQUAL(output.Str(), "razraz"); + } + Y_UNIT_TEST(ReadTo) { - TBuffer buffer("1234567890", 10); - TBufferInput input(buffer); - + TBuffer buffer("1234567890", 10); + TBufferInput input(buffer); + TString tmp; - UNIT_ASSERT_VALUES_EQUAL(input.ReadTo(tmp, '3'), 3); - UNIT_ASSERT_VALUES_EQUAL(tmp, "12"); - - UNIT_ASSERT_VALUES_EQUAL(input.ReadTo(tmp, 'z'), 7); - UNIT_ASSERT_VALUES_EQUAL(tmp, "4567890"); - } + UNIT_ASSERT_VALUES_EQUAL(input.ReadTo(tmp, '3'), 3); + UNIT_ASSERT_VALUES_EQUAL(tmp, "12"); + + UNIT_ASSERT_VALUES_EQUAL(input.ReadTo(tmp, 'z'), 7); + UNIT_ASSERT_VALUES_EQUAL(tmp, "4567890"); + } Y_UNIT_TEST(WriteViaNextAndUndo) { TBuffer buffer; @@ -82,4 +82,4 @@ Y_UNIT_TEST_SUITE(TBufferTest) { UNIT_ASSERT(0 == memcmp(buffer.data(), "1234567890", buffer.size())); } -} +} diff --git a/util/stream/buffered.cpp b/util/stream/buffered.cpp index a00e592e1c..8a00fe56c8 100644 --- a/util/stream/buffered.cpp +++ b/util/stream/buffered.cpp @@ -15,12 +15,12 @@ public: inline ~TImpl() = default; - inline size_t Next(const void** ptr, size_t len) { + inline size_t Next(const void** ptr, size_t len) { if (MemInput_.Exhausted()) { MemInput_.Reset(Buf(), Slave_->Read(Buf(), BufLen())); } - return MemInput_.Next(ptr, len); + return MemInput_.Next(ptr, len); } inline size_t Read(void* buf, size_t len) { @@ -67,7 +67,7 @@ public: TString s_tmp; - size_t ret = 0; + size_t ret = 0; while (true) { if (MemInput_.Exhausted()) { @@ -135,7 +135,7 @@ size_t TBufferedInput::DoSkip(size_t len) { return Impl_->Skip(len); } -size_t TBufferedInput::DoNext(const void** ptr, size_t len) { +size_t TBufferedInput::DoNext(const void** ptr, size_t len) { return Impl_->Next(ptr, len); } diff --git a/util/stream/buffered.h b/util/stream/buffered.h index 0847186141..a11da0ccf0 100644 --- a/util/stream/buffered.h +++ b/util/stream/buffered.h @@ -1,6 +1,6 @@ #pragma once -#include "zerocopy.h" +#include "zerocopy.h" #include "zerocopy_output.h" #include <utility> @@ -8,18 +8,18 @@ #include <util/generic/typetraits.h> #include <util/generic/store_policy.h> -/** - * @addtogroup Streams_Buffered - * @{ - */ - -/** - * Input stream that wraps the given stream and adds a buffer on top of it, - * thus making sure that data is read from the underlying stream in big chunks. - * - * Note that it does not claim ownership of the underlying stream, so it's up - * to the user to free it. - */ +/** + * @addtogroup Streams_Buffered + * @{ + */ + +/** + * Input stream that wraps the given stream and adds a buffer on top of it, + * thus making sure that data is read from the underlying stream in big chunks. + * + * Note that it does not claim ownership of the underlying stream, so it's up + * to the user to free it. + */ class TBufferedInput: public IZeroCopyInput { public: TBufferedInput(IInputStream* slave, size_t buflen = 8192); @@ -29,11 +29,11 @@ public: ~TBufferedInput() override; - /** - * Switches the underlying stream to the one provided. Does not clear the - * data that was already buffered. - * - * @param slave New underlying stream. + /** + * Switches the underlying stream to the one provided. Does not clear the + * data that was already buffered. + * + * @param slave New underlying stream. */ void Reset(IInputStream* slave); @@ -48,64 +48,64 @@ private: THolder<TImpl> Impl_; }; -/** - * Output stream that wraps the given stream and adds a buffer on top of it, - * thus making sure that data is written to the underlying stream in big chunks. - * - * Note that by default this stream does not propagate `Flush` and `Finish` - * calls to the underlying stream, instead simply flushing out the buffer. - * You can change this behavior by using propagation mode setters. - * - * Also note that this stream does not claim ownership of the underlying stream, - * so it's up to the user to free it. - */ +/** + * Output stream that wraps the given stream and adds a buffer on top of it, + * thus making sure that data is written to the underlying stream in big chunks. + * + * Note that by default this stream does not propagate `Flush` and `Finish` + * calls to the underlying stream, instead simply flushing out the buffer. + * You can change this behavior by using propagation mode setters. + * + * Also note that this stream does not claim ownership of the underlying stream, + * so it's up to the user to free it. + */ class TBufferedOutputBase: public IZeroCopyOutput { public: - /** - * Constructs a buffered stream that dynamically adjusts the size of the - * buffer. This works best when the amount of data that will be passed - * through this stream is not known and can range in size from several - * kilobytes to several gigabytes. - * - * @param slave Underlying stream. - */ + /** + * Constructs a buffered stream that dynamically adjusts the size of the + * buffer. This works best when the amount of data that will be passed + * through this stream is not known and can range in size from several + * kilobytes to several gigabytes. + * + * @param slave Underlying stream. + */ TBufferedOutputBase(IOutputStream* slave); - - /** - * Constructs a buffered stream with the given size of the buffer. - * - * @param slave Underlying stream. - * @param buflen Size of the buffer. - */ + + /** + * Constructs a buffered stream with the given size of the buffer. + * + * @param slave Underlying stream. + * @param buflen Size of the buffer. + */ TBufferedOutputBase(IOutputStream* slave, size_t buflen); - + TBufferedOutputBase(TBufferedOutputBase&&) noexcept; TBufferedOutputBase& operator=(TBufferedOutputBase&&) noexcept; ~TBufferedOutputBase() override; - /** - * @param propagate Whether `Flush` and `Finish` calls should - * be propagated to the underlying stream. - * By default they are not. + /** + * @param propagate Whether `Flush` and `Finish` calls should + * be propagated to the underlying stream. + * By default they are not. */ inline void SetPropagateMode(bool propagate) noexcept { SetFlushPropagateMode(propagate); SetFinishPropagateMode(propagate); } - /** - * @param propagate Whether `Flush` calls should be propagated - * to the underlying stream. By default they - * are not. - */ + /** + * @param propagate Whether `Flush` calls should be propagated + * to the underlying stream. By default they + * are not. + */ void SetFlushPropagateMode(bool propagate) noexcept; - - /** - * @param propagate Whether `Finish` calls should be propagated - * to the underlying stream. By default they - * are not. - */ + + /** + * @param propagate Whether `Finish` calls should be propagated + * to the underlying stream. By default they + * are not. + */ void SetFinishPropagateMode(bool propagate) noexcept; class TImpl; @@ -122,11 +122,11 @@ private: THolder<TImpl> Impl_; }; -/** - * Buffered output stream with a fixed-size buffer. - * - * @see TBufferedOutputBase - */ +/** + * Buffered output stream with a fixed-size buffer. + * + * @see TBufferedOutputBase + */ class TBufferedOutput: public TBufferedOutputBase { public: TBufferedOutput(IOutputStream* slave, size_t buflen = 8192); @@ -136,12 +136,12 @@ public: TBufferedOutput& operator=(TBufferedOutput&&) noexcept = default; }; -/** - * Buffered output stream that dynamically adjusts the size of the buffer based - * on the amount of data that's passed through it. - * - * @see TBufferedOutputBase - */ +/** + * Buffered output stream that dynamically adjusts the size of the buffer based + * on the amount of data that's passed through it. + * + * @see TBufferedOutputBase + */ class TAdaptiveBufferedOutput: public TBufferedOutputBase { public: TAdaptiveBufferedOutput(IOutputStream* slave); @@ -166,19 +166,19 @@ namespace NPrivate { }; } -/** - * A mixin class that turns unbuffered stream into a buffered one. - * - * Note that using this mixin with a stream that is already buffered won't +/** + * A mixin class that turns unbuffered stream into a buffered one. + * + * Note that using this mixin with a stream that is already buffered won't * result in double buffering, e.g. `TBuffered<TBuffered<TUnbufferedFileInput>>` and * `TBuffered<TUnbufferedFileInput>` are basically the same types. - * - * Example usage: - * @code + * + * Example usage: + * @code * TBuffered<TUnbufferedFileInput> file_input(1024, "/path/to/file"); * TBuffered<TUnbufferedFileOutput> file_output(1024, "/path/to/file"); - * @endcode - * Here 1024 is the size of the buffer. + * @endcode + * Here 1024 is the size of the buffer. */ template <class TSlave> class TBuffered: private TEmbedPolicy<TSlave>, public ::NPrivate::TBufferedStreamFor<TSlave>::TResult { @@ -203,17 +203,17 @@ public: TBuffered& operator=(TBuffered&&) = delete; }; -/** - * A mixin class that turns unbuffered stream into an adaptively buffered one. - * Created stream differs from the one created via `TBuffered` template in that - * it dynamically adjusts the size of the buffer based on the amount of data - * that's passed through it. - * - * Example usage: - * @code +/** + * A mixin class that turns unbuffered stream into an adaptively buffered one. + * Created stream differs from the one created via `TBuffered` template in that + * it dynamically adjusts the size of the buffer based on the amount of data + * that's passed through it. + * + * Example usage: + * @code * TAdaptivelyBuffered<TUnbufferedFileOutput> file_output("/path/to/file"); - * @endcode - */ + * @endcode + */ template <class TSlave> class TAdaptivelyBuffered: private TEmbedPolicy<TSlave>, public TAdaptiveBufferedOutput { using TSlaveBase = TEmbedPolicy<TSlave>; @@ -232,4 +232,4 @@ public: TAdaptivelyBuffered& operator=(TAdaptivelyBuffered&& other) = delete; }; -/** @} */ +/** @} */ diff --git a/util/stream/buffered_ut.cpp b/util/stream/buffered_ut.cpp index 41d2fc3030..8da9761943 100644 --- a/util/stream/buffered_ut.cpp +++ b/util/stream/buffered_ut.cpp @@ -127,16 +127,16 @@ Y_UNIT_TEST_SUITE(TestBufferedIO) { UNIT_ASSERT_VALUES_EQUAL(c, 'k'); UNIT_ASSERT_VALUES_EQUAL(in.Skip(6), 3); //24 eof } - + Y_UNIT_TEST(TestReadTo) { TString s("0123456789abc"); - TBuffered<TStringInput> in(2, s); + TBuffered<TStringInput> in(2, s); TString t; - UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '7'), 8); - UNIT_ASSERT_VALUES_EQUAL(t, "0123456"); - UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '8'), 1); - UNIT_ASSERT_VALUES_EQUAL(t, ""); - UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, 'z'), 4); - UNIT_ASSERT_VALUES_EQUAL(t, "9abc"); - } + UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '7'), 8); + UNIT_ASSERT_VALUES_EQUAL(t, "0123456"); + UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '8'), 1); + UNIT_ASSERT_VALUES_EQUAL(t, ""); + UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, 'z'), 4); + UNIT_ASSERT_VALUES_EQUAL(t, "9abc"); + } } diff --git a/util/stream/debug.h b/util/stream/debug.h index 92d6d4b42d..c5269d6e82 100644 --- a/util/stream/debug.h +++ b/util/stream/debug.h @@ -2,14 +2,14 @@ #include "output.h" -/** - * @addtogroup Streams - * @{ - */ - -/** - * Debug output stream. Writes into `stderr`. - */ +/** + * @addtogroup Streams + * @{ + */ + +/** + * Debug output stream. Writes into `stderr`. + */ class TDebugOutput: public IOutputStream { public: inline TDebugOutput() noexcept = default; @@ -22,32 +22,32 @@ private: void DoWrite(const void* buf, size_t len) override; }; -/** - * @returns Standard debug stream. - * @see Cdbg - */ +/** + * @returns Standard debug stream. + * @see Cdbg + */ IOutputStream& StdDbgStream() noexcept; -/** - * This function returns the current debug level as set via `DBGOUT` environment - * variable. - * - * Note that the proper way to use this function is via `Y_DBGTRACE` macro. - * There are very few cases when there is a need to use it directly. - * - * @returns Debug level. - * @see ETraceLevel - * @see DBGTRACE - */ +/** + * This function returns the current debug level as set via `DBGOUT` environment + * variable. + * + * Note that the proper way to use this function is via `Y_DBGTRACE` macro. + * There are very few cases when there is a need to use it directly. + * + * @returns Debug level. + * @see ETraceLevel + * @see DBGTRACE + */ int StdDbgLevel() noexcept; -/** - * Standard debug stream. - * - * Behavior of this stream is controlled via `DBGOUT` environment variable. - * If this variable is set, then this stream is redirected into `stderr`, - * otherwise whatever is written into it is simply ignored. - */ +/** + * Standard debug stream. + * + * Behavior of this stream is controlled via `DBGOUT` environment variable. + * If this variable is set, then this stream is redirected into `stderr`, + * otherwise whatever is written into it is simply ignored. + */ #define Cdbg (StdDbgStream()) -/** @} */ +/** @} */ diff --git a/util/stream/file.cpp b/util/stream/file.cpp index dc5d2f6311..3e8b881a8b 100644 --- a/util/stream/file.cpp +++ b/util/stream/file.cpp @@ -25,13 +25,13 @@ size_t TUnbufferedFileInput::DoRead(void* buf, size_t len) { size_t TUnbufferedFileInput::DoSkip(size_t len) { if (len < 384) { - /* Base implementation calls DoRead, which results in one system call - * instead of three as in fair skip implementation. For small sizes - * actually doing one read is cheaper. Experiments show that the - * border that separates two implementations performance-wise lies - * in the range of 384-512 bytes (assuming that the file is in OS cache). */ + /* Base implementation calls DoRead, which results in one system call + * instead of three as in fair skip implementation. For small sizes + * actually doing one read is cheaper. Experiments show that the + * border that separates two implementations performance-wise lies + * in the range of 384-512 bytes (assuming that the file is in OS cache). */ return IInputStream::DoSkip(len); - } + } /* TFile::Seek can seek beyond the end of file, so we need to do * size check here. */ @@ -40,8 +40,8 @@ size_t TUnbufferedFileInput::DoSkip(size_t len) { i64 newPos = File_.Seek(Min<i64>(size, oldPos + len), sSet); return newPos - oldPos; -} - +} + TUnbufferedFileOutput::TUnbufferedFileOutput(const TString& path) : File_(path, CreateAlways | WrOnly | Seq) { diff --git a/util/stream/file.h b/util/stream/file.h index c1cf4f591d..8a667c4241 100644 --- a/util/stream/file.h +++ b/util/stream/file.h @@ -1,25 +1,25 @@ #pragma once #include "fwd.h" -#include "input.h" -#include "output.h" +#include "input.h" +#include "output.h" #include "buffered.h" -#include "mem.h" +#include "mem.h" #include <util/system/file.h> #include <utility> -/** - * @addtogroup Streams_Files - * @{ +/** + * @addtogroup Streams_Files + * @{ */ - -/** + +/** * Unbuffered file input stream. - * - * Note that the input is not buffered, which means that `ReadLine` calls will - * be _very_ slow. - */ + * + * Note that the input is not buffered, which means that `ReadLine` calls will + * be _very_ slow. + */ class TUnbufferedFileInput: public IInputStream { public: TUnbufferedFileInput(const TFile& file); @@ -33,9 +33,9 @@ private: TFile File_; }; -/** - * Memory-mapped file input stream. - */ +/** + * Memory-mapped file input stream. + */ class TMappedFileInput: public TMemoryInput { public: TMappedFileInput(const TFile& file); @@ -47,12 +47,12 @@ private: THolder<TImpl> Impl_; }; -/** - * File output stream. - * - * Note that the output is unbuffered, thus writing in many small chunks is - * likely to be quite slow. - */ +/** + * File output stream. + * + * Note that the output is unbuffered, thus writing in many small chunks is + * likely to be quite slow. + */ class TUnbufferedFileOutput: public IOutputStream { public: TUnbufferedFileOutput(const TString& path); @@ -70,11 +70,11 @@ private: TFile File_; }; -/** - * Buffered file input stream. - * - * @see TBuffered - */ +/** + * Buffered file input stream. + * + * @see TBuffered + */ class TFileInput: public TBuffered<TUnbufferedFileInput> { public: template <class T> @@ -86,14 +86,14 @@ public: ~TFileInput() override = default; }; -/** - * Buffered file output stream. - * +/** + * Buffered file output stream. + * * Currently deprecated, please use TFileOutput in new code. - * - * @deprecated - * @see TBuffered - */ + * + * @deprecated + * @see TBuffered + */ class TFixedBufferFileOutput: public TBuffered<TUnbufferedFileOutput> { public: template <class T> @@ -105,4 +105,4 @@ public: ~TFixedBufferFileOutput() override = default; }; -/** @} */ +/** @} */ diff --git a/util/stream/file_ut.cpp b/util/stream/file_ut.cpp index ac0f09796e..1ed0beb1e5 100644 --- a/util/stream/file_ut.cpp +++ b/util/stream/file_ut.cpp @@ -1,61 +1,61 @@ -#include "file.h" - +#include "file.h" + #include <library/cpp/testing/unittest/registar.h> - -#include <util/system/tempfile.h> - -static const char* TmpFileName = "./fileio"; -static const char* TmpFileContents = "To do good to Mankind is the chivalrous plan"; -static const char* TmpFileSubstring = strstr(TmpFileContents, "chivalrous"); - + +#include <util/system/tempfile.h> + +static const char* TmpFileName = "./fileio"; +static const char* TmpFileContents = "To do good to Mankind is the chivalrous plan"; +static const char* TmpFileSubstring = strstr(TmpFileContents, "chivalrous"); + Y_UNIT_TEST_SUITE(TFileTest) { Y_UNIT_TEST(InputTest) { - TTempFile tmp(TmpFileName); - - { + TTempFile tmp(TmpFileName); + + { TUnbufferedFileOutput output(TmpFileName); - output.Write(TmpFileContents, strlen(TmpFileContents)); - } - - { + output.Write(TmpFileContents, strlen(TmpFileContents)); + } + + { TUnbufferedFileInput input(TmpFileName); TString s = input.ReadAll(); - UNIT_ASSERT_VALUES_EQUAL(s, TmpFileContents); - } - - { + UNIT_ASSERT_VALUES_EQUAL(s, TmpFileContents); + } + + { TUnbufferedFileInput input(TmpFileName); - input.Skip(TmpFileSubstring - TmpFileContents); + input.Skip(TmpFileSubstring - TmpFileContents); TString s = input.ReadAll(); - UNIT_ASSERT_VALUES_EQUAL(s, "chivalrous plan"); - } - - { + UNIT_ASSERT_VALUES_EQUAL(s, "chivalrous plan"); + } + + { TUnbufferedFileOutput output(TFile::ForAppend(TmpFileName)); - output.Write(TmpFileContents, strlen(TmpFileContents)); - } - - { + output.Write(TmpFileContents, strlen(TmpFileContents)); + } + + { TUnbufferedFileInput input(TmpFileName); TString s = input.ReadAll(); UNIT_ASSERT_VALUES_EQUAL(s, TString::Join(TmpFileContents, TmpFileContents)); - } - } - + } + } + Y_UNIT_TEST(EmptyMapTest) { - TTempFile tmp(TmpFileName); - - { + TTempFile tmp(TmpFileName); + + { TUnbufferedFileOutput output(TmpFileName); - /* Write nothing. */ - } - - { - TMappedFileInput input(TmpFileName); + /* Write nothing. */ + } + + { + TMappedFileInput input(TmpFileName); TString s = input.ReadAll(); - UNIT_ASSERT(s.empty()); - } - } + UNIT_ASSERT(s.empty()); + } + } #ifdef _unix_ Y_UNIT_TEST(PipeReadLineTest) { @@ -71,4 +71,4 @@ Y_UNIT_TEST_SUITE(TFileTest) { close(fds[1]); } #endif -} +} diff --git a/util/stream/format.cpp b/util/stream/format.cpp index 3996130df5..35b2c83444 100644 --- a/util/stream/format.cpp +++ b/util/stream/format.cpp @@ -38,10 +38,10 @@ namespace NFormatPrivate { template <> void Out<NFormatPrivate::THumanReadableSize>(IOutputStream& stream, const NFormatPrivate::THumanReadableSize& value) { - ui64 base = value.Format == SF_BYTES ? 1024 : 1000; - ui64 base2 = base * base; - ui64 base3 = base * base2; - ui64 base4 = base * base3; + ui64 base = value.Format == SF_BYTES ? 1024 : 1000; + ui64 base2 = base * base; + ui64 base3 = base * base2; + ui64 base4 = base * base3; double v = value.Value; if (v < 0) { @@ -61,13 +61,13 @@ void Out<NFormatPrivate::THumanReadableSize>(IOutputStream& stream, const NForma NFormatPrivate::PrintDoubleShortly(stream, v / (double)base4) << 'T'; } - if (value.Format == SF_BYTES) { + if (value.Format == SF_BYTES) { if (v < base) { - stream << "B"; - } else { - stream << "iB"; - } - } + stream << "B"; + } else { + stream << "iB"; + } + } } template <> diff --git a/util/stream/format.h b/util/stream/format.h index b033208a1b..8f8526f479 100644 --- a/util/stream/format.h +++ b/util/stream/format.h @@ -5,29 +5,29 @@ #include <util/datetime/base.h> #include <util/generic/strbuf.h> -#include <util/generic/flags.h> +#include <util/generic/flags.h> #include <util/memory/tempbuf.h> #include <util/string/cast.h> -enum ENumberFormatFlag { - HF_FULL = 0x01, /**< Output number with leading zeros. */ - HF_ADDX = 0x02, /**< Output '0x' or '0b' before hex/bin digits. */ +enum ENumberFormatFlag { + HF_FULL = 0x01, /**< Output number with leading zeros. */ + HF_ADDX = 0x02, /**< Output '0x' or '0b' before hex/bin digits. */ }; -Y_DECLARE_FLAGS(ENumberFormat, ENumberFormatFlag) -Y_DECLARE_OPERATORS_FOR_FLAGS(ENumberFormat) +Y_DECLARE_FLAGS(ENumberFormat, ENumberFormatFlag) +Y_DECLARE_OPERATORS_FOR_FLAGS(ENumberFormat) -enum ESizeFormat { +enum ESizeFormat { SF_QUANTITY, /**< Base 1000, usual suffixes. 1100 gets turned into "1.1K". */ SF_BYTES, /**< Base 1024, byte suffix. 1100 gets turned into "1.07KiB". */ -}; - +}; + namespace NFormatPrivate { template <size_t Value> struct TLog2: std::integral_constant<size_t, TLog2<Value / 2>::value + 1> {}; - + template <> struct TLog2<1>: std::integral_constant<size_t, 0> {}; - + static inline void WriteChars(IOutputStream& os, char c, size_t count) { if (count == 0) return; @@ -90,65 +90,65 @@ namespace NFormatPrivate { return o; } - template <typename T, size_t Base> - struct TBaseNumber { - T Value; - ENumberFormat Flags; + template <typename T, size_t Base> + struct TBaseNumber { + T Value; + ENumberFormat Flags; - template <typename OtherT> - inline TBaseNumber(OtherT value, ENumberFormat flags) + template <typename OtherT> + inline TBaseNumber(OtherT value, ENumberFormat flags) : Value(value) , Flags(flags) { } }; - template <typename T, size_t Base> + template <typename T, size_t Base> using TUnsignedBaseNumber = TBaseNumber<std::make_unsigned_t<std::remove_cv_t<T>>, Base>; - template <typename T, size_t Base> + template <typename T, size_t Base> IOutputStream& operator<<(IOutputStream& stream, const TBaseNumber<T, Base>& value) { - char buf[8 * sizeof(T) + 1]; /* Add 1 for sign. */ - TStringBuf str(buf, IntToString<Base>(value.Value, buf, sizeof(buf))); + char buf[8 * sizeof(T) + 1]; /* Add 1 for sign. */ + TStringBuf str(buf, IntToString<Base>(value.Value, buf, sizeof(buf))); - if (str[0] == '-') { - stream << '-'; + if (str[0] == '-') { + stream << '-'; str.Skip(1); } - if (value.Flags & HF_ADDX) { - if (Base == 16) { + if (value.Flags & HF_ADDX) { + if (Base == 16) { stream << TStringBuf("0x"); - } else if (Base == 2) { + } else if (Base == 2) { stream << TStringBuf("0b"); - } + } } - if (value.Flags & HF_FULL) { - WriteChars(stream, '0', (8 * sizeof(T) + TLog2<Base>::value - 1) / TLog2<Base>::value - str.size()); + if (value.Flags & HF_FULL) { + WriteChars(stream, '0', (8 * sizeof(T) + TLog2<Base>::value - 1) / TLog2<Base>::value - str.size()); } - stream << str; - return stream; + stream << str; + return stream; } - template <typename Char, size_t Base> - struct TBaseText { - TBasicStringBuf<Char> Text; + template <typename Char, size_t Base> + struct TBaseText { + TBasicStringBuf<Char> Text; - inline TBaseText(const TBasicStringBuf<Char> text) + inline TBaseText(const TBasicStringBuf<Char> text) : Text(text) { } }; - template <typename Char, size_t Base> + template <typename Char, size_t Base> IOutputStream& operator<<(IOutputStream& os, const TBaseText<Char, Base>& text) { for (size_t i = 0; i < text.Text.size(); ++i) { if (i != 0) { os << ' '; } - os << TUnsignedBaseNumber<Char, Base>(text.Text[i], HF_FULL); + os << TUnsignedBaseNumber<Char, Base>(text.Text[i], HF_FULL); } return os; } @@ -182,27 +182,27 @@ namespace NFormatPrivate { struct THumanReadableSize { double Value; - ESizeFormat Format; + ESizeFormat Format; }; } -/** - * Output manipulator basically equivalent to `std::setw` and `std::setfill` - * combined. - * +/** + * Output manipulator basically equivalent to `std::setw` and `std::setfill` + * combined. + * * When written into a `IOutputStream`, writes out padding characters first, - * and then provided value. - * - * Example usage: - * @code - * stream << LeftPad(12345, 10, '0'); // Will output "0000012345" - * @endcode - * - * @param value Value to output. - * @param width Target total width. - * @param padc Character to use for padding. + * and then provided value. + * + * Example usage: + * @code + * stream << LeftPad(12345, 10, '0'); // Will output "0000012345" + * @endcode + * + * @param value Value to output. + * @param width Target total width. + * @param padc Character to use for padding. * @see RightPad - */ + */ template <typename T> static constexpr ::NFormatPrivate::TLeftPad<T> LeftPad(const T& value, const size_t width, const char padc = ' ') noexcept { return ::NFormatPrivate::TLeftPad<T>(value, width, padc); @@ -213,22 +213,22 @@ static constexpr ::NFormatPrivate::TLeftPad<const T*> LeftPad(const T (&value)[N return ::NFormatPrivate::TLeftPad<const T*>(value, width, padc); } -/** - * Output manipulator similar to `std::setw` and `std::setfill`. - * +/** + * Output manipulator similar to `std::setw` and `std::setfill`. + * * When written into a `IOutputStream`, writes provided value first, and then - * the padding characters. - * - * Example usage: - * @code + * the padding characters. + * + * Example usage: + * @code * stream << RightPad("column1", 10, ' '); // Will output "column1 " - * @endcode - * - * @param value Value to output. - * @param width Target total width. - * @param padc Character to use for padding. - * @see LeftPad - */ + * @endcode + * + * @param value Value to output. + * @param width Target total width. + * @param padc Character to use for padding. + * @see LeftPad + */ template <typename T> static constexpr ::NFormatPrivate::TRightPad<T> RightPad(const T& value, const size_t width, const char padc = ' ') noexcept { return ::NFormatPrivate::TRightPad<T>(value, width, padc); @@ -239,163 +239,163 @@ static constexpr ::NFormatPrivate::TRightPad<const T*> RightPad(const T (&value) return ::NFormatPrivate::TRightPad<const T*>(value, width, padc); } -/** - * Output manipulator similar to `std::setbase(16)`. - * +/** + * Output manipulator similar to `std::setbase(16)`. + * * When written into a `IOutputStream`, writes out the provided value in - * hexadecimal form. The value is treated as unsigned, even if its type is in - * fact signed. - * - * Example usage: - * @code - * stream << Hex(-1); // Will output "0xFFFFFFFF" - * stream << Hex(1ull); // Will output "0x0000000000000001" - * @endcode - * - * @param value Value to output. - * @param flags Output flags. - */ + * hexadecimal form. The value is treated as unsigned, even if its type is in + * fact signed. + * + * Example usage: + * @code + * stream << Hex(-1); // Will output "0xFFFFFFFF" + * stream << Hex(1ull); // Will output "0x0000000000000001" + * @endcode + * + * @param value Value to output. + * @param flags Output flags. + */ template <typename T> -static constexpr ::NFormatPrivate::TUnsignedBaseNumber<T, 16> Hex(const T& value, const ENumberFormat flags = HF_FULL | HF_ADDX) noexcept { - return {value, flags}; +static constexpr ::NFormatPrivate::TUnsignedBaseNumber<T, 16> Hex(const T& value, const ENumberFormat flags = HF_FULL | HF_ADDX) noexcept { + return {value, flags}; } -/** - * Output manipulator similar to `std::setbase(16)`. - * +/** + * Output manipulator similar to `std::setbase(16)`. + * * When written into a `IOutputStream`, writes out the provided value in - * hexadecimal form. - * - * Example usage: - * @code - * stream << SHex(-1); // Will output "-0x00000001" - * stream << SHex(1ull); // Will output "0x0000000000000001" - * @endcode - * - * @param value Value to output. - * @param flags Output flags. - */ + * hexadecimal form. + * + * Example usage: + * @code + * stream << SHex(-1); // Will output "-0x00000001" + * stream << SHex(1ull); // Will output "0x0000000000000001" + * @endcode + * + * @param value Value to output. + * @param flags Output flags. + */ template <typename T> -static constexpr ::NFormatPrivate::TBaseNumber<T, 16> SHex(const T& value, const ENumberFormat flags = HF_FULL | HF_ADDX) noexcept { - return {value, flags}; +static constexpr ::NFormatPrivate::TBaseNumber<T, 16> SHex(const T& value, const ENumberFormat flags = HF_FULL | HF_ADDX) noexcept { + return {value, flags}; } /** - * Output manipulator similar to `std::setbase(2)`. - * + * Output manipulator similar to `std::setbase(2)`. + * * When written into a `IOutputStream`, writes out the provided value in - * binary form. The value is treated as unsigned, even if its type is in - * fact signed. - * - * Example usage: - * @code - * stream << Bin(-1); // Will output "0b11111111111111111111111111111111" - * stream << Bin(1); // Will output "0b00000000000000000000000000000001" - * @endcode - * - * @param value Value to output. - * @param flags Output flags. - */ -template <typename T> -static constexpr ::NFormatPrivate::TUnsignedBaseNumber<T, 2> Bin(const T& value, const ENumberFormat flags = HF_FULL | HF_ADDX) noexcept { - return {value, flags}; -} - -/** - * Output manipulator similar to `std::setbase(2)`. - * + * binary form. The value is treated as unsigned, even if its type is in + * fact signed. + * + * Example usage: + * @code + * stream << Bin(-1); // Will output "0b11111111111111111111111111111111" + * stream << Bin(1); // Will output "0b00000000000000000000000000000001" + * @endcode + * + * @param value Value to output. + * @param flags Output flags. + */ +template <typename T> +static constexpr ::NFormatPrivate::TUnsignedBaseNumber<T, 2> Bin(const T& value, const ENumberFormat flags = HF_FULL | HF_ADDX) noexcept { + return {value, flags}; +} + +/** + * Output manipulator similar to `std::setbase(2)`. + * * When written into a `IOutputStream`, writes out the provided value in - * binary form. - * - * Example usage: - * @code - * stream << SBin(-1); // Will output "-0b00000000000000000000000000000001" - * stream << SBin(1); // Will output "0b00000000000000000000000000000001" - * @endcode - * - * @param value Value to output. - * @param flags Output flags. - */ -template <typename T> -static constexpr ::NFormatPrivate::TBaseNumber<T, 2> SBin(const T& value, const ENumberFormat flags = HF_FULL | HF_ADDX) noexcept { - return {value, flags}; -} - -/** - * Output manipulator for hexadecimal string output. - * + * binary form. + * + * Example usage: + * @code + * stream << SBin(-1); // Will output "-0b00000000000000000000000000000001" + * stream << SBin(1); // Will output "0b00000000000000000000000000000001" + * @endcode + * + * @param value Value to output. + * @param flags Output flags. + */ +template <typename T> +static constexpr ::NFormatPrivate::TBaseNumber<T, 2> SBin(const T& value, const ENumberFormat flags = HF_FULL | HF_ADDX) noexcept { + return {value, flags}; +} + +/** + * Output manipulator for hexadecimal string output. + * * When written into a `IOutputStream`, writes out the provided characters - * in hexadecimal form divided by space character. - * - * Example usage: - * @code + * in hexadecimal form divided by space character. + * + * Example usage: + * @code * stream << HexText(TStringBuf("abcи")); // Will output "61 62 63 D0 B8" - * stream << HexText(TWtringBuf(u"abcи")); // Will output "0061 0062 0063 0438" - * @endcode - * - * @param value String to output. - */ + * stream << HexText(TWtringBuf(u"abcи")); // Will output "0061 0062 0063 0438" + * @endcode + * + * @param value String to output. + */ template <typename TChar> -static inline ::NFormatPrivate::TBaseText<TChar, 16> HexText(const TBasicStringBuf<TChar> value) { - return ::NFormatPrivate::TBaseText<TChar, 16>(value); +static inline ::NFormatPrivate::TBaseText<TChar, 16> HexText(const TBasicStringBuf<TChar> value) { + return ::NFormatPrivate::TBaseText<TChar, 16>(value); } /** - * Output manipulator for binary string output. - * + * Output manipulator for binary string output. + * * When written into a `IOutputStream`, writes out the provided characters - * in binary form divided by space character. - * - * Example usage: - * @code + * in binary form divided by space character. + * + * Example usage: + * @code * stream << BinText(TStringBuf("aaa")); // Will output "01100001 01100001 01100001" - * @endcode - * - * @param value String to output. - */ -template <typename TChar> -static inline ::NFormatPrivate::TBaseText<TChar, 2> BinText(const TBasicStringBuf<TChar> value) { - return ::NFormatPrivate::TBaseText<TChar, 2>(value); -} - -/** - * Output manipulator for printing `TDuration` values. - * + * @endcode + * + * @param value String to output. + */ +template <typename TChar> +static inline ::NFormatPrivate::TBaseText<TChar, 2> BinText(const TBasicStringBuf<TChar> value) { + return ::NFormatPrivate::TBaseText<TChar, 2>(value); +} + +/** + * Output manipulator for printing `TDuration` values. + * * When written into a `IOutputStream`, writes out the provided `TDuration` - * in auto-adjusted human-readable format. - * - * Example usage: - * @code - * stream << HumanReadable(TDuration::MicroSeconds(100)); // Will output "100us" - * stream << HumanReadable(TDuration::Seconds(3672)); // Will output "1h 1m 12s" - * @endcode - * - * @param value Value to output. - */ + * in auto-adjusted human-readable format. + * + * Example usage: + * @code + * stream << HumanReadable(TDuration::MicroSeconds(100)); // Will output "100us" + * stream << HumanReadable(TDuration::Seconds(3672)); // Will output "1h 1m 12s" + * @endcode + * + * @param value Value to output. + */ static constexpr ::NFormatPrivate::THumanReadableDuration HumanReadable(const TDuration duration) noexcept { return ::NFormatPrivate::THumanReadableDuration(duration); } /** - * Output manipulator for writing out human-readable number of elements / memory - * amount in `ls -h` style. - * + * Output manipulator for writing out human-readable number of elements / memory + * amount in `ls -h` style. + * * When written into a `IOutputStream`, writes out the provided unsigned integer - * variable with small precision and a suffix (like 'K', 'M', 'G' for numbers, or - * 'B', 'KiB', 'MiB', 'GiB' for bytes). - * - * For quantities, base 1000 is used. For bytes, base is 1024. - * - * Example usage: - * @code + * variable with small precision and a suffix (like 'K', 'M', 'G' for numbers, or + * 'B', 'KiB', 'MiB', 'GiB' for bytes). + * + * For quantities, base 1000 is used. For bytes, base is 1024. + * + * Example usage: + * @code * stream << HumanReadableSize(1024, SF_QUANTITY); // Will output "1.02K" * stream << HumanReadableSize(1024, SF_BYTES); // Will output "1KiB" * stream << "average usage " << HumanReadableSize(100 / 3., SF_BYTES); // Will output "average usage "33.3B"" - * @endcode - * - * @param value Value to output. - * @param format Format to use. - */ + * @endcode + * + * @param value Value to output. + * @param format Format to use. + */ static constexpr ::NFormatPrivate::THumanReadableSize HumanReadableSize(const double size, ESizeFormat format) noexcept { return {size, format}; } @@ -404,40 +404,40 @@ void Time(IOutputStream& l); void TimeHumanReadable(IOutputStream& l); /** - * Output manipulator for adjusting precision of floating point values. - * + * Output manipulator for adjusting precision of floating point values. + * * When written into a `IOutputStream`, writes out the provided floating point - * variable with given precision. The behavior depends on provided `mode`. - * - * Example usage: - * @code - * stream << Prec(1.2345678901234567, PREC_AUTO); // Will output "1.2345678901234567" - * @endcode - * - * @param value float or double to output. - * @param mode Output mode. - * @param ndigits Number of significant digits (in `PREC_NDIGITS` and `PREC_POINT_DIGITS` mode). - * @see EFloatToStringMode - */ + * variable with given precision. The behavior depends on provided `mode`. + * + * Example usage: + * @code + * stream << Prec(1.2345678901234567, PREC_AUTO); // Will output "1.2345678901234567" + * @endcode + * + * @param value float or double to output. + * @param mode Output mode. + * @param ndigits Number of significant digits (in `PREC_NDIGITS` and `PREC_POINT_DIGITS` mode). + * @see EFloatToStringMode + */ template <typename T> static constexpr ::NFormatPrivate::TFloatPrecision<T> Prec(const T& value, const EFloatToStringMode mode, const int ndigits = 0) noexcept { return {value, mode, ndigits}; } /** - * Output manipulator for adjusting precision of floating point values. - * + * Output manipulator for adjusting precision of floating point values. + * * When written into a `IOutputStream`, writes out the provided floating point - * variable with given precision. The behavior is equivalent to `Prec(value, PREC_NDIGITS, ndigits)`. - * - * Example usage: - * @code - * stream << Prec(1.2345678901234567, 3); // Will output "1.23" - * @endcode - * - * @param value float or double to output. - * @param ndigits Number of significant digits. - */ + * variable with given precision. The behavior is equivalent to `Prec(value, PREC_NDIGITS, ndigits)`. + * + * Example usage: + * @code + * stream << Prec(1.2345678901234567, 3); // Will output "1.23" + * @endcode + * + * @param value float or double to output. + * @param ndigits Number of significant digits. + */ template <typename T> static constexpr ::NFormatPrivate::TFloatPrecision<T> Prec(const T& value, const int ndigits) noexcept { return {value, PREC_NDIGITS, ndigits}; diff --git a/util/stream/format_ut.cpp b/util/stream/format_ut.cpp index 43245aeb48..85fae033c2 100644 --- a/util/stream/format_ut.cpp +++ b/util/stream/format_ut.cpp @@ -9,7 +9,7 @@ Y_UNIT_TEST_SUITE(TOutputStreamFormattingTest) { ss << LeftPad(10, 4, '0'); UNIT_ASSERT_VALUES_EQUAL("0010", ss.Str()); - ss.Clear(); + ss.Clear(); ss << LeftPad(222, 1); UNIT_ASSERT_VALUES_EQUAL("222", ss.Str()); } @@ -19,7 +19,7 @@ Y_UNIT_TEST_SUITE(TOutputStreamFormattingTest) { ss << RightPad("aa", 4); UNIT_ASSERT_VALUES_EQUAL("aa ", ss.Str()); - ss.Clear(); + ss.Clear(); ss << RightPad("aa", 1); UNIT_ASSERT_VALUES_EQUAL("aa", ss.Str()); } @@ -63,24 +63,24 @@ Y_UNIT_TEST_SUITE(TOutputStreamFormattingTest) { Y_UNIT_TEST(TestBin) { UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<ui32>(2), nullptr)), "10"); UNIT_ASSERT_VALUES_EQUAL(ToString(SBin(static_cast<i32>(-2), nullptr)), "-10"); - UNIT_ASSERT_VALUES_EQUAL(ToString(SBin(static_cast<i32>(-2))), "-0b00000000000000000000000000000010"); - UNIT_ASSERT_VALUES_EQUAL(ToString(SBin(static_cast<i32>(-2), HF_FULL)), "-00000000000000000000000000000010"); + UNIT_ASSERT_VALUES_EQUAL(ToString(SBin(static_cast<i32>(-2))), "-0b00000000000000000000000000000010"); + UNIT_ASSERT_VALUES_EQUAL(ToString(SBin(static_cast<i32>(-2), HF_FULL)), "-00000000000000000000000000000010"); UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<ui32>(15), nullptr)), "1111"); UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<ui32>(1))), "0b00000000000000000000000000000001"); - UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<ui32>(-1))), "0b11111111111111111111111111111111"); - UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<i32>(-1))), "0b11111111111111111111111111111111"); + UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<ui32>(-1))), "0b11111111111111111111111111111111"); + UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<i32>(-1))), "0b11111111111111111111111111111111"); UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<i32>(-1), nullptr)), "11111111111111111111111111111111"); - UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<ui32>(256))), "0b00000000000000000000000100000000"); - UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<ui8>(16))), "0b00010000"); - UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<ui64>(1234587912357ull))), "0b0000000000000000000000010001111101110011001011001000100010100101"); - } - + UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<ui32>(256))), "0b00000000000000000000000100000000"); + UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<ui8>(16))), "0b00010000"); + UNIT_ASSERT_VALUES_EQUAL(ToString(Bin(static_cast<ui64>(1234587912357ull))), "0b0000000000000000000000010001111101110011001011001000100010100101"); + } + Y_UNIT_TEST(TestBinText) { UNIT_ASSERT_VALUES_EQUAL(ToString(BinText(TStringBuf("\1"))), "00000001"); UNIT_ASSERT_VALUES_EQUAL(ToString(BinText(TStringBuf("\1\1"))), "00000001 00000001"); UNIT_ASSERT_VALUES_EQUAL(ToString(BinText(TStringBuf("aaa"))), "01100001 01100001 01100001"); - } - + } + Y_UNIT_TEST(TestPrec) { TStringStream ss; ss << Prec(1.2345678901234567, PREC_AUTO); @@ -96,13 +96,13 @@ Y_UNIT_TEST_SUITE(TOutputStreamFormattingTest) { } Y_UNIT_TEST(TestHumanReadableSize1000) { - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(0, SF_QUANTITY)), "0"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(0, SF_QUANTITY)), "0"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1, SF_QUANTITY)), "1"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1000, SF_QUANTITY)), "1K"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1234567, SF_QUANTITY)), "1.23M"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(12345678, SF_QUANTITY)), "12.3M"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(12345678 * 1000ull, SF_QUANTITY)), "12.3G"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1, SF_QUANTITY)), "1"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1000, SF_QUANTITY)), "1K"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1234567, SF_QUANTITY)), "1.23M"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(12345678, SF_QUANTITY)), "12.3M"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(12345678 * 1000ull, SF_QUANTITY)), "12.3G"); UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(-1, SF_QUANTITY)), "-1"); UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(-1000, SF_QUANTITY)), "-1K"); @@ -112,17 +112,17 @@ Y_UNIT_TEST_SUITE(TOutputStreamFormattingTest) { } Y_UNIT_TEST(TestHumanReadableSize1024) { - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(0, SF_BYTES)), "0B"); - - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(100, SF_BYTES)), "100B"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1024, SF_BYTES)), "1KiB"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(2.25 * 1024 * 1024, SF_BYTES)), "2.25MiB"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(2.5 * 1024, SF_BYTES)), "2.5KiB"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(45.3 * 1024, SF_BYTES)), "45.3KiB"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1024 * 1024, SF_BYTES)), "1MiB"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(5 * 1024 * 1024, SF_BYTES)), "5MiB"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1236 * 1024 * 1024, SF_BYTES)), "1.21GiB"); - UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1024ull * 1024 * 1024 * 1024, SF_BYTES)), "1TiB"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(0, SF_BYTES)), "0B"); + + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(100, SF_BYTES)), "100B"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1024, SF_BYTES)), "1KiB"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(2.25 * 1024 * 1024, SF_BYTES)), "2.25MiB"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(2.5 * 1024, SF_BYTES)), "2.5KiB"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(45.3 * 1024, SF_BYTES)), "45.3KiB"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1024 * 1024, SF_BYTES)), "1MiB"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(5 * 1024 * 1024, SF_BYTES)), "5MiB"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1236 * 1024 * 1024, SF_BYTES)), "1.21GiB"); + UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(1024ull * 1024 * 1024 * 1024, SF_BYTES)), "1TiB"); UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(100 / 3., SF_BYTES)), "33.3B"); UNIT_ASSERT_VALUES_EQUAL(ToString(HumanReadableSize(-100, SF_BYTES)), "-100B"); diff --git a/util/stream/holder.h b/util/stream/holder.h index c60a4e510c..edd4e98ea6 100644 --- a/util/stream/holder.h +++ b/util/stream/holder.h @@ -1,44 +1,44 @@ #pragma once #include <util/generic/ptr.h> - + #include <utility> -#include <type_traits> +#include <type_traits> class IInputStream; class IOutputStream; -namespace NPrivate { +namespace NPrivate { template <class Stream, bool isInput = std::is_base_of<IInputStream, Stream>::value> struct TStreamBase { using TType = IInputStream; }; - + template <class Stream> struct TStreamBase<Stream, false> { using TType = IOutputStream; }; - + } - -/** - * An ownership-gaining wrapper for proxy streams. - * - * Example usage: - * \code - * TCountingInput* input = new THoldingStream<TCountingInput>(new TStringInput(s)); - * \encode - * - * In this example, resulting counting input also owns a string input that it - * was constructed on top of. - */ -template <class Base, class StreamBase = typename ::NPrivate::TStreamBase<Base>::TType> + +/** + * An ownership-gaining wrapper for proxy streams. + * + * Example usage: + * \code + * TCountingInput* input = new THoldingStream<TCountingInput>(new TStringInput(s)); + * \encode + * + * In this example, resulting counting input also owns a string input that it + * was constructed on top of. + */ +template <class Base, class StreamBase = typename ::NPrivate::TStreamBase<Base>::TType> class THoldingStream: private THolder<StreamBase>, public Base { public: - template <class... Args> + template <class... Args> inline THoldingStream(THolder<StreamBase> stream, Args&&... args) : THolder<StreamBase>(std::move(stream)) - , Base(this->Get(), std::forward<Args>(args)...) + , Base(this->Get(), std::forward<Args>(args)...) { } }; diff --git a/util/stream/input.cpp b/util/stream/input.cpp index 6e8170f2f9..a0e4ce36b8 100644 --- a/util/stream/input.cpp +++ b/util/stream/input.cpp @@ -1,6 +1,6 @@ #include "input.h" -#include "output.h" -#include "str.h" +#include "output.h" +#include "str.h" #include <util/charset/wide.h> #include <util/memory/tempbuf.h> @@ -21,15 +21,15 @@ size_t IInputStream::DoReadTo(TString& st, char to) { char ch; if (!Read(&ch, 1)) { - return 0; + return 0; } st.clear(); - size_t result = 0; + size_t result = 0; do { ++result; - + if (ch == to) { break; } @@ -37,23 +37,23 @@ size_t IInputStream::DoReadTo(TString& st, char to) { st += ch; } while (Read(&ch, 1)); - return result; + return result; } ui64 IInputStream::DoReadAll(IOutputStream& out) { - TTempBuf buffer; - void* ptr = buffer.Data(); - size_t size = buffer.Size(); - - ui64 result = 0; - while (size_t read = Read(ptr, size)) { - out.Write(ptr, read); - result += read; - } - - return result; -} - + TTempBuf buffer; + void* ptr = buffer.Data(); + size_t size = buffer.Size(); + + ui64 result = 0; + while (size_t read = Read(ptr, size)) { + out.Write(ptr, read); + result += read; + } + + return result; +} + size_t IInputStream::Load(void* buf_in, size_t len) { char* buf = (char*)buf_in; @@ -79,24 +79,24 @@ void IInputStream::LoadOrFail(void* buf, size_t len) { } size_t IInputStream::ReadLine(TString& st) { - const size_t ret = ReadTo(st, '\n'); + const size_t ret = ReadTo(st, '\n'); if (ret && !st.empty() && st.back() == '\r') { st.pop_back(); } - return ret; + return ret; } size_t IInputStream::ReadLine(TUtf16String& w) { TString s; - size_t result = ReadLine(s); + size_t result = ReadLine(s); - if (result) { - UTF8ToWide(s, w); - } - - return result; + if (result) { + UTF8ToWide(s, w); + } + + return result; } TString IInputStream::ReadLine() { @@ -147,21 +147,21 @@ size_t IInputStream::DoSkip(size_t sz) { TString IInputStream::ReadAll() { TString result; - TStringOutput stream(result); - - DoReadAll(stream); + TStringOutput stream(result); - return result; -} + DoReadAll(stream); + + return result; +} ui64 IInputStream::ReadAll(IOutputStream& out) { - return DoReadAll(out); + return DoReadAll(out); } ui64 TransferData(IInputStream* in, IOutputStream* out) { - return in->ReadAll(*out); -} - + return in->ReadAll(*out); +} + namespace { struct TStdIn: public IInputStream { ~TStdIn() override = default; diff --git a/util/stream/input.h b/util/stream/input.h index f0d5807ed2..afedc6fb29 100644 --- a/util/stream/input.h +++ b/util/stream/input.h @@ -5,15 +5,15 @@ #include <util/system/defaults.h> class IOutputStream; - -/** - * @addtogroup Streams_Base - * @{ - */ - -/** - * Abstract input stream. - */ + +/** + * @addtogroup Streams_Base + * @{ + */ + +/** + * Abstract input stream. + */ class IInputStream: public TNonCopyable { public: IInputStream() noexcept; @@ -26,16 +26,16 @@ public: return *this; } - /** - * Reads some data from the stream. Note that this function might read less - * data than what was requested. Use `Load` function if you want to read as - * much data as possible. - * - * @param buf Buffer to read into. - * @param len Number of bytes to read. - * @returns Number of bytes that were actually read. - * A return value of zero signals end of stream. - */ + /** + * Reads some data from the stream. Note that this function might read less + * data than what was requested. Use `Load` function if you want to read as + * much data as possible. + * + * @param buf Buffer to read into. + * @param len Number of bytes to read. + * @returns Number of bytes that were actually read. + * A return value of zero signals end of stream. + */ inline size_t Read(void* buf, size_t len) { if (len == 0) { return 0; @@ -44,230 +44,230 @@ public: return DoRead(buf, len); } - /** - * Reads one character from the stream. - * - * @param[out] c Character to read. - * @returns Whether the character was read. - * A return value of false signals the end - * of stream. - */ + /** + * Reads one character from the stream. + * + * @param[out] c Character to read. + * @returns Whether the character was read. + * A return value of false signals the end + * of stream. + */ inline bool ReadChar(char& c) { return DoRead(&c, 1) > 0; } - /** - * Reads all characters from the stream until the given character is - * encountered, and stores them into the given string. The character itself - * is read from the stream, but not stored in the string. - * - * @param[out] st String to read into. - * @param ch Character to stop at. - * @returns Total number of characters read from the stream. - * A return value of zero signals end of stream. - */ + /** + * Reads all characters from the stream until the given character is + * encountered, and stores them into the given string. The character itself + * is read from the stream, but not stored in the string. + * + * @param[out] st String to read into. + * @param ch Character to stop at. + * @returns Total number of characters read from the stream. + * A return value of zero signals end of stream. + */ inline size_t ReadTo(TString& st, char ch) { return DoReadTo(st, ch); } - /** - * Reads the requested amount of data from the stream. Unlike `Read`, this - * function stops only when the requested amount of data is read, or when - * end of stream is reached. - * - * @param buf Buffer to read into. - * @param len Number of bytes to read. - * @returns Number of bytes that were actually read. - * A return value different from `len` - * signals end of stream. - */ + /** + * Reads the requested amount of data from the stream. Unlike `Read`, this + * function stops only when the requested amount of data is read, or when + * end of stream is reached. + * + * @param buf Buffer to read into. + * @param len Number of bytes to read. + * @returns Number of bytes that were actually read. + * A return value different from `len` + * signals end of stream. + */ size_t Load(void* buf, size_t len); - /** - * Reads the requested amount of data from the stream, or fails with an - * exception if unable to do so. - * - * @param buf Buffer to read into. - * @param len Number of bytes to read. - * @see Load - */ + /** + * Reads the requested amount of data from the stream, or fails with an + * exception if unable to do so. + * + * @param buf Buffer to read into. + * @param len Number of bytes to read. + * @see Load + */ void LoadOrFail(void* buf, size_t len); - /** - * Reads all data from this stream and returns it as a string. - * - * @returns Contents of this stream as a string. - */ + /** + * Reads all data from this stream and returns it as a string. + * + * @returns Contents of this stream as a string. + */ TString ReadAll(); - /** - * Reads all data from this stream and writes it into a provided output - * stream. - * - * @param out Output stream to use. - * @returns Total number of characters read from the stream. - */ + /** + * Reads all data from this stream and writes it into a provided output + * stream. + * + * @param out Output stream to use. + * @returns Total number of characters read from the stream. + */ ui64 ReadAll(IOutputStream& out); - - /** - * Reads all data from the stream until the first occurrence of '\n'. Also - * handles Windows line breaks correctly. - * - * @returns Next line read from this stream, - * excluding the line terminator. - * @throws yexception If no data could be read from a stream - * because end of stream has already been - * reached. - */ + + /** + * Reads all data from the stream until the first occurrence of '\n'. Also + * handles Windows line breaks correctly. + * + * @returns Next line read from this stream, + * excluding the line terminator. + * @throws yexception If no data could be read from a stream + * because end of stream has already been + * reached. + */ TString ReadLine(); - /** - * Reads all characters from the stream until the given character is - * encountered and returns them as a string. The character itself is read - * from the stream, but not stored in the string. - * - * @param ch Character to stop at. - * @returns String containing all the characters read. - * @throws yexception If no data could be read from a stream - * because end of stream has already been - * reached. - */ + /** + * Reads all characters from the stream until the given character is + * encountered and returns them as a string. The character itself is read + * from the stream, but not stored in the string. + * + * @param ch Character to stop at. + * @returns String containing all the characters read. + * @throws yexception If no data could be read from a stream + * because end of stream has already been + * reached. + */ TString ReadTo(char ch); - /** - * Reads all data from the stream until the first occurrence of '\n' and - * stores it into provided string. Also handles Windows line breaks correctly. - * - * @param[out] st String to store read characters into, - * excluding the line terminator. - * @returns Total number of characters read from the stream. - * A return value of zero signals end of stream. - */ + /** + * Reads all data from the stream until the first occurrence of '\n' and + * stores it into provided string. Also handles Windows line breaks correctly. + * + * @param[out] st String to store read characters into, + * excluding the line terminator. + * @returns Total number of characters read from the stream. + * A return value of zero signals end of stream. + */ size_t ReadLine(TString& st); - /** - * Reads UTF8 encoded characters from the stream the first occurrence of '\n', - * converts them into wide ones, and stores into provided string. Also handles - * Windows line breaks correctly. - * - * @param[out] w Wide string to store read characters into, - * excluding the line terminator. - * @returns Total number of characters read from the stream. - * A return value of zero signals end of stream. - */ + /** + * Reads UTF8 encoded characters from the stream the first occurrence of '\n', + * converts them into wide ones, and stores into provided string. Also handles + * Windows line breaks correctly. + * + * @param[out] w Wide string to store read characters into, + * excluding the line terminator. + * @returns Total number of characters read from the stream. + * A return value of zero signals end of stream. + */ size_t ReadLine(TUtf16String& w); - /** - * Skips some data from the stream without reading / copying it. Note that - * this function might skip less data than what was requested. - * - * @param len Number of bytes to skip. - * @returns Number of bytes that were actually skipped. - * A return value of zero signals end of stream. - */ + /** + * Skips some data from the stream without reading / copying it. Note that + * this function might skip less data than what was requested. + * + * @param len Number of bytes to skip. + * @returns Number of bytes that were actually skipped. + * A return value of zero signals end of stream. + */ size_t Skip(size_t len); protected: /** - * Reads some data from the stream. Might read less data than what was - * requested. - * - * @param buf Buffer to read into. - * @param len Number of bytes to read. - * @returns Number of bytes that were actually read. - * A return value of zero signals end of stream. - * @throws yexception If IO error occurs. - */ + * Reads some data from the stream. Might read less data than what was + * requested. + * + * @param buf Buffer to read into. + * @param len Number of bytes to read. + * @returns Number of bytes that were actually read. + * A return value of zero signals end of stream. + * @throws yexception If IO error occurs. + */ virtual size_t DoRead(void* buf, size_t len) = 0; - + /** - * Skips some data from the stream. Might skip less data than what was - * requested. - * - * @param len Number of bytes to skip. - * @returns Number of bytes that were actually skipped. - * A return value of zero signals end of stream. - * @throws yexception If IO error occurs. - */ + * Skips some data from the stream. Might skip less data than what was + * requested. + * + * @param len Number of bytes to skip. + * @returns Number of bytes that were actually skipped. + * A return value of zero signals end of stream. + * @throws yexception If IO error occurs. + */ virtual size_t DoSkip(size_t len); - + /** - * Reads all characters from the stream until the given character is - * encountered, and stores them into the given string. The character itself - * is read from the stream, but not stored in the string. - * - * Provided string is cleared only if there is data in the stream. - * - * @param[out] st String to read into. - * @param ch Character to stop at. - * @returns Total number of characters read from the stream. - * A return value of zero signals end of stream. - * @throws yexception If IO error occurs. - */ + * Reads all characters from the stream until the given character is + * encountered, and stores them into the given string. The character itself + * is read from the stream, but not stored in the string. + * + * Provided string is cleared only if there is data in the stream. + * + * @param[out] st String to read into. + * @param ch Character to stop at. + * @returns Total number of characters read from the stream. + * A return value of zero signals end of stream. + * @throws yexception If IO error occurs. + */ virtual size_t DoReadTo(TString& st, char ch); - - /** - * Reads all data from this stream and writes it into a provided output - * stream. - * - * @param out Output stream to use. - * @returns Total number of characters read from - * this stream. - * @throws yexception If IO error occurs. - */ + + /** + * Reads all data from this stream and writes it into a provided output + * stream. + * + * @param out Output stream to use. + * @returns Total number of characters read from + * this stream. + * @throws yexception If IO error occurs. + */ virtual ui64 DoReadAll(IOutputStream& out); }; -/** - * Transfers all data from the given input stream into the given output stream. - * - * @param in Input stream. - * @param out Output stream. - */ +/** + * Transfers all data from the given input stream into the given output stream. + * + * @param in Input stream. + * @param out Output stream. + */ ui64 TransferData(IInputStream* in, IOutputStream* out); - -/** + +/** * `operator>>` for `IInputStream` by default delegates to this function. - * - * Note that while `operator>>` uses overloading (and thus argument-dependent - * lookup), `In` uses template specializations. This makes it possible to - * have a single `In` declaration, and then just provide specializations in - * cpp files, letting the linker figure everything else out. This approach - * reduces compilation times. - * - * However, if the flexibility of overload resolution is needed, then one should - * just overload `operator>>`. - * - * @param in Input stream to read from. - * @param[out] value Value to read. - * @throws `yexception` on invalid input or end of stream. + * + * Note that while `operator>>` uses overloading (and thus argument-dependent + * lookup), `In` uses template specializations. This makes it possible to + * have a single `In` declaration, and then just provide specializations in + * cpp files, letting the linker figure everything else out. This approach + * reduces compilation times. + * + * However, if the flexibility of overload resolution is needed, then one should + * just overload `operator>>`. + * + * @param in Input stream to read from. + * @param[out] value Value to read. + * @throws `yexception` on invalid input or end of stream. * @see Out(IOutputStream&, T&) - */ + */ template <typename T> void In(IInputStream& in, T& value); -/** - * Reads a value from the stream. - * - * @param in Input stream to read from. - * @param[out] value Value to read. - * @returns Input stream. - * @throws `yexception` on invalid input or end of stream. +/** + * Reads a value from the stream. + * + * @param in Input stream to read from. + * @param[out] value Value to read. + * @returns Input stream. + * @throws `yexception` on invalid input or end of stream. * @see operator<<(IOutputStream&, T&) - */ + */ template <typename T> inline IInputStream& operator>>(IInputStream& in, T& value) { - In<T>(in, value); + In<T>(in, value); return in; } -namespace NPrivate { +namespace NPrivate { IInputStream& StdInStream() noexcept; -} - -/** - * Standard input stream. - */ -#define Cin (::NPrivate::StdInStream()) +} + +/** + * Standard input stream. + */ +#define Cin (::NPrivate::StdInStream()) -/** @} */ +/** @} */ diff --git a/util/stream/input_ut.cpp b/util/stream/input_ut.cpp index 4a93f5458e..ba5c1068ee 100644 --- a/util/stream/input_ut.cpp +++ b/util/stream/input_ut.cpp @@ -1,11 +1,11 @@ -#include "input.h" -#include "output.h" - +#include "input.h" +#include "output.h" + #include <library/cpp/testing/unittest/registar.h> - + #include <util/system/file.h> -#include <util/system/yassert.h> - +#include <util/system/yassert.h> + #ifdef _win_ #include <io.h> #endif @@ -42,94 +42,94 @@ private: }; class TNoInput: public IInputStream { -public: +public: TNoInput(ui64 size) : Size_(size) { } - -protected: + +protected: size_t DoRead(void*, size_t len) override { - len = Min(static_cast<ui64>(len), Size_); - Size_ -= len; - return len; - } - -private: - ui64 Size_; -}; - + len = Min(static_cast<ui64>(len), Size_); + Size_ -= len; + return len; + } + +private: + ui64 Size_; +}; + class TNoOutput: public IOutputStream { -public: +public: TNoOutput() = default; - -protected: + +protected: void DoWrite(const void*, size_t) override { } -}; - +}; + class TSimpleStringInput: public IInputStream { -public: +public: TSimpleStringInput(const TString& string) : String_(string) { } - -protected: + +protected: size_t DoRead(void* buf, size_t len) override { Y_ASSERT(len != 0); - + if (String_.empty()) { - return 0; + return 0; } - - *static_cast<char*>(buf) = String_[0]; - String_.remove(0, 1); - return 1; - } - -private: + + *static_cast<char*>(buf) = String_[0]; + String_.remove(0, 1); + return 1; + } + +private: TString String_; -}; - +}; + Y_UNIT_TEST_SUITE(TInputTest) { Y_UNIT_TEST(BigTransfer) { - ui64 size = 1024ull * 1024ull * 1024ull * 5; - TNoInput input(size); - TNoOutput output; - - ui64 transferred = TransferData(&input, &output); - - UNIT_ASSERT_VALUES_EQUAL(transferred, size); - } - + ui64 size = 1024ull * 1024ull * 1024ull * 5; + TNoInput input(size); + TNoOutput output; + + ui64 transferred = TransferData(&input, &output); + + UNIT_ASSERT_VALUES_EQUAL(transferred, size); + } + Y_UNIT_TEST(TestReadTo) { - /* This one tests default implementation of ReadTo. */ - - TSimpleStringInput in("0123456789abc"); - + /* This one tests default implementation of ReadTo. */ + + TSimpleStringInput in("0123456789abc"); + TString t; - UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '7'), 8); - UNIT_ASSERT_VALUES_EQUAL(t, "0123456"); - UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, 'z'), 5); - UNIT_ASSERT_VALUES_EQUAL(t, "89abc"); - UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, 'z'), 0); - UNIT_ASSERT_VALUES_EQUAL(t, "89abc"); - } - + UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '7'), 8); + UNIT_ASSERT_VALUES_EQUAL(t, "0123456"); + UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, 'z'), 5); + UNIT_ASSERT_VALUES_EQUAL(t, "89abc"); + UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, 'z'), 0); + UNIT_ASSERT_VALUES_EQUAL(t, "89abc"); + } + Y_UNIT_TEST(TestReadLine) { - TSimpleStringInput in("1\n22\n333"); - + TSimpleStringInput in("1\n22\n333"); + TString t; - UNIT_ASSERT_VALUES_EQUAL(in.ReadLine(t), 2); - UNIT_ASSERT_VALUES_EQUAL(t, "1"); - UNIT_ASSERT_VALUES_EQUAL(in.ReadLine(t), 3); - UNIT_ASSERT_VALUES_EQUAL(t, "22"); - UNIT_ASSERT_VALUES_EQUAL(in.ReadLine(t), 3); - UNIT_ASSERT_VALUES_EQUAL(t, "333"); - UNIT_ASSERT_VALUES_EQUAL(in.ReadLine(t), 0); - UNIT_ASSERT_VALUES_EQUAL(t, "333"); - } + UNIT_ASSERT_VALUES_EQUAL(in.ReadLine(t), 2); + UNIT_ASSERT_VALUES_EQUAL(t, "1"); + UNIT_ASSERT_VALUES_EQUAL(in.ReadLine(t), 3); + UNIT_ASSERT_VALUES_EQUAL(t, "22"); + UNIT_ASSERT_VALUES_EQUAL(in.ReadLine(t), 3); + UNIT_ASSERT_VALUES_EQUAL(t, "333"); + UNIT_ASSERT_VALUES_EQUAL(in.ReadLine(t), 0); + UNIT_ASSERT_VALUES_EQUAL(t, "333"); + } Y_UNIT_TEST(TestStdInReadTo) { std::pair<std::pair<TStringBuf, char>, TStringBuf> testPairs[] = { @@ -154,4 +154,4 @@ Y_UNIT_TEST_SUITE(TInputTest) { }); } } -} +} diff --git a/util/stream/labeled.h b/util/stream/labeled.h index 2cc539d241..a4edffd715 100644 --- a/util/stream/labeled.h +++ b/util/stream/labeled.h @@ -2,18 +2,18 @@ #include <util/generic/va_args.h> -/** - * Generates an output sequence for the provided expressions that is formatted - * as a labeled comma-separated list. - * - * Example usage: - * @code - * int a = 1, b = 2, c = 3; - * stream << LabeledOutput(a, b, c, a + b + c); - * // Outputs "a = 1, b = 2, c = 3, a + b + c = 6" - * @endcode - */ +/** + * Generates an output sequence for the provided expressions that is formatted + * as a labeled comma-separated list. + * + * Example usage: + * @code + * int a = 1, b = 2, c = 3; + * stream << LabeledOutput(a, b, c, a + b + c); + * // Outputs "a = 1, b = 2, c = 3, a + b + c = 6" + * @endcode + */ #define LabeledOutput(...) "" Y_PASS_VA_ARGS(Y_MAP_ARGS_WITH_LAST(__LABELED_OUTPUT_NONLAST__, __LABELED_OUTPUT_IMPL__, __VA_ARGS__)) - + #define __LABELED_OUTPUT_IMPL__(x) << #x " = " << (x) #define __LABELED_OUTPUT_NONLAST__(x) __LABELED_OUTPUT_IMPL__(x) << ", " diff --git a/util/stream/length.cpp b/util/stream/length.cpp index 9907fe2ac9..1ab3c6aadf 100644 --- a/util/stream/length.cpp +++ b/util/stream/length.cpp @@ -27,21 +27,21 @@ size_t TCountingInput::DoSkip(size_t len) { Count_ += ret; return ret; } - + size_t TCountingInput::DoReadTo(TString& st, char ch) { - const size_t ret = Slave_->ReadTo(st, ch); - Count_ += ret; - return ret; -} - + const size_t ret = Slave_->ReadTo(st, ch); + Count_ += ret; + return ret; +} + ui64 TCountingInput::DoReadAll(IOutputStream& out) { - const ui64 ret = Slave_->ReadAll(out); - Count_ += ret; - return ret; -} - -void TCountingOutput::DoWrite(const void* buf, size_t len) { - Slave_->Write(buf, len); - - Count_ += len; -} + const ui64 ret = Slave_->ReadAll(out); + Count_ += ret; + return ret; +} + +void TCountingOutput::DoWrite(const void* buf, size_t len) { + Slave_->Write(buf, len); + + Count_ += len; +} diff --git a/util/stream/length.h b/util/stream/length.h index 4d508ae24d..049b91c05b 100644 --- a/util/stream/length.h +++ b/util/stream/length.h @@ -1,17 +1,17 @@ #pragma once #include "input.h" -#include "output.h" +#include "output.h" #include <util/generic/utility.h> -/** - * Proxy input stream that can read a limited number of characters from a slave - * stream. - * - * This can be useful for breaking up the slave stream into small chunks and - * treat these as separate streams. - */ +/** + * Proxy input stream that can read a limited number of characters from a slave + * stream. + * + * This can be useful for breaking up the slave stream into small chunks and + * treat these as separate streams. + */ class TLengthLimitedInput: public IInputStream { public: inline TLengthLimitedInput(IInputStream* slave, ui64 length) noexcept @@ -35,9 +35,9 @@ private: ui64 Length_; }; -/** - * Proxy input stream that counts the number of characters read. - */ +/** + * Proxy input stream that counts the number of characters read. + */ class TCountingInput: public IInputStream { public: inline TCountingInput(IInputStream* slave) noexcept @@ -48,10 +48,10 @@ public: ~TCountingInput() override = default; - /** - * \returns The total number of characters read from - * this stream. - */ + /** + * \returns The total number of characters read from + * this stream. + */ inline ui64 Counter() const noexcept { return Count_; } @@ -66,35 +66,35 @@ private: IInputStream* Slave_; ui64 Count_; }; - -/** - * Proxy output stream that counts the number of characters written. - */ + +/** + * Proxy output stream that counts the number of characters written. + */ class TCountingOutput: public IOutputStream { -public: +public: inline TCountingOutput(IOutputStream* slave) noexcept : Slave_(slave) , Count_() { - } - + } + ~TCountingOutput() override = default; - + TCountingOutput(TCountingOutput&&) noexcept = default; TCountingOutput& operator=(TCountingOutput&&) noexcept = default; - /** - * \returns The total number of characters written - * into this stream. - */ + /** + * \returns The total number of characters written + * into this stream. + */ inline ui64 Counter() const noexcept { - return Count_; - } - -private: + return Count_; + } + +private: void DoWrite(const void* buf, size_t len) override; - -private: + +private: IOutputStream* Slave_; - ui64 Count_; -}; + ui64 Count_; +}; diff --git a/util/stream/length_ut.cpp b/util/stream/length_ut.cpp index 8968448954..beecc0f4be 100644 --- a/util/stream/length_ut.cpp +++ b/util/stream/length_ut.cpp @@ -1,52 +1,52 @@ -#include "length.h" - +#include "length.h" + #include <library/cpp/testing/unittest/registar.h> - + #include <util/generic/string.h> - + Y_UNIT_TEST_SUITE(TestLengthIO) { Y_UNIT_TEST(TestLengthLimitedInput) { - char buf[16]; - - TStringStream s1("abcd"); - TLengthLimitedInput l1(&s1, 2); - UNIT_ASSERT_VALUES_EQUAL(l1.Load(buf, 3), 2); - UNIT_ASSERT_VALUES_EQUAL(l1.Read(buf, 1), 0); - } - + char buf[16]; + + TStringStream s1("abcd"); + TLengthLimitedInput l1(&s1, 2); + UNIT_ASSERT_VALUES_EQUAL(l1.Load(buf, 3), 2); + UNIT_ASSERT_VALUES_EQUAL(l1.Read(buf, 1), 0); + } + Y_UNIT_TEST(TestCountingInput) { - char buf[16]; - - TStringStream s1("abc\ndef\n"); - TCountingInput l1(&s1); - + char buf[16]; + + TStringStream s1("abc\ndef\n"); + TCountingInput l1(&s1); + TString s; - l1.ReadLine(s); - UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 4); - - l1.Load(buf, 1); - UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 5); - - l1.Skip(1); - UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 6); - - l1.ReadLine(s); - UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 8); - } - + l1.ReadLine(s); + UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 4); + + l1.Load(buf, 1); + UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 5); + + l1.Skip(1); + UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 6); + + l1.ReadLine(s); + UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 8); + } + Y_UNIT_TEST(TestCountingOutput) { - TStringStream s1; - TCountingOutput l1(&s1); - - l1.Write('1'); - UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 1); - + TStringStream s1; + TCountingOutput l1(&s1); + + l1.Write('1'); + UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 1); + l1.Write(TString("abcd")); - UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 5); - + UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 5); + TString buf("aaa"); IOutputStream::TPart parts[] = {{buf.data(), buf.size()}, {buf.data(), buf.size()}, {buf.data(), buf.size()}}; - l1.Write(parts, 3); - UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 14); - } -} + l1.Write(parts, 3); + UNIT_ASSERT_VALUES_EQUAL(l1.Counter(), 14); + } +} diff --git a/util/stream/mem.cpp b/util/stream/mem.cpp index 22a3339e27..c2c34dcc59 100644 --- a/util/stream/mem.cpp +++ b/util/stream/mem.cpp @@ -22,13 +22,13 @@ TMemoryInput::TMemoryInput(const TStringBuf buf) noexcept TMemoryInput::~TMemoryInput() = default; -size_t TMemoryInput::DoNext(const void** ptr, size_t len) { - len = Min(Len_, len); - - *ptr = Buf_; - Len_ -= len; - Buf_ += len; - return len; +size_t TMemoryInput::DoNext(const void** ptr, size_t len) { + len = Min(Len_, len); + + *ptr = Buf_; + Len_ -= len; + Buf_ += len; + return len; } void TMemoryInput::DoUndo(size_t len) { diff --git a/util/stream/mem.h b/util/stream/mem.h index 18a5d46772..faed25c5f8 100644 --- a/util/stream/mem.h +++ b/util/stream/mem.h @@ -5,26 +5,26 @@ #include <util/generic/strbuf.h> -/** - * @addtogroup Streams_Memory - * @{ - */ - -/** - * Input stream that reads data from a memory block. - */ +/** + * @addtogroup Streams_Memory + * @{ + */ + +/** + * Input stream that reads data from a memory block. + */ class TMemoryInput: public IZeroCopyInputFastReadTo { public: TMemoryInput() noexcept; - /** - * Constructs a stream that reads from the provided memory block. It's up - * to the user to make sure that the memory block doesn't get freed while - * this stream is in use. - * - * @param buf Memory block to use. - * @param len Size of the memory block. - */ + /** + * Constructs a stream that reads from the provided memory block. It's up + * to the user to make sure that the memory block doesn't get freed while + * this stream is in use. + * + * @param buf Memory block to use. + * @param len Size of the memory block. + */ TMemoryInput(const void* buf, size_t len) noexcept; explicit TMemoryInput(const TStringBuf buf) noexcept; ~TMemoryInput() override; @@ -48,50 +48,50 @@ public: TMemoryInput(TMemoryInput&&) noexcept = default; TMemoryInput& operator=(TMemoryInput&&) noexcept = default; - /** - * Initializes this stream with a new memory block. It's up to the - * user to make sure that the memory block doesn't get freed while this - * stream is in use. - * - * @param buf New memory block to use. - * @param len Size of the new memory block. - */ + /** + * Initializes this stream with a new memory block. It's up to the + * user to make sure that the memory block doesn't get freed while this + * stream is in use. + * + * @param buf New memory block to use. + * @param len Size of the new memory block. + */ void Reset(const void* buf, size_t len) noexcept { Buf_ = (const char*)buf; Len_ = len; } - /** - * @returns Whether there is more data in the stream. - */ + /** + * @returns Whether there is more data in the stream. + */ bool Exhausted() const noexcept { return !Avail(); } - /** - * @returns Number of bytes available in the stream. - */ + /** + * @returns Number of bytes available in the stream. + */ size_t Avail() const noexcept { return Len_; } - /** - * @returns Current read position in the memory block - * used by this stream. - */ + /** + * @returns Current read position in the memory block + * used by this stream. + */ const char* Buf() const noexcept { return Buf_; } - /** - * Initializes this stream with a next chunk extracted from the given zero - * copy stream. - * - * @param stream Zero copy stream to initialize from. - */ + /** + * Initializes this stream with a next chunk extracted from the given zero + * copy stream. + * + * @param stream Zero copy stream to initialize from. + */ void Fill(IZeroCopyInput* stream) { - Len_ = stream->Next(&Buf_); - if (!Len_) { + Len_ = stream->Next(&Buf_); + if (!Len_) { Reset(nullptr, 0); } } @@ -105,19 +105,19 @@ private: size_t Len_; }; -/** - * Output stream that writes data to a memory block. - */ +/** + * Output stream that writes data to a memory block. + */ class TMemoryOutput: public IZeroCopyOutput { public: - /** - * Constructs a stream that writes to the provided memory block. It's up - * to the user to make sure that the memory block doesn't get freed while - * this stream is in use. - * - * @param buf Memory block to use. - * @param len Size of the memory block. - */ + /** + * Constructs a stream that writes to the provided memory block. It's up + * to the user to make sure that the memory block doesn't get freed while + * this stream is in use. + * + * @param buf Memory block to use. + * @param len Size of the memory block. + */ TMemoryOutput(void* buf, size_t len) noexcept : Buf_(static_cast<char*>(buf)) , End_(Buf_ + len) @@ -128,47 +128,47 @@ public: TMemoryOutput(TMemoryOutput&&) noexcept = default; TMemoryOutput& operator=(TMemoryOutput&&) noexcept = default; - /** - * Initializes this stream with a new memory block. It's up to the - * user to make sure that the memory block doesn't get freed while this - * stream is in use. - * - * @param buf New memory block to use. - * @param len Size of the new memory block. - */ + /** + * Initializes this stream with a new memory block. It's up to the + * user to make sure that the memory block doesn't get freed while this + * stream is in use. + * + * @param buf New memory block to use. + * @param len Size of the new memory block. + */ inline void Reset(void* buf, size_t len) noexcept { Buf_ = static_cast<char*>(buf); End_ = Buf_ + len; } - /** - * @returns Whether there is more space in the - * stream for writing. - */ + /** + * @returns Whether there is more space in the + * stream for writing. + */ inline bool Exhausted() const noexcept { return !Avail(); } - /** - * @returns Number of bytes available for writing - * in the stream. - */ + /** + * @returns Number of bytes available for writing + * in the stream. + */ inline size_t Avail() const noexcept { return End_ - Buf_; } - /** - * @returns Current write position in the memory block - * used by this stream. - */ + /** + * @returns Current write position in the memory block + * used by this stream. + */ inline char* Buf() const noexcept { return Buf_; } - /** - * @returns Pointer to the end of the memory block - * used by this stream. - */ + /** + * @returns Pointer to the end of the memory block + * used by this stream. + */ char* End() const { return End_; } @@ -184,12 +184,12 @@ protected: char* End_; }; -/** - * Memory output stream that supports changing the position of the - * write pointer. - * - * @see TMemoryOutput - */ +/** + * Memory output stream that supports changing the position of the + * write pointer. + * + * @see TMemoryOutput + */ class TMemoryWriteBuffer: public TMemoryOutput { public: TMemoryWriteBuffer(void* buf, size_t len) @@ -211,10 +211,10 @@ public: return Buf() == Beg(); } - /** - * @returns Data that has been written into this - * stream as a string. - */ + /** + * @returns Data that has been written into this + * stream as a string. + */ TStringBuf Str() const { return TStringBuf(Beg(), Buf()); } @@ -223,21 +223,21 @@ public: return Beg_; } - /** - * @param ptr New write position for this stream. - * Must be inside the memory block that - * this stream uses. - */ + /** + * @param ptr New write position for this stream. + * Must be inside the memory block that + * this stream uses. + */ void SetPos(char* ptr) { Y_ASSERT(Beg_ <= ptr); SetPosImpl(ptr); } - /** - * @param pos New write position for this stream, - * relative to the beginning of the memory - * block that this stream uses. - */ + /** + * @param pos New write position for this stream, + * relative to the beginning of the memory + * block that this stream uses. + */ void SetPos(size_t pos) { SetPosImpl(Beg_ + pos); } @@ -252,4 +252,4 @@ protected: char* Beg_; }; -/** @} */ +/** @} */ diff --git a/util/stream/mem_ut.cpp b/util/stream/mem_ut.cpp index f388ae66ac..ba4dfb869b 100644 --- a/util/stream/mem_ut.cpp +++ b/util/stream/mem_ut.cpp @@ -1,18 +1,18 @@ -#include "mem.h" - +#include "mem.h" + #include <library/cpp/testing/unittest/registar.h> - + Y_UNIT_TEST_SUITE(TestMemIO) { Y_UNIT_TEST(TestReadTo) { TString s("0123456789abc"); - TMemoryInput in(s); - + TMemoryInput in(s); + TString t; - UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '7'), 8); - UNIT_ASSERT_VALUES_EQUAL(t, "0123456"); - UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, 'z'), 5); - UNIT_ASSERT_VALUES_EQUAL(t, "89abc"); - } + UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '7'), 8); + UNIT_ASSERT_VALUES_EQUAL(t, "0123456"); + UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, 'z'), 5); + UNIT_ASSERT_VALUES_EQUAL(t, "89abc"); + } Y_UNIT_TEST(NextAndUndo) { char buffer[20]; @@ -75,4 +75,4 @@ Y_UNIT_TEST_SUITE(TestMemIO) { const char* const result = "1234567890"; UNIT_ASSERT(0 == memcmp(buffer, result, strlen(result))); } -} +} diff --git a/util/stream/multi.cpp b/util/stream/multi.cpp index b2354298a0..a6d19c71a9 100644 --- a/util/stream/multi.cpp +++ b/util/stream/multi.cpp @@ -21,7 +21,7 @@ size_t TMultiInput::DoRead(void* buf, size_t len) { return C_->Read(buf, len); } - + size_t TMultiInput::DoReadTo(TString& st, char ch) { size_t ret = C_->ReadTo(st, ch); if (ret == st.size() + 1) { // found a symbol, not eof @@ -42,15 +42,15 @@ size_t TMultiInput::DoReadTo(TString& st, char ch) { return ret; } -size_t TMultiInput::DoSkip(size_t len) { - const size_t ret = C_->Skip(len); - - if (ret) { - return ret; - } - - C_ = N_; - N_ = &Cnull; - - return C_->Skip(len); -} +size_t TMultiInput::DoSkip(size_t len) { + const size_t ret = C_->Skip(len); + + if (ret) { + return ret; + } + + C_ = N_; + N_ = &Cnull; + + return C_->Skip(len); +} diff --git a/util/stream/multi.h b/util/stream/multi.h index 8bfd462d99..9c53189add 100644 --- a/util/stream/multi.h +++ b/util/stream/multi.h @@ -2,14 +2,14 @@ #include "input.h" -/** - * @addtogroup Streams_Multi - * @{ - */ - -/** - * A proxy input stream that concatenates two slave streams into one. +/** + * @addtogroup Streams_Multi + * @{ */ + +/** + * A proxy input stream that concatenates two slave streams into one. + */ class TMultiInput: public IInputStream { public: TMultiInput(IInputStream* f, IInputStream* s) noexcept; @@ -29,4 +29,4 @@ private: * See also "util/stream/tee.h" for multi output. */ -/** @} */ +/** @} */ diff --git a/util/stream/null.cpp b/util/stream/null.cpp index 4e8b298145..0e438e3991 100644 --- a/util/stream/null.cpp +++ b/util/stream/null.cpp @@ -11,16 +11,16 @@ TNullInput::TNullInput() noexcept { TNullInput::~TNullInput() = default; -size_t TNullInput::DoRead(void*, size_t) { +size_t TNullInput::DoRead(void*, size_t) { return 0; } -size_t TNullInput::DoSkip(size_t) { - return 0; -} - -size_t TNullInput::DoNext(const void**, size_t) { - return 0; +size_t TNullInput::DoSkip(size_t) { + return 0; +} + +size_t TNullInput::DoNext(const void**, size_t) { + return 0; } TNullOutput::TNullOutput() noexcept = default; diff --git a/util/stream/null.h b/util/stream/null.h index 8c335a9a78..bb97148ac6 100644 --- a/util/stream/null.h +++ b/util/stream/null.h @@ -1,16 +1,16 @@ #pragma once -#include "zerocopy.h" -#include "output.h" - -/** - * @addtogroup Streams - * @{ - */ - -/** - * Null input stream. Does nothing, contains no data. - */ +#include "zerocopy.h" +#include "output.h" + +/** + * @addtogroup Streams + * @{ + */ + +/** + * Null input stream. Does nothing, contains no data. + */ class TNullInput: public IZeroCopyInput { public: TNullInput() noexcept; @@ -22,9 +22,9 @@ private: size_t DoNext(const void** ptr, size_t len) override; }; -/** - * Null output stream. Just ignores whatever is written into it. - */ +/** + * Null output stream. Just ignores whatever is written into it. + */ class TNullOutput: public IOutputStream { public: TNullOutput() noexcept; @@ -37,25 +37,25 @@ private: void DoWrite(const void* buf, size_t len) override; }; -/** - * Null input-output stream. - * - * @see TNullInput - * @see TNullOutput - */ +/** + * Null input-output stream. + * + * @see TNullInput + * @see TNullOutput + */ class TNullIO: public TNullInput, public TNullOutput { public: TNullIO() noexcept; ~TNullIO() override; }; -namespace NPrivate { +namespace NPrivate { TNullIO& StdNullStream() noexcept; -} +} -/** - * Standard null stream. - */ -#define Cnull (::NPrivate::StdNullStream()) +/** + * Standard null stream. + */ +#define Cnull (::NPrivate::StdNullStream()) -/** @} */ +/** @} */ diff --git a/util/stream/output.cpp b/util/stream/output.cpp index db81b81b70..c172c58a25 100644 --- a/util/stream/output.cpp +++ b/util/stream/output.cpp @@ -17,7 +17,7 @@ #include <cerrno> #include <string> -#include <string_view> +#include <string_view> #include <cstdio> #if defined(_win_) @@ -71,14 +71,14 @@ void Out<wchar32>(IOutputStream& o, wchar32 ch) { static void WriteString(IOutputStream& o, const wchar16* w, size_t n) { const size_t buflen = (n * MAX_UTF8_BYTES); // * 4 because the conversion functions can convert unicode character into maximum 4 bytes of UTF8 - TTempBuf buffer(buflen + 1); - char* const data = buffer.Data(); - size_t written = 0; - WideToUTF8(w, n, data, written); - data[written] = 0; - o.Write(data, written); -} - + TTempBuf buffer(buflen + 1); + char* const data = buffer.Data(); + size_t written = 0; + WideToUTF8(w, n, data, written); + data[written] = 0; + o.Write(data, written); +} + static void WriteString(IOutputStream& o, const wchar32* w, size_t n) { const size_t buflen = (n * MAX_UTF8_BYTES); // * 4 because the conversion functions can convert unicode character into maximum 4 bytes of UTF8 TTempBuf buffer(buflen + 1); @@ -100,11 +100,11 @@ void Out<std::string>(IOutputStream& o, const std::string& p) { } template <> -void Out<std::string_view>(IOutputStream& o, const std::string_view& p) { - o.Write(p.data(), p.length()); -} - -template <> +void Out<std::string_view>(IOutputStream& o, const std::string_view& p) { + o.Write(p.data(), p.length()); +} + +template <> void Out<std::u16string_view>(IOutputStream& o, const std::u16string_view& p) { WriteString(o, p.data(), p.length()); } @@ -131,14 +131,14 @@ void Out<TUtf32StringBuf>(IOutputStream& o, const TUtf32StringBuf& p) { template <> void Out<const wchar16*>(IOutputStream& o, const wchar16* w) { - if (w) { + if (w) { WriteString(o, w, std::char_traits<wchar16>::length(w)); - } else { - o.Write("(null)"); - } + } else { + o.Write("(null)"); + } } -template <> +template <> void Out<const wchar32*>(IOutputStream& o, const wchar32* w) { if (w) { WriteString(o, w, std::char_traits<wchar32>::length(w)); @@ -149,9 +149,9 @@ void Out<const wchar32*>(IOutputStream& o, const wchar32* w) { template <> void Out<TUtf16String>(IOutputStream& o, const TUtf16String& w) { - WriteString(o, w.c_str(), w.size()); -} - + WriteString(o, w.c_str(), w.size()); +} + template <> void Out<TUtf32String>(IOutputStream& o, const TUtf32String& w) { WriteString(o, w.c_str(), w.size()); @@ -217,7 +217,7 @@ void Out<TBasicCharRef<TString>>(IOutputStream& o, const TBasicCharRef<TString>& template <> void Out<TBasicCharRef<TUtf16String>>(IOutputStream& o, const TBasicCharRef<TUtf16String>& c) { - o << static_cast<wchar16>(c); + o << static_cast<wchar16>(c); } template <> diff --git a/util/stream/output.h b/util/stream/output.h index 00eef50b95..14b82509e6 100644 --- a/util/stream/output.h +++ b/util/stream/output.h @@ -10,19 +10,19 @@ #include <type_traits> -/** - * @addtogroup Streams_Base - * @{ - */ - -/** - * Abstract output stream. - */ +/** + * @addtogroup Streams_Base + * @{ + */ + +/** + * Abstract output stream. + */ class IOutputStream: public TNonCopyable { public: - /** - * Data block for output. - */ + /** + * Data block for output. + */ struct TPart { inline TPart(const void* Buf, size_t Len) noexcept : buf(Buf) @@ -62,34 +62,34 @@ public: return *this; }; - /** - * Writes into this stream. - * - * @param buf Data to write. - * @param len Number of bytes to write. - */ + /** + * Writes into this stream. + * + * @param buf Data to write. + * @param len Number of bytes to write. + */ inline void Write(const void* buf, size_t len) { if (len) { DoWrite(buf, len); } } - /** - * Writes a string into this stream. - * - * @param st String to write. - */ + /** + * Writes a string into this stream. + * + * @param st String to write. + */ inline void Write(const TStringBuf st) { Write(st.data(), st.size()); } - /** - * Writes several data blocks into this stream. - * - * @param parts Pointer to the start of the data blocks - * array. - * @param count Number of data blocks to write. - */ + /** + * Writes several data blocks into this stream. + * + * @param parts Pointer to the start of the data blocks + * array. + * @param count Number of data blocks to write. + */ inline void Write(const TPart* parts, size_t count) { if (count > 1) { DoWriteV(parts, count); @@ -98,56 +98,56 @@ public: } } - /** - * Writes a single character into this stream. - * - * @param ch Character to write. - */ + /** + * Writes a single character into this stream. + * + * @param ch Character to write. + */ inline void Write(char ch) { DoWriteC(ch); } - /** - * Flushes this stream's buffer, if any. - * - * Note that this can also be done with a `Flush` manipulator: - * @code - * stream << "some string" << Flush; - * @endcode - */ + /** + * Flushes this stream's buffer, if any. + * + * Note that this can also be done with a `Flush` manipulator: + * @code + * stream << "some string" << Flush; + * @endcode + */ inline void Flush() { DoFlush(); } - /** - * Flushes and closes this stream. No more data can be written into a stream - * once it's closed. - */ + /** + * Flushes and closes this stream. No more data can be written into a stream + * once it's closed. + */ inline void Finish() { DoFinish(); } protected: - /** - * Writes into this stream. - * - * @param buf Data to write. - * @param len Number of bytes to write. - * @throws yexception If IO error occurs. - */ + /** + * Writes into this stream. + * + * @param buf Data to write. + * @param len Number of bytes to write. + * @throws yexception If IO error occurs. + */ virtual void DoWrite(const void* buf, size_t len) = 0; - - /** - * Writes several data blocks into this stream. - * - * @param parts Pointer to the start of the data blocks - * array. - * @param count Number of data blocks to write. - * @throws yexception If IO error occurs. - */ + + /** + * Writes several data blocks into this stream. + * + * @param parts Pointer to the start of the data blocks + * array. + * @param count Number of data blocks to write. + * @throws yexception If IO error occurs. + */ virtual void DoWriteV(const TPart* parts, size_t count); - - /** + + /** * Writes a single character into this stream. Can be overridden with a faster implementation. * * @param ch Character to write. @@ -155,36 +155,36 @@ protected: virtual void DoWriteC(char ch); /** - * Flushes this stream's buffer, if any. - * - * @throws yexception If IO error occurs. - */ + * Flushes this stream's buffer, if any. + * + * @throws yexception If IO error occurs. + */ virtual void DoFlush(); - - /** - * Flushes and closes this stream. No more data can be written into a stream - * once it's closed. - * - * @throws yexception If IO error occurs. - */ + + /** + * Flushes and closes this stream. No more data can be written into a stream + * once it's closed. + * + * @throws yexception If IO error occurs. + */ virtual void DoFinish(); }; -/** +/** * `operator<<` for `IOutputStream` by default delegates to this function. - * - * Note that while `operator<<` uses overloading (and thus argument-dependent - * lookup), `Out` uses template specializations. This makes it possible to - * have a single `Out` declaration, and then just provide specializations in - * cpp files, letting the linker figure everything else out. This approach - * reduces compilation times. - * - * However, if the flexibility of overload resolution is needed, then one should - * just overload `operator<<`. - * - * @param out Output stream to write into. - * @param value Value to write. - */ + * + * Note that while `operator<<` uses overloading (and thus argument-dependent + * lookup), `Out` uses template specializations. This makes it possible to + * have a single `Out` declaration, and then just provide specializations in + * cpp files, letting the linker figure everything else out. This approach + * reduces compilation times. + * + * However, if the flexibility of overload resolution is needed, then one should + * just overload `operator<<`. + * + * @param out Output stream to write into. + * @param value Value to write. + */ template <class T> void Out(IOutputStream& out, typename TTypeTraits<T>::TFuncParam value); @@ -259,46 +259,46 @@ static inline IOutputStream& operator<<(IOutputStream& o, wchar32* t) { return o; } -namespace NPrivate { +namespace NPrivate { IOutputStream& StdOutStream() noexcept; IOutputStream& StdErrStream() noexcept; -} - -/** - * Standard output stream. - */ -#define Cout (::NPrivate::StdOutStream()) - -/** - * Standard error stream. - */ -#define Cerr (::NPrivate::StdErrStream()) - -/** - * Standard log stream. - */ +} + +/** + * Standard output stream. + */ +#define Cout (::NPrivate::StdOutStream()) + +/** + * Standard error stream. + */ +#define Cerr (::NPrivate::StdErrStream()) + +/** + * Standard log stream. + */ #define Clog Cerr -/** - * End-of-line output manipulator, basically the same as `std::endl`. +/** + * End-of-line output manipulator, basically the same as `std::endl`. */ static inline void Endl(IOutputStream& o) { (o << '\n').Flush(); } -/** - * Flushing stream manipulator, basically the same as `std::flush`. - */ +/** + * Flushing stream manipulator, basically the same as `std::flush`. + */ static inline void Flush(IOutputStream& o) { o.Flush(); } /* - * Also see format.h for additional manipulators. - */ + * Also see format.h for additional manipulators. + */ #include "debug.h" void RedirectStdioToAndroidLog(bool redirect); -/** @} */ +/** @} */ diff --git a/util/stream/pipe.cpp b/util/stream/pipe.cpp index 51be1934a7..f3081acf46 100644 --- a/util/stream/pipe.cpp +++ b/util/stream/pipe.cpp @@ -17,7 +17,7 @@ public: #endif Pipe_ = ::popen(command.data(), mode); if (Pipe_ == nullptr) { - ythrow TSystemError() << "failed to open pipe: " << command.Quote(); + ythrow TSystemError() << "failed to open pipe: " << command.Quote(); } } diff --git a/util/stream/pipe.h b/util/stream/pipe.h index 18525b9517..15ad8fd910 100644 --- a/util/stream/pipe.h +++ b/util/stream/pipe.h @@ -1,29 +1,29 @@ #pragma once -#include "input.h" -#include "output.h" +#include "input.h" +#include "output.h" #include <util/system/pipe.h> #include <util/generic/ptr.h> #include <util/generic/string.h> -/** - * @addtogroup Streams_Pipes - * @{ - */ - -/** - * Base class for starting a process and communicating with it via pipes. - */ +/** + * @addtogroup Streams_Pipes + * @{ + */ + +/** + * Base class for starting a process and communicating with it via pipes. + */ class TPipeBase { protected: - /** - * Starts a new process and opens a pipe. - * - * @param command Command line to start a process with. - * @param mode Data transfer mode for the pipe. Use - * "r" for reading and "w" for writing. - */ + /** + * Starts a new process and opens a pipe. + * + * @param command Command line to start a process with. + * @param mode Data transfer mode for the pipe. Use + * "r" for reading and "w" for writing. + */ TPipeBase(const TString& command, const char* mode); virtual ~TPipeBase(); @@ -32,44 +32,44 @@ protected: THolder<TImpl> Impl_; }; -/** - * Input stream that binds to a standard output stream of a newly started process. - * - * Note that if the process ends with non-zero exit status, `Read` function will - * throw an exception. - */ +/** + * Input stream that binds to a standard output stream of a newly started process. + * + * Note that if the process ends with non-zero exit status, `Read` function will + * throw an exception. + */ class TPipeInput: protected TPipeBase, public IInputStream { public: - /** - * Starts a new process and opens a pipe. - * - * @param command Command line to start a process with. - */ + /** + * Starts a new process and opens a pipe. + * + * @param command Command line to start a process with. + */ TPipeInput(const TString& command); private: size_t DoRead(void* buf, size_t len) override; }; -/** - * Output stream that binds to a standard input stream of a newly started process. - * - * Note that if the process ends with non-zero exit status, `Close` function will - * throw an exception. - */ +/** + * Output stream that binds to a standard input stream of a newly started process. + * + * Note that if the process ends with non-zero exit status, `Close` function will + * throw an exception. + */ class TPipeOutput: protected TPipeBase, public IOutputStream { public: - /** - * Starts a new process and opens a pipe. - * - * @param command Command line to start a process with. - */ + /** + * Starts a new process and opens a pipe. + * + * @param command Command line to start a process with. + */ TPipeOutput(const TString& command); - - /** - * Waits for the process to terminate and throws an exception if it ended - * with a non-zero exit status. - */ + + /** + * Waits for the process to terminate and throws an exception if it ended + * with a non-zero exit status. + */ void Close(); private: @@ -85,9 +85,9 @@ protected: TPipeHandle Handle_; }; -/** - * Input stream that binds to a standard output stream of an existing process. - */ +/** + * Input stream that binds to a standard output stream of an existing process. + */ class TPipedInput: public TPipedBase, public IInputStream { public: TPipedInput(PIPEHANDLE fd); @@ -97,9 +97,9 @@ private: size_t DoRead(void* buf, size_t len) override; }; -/** - * Output stream that binds to a standard input stream of an existing process. - */ +/** + * Output stream that binds to a standard input stream of an existing process. + */ class TPipedOutput: public TPipedBase, public IOutputStream { public: TPipedOutput(PIPEHANDLE fd); @@ -109,4 +109,4 @@ private: void DoWrite(const void* buf, size_t len) override; }; -/** @} */ +/** @} */ diff --git a/util/stream/printf.h b/util/stream/printf.h index 1c7ddc0664..180dc3a93c 100644 --- a/util/stream/printf.h +++ b/util/stream/printf.h @@ -4,22 +4,22 @@ class IOutputStream; -/** - * Stream-based `printf` function. Prints formatted data into the provided stream. - * Works the same way as a standard C `printf`. - * - * @param out Stream to write into. - * @param fmt Format string. - * @param ... Additional arguments. - */ +/** + * Stream-based `printf` function. Prints formatted data into the provided stream. + * Works the same way as a standard C `printf`. + * + * @param out Stream to write into. + * @param fmt Format string. + * @param ... Additional arguments. + */ size_t Y_PRINTF_FORMAT(2, 3) Printf(IOutputStream& out, const char* fmt, ...); - -/** - * Stream-based `vprintf` function. Prints formatted data from variable argument - * list into the provided stream. Works the same way as a standard C `vprintf`. - * - * @param out Stream to write into. - * @param fmt Format string. - * @param params Additional arguments as a variable argument list. - */ + +/** + * Stream-based `vprintf` function. Prints formatted data from variable argument + * list into the provided stream. Works the same way as a standard C `vprintf`. + * + * @param out Stream to write into. + * @param fmt Format string. + * @param params Additional arguments as a variable argument list. + */ size_t Y_PRINTF_FORMAT(2, 0) Printf(IOutputStream& out, const char* fmt, va_list params); diff --git a/util/stream/str.cpp b/util/stream/str.cpp index 13f0e8ef28..36891ea1ed 100644 --- a/util/stream/str.cpp +++ b/util/stream/str.cpp @@ -4,13 +4,13 @@ static constexpr size_t MIN_BUFFER_GROW_SIZE = 16; TStringInput::~TStringInput() = default; -size_t TStringInput::DoNext(const void** ptr, size_t len) { +size_t TStringInput::DoNext(const void** ptr, size_t len) { len = Min(len, S_->size() - Pos_); *ptr = S_->data() + Pos_; - Pos_ += len; - return len; -} - + Pos_ += len; + return len; +} + void TStringInput::DoUndo(size_t len) { Y_VERIFY(len <= Pos_); Pos_ -= len; diff --git a/util/stream/str.h b/util/stream/str.h index 028bd572c0..f00a3bcba7 100644 --- a/util/stream/str.h +++ b/util/stream/str.h @@ -1,34 +1,34 @@ #pragma once -#include "zerocopy.h" +#include "zerocopy.h" #include "zerocopy_output.h" #include <util/generic/string.h> #include <util/generic/noncopyable.h> -#include <util/generic/store_policy.h> +#include <util/generic/store_policy.h> /** - * @addtogroup Streams_Strings - * @{ - */ - -/** - * Input stream for reading data from a string. - */ + * @addtogroup Streams_Strings + * @{ + */ + +/** + * Input stream for reading data from a string. + */ class TStringInput: public IZeroCopyInputFastReadTo { public: - /** - * Constructs a string input stream that reads character data from the - * provided string. + /** + * Constructs a string input stream that reads character data from the + * provided string. + * + * Note that this stream keeps a reference to the provided string, so it's + * up to the user to make sure that the string doesn't get destroyed while + * this stream is in use. + * + * For reading data from `TStringBuf`s, see `TMemoryInput` (`util/stream/mem.h`). * - * Note that this stream keeps a reference to the provided string, so it's - * up to the user to make sure that the string doesn't get destroyed while - * this stream is in use. - * - * For reading data from `TStringBuf`s, see `TMemoryInput` (`util/stream/mem.h`). - * - * @param s String to read from. - */ + * @param s String to read from. + */ inline TStringInput(const TString& s) noexcept : S_(&s) , Pos_(0) @@ -36,7 +36,7 @@ public: } TStringInput(const TString&&) = delete; - + ~TStringInput() override; TStringInput(TStringInput&&) noexcept = default; @@ -47,7 +47,7 @@ public: DoSwap(Pos_, s.Pos_); } -protected: +protected: size_t DoNext(const void** ptr, size_t len) override; void DoUndo(size_t len) override; @@ -59,20 +59,20 @@ private: }; /** - * Stream for writing data into a string. - */ + * Stream for writing data into a string. + */ class TStringOutput: public IZeroCopyOutput { public: - /** + /** * Constructs a string output stream that appends character data to the - * provided string. - * - * Note that this stream keeps a reference to the provided string, so it's - * up to the user to make sure that the string doesn't get destroyed while - * this stream is in use. - * - * @param s String to append to. - */ + * provided string. + * + * Note that this stream keeps a reference to the provided string, so it's + * up to the user to make sure that the string doesn't get destroyed while + * this stream is in use. + * + * @param s String to append to. + */ inline TStringOutput(TString& s) noexcept : S_(&s) { @@ -82,14 +82,14 @@ public: ~TStringOutput() override; - /** - * @param size Number of additional characters to - * reserve in output string. - */ - inline void Reserve(size_t size) { + /** + * @param size Number of additional characters to + * reserve in output string. + */ + inline void Reserve(size_t size) { S_->reserve(S_->size() + size); - } - + } + inline void Swap(TStringOutput& s) noexcept { DoSwap(S_, s.S_); } @@ -104,9 +104,9 @@ private: TString* S_; }; -/** - * String input/output stream, similar to `std::stringstream`. - */ +/** + * String input/output stream, similar to `std::stringstream`. + */ class TStringStream: private TEmbedPolicy<TString>, public TStringInput, public TStringOutput { using TEmbeddedString = TEmbedPolicy<TString>; @@ -124,7 +124,7 @@ public: , TStringOutput(*TEmbeddedString::Ptr()) { } - + inline TStringStream(const TStringStream& other) : TEmbeddedString(other.Str()) , TStringInput(*TEmbeddedString::Ptr()) @@ -134,7 +134,7 @@ public: inline TStringStream& operator=(const TStringStream& other) { // All references remain alive, we need to change position only - Str() = other.Str(); + Str() = other.Str(); Pos_ = other.Pos_; return *this; @@ -142,7 +142,7 @@ public: ~TStringStream() override; - /** + /** * @returns Whether @c this contains any data */ explicit operator bool() const noexcept { @@ -150,66 +150,66 @@ public: } /** - * @returns String that this stream is writing into. + * @returns String that this stream is writing into. */ inline TString& Str() noexcept { - return *Ptr(); + return *Ptr(); } - /** - * @returns String that this stream is writing into. - */ + /** + * @returns String that this stream is writing into. + */ inline const TString& Str() const noexcept { - return *Ptr(); - } - - /** - * @returns Pointer to the character data contained - * in this stream. The data is guaranteed - * to be null-terminated. - */ + return *Ptr(); + } + + /** + * @returns Pointer to the character data contained + * in this stream. The data is guaranteed + * to be null-terminated. + */ inline const char* Data() const noexcept { return Ptr()->data(); - } - - /** - * @returns Total number of characters in this - * stream. Note that this is not the same - * as the total number of characters - * available for reading. - */ + } + + /** + * @returns Total number of characters in this + * stream. Note that this is not the same + * as the total number of characters + * available for reading. + */ inline size_t Size() const noexcept { return Ptr()->size(); - } - - /** - * @returns Whether the string that this stream - * operates on is empty. - */ + } + + /** + * @returns Whether the string that this stream + * operates on is empty. + */ Y_PURE_FUNCTION inline bool Empty() const noexcept { - return Str().empty(); - } - - using TStringOutput::Reserve; - - /** - * Clears the string that this stream operates on and resets the - * read/write pointers. - */ - inline void Clear() { - Str().clear(); - Pos_ = 0; - } - - // TODO: compatibility with existing code, remove - + return Str().empty(); + } + + using TStringOutput::Reserve; + + /** + * Clears the string that this stream operates on and resets the + * read/write pointers. + */ + inline void Clear() { + Str().clear(); + Pos_ = 0; + } + + // TODO: compatibility with existing code, remove + Y_PURE_FUNCTION bool empty() const { - return Empty(); - } - - void clear() { - Clear(); - } + return Empty(); + } + + void clear() { + Clear(); + } }; -/** @} */ +/** @} */ diff --git a/util/stream/str_ut.cpp b/util/stream/str_ut.cpp index fc6b46c31a..c688ee1e5c 100644 --- a/util/stream/str_ut.cpp +++ b/util/stream/str_ut.cpp @@ -13,7 +13,7 @@ Y_UNIT_TEST_SUITE(TStringInputOutputTest) { TString result = input.ReadAll(); - UNIT_ASSERT_VALUES_EQUAL(result, str); + UNIT_ASSERT_VALUES_EQUAL(result, str); } Y_UNIT_TEST(ConstRef) { @@ -23,7 +23,7 @@ Y_UNIT_TEST_SUITE(TStringInputOutputTest) { TString result = input.ReadAll(); - UNIT_ASSERT_VALUES_EQUAL(result, str); + UNIT_ASSERT_VALUES_EQUAL(result, str); } Y_UNIT_TEST(NonConstRef) { @@ -33,40 +33,40 @@ Y_UNIT_TEST_SUITE(TStringInputOutputTest) { TString result = input.ReadAll(); - UNIT_ASSERT_VALUES_EQUAL(result, str); + UNIT_ASSERT_VALUES_EQUAL(result, str); } Y_UNIT_TEST(Transfer) { TString inputString = "some_string"; - TStringInput input(inputString); - + TStringInput input(inputString); + TString outputString; - TStringOutput output(outputString); - - TransferData(&input, &output); - - UNIT_ASSERT_VALUES_EQUAL(inputString, outputString); - } - + TStringOutput output(outputString); + + TransferData(&input, &output); + + UNIT_ASSERT_VALUES_EQUAL(inputString, outputString); + } + Y_UNIT_TEST(SkipReadAll) { TString string0 = "All animals are equal, but some animals are more equal than others."; - + TString string1; for (size_t i = 1; i <= string0.size(); i++) { - string1 += string0.substr(0, i); + string1 += string0.substr(0, i); } - - TStringInput input0(string1); - - size_t left = 5; + + TStringInput input0(string1); + + size_t left = 5; while (left > 0) { - left -= input0.Skip(left); + left -= input0.Skip(left); } - + TString string2 = input0.ReadAll(); - - UNIT_ASSERT_VALUES_EQUAL(string2, string1.substr(5)); - } + + UNIT_ASSERT_VALUES_EQUAL(string2, string1.substr(5)); + } Y_UNIT_TEST(OperatorBool) { TStringStream str; @@ -76,17 +76,17 @@ Y_UNIT_TEST_SUITE(TStringInputOutputTest) { str.Clear(); UNIT_ASSERT(!str); } - + Y_UNIT_TEST(TestReadTo) { TString s("0123456789abc"); TString t; - - TStringInput in0(s); - UNIT_ASSERT_VALUES_EQUAL(in0.ReadTo(t, '7'), 8); - UNIT_ASSERT_VALUES_EQUAL(t, "0123456"); - UNIT_ASSERT_VALUES_EQUAL(in0.ReadTo(t, 'z'), 5); - UNIT_ASSERT_VALUES_EQUAL(t, "89abc"); - } + + TStringInput in0(s); + UNIT_ASSERT_VALUES_EQUAL(in0.ReadTo(t, '7'), 8); + UNIT_ASSERT_VALUES_EQUAL(t, "0123456"); + UNIT_ASSERT_VALUES_EQUAL(in0.ReadTo(t, 'z'), 5); + UNIT_ASSERT_VALUES_EQUAL(t, "89abc"); + } Y_UNIT_TEST(WriteViaNextAndUndo) { TString str1; diff --git a/util/stream/tee.h b/util/stream/tee.h index c69e232fb9..52fe3d3fe4 100644 --- a/util/stream/tee.h +++ b/util/stream/tee.h @@ -2,14 +2,14 @@ #include "output.h" -/** - * @addtogroup Streams_Multi - * @{ - */ - -/** - * A proxy output stream that writes into two slave streams simultaneously. - */ +/** + * @addtogroup Streams_Multi + * @{ + */ + +/** + * A proxy output stream that writes into two slave streams simultaneously. + */ class TTeeOutput: public IOutputStream { public: TTeeOutput(IOutputStream* l, IOutputStream* r) noexcept; @@ -25,4 +25,4 @@ private: IOutputStream* R_; }; -/** @} */ +/** @} */ diff --git a/util/stream/tokenizer.h b/util/stream/tokenizer.h index b2398efdd1..fc0f62d84d 100644 --- a/util/stream/tokenizer.h +++ b/util/stream/tokenizer.h @@ -8,21 +8,21 @@ #include <util/system/compiler.h> #include <util/system/yassert.h> -/** - * @addtogroup Streams - * @{ - */ - -/** - * Simple stream tokenizer. Splits the stream into tokens that are available - * via iterator interface. - * - * @tparam TEndOfToken Predicate for token delimiter characters. - * @see TEol +/** + * @addtogroup Streams + * @{ */ + +/** + * Simple stream tokenizer. Splits the stream into tokens that are available + * via iterator interface. + * + * @tparam TEndOfToken Predicate for token delimiter characters. + * @see TEol + */ template <typename TEndOfToken> class TStreamTokenizer { -public: +public: class TIterator { public: inline TIterator(TStreamTokenizer* const parent) @@ -143,15 +143,15 @@ public: Y_ASSERT(blen == Buf_.Capacity()); /* - * do reallocate - */ + * do reallocate + */ Buf_.Reserve(Buf_.Capacity() * 4); CheckBuf(); } else { /* - * do move - */ + * do move + */ MemMove(BufBegin(), Cur_, blen); } @@ -202,8 +202,8 @@ private: TEndOfToken Eot_; }; -/** - * Predicate for `TStreamTokenizer` that uses '\\n' as a delimiter. +/** + * Predicate for `TStreamTokenizer` that uses '\\n' as a delimiter. */ struct TEol { inline bool operator()(char ch) const noexcept { @@ -211,4 +211,4 @@ struct TEol { } }; -/** @} */ +/** @} */ diff --git a/util/stream/trace.h b/util/stream/trace.h index e74b6ecf3e..03cab37a8f 100644 --- a/util/stream/trace.h +++ b/util/stream/trace.h @@ -2,9 +2,9 @@ #include "debug.h" -/** - * Debug level, as set via `DBGOUT` environment variable. - */ +/** + * Debug level, as set via `DBGOUT` environment variable. + */ enum ETraceLevel: ui8 { TRACE_ERR = 1, TRACE_WARN = 2, @@ -22,24 +22,24 @@ enum ETraceLevel: ui8 { #ifdef Y_ENABLE_TRACE /** - * Writes the given data into standard debug stream if current debug level set - * via `DBGOUT` environment variable permits it. - * + * Writes the given data into standard debug stream if current debug level set + * via `DBGOUT` environment variable permits it. + * * Does nothing in release builds unless `Y_ENABLE_TRACE` is defined. - * - * Example usage: - * @code + * + * Example usage: + * @code * Y_DBGTRACE(DEBUG, "Advance from " << node1 << " to " << node2); - * @endcode - * - * @param elevel Debug level of this trace command, e.g. - * `WARN` or `DEBUG`. Basically a suffix of - * one of the values of `ETraceLevel` enum. - * @param args Argument chain to be written out into - * standard debug stream, joined with `<<` - * operator. - * @see ETraceLevel - */ + * @endcode + * + * @param elevel Debug level of this trace command, e.g. + * `WARN` or `DEBUG`. Basically a suffix of + * one of the values of `ETraceLevel` enum. + * @param args Argument chain to be written out into + * standard debug stream, joined with `<<` + * operator. + * @see ETraceLevel + */ #define Y_DBGTRACE(elevel, args) Y_DBGTRACE0(int(TRACE_##elevel), args) #define Y_DBGTRACE0(level, args) \ do \ diff --git a/util/stream/walk.cpp b/util/stream/walk.cpp index 57dc9ab036..8a6404699f 100644 --- a/util/stream/walk.cpp +++ b/util/stream/walk.cpp @@ -5,8 +5,8 @@ void IWalkInput::DoUndo(size_t len) { Len_ += len; Buf_ = static_cast<const char*>(Buf_) - len; -} - +} + size_t IWalkInput::DoNext(const void** ptr, size_t len) { if (!Len_) { Len_ = DoUnboundedNext(&Buf_); diff --git a/util/stream/walk.h b/util/stream/walk.h index 7e62cb44dc..202bada18b 100644 --- a/util/stream/walk.h +++ b/util/stream/walk.h @@ -2,34 +2,34 @@ #include "zerocopy.h" -/** - * Zero-copy stream that simplifies implementation of derived classes. - * - * Derived classes must implement `DoUnboundedNext` method. - */ +/** + * Zero-copy stream that simplifies implementation of derived classes. + * + * Derived classes must implement `DoUnboundedNext` method. + */ class IWalkInput: public IZeroCopyInputFastReadTo { public: IWalkInput() : Buf_(nullptr) - , Len_(0) + , Len_(0) { } -protected: +protected: void DoUndo(size_t len) override; size_t DoNext(const void** ptr, size_t len) override; - - /** - * Returns the next data chunk from this input stream. There are no - * restrictions on the size of the data chunk. - * - * @param ptr[out] Pointer to the start of the data chunk. - * @returns Size of the returned data chunk, in bytes. - * Return value of zero signals end of stream. - */ - virtual size_t DoUnboundedNext(const void** ptr) = 0; - + + /** + * Returns the next data chunk from this input stream. There are no + * restrictions on the size of the data chunk. + * + * @param ptr[out] Pointer to the start of the data chunk. + * @returns Size of the returned data chunk, in bytes. + * Return value of zero signals end of stream. + */ + virtual size_t DoUnboundedNext(const void** ptr) = 0; + private: - const void* Buf_; - size_t Len_; + const void* Buf_; + size_t Len_; }; diff --git a/util/stream/walk_ut.cpp b/util/stream/walk_ut.cpp index e0a783799f..115e249607 100644 --- a/util/stream/walk_ut.cpp +++ b/util/stream/walk_ut.cpp @@ -1,55 +1,55 @@ -#include "walk.h" - +#include "walk.h" + #include <library/cpp/testing/unittest/registar.h> - + class TStringListInput: public IWalkInput { -public: +public: TStringListInput(const TVector<TString>& data) : Data_(data) , Index_(0) { } - -protected: + +protected: size_t DoUnboundedNext(const void** ptr) override { - if (Index_ >= Data_.size()) { - return 0; - } - + if (Index_ >= Data_.size()) { + return 0; + } + const TString& string = Data_[Index_++]; - - *ptr = string.data(); - return string.size(); - } - -private: + + *ptr = string.data(); + return string.size(); + } + +private: const TVector<TString>& Data_; - size_t Index_; -}; - + size_t Index_; +}; + Y_UNIT_TEST_SUITE(TWalkTest) { Y_UNIT_TEST(ReadTo) { TVector<TString> data; - data.push_back("111a"); - data.push_back("222b"); - data.push_back("333c"); - data.push_back("444d"); - data.push_back("555e"); - data.push_back("666f"); - - TStringListInput input(data); - + data.push_back("111a"); + data.push_back("222b"); + data.push_back("333c"); + data.push_back("444d"); + data.push_back("555e"); + data.push_back("666f"); + + TStringListInput input(data); + TString tmp1 = input.ReadTo('c'); - UNIT_ASSERT_VALUES_EQUAL(tmp1, "111a222b333"); - - char tmp2; - input.Read(&tmp2, 1); - UNIT_ASSERT_VALUES_EQUAL(tmp2, '4'); - + UNIT_ASSERT_VALUES_EQUAL(tmp1, "111a222b333"); + + char tmp2; + input.Read(&tmp2, 1); + UNIT_ASSERT_VALUES_EQUAL(tmp2, '4'); + TString tmp3 = input.ReadTo('6'); - UNIT_ASSERT_VALUES_EQUAL(tmp3, "44d555e"); - + UNIT_ASSERT_VALUES_EQUAL(tmp3, "44d555e"); + TString tmp4 = input.ReadAll(); - UNIT_ASSERT_VALUES_EQUAL(tmp4, "66f"); - } -} + UNIT_ASSERT_VALUES_EQUAL(tmp4, "66f"); + } +} diff --git a/util/stream/zerocopy.cpp b/util/stream/zerocopy.cpp index dc2982ad55..4cf08b2aad 100644 --- a/util/stream/zerocopy.cpp +++ b/util/stream/zerocopy.cpp @@ -1,36 +1,36 @@ -#include "zerocopy.h" +#include "zerocopy.h" #include "output.h" IZeroCopyInput::~IZeroCopyInput() = default; size_t IZeroCopyInput::DoRead(void* buf, size_t len) { - const void* ptr; - size_t result = DoNext(&ptr, len); + const void* ptr; + size_t result = DoNext(&ptr, len); if (result) { memcpy(buf, ptr, result); } - return result; -} - + return result; +} + ui64 IZeroCopyInput::DoReadAll(IOutputStream& out) { - ui64 result = 0; + ui64 result = 0; const void* ptr; - - while (size_t len = Next(&ptr)) { - out.Write(ptr, len); - result += len; - } - - return result; -} - + + while (size_t len = Next(&ptr)) { + out.Write(ptr, len); + result += len; + } + + return result; +} + size_t IZeroCopyInput::DoSkip(size_t len) { - const void* ptr; + const void* ptr; - return DoNext(&ptr, len); -} + return DoNext(&ptr, len); +} IZeroCopyInputFastReadTo::~IZeroCopyInputFastReadTo() = default; diff --git a/util/stream/zerocopy.h b/util/stream/zerocopy.h index 3315aa3a51..f55c0e10df 100644 --- a/util/stream/zerocopy.h +++ b/util/stream/zerocopy.h @@ -2,22 +2,22 @@ #include <util/system/yassert.h> #include <util/system/defaults.h> -#include <util/generic/ylimits.h> - -#include "input.h" +#include <util/generic/ylimits.h> +#include "input.h" + class IOutputStream; -/** - * @addtogroup Streams - * @{ - */ - -/** - * Input stream with direct access to the input buffer. - * - * Derived classes must implement `DoNext` method. - */ +/** + * @addtogroup Streams + * @{ + */ + +/** + * Input stream with direct access to the input buffer. + * + * Derived classes must implement `DoNext` method. + */ class IZeroCopyInput: public IInputStream { public: IZeroCopyInput() noexcept = default; @@ -26,22 +26,22 @@ public: IZeroCopyInput(IZeroCopyInput&&) noexcept = default; IZeroCopyInput& operator=(IZeroCopyInput&&) noexcept = default; - /** - * Returns the next data chunk from this input stream. - * - * Note that this function is not guaranteed to return the requested number - * of bytes, even if they are in fact available in the stream. - * - * @param ptr[out] Pointer to the start of the data chunk. - * @param len[in] Maximal size of the data chunk to be returned, in bytes. - * @returns Size of the returned data chunk, in bytes. - * Return value of zero signals end of stream. - */ + /** + * Returns the next data chunk from this input stream. + * + * Note that this function is not guaranteed to return the requested number + * of bytes, even if they are in fact available in the stream. + * + * @param ptr[out] Pointer to the start of the data chunk. + * @param len[in] Maximal size of the data chunk to be returned, in bytes. + * @returns Size of the returned data chunk, in bytes. + * Return value of zero signals end of stream. + */ template <class T> inline size_t Next(T** ptr, size_t len) { Y_ASSERT(ptr); - return DoNext((const void**)ptr, len); + return DoNext((const void**)ptr, len); } template <class T> @@ -49,11 +49,11 @@ public: return Next(ptr, Max<size_t>()); } -protected: +protected: size_t DoRead(void* buf, size_t len) override; size_t DoSkip(size_t len) override; ui64 DoReadAll(IOutputStream& out) override; - virtual size_t DoNext(const void** ptr, size_t len) = 0; + virtual size_t DoNext(const void** ptr, size_t len) = 0; }; /** @@ -88,4 +88,4 @@ private: virtual void DoUndo(size_t len) = 0; }; -/** @} */ +/** @} */ diff --git a/util/stream/zlib.cpp b/util/stream/zlib.cpp index 60f4e9439f..3ccfa625ab 100644 --- a/util/stream/zlib.cpp +++ b/util/stream/zlib.cpp @@ -55,8 +55,8 @@ namespace { template <class P, class T> inline bool Next(P** buf, T* len) { if (!Len) { - Len = In->Next(&Buf); - if (!Len) { + Len = In->Next(&Buf); + if (!Len) { return false; } } @@ -177,7 +177,7 @@ namespace { void* buf = AdditionalData(); *ptr = buf; - return Stream_->Read(buf, Min(len, AdditionalDataLength())); + return Stream_->Read(buf, Min(len, AdditionalDataLength())); } private: @@ -327,7 +327,7 @@ TZLibDecompress::TZLibDecompress(IInputStream* input, ZLib::StreamType type, siz : Impl_(new (buflen) TDecompressStream(input, type, dict)) { } - + void TZLibDecompress::SetAllowMultipleStreams(bool allowMultipleStreams) { Impl_->SetAllowMultipleStreams(allowMultipleStreams); } @@ -338,9 +338,9 @@ size_t TZLibDecompress::DoRead(void* buf, size_t size) { return Impl_->Read(buf, MaxPortion(size)); } -void TZLibCompress::Init(const TParams& params) { +void TZLibCompress::Init(const TParams& params) { Y_ENSURE(params.BufLen >= 16, "ZLib buffer too small"); - Impl_.Reset(new (params.BufLen) TImpl(params)); + Impl_.Reset(new (params.BufLen) TImpl(params)); } void TZLibCompress::TDestruct::Destroy(TImpl* impl) { diff --git a/util/stream/zlib.h b/util/stream/zlib.h index e7de7c81b7..084a99a41b 100644 --- a/util/stream/zlib.h +++ b/util/stream/zlib.h @@ -1,19 +1,19 @@ #pragma once #include "fwd.h" -#include "input.h" -#include "output.h" -#include "buffered.h" +#include "input.h" +#include "output.h" +#include "buffered.h" #include <util/system/defaults.h> #include <util/generic/ptr.h> #include <util/generic/yexception.h> -/** - * @addtogroup Streams_Archs - * @{ - */ - +/** + * @addtogroup Streams_Archs + * @{ + */ + struct TZLibError: public yexception { }; @@ -31,20 +31,20 @@ namespace ZLib { Raw = 3, Invalid = 4 }; - - enum { - ZLIB_BUF_LEN = 8 * 1024 - }; + + enum { + ZLIB_BUF_LEN = 8 * 1024 + }; } /** - * Non-buffered ZLib decompressing stream. + * Non-buffered ZLib decompressing stream. * - * Please don't use `TZLibDecompress` if you read text data from stream using - * `ReadLine`, it is VERY slow (approx 10 times slower, according to synthetic - * benchmark). For fast buffered ZLib stream reading use `TBufferedZLibDecompress` - * aka `TZDecompress`. - */ + * Please don't use `TZLibDecompress` if you read text data from stream using + * `ReadLine`, it is VERY slow (approx 10 times slower, according to synthetic + * benchmark). For fast buffered ZLib stream reading use `TBufferedZLibDecompress` + * aka `TZDecompress`. + */ class TZLibDecompress: public IInputStream { public: TZLibDecompress(IZeroCopyInput* input, ZLib::StreamType type = ZLib::Auto, TStringBuf dict = {}); @@ -63,18 +63,18 @@ public: void SetAllowMultipleStreams(bool allowMultipleStreams); ~TZLibDecompress() override; - -protected: + +protected: size_t DoRead(void* buf, size_t size) override; - + public: class TImpl; THolder<TImpl> Impl_; }; -/** - * Non-buffered ZLib compressing stream. - */ +/** + * Non-buffered ZLib compressing stream. + */ class TZLibCompress: public IOutputStream { public: struct TParams { @@ -82,7 +82,7 @@ public: : Out(out) , Type(ZLib::ZLib) , CompressionLevel(6) - , BufLen(ZLib::ZLIB_BUF_LEN) + , BufLen(ZLib::ZLIB_BUF_LEN) { } @@ -145,7 +145,7 @@ private: public: class TImpl; - /** To allow inline constructors. */ + /** To allow inline constructors. */ struct TDestruct { static void Destroy(TImpl* impl); }; @@ -153,12 +153,12 @@ public: THolder<TImpl, TDestruct> Impl_; }; -/** - * Buffered ZLib decompressing stream. - * - * Supports efficient `ReadLine` calls and similar "reading in small pieces" - * usage patterns. - */ +/** + * Buffered ZLib decompressing stream. + * + * Supports efficient `ReadLine` calls and similar "reading in small pieces" + * usage patterns. + */ class TBufferedZLibDecompress: public TBuffered<TZLibDecompress> { public: template <class T> @@ -170,4 +170,4 @@ public: ~TBufferedZLibDecompress() override; }; -/** @} */ +/** @} */ diff --git a/util/string/ascii.h b/util/string/ascii.h index 10344384d3..a7010453fd 100644 --- a/util/string/ascii.h +++ b/util/string/ascii.h @@ -100,37 +100,37 @@ inline bool IsAsciiPunct(unsigned char c) { template <class T> inline bool IsAsciiSpace(T c) { - return ::NPrivate::RangeOk(c) && IsAsciiSpace(static_cast<unsigned char>(c)); + return ::NPrivate::RangeOk(c) && IsAsciiSpace(static_cast<unsigned char>(c)); } template <class T> inline bool IsAsciiUpper(T c) { - return ::NPrivate::RangeOk(c) && IsAsciiUpper(static_cast<unsigned char>(c)); + return ::NPrivate::RangeOk(c) && IsAsciiUpper(static_cast<unsigned char>(c)); } template <class T> inline bool IsAsciiLower(T c) { - return ::NPrivate::RangeOk(c) && IsAsciiLower(static_cast<unsigned char>(c)); + return ::NPrivate::RangeOk(c) && IsAsciiLower(static_cast<unsigned char>(c)); } template <class T> inline bool IsAsciiDigit(T c) { - return ::NPrivate::RangeOk(c) && IsAsciiDigit(static_cast<unsigned char>(c)); + return ::NPrivate::RangeOk(c) && IsAsciiDigit(static_cast<unsigned char>(c)); } template <class T> inline bool IsAsciiAlpha(T c) { - return ::NPrivate::RangeOk(c) && IsAsciiAlpha(static_cast<unsigned char>(c)); + return ::NPrivate::RangeOk(c) && IsAsciiAlpha(static_cast<unsigned char>(c)); } template <class T> inline bool IsAsciiAlnum(T c) { - return ::NPrivate::RangeOk(c) && IsAsciiAlnum(static_cast<unsigned char>(c)); + return ::NPrivate::RangeOk(c) && IsAsciiAlnum(static_cast<unsigned char>(c)); } template <class T> inline bool IsAsciiHex(T c) { - return ::NPrivate::RangeOk(c) && IsAsciiHex(static_cast<unsigned char>(c)); + return ::NPrivate::RangeOk(c) && IsAsciiHex(static_cast<unsigned char>(c)); } template <class T> @@ -160,12 +160,12 @@ inline ::NPrivate::TDereferenced<T> AsciiToUpper(T c) noexcept { /** * ASCII case-insensitive string comparison (for proper UTF8 strings * case-insensitive comparison consider using @c library/cpp/charset). - * + * * BUGS: Currently will NOT work properly with strings that contain * 0-terminator character inside. See IGNIETFERRO-1641 for details. * - * @return true iff @c s1 ans @c s2 are case-insensitively equal. - */ + * @return true iff @c s1 ans @c s2 are case-insensitively equal. + */ static inline bool AsciiEqualsIgnoreCase(const char* s1, const char* s2) noexcept { return stricmp(s1, s2) == 0; } @@ -173,12 +173,12 @@ static inline bool AsciiEqualsIgnoreCase(const char* s1, const char* s2) noexcep /** * ASCII case-insensitive string comparison (for proper UTF8 strings * case-insensitive comparison consider using @c library/cpp/charset). - * + * * BUGS: Currently will NOT work properly with strings that contain * 0-terminator character inside. See IGNIETFERRO-1641 for details. * - * @return true iff @c s1 ans @c s2 are case-insensitively equal. - */ + * @return true iff @c s1 ans @c s2 are case-insensitively equal. + */ static inline bool AsciiEqualsIgnoreCase(const TStringBuf s1, const TStringBuf s2) noexcept { return (s1.size() == s2.size()) && strnicmp(s1.data(), s2.data(), s1.size()) == 0; } @@ -186,14 +186,14 @@ static inline bool AsciiEqualsIgnoreCase(const TStringBuf s1, const TStringBuf s /** * ASCII case-insensitive string comparison (for proper UTF8 strings * case-insensitive comparison consider using @c library/cpp/charset). - * + * * BUGS: Currently will NOT work properly with strings that contain * 0-terminator character inside. See IGNIETFERRO-1641 for details. * - * @return 0 if strings are equal, negative if @c s1 < @c s2 - * and positive otherwise. - * (same value as @c stricmp does). - */ + * @return 0 if strings are equal, negative if @c s1 < @c s2 + * and positive otherwise. + * (same value as @c stricmp does). + */ static inline int AsciiCompareIgnoreCase(const char* s1, const char* s2) noexcept { return stricmp(s1, s2); } @@ -201,7 +201,7 @@ static inline int AsciiCompareIgnoreCase(const char* s1, const char* s2) noexcep /** * ASCII case-insensitive string comparison (for proper UTF8 strings * case-insensitive comparison consider using @c library/cpp/charset). - * + * * BUGS: Currently will NOT work properly with strings that contain * 0-terminator character inside. See IGNIETFERRO-1641 for details. * @@ -210,7 +210,7 @@ static inline int AsciiCompareIgnoreCase(const char* s1, const char* s2) noexcep * - negative if @c s1 < @c s2 * - positive otherwise, * similar to stricmp. - */ + */ Y_PURE_FUNCTION int AsciiCompareIgnoreCase(const TStringBuf s1, const TStringBuf s2) noexcept; /** diff --git a/util/string/cast.cpp b/util/string/cast.cpp index aa1e65a8e9..f70ca427f9 100644 --- a/util/string/cast.cpp +++ b/util/string/cast.cpp @@ -8,9 +8,9 @@ #include <string> #include <cmath> -#include <util/string/type.h> -#include <util/string/cast.h> -#include <util/string/escape.h> +#include <util/string/type.h> +#include <util/string/cast.h> +#include <util/string/escape.h> #include <contrib/libs/double-conversion/double-conversion.h> @@ -18,7 +18,7 @@ #include <util/system/yassert.h> #include <util/generic/yexception.h> #include <util/generic/typetraits.h> -#include <util/generic/ylimits.h> +#include <util/generic/ylimits.h> #include <util/generic/singleton.h> #include <util/generic/utility.h> @@ -259,19 +259,19 @@ namespace { return PS_OK; } - }; - + }; + template <class T> struct TBounds { T PositiveMax; T NegativeMax; }; - + template <class T, unsigned base, class TChar> struct TIntParser { static_assert(1 < base && base < 17, "Expect 1 < base && base < 17."); static_assert(std::is_integral<T>::value, "T must be an integral type."); - + enum { IsSigned = std::is_signed<T>::value }; @@ -431,11 +431,11 @@ namespace { DEF_INT_SPEC_II(TYPE, ITYPE, 2) \ DEF_INT_SPEC_II(TYPE, ITYPE, 8) \ DEF_INT_SPEC_II(TYPE, ITYPE, 10) \ - DEF_INT_SPEC_II(TYPE, ITYPE, 16) + DEF_INT_SPEC_II(TYPE, ITYPE, 16) #define DEF_INT_SPEC(TYPE) \ DEF_INT_SPEC_I(signed TYPE, i64) \ - DEF_INT_SPEC_I(unsigned TYPE, ui64) + DEF_INT_SPEC_I(unsigned TYPE, ui64) DEF_INT_SPEC(char) DEF_INT_SPEC(short) @@ -452,15 +452,15 @@ size_t ToStringImpl<char8_t>(char8_t value, char* buf, size_t len) { using TCharIType = std::conditional_t<std::is_signed<char>::value, i64, ui64>; using TWCharIType = std::conditional_t<std::is_signed<wchar_t>::value, i64, ui64>; - -DEF_INT_SPEC_I(char, TCharIType) -DEF_INT_SPEC_I(wchar_t, TWCharIType) + +DEF_INT_SPEC_I(char, TCharIType) +DEF_INT_SPEC_I(wchar_t, TWCharIType) DEF_INT_SPEC_I(wchar16, ui64) // wchar16 is always unsigned DEF_INT_SPEC_I(wchar32, ui64) // wchar32 is always unsigned - + #undef DEF_INT_SPEC -#undef DEF_INT_SPEC_I -#undef DEF_INT_SPEC_II +#undef DEF_INT_SPEC_I +#undef DEF_INT_SPEC_II #define DEF_FLT_SPEC(type) \ template <> \ @@ -604,31 +604,31 @@ bool TryFromStringImpl<TUtf16String>(const wchar16* data, size_t len, TUtf16Stri DEF_INT_SPEC_III(CHAR, TYPE, ITYPE, BOUNDS, 2) \ DEF_INT_SPEC_III(CHAR, TYPE, ITYPE, BOUNDS, 8) \ DEF_INT_SPEC_III(CHAR, TYPE, ITYPE, BOUNDS, 10) \ - DEF_INT_SPEC_III(CHAR, TYPE, ITYPE, BOUNDS, 16) + DEF_INT_SPEC_III(CHAR, TYPE, ITYPE, BOUNDS, 16) #define DEF_INT_SPEC_I(TYPE, ITYPE, BOUNDS) \ DEF_INT_SPEC_II(char, TYPE, ITYPE, BOUNDS) \ DEF_INT_SPEC_II(wchar16, TYPE, ITYPE, BOUNDS) - + #define DEF_INT_SPEC(TYPE, ID) \ DEF_INT_SPEC_I(signed TYPE, i64, ID##SBounds) \ - DEF_INT_SPEC_I(unsigned TYPE, ui64, ID##UBounds) - + DEF_INT_SPEC_I(unsigned TYPE, ui64, ID##UBounds) + #define DEF_INT_SPEC_FIXED_WIDTH(TYPE, ID) \ DEF_INT_SPEC_I(TYPE, i64, ID##SBounds) \ DEF_INT_SPEC_I(u##TYPE, ui64, ID##UBounds) DEF_INT_SPEC_FIXED_WIDTH(i8, b) -DEF_INT_SPEC(short, s) -DEF_INT_SPEC(int, i) -DEF_INT_SPEC(long, l) -DEF_INT_SPEC(long long, ll) - +DEF_INT_SPEC(short, s) +DEF_INT_SPEC(int, i) +DEF_INT_SPEC(long, l) +DEF_INT_SPEC(long long, ll) + #undef DEF_INT_SPEC_FIXED_WIDTH #undef DEF_INT_SPEC -#undef DEF_INT_SPEC_I -#undef DEF_INT_SPEC_II -#undef DEF_INT_SPEC_III +#undef DEF_INT_SPEC_I +#undef DEF_INT_SPEC_II +#undef DEF_INT_SPEC_III #define DEF_FLT_SPEC(type) \ template <> \ diff --git a/util/string/cast.h b/util/string/cast.h index 90e925c194..14f90a0896 100644 --- a/util/string/cast.h +++ b/util/string/cast.h @@ -15,10 +15,10 @@ template <class T> size_t ToStringImpl(T t, char* buf, size_t len); /** - * Converts @c t to string writing not more than @c len bytes to output buffer @c buf. - * No NULL terminator appended! Throws exception on buffer overflow. - * @return number of bytes written - */ + * Converts @c t to string writing not more than @c len bytes to output buffer @c buf. + * No NULL terminator appended! Throws exception on buffer overflow. + * @return number of bytes written + */ template <class T> inline size_t ToString(const T& t, char* buf, size_t len) { using TParam = typename TTypeTraits<T>::TFuncParam; @@ -26,20 +26,20 @@ inline size_t ToString(const T& t, char* buf, size_t len) { return ToStringImpl<TParam>(t, buf, len); } -/** - * Floating point to string conversion mode, values are enforced by `dtoa_impl.cpp`. - */ +/** + * Floating point to string conversion mode, values are enforced by `dtoa_impl.cpp`. + */ enum EFloatToStringMode { - /** 0.1f -> "0.1", 0.12345678f -> "0.12345678", ignores ndigits. */ - PREC_AUTO = 0, - - /** "%g" mode, writes up to the given number of significant digits: - * 0.1f -> "0.1", 0.12345678f -> "0.123457" for ndigits=6, 1.2e-06f -> "1.2e-06" */ - PREC_NDIGITS = 2, - - /** "%f" mode, writes the given number of digits after decimal point: - * 0.1f -> "0.100000", 1.2e-06f -> "0.000001" for ndigits=6 */ - PREC_POINT_DIGITS = 3, + /** 0.1f -> "0.1", 0.12345678f -> "0.12345678", ignores ndigits. */ + PREC_AUTO = 0, + + /** "%g" mode, writes up to the given number of significant digits: + * 0.1f -> "0.1", 0.12345678f -> "0.123457" for ndigits=6, 1.2e-06f -> "1.2e-06" */ + PREC_NDIGITS = 2, + + /** "%f" mode, writes the given number of digits after decimal point: + * 0.1f -> "0.100000", 1.2e-06f -> "0.000001" for ndigits=6 */ + PREC_POINT_DIGITS = 3, /** same as PREC_POINT_DIGITS, but stripping trailing zeroes: * 0.1f for ndgigits=6 -> "0.1" */ @@ -177,7 +177,7 @@ inline T FromString(const TWtringBuf& s) { template <class T> inline T FromString(const TUtf16String& s) { - return ::FromString<T, wchar16>(s.data(), s.size()); + return ::FromString<T, wchar16>(s.data(), s.size()); } namespace NPrivate { diff --git a/util/string/cast_ut.cpp b/util/string/cast_ut.cpp index 033450c38c..ab25d83fe2 100644 --- a/util/string/cast_ut.cpp +++ b/util/string/cast_ut.cpp @@ -222,11 +222,11 @@ Y_UNIT_TEST_SUITE(TCastTest) { test1(long int, LONG_MIN); test1(long int, LONG_MAX - 1); test1(long int, LONG_MIN + 1); - - test1(long long int, LLONG_MAX); - test1(long long int, LLONG_MIN); - test1(long long int, LLONG_MAX - 1); - test1(long long int, LLONG_MIN + 1); + + test1(long long int, LLONG_MAX); + test1(long long int, LLONG_MIN); + test1(long long int, LLONG_MAX - 1); + test1(long long int, LLONG_MIN + 1); } Y_UNIT_TEST(TestVolatile) { diff --git a/util/string/escape.h b/util/string/escape.h index b01be65b0e..3814bd729f 100644 --- a/util/string/escape.h +++ b/util/string/escape.h @@ -52,7 +52,7 @@ static inline TBasicString<TChar> EscapeC(const TChar* str) { TString& EscapeC(const TStringBuf str, TString& res); TUtf16String& EscapeC(const TWtringBuf str, TUtf16String& res); -// these two need to be methods, because of TBasicString::Quote implementation +// these two need to be methods, because of TBasicString::Quote implementation TString EscapeC(const TString& str); TUtf16String EscapeC(const TUtf16String& str); diff --git a/util/string/split.h b/util/string/split.h index bc46d9e64c..b5fcfa4501 100644 --- a/util/string/split.h +++ b/util/string/split.h @@ -102,21 +102,21 @@ static inline I* FastStrStr(I* str, I* f, size_t l) noexcept { } } -template <class Char> +template <class Char> struct TStringDelimiter { - inline TStringDelimiter(Char* delim) noexcept + inline TStringDelimiter(Char* delim) noexcept : Delim(delim) , Len(std::char_traits<Char>::length(delim)) { } - inline TStringDelimiter(Char* delim, size_t len) noexcept + inline TStringDelimiter(Char* delim, size_t len) noexcept : Delim(delim) , Len(len) { } - inline Char* Find(Char*& b, Char* e) const noexcept { + inline Char* Find(Char*& b, Char* e) const noexcept { const auto ret = std::basic_string_view<Char>(b, e - b).find(Delim, 0, Len); if (ret != std::string::npos) { @@ -128,26 +128,26 @@ struct TStringDelimiter { return (b = e); } - inline Char* Find(Char*& b) const noexcept { - Char* ret = FastStrStr(b, Delim, Len); + inline Char* Find(Char*& b) const noexcept { + Char* ret = FastStrStr(b, Delim, Len); b = *ret ? ret + Len : ret; return ret; } - Char* Delim; + Char* Delim; const size_t Len; }; -template <class Char> +template <class Char> struct TCharDelimiter { - inline TCharDelimiter(Char ch) noexcept + inline TCharDelimiter(Char ch) noexcept : Ch(ch) { } - inline Char* Find(Char*& b, Char* e) const noexcept { + inline Char* Find(Char*& b, Char* e) const noexcept { const auto ret = std::basic_string_view<Char>(b, e - b).find(Ch); if (ret != std::string::npos) { @@ -159,8 +159,8 @@ struct TCharDelimiter { return (b = e); } - inline Char* Find(Char*& b) const noexcept { - Char* ret = FastStrChr(b, Ch); + inline Char* Find(Char*& b) const noexcept { + Char* ret = FastStrChr(b, Ch); if (*ret) { b = ret + 1; @@ -171,19 +171,19 @@ struct TCharDelimiter { return ret; } - Char Ch; + Char Ch; }; -template <class Iterator, class Condition> +template <class Iterator, class Condition> struct TFuncDelimiter { public: template <class... Args> - TFuncDelimiter(Args&&... args) - : Fn(std::forward<Args>(args)...) + TFuncDelimiter(Args&&... args) + : Fn(std::forward<Args>(args)...) { } - inline Iterator Find(Iterator& b, Iterator e) const noexcept { + inline Iterator Find(Iterator& b, Iterator e) const noexcept { if ((b = std::find_if(b, e, Fn)) != e) { return b++; } @@ -192,18 +192,18 @@ public: } private: - Condition Fn; + Condition Fn; }; -template <class Char> +template <class Char> struct TFindFirstOf { - inline TFindFirstOf(Char* set) + inline TFindFirstOf(Char* set) : Set(set) { } - inline Char* FindFirstOf(Char* b, Char* e) const noexcept { - Char* ret = b; + inline Char* FindFirstOf(Char* b, Char* e) const noexcept { + Char* ret = b; for (; ret != e; ++ret) { if (NStringSplitPrivate::Find(Set, *ret)) break; @@ -211,13 +211,13 @@ struct TFindFirstOf { return ret; } - inline Char* FindFirstOf(Char* b) const noexcept { + inline Char* FindFirstOf(Char* b) const noexcept { const std::basic_string_view<Char> bView(b); const auto ret = bView.find_first_of(Set); return ret != std::string::npos ? b + ret : b + bView.size(); } - Char* Set; + Char* Set; }; template <> @@ -233,12 +233,12 @@ struct TFindFirstOf<const char>: public TCompactStrSpn { } }; -template <class Char> -struct TSetDelimiter: private TFindFirstOf<const Char> { - using TFindFirstOf<const Char>::TFindFirstOf; +template <class Char> +struct TSetDelimiter: private TFindFirstOf<const Char> { + using TFindFirstOf<const Char>::TFindFirstOf; - inline Char* Find(Char*& b, Char* e) const noexcept { - Char* ret = const_cast<Char*>(this->FindFirstOf(b, e)); + inline Char* Find(Char*& b, Char* e) const noexcept { + Char* ret = const_cast<Char*>(this->FindFirstOf(b, e)); if (ret != e) { b = ret + 1; @@ -248,8 +248,8 @@ struct TSetDelimiter: private TFindFirstOf<const Char> { return (b = e); } - inline Char* Find(Char*& b) const noexcept { - Char* ret = const_cast<Char*>(this->FindFirstOf(b)); + inline Char* Find(Char*& b) const noexcept { + Char* ret = const_cast<Char*>(this->FindFirstOf(b)); if (*ret) { b = ret + 1; diff --git a/util/string/strip.h b/util/string/strip.h index d5ef6da96d..3db606cc24 100644 --- a/util/string/strip.h +++ b/util/string/strip.h @@ -86,7 +86,7 @@ struct TStripImpl { auto e = from.end(); if (StripRange(b, e, criterion)) { - to = T(b, e - b); + to = T(b, e - b); return true; } diff --git a/util/string/subst.cpp b/util/string/subst.cpp index b2df328dc1..f9036f8ef6 100644 --- a/util/string/subst.cpp +++ b/util/string/subst.cpp @@ -43,9 +43,9 @@ static bool IsIntersect(const T& a, const U& b) noexcept { * Uses two separate implementations (inplace for shrink and append for grow case) * See IGNIETFERRO-394 **/ -template <class TStringType, typename TStringViewType = TBasicStringBuf<typename TStringType::value_type>> +template <class TStringType, typename TStringViewType = TBasicStringBuf<typename TStringType::value_type>> static inline size_t SubstGlobalImpl(TStringType& s, const TStringViewType from, const TStringViewType to, size_t fromPos = 0) { - if (from.empty()) { + if (from.empty()) { return 0; } @@ -174,7 +174,7 @@ size_t SubstGlobal(TUtf32String& text, const TUtf32StringBuf what, const TUtf32S } size_t SubstGlobal(std::u16string& text, const TWtringBuf what, const TWtringBuf with, size_t from) { - return SubstGlobalImpl(text, + return SubstGlobalImpl(text, std::u16string_view(reinterpret_cast<const char16_t*>(what.data()), what.size()), std::u16string_view(reinterpret_cast<const char16_t*>(with.data()), with.size()), from); diff --git a/util/system/backtrace.cpp b/util/system/backtrace.cpp index b77fe58fb1..a9907b78a0 100644 --- a/util/system/backtrace.cpp +++ b/util/system/backtrace.cpp @@ -281,7 +281,7 @@ void TBackTrace::PrintTo(IOutputStream& out) const { TString TBackTrace::PrintToString() const { TStringStream ss; PrintTo(ss); - return ss.Str(); + return ss.Str(); } size_t TBackTrace::size() const { diff --git a/util/system/backtrace_ut.cpp b/util/system/backtrace_ut.cpp index 9b5ead71bc..b8240483a5 100644 --- a/util/system/backtrace_ut.cpp +++ b/util/system/backtrace_ut.cpp @@ -51,7 +51,7 @@ class TBackTraceTest: public TTestBase { TStringStream out; FormatBackTrace(&out); SetFormatBackTraceFn(prevFn); - UNIT_ASSERT(out.Str().Contains("WorksLikeACharm")); + UNIT_ASSERT(out.Str().Contains("WorksLikeACharm")); TestPrintBackTrace(); } diff --git a/util/system/compiler.h b/util/system/compiler.h index b373edcc46..25035b92d0 100644 --- a/util/system/compiler.h +++ b/util/system/compiler.h @@ -52,7 +52,7 @@ #define Y_NO_SANITIZE(...) #endif -/** +/** * @def Y_DECLARE_UNUSED * * Macro is needed to silence compiler warning about unused entities (e.g. function or argument). @@ -83,12 +83,12 @@ #define Y_PREFETCH_WRITE(Pointer, Priority) __builtin_prefetch((const void*)(Pointer), 1, Priority) #endif -/** +/** * @def Y_FORCE_INLINE - * - * Macro to use in place of 'inline' in function declaration/definition to force - * it to be inlined. - */ + * + * Macro to use in place of 'inline' in function declaration/definition to force + * it to be inlined. + */ #if !defined(Y_FORCE_INLINE) #if defined(CLANG_COVERAGE) #/* excessive __always_inline__ might significantly slow down compilation of an instrumented unit */ @@ -101,14 +101,14 @@ #else #define Y_FORCE_INLINE inline #endif -#endif - -/** +#endif + +/** * @def Y_NO_INLINE - * - * Macro to use in place of 'inline' in function declaration/definition to - * prevent it from being inlined. - */ + * + * Macro to use in place of 'inline' in function declaration/definition to + * prevent it from being inlined. + */ #if !defined(Y_NO_INLINE) #if defined(_MSC_VER) #define Y_NO_INLINE __declspec(noinline) @@ -118,8 +118,8 @@ #else #define Y_NO_INLINE #endif -#endif - +#endif + //to cheat compiler about strict aliasing or similar problems #if defined(__GNUC__) #define Y_FAKE_READ(X) \ diff --git a/util/system/defaults.h b/util/system/defaults.h index dcd7abea38..25a3b38f9f 100644 --- a/util/system/defaults.h +++ b/util/system/defaults.h @@ -134,12 +134,12 @@ constexpr bool Y_IS_DEBUG_BUILD = true; #undef Y_ARRAY_END #define Y_ARRAY_END(arr) ((arr) + Y_ARRAY_SIZE(arr)) -/** - * Concatenates two symbols, even if one of them is itself a macro. - */ -#define Y_CAT(X, Y) Y_CAT_I(X, Y) -#define Y_CAT_I(X, Y) Y_CAT_II(X, Y) -#define Y_CAT_II(X, Y) X##Y +/** + * Concatenates two symbols, even if one of them is itself a macro. + */ +#define Y_CAT(X, Y) Y_CAT_I(X, Y) +#define Y_CAT_I(X, Y) Y_CAT_II(X, Y) +#define Y_CAT_II(X, Y) X##Y #define Y_STRINGIZE(X) UTIL_PRIVATE_STRINGIZE_AUX(X) #define UTIL_PRIVATE_STRINGIZE_AUX(X) #X diff --git a/util/system/direct_io.h b/util/system/direct_io.h index 6a3325a960..0fdde3a847 100644 --- a/util/system/direct_io.h +++ b/util/system/direct_io.h @@ -47,11 +47,11 @@ public: private: inline bool IsAligned(i64 value) { - return Alignment ? value == AlignDown<i64>(value, Alignment) : true; + return Alignment ? value == AlignDown<i64>(value, Alignment) : true; } inline bool IsAligned(const void* value) { - return Alignment ? value == AlignDown(value, Alignment) : true; + return Alignment ? value == AlignDown(value, Alignment) : true; } size_t PreadSafe(void* buffer, size_t byteCount, ui64 offset); diff --git a/util/system/execpath.cpp b/util/system/execpath.cpp index 33198af58b..ac2141ef03 100644 --- a/util/system/execpath.cpp +++ b/util/system/execpath.cpp @@ -158,15 +158,15 @@ static TString GetExecPathImpl() { static bool GetPersistentExecPathImpl(TString& to) { #if defined(_solaris_) to = TString("/proc/self/object/a.out"); - return true; + return true; #elif defined(_linux_) || defined(_cygwin_) to = TString("/proc/self/exe"); - return true; + return true; #elif defined(_freebsd_) to = TString("/proc/curproc/file"); - return true; + return true; #else // defined(_win_) || defined(_darwin_) or unknown - Y_UNUSED(to); + Y_UNUSED(to); return false; #endif } diff --git a/util/system/file.cpp b/util/system/file.cpp index 4a261d020c..32dd508ded 100644 --- a/util/system/file.cpp +++ b/util/system/file.cpp @@ -105,7 +105,7 @@ TFileHandle::TFileHandle(const TString& fName, EOpenMode oMode) noexcept { // WrOnly or RdWr faMode |= GENERIC_WRITE; } - if (oMode & ::ForAppend) { + if (oMode & ::ForAppend) { faMode |= GENERIC_WRITE; faMode |= FILE_APPEND_DATA; faMode &= ~FILE_WRITE_DATA; @@ -171,7 +171,7 @@ TFileHandle::TFileHandle(const TString& fName, EOpenMode oMode) noexcept { fcMode |= O_WRONLY; } - if (oMode & ::ForAppend) { + if (oMode & ::ForAppend) { fcMode |= O_APPEND; } @@ -1243,60 +1243,60 @@ TFile TFile::Temporary(const TString& prefix) { } TFile TFile::ForAppend(const TString& path) { - return TFile(path, OpenAlways | WrOnly | Seq | ::ForAppend); -} - + return TFile(path, OpenAlways | WrOnly | Seq | ::ForAppend); +} + TFile Duplicate(FILE* f) { return Duplicate(fileno(f)); } TFile Duplicate(int fd) { #if defined(_win_) - /* There are two options of how to duplicate a file descriptor on Windows: - * - * 1: - * - Call dup. - * - Call _get_osfhandle on the result. - * - Use returned handle. - * - Call _close on file descriptor returned by dup. This will also close - * the handle. - * - * 2: - * - Call _get_osfhandle. - * - Call DuplicateHandle on the result. - * - Use returned handle. - * - Call CloseHandle. - * - * TFileHandle calls CloseHandle when destroyed, leaving us with option #2. */ - FHANDLE handle = reinterpret_cast<FHANDLE>(::_get_osfhandle(fd)); - - FHANDLE dupHandle; + /* There are two options of how to duplicate a file descriptor on Windows: + * + * 1: + * - Call dup. + * - Call _get_osfhandle on the result. + * - Use returned handle. + * - Call _close on file descriptor returned by dup. This will also close + * the handle. + * + * 2: + * - Call _get_osfhandle. + * - Call DuplicateHandle on the result. + * - Use returned handle. + * - Call CloseHandle. + * + * TFileHandle calls CloseHandle when destroyed, leaving us with option #2. */ + FHANDLE handle = reinterpret_cast<FHANDLE>(::_get_osfhandle(fd)); + + FHANDLE dupHandle; if (!::DuplicateHandle(GetCurrentProcess(), handle, GetCurrentProcess(), &dupHandle, 0, TRUE, DUPLICATE_SAME_ACCESS)) { ythrow TFileError() << "can not duplicate file descriptor " << LastSystemError() << Endl; } - - return TFile(dupHandle); + + return TFile(dupHandle); #elif defined(_unix_) return TFile(::dup(fd)); #else #error unsupported platform #endif } - + bool PosixDisableReadAhead(FHANDLE fileHandle, void* addr) noexcept { - int ret = -1; - -#if HAVE_POSIX_FADVISE + int ret = -1; + +#if HAVE_POSIX_FADVISE #if defined(_linux_) Y_UNUSED(fileHandle); - ret = madvise(addr, 0, MADV_RANDOM); // according to klamm@ posix_fadvise does not work under linux, madvise does work + ret = madvise(addr, 0, MADV_RANDOM); // according to klamm@ posix_fadvise does not work under linux, madvise does work #else Y_UNUSED(addr); ret = ::posix_fadvise(fileHandle, 0, 0, POSIX_FADV_RANDOM); #endif -#else +#else Y_UNUSED(fileHandle); Y_UNUSED(addr); -#endif - return ret == 0; -} +#endif + return ret == 0; +} diff --git a/util/system/file.h b/util/system/file.h index 9502e159b6..538d006260 100644 --- a/util/system/file.h +++ b/util/system/file.h @@ -71,11 +71,11 @@ public: } inline TFileHandle(TFileHandle&& other) noexcept - : Fd_(other.Fd_) - { - other.Fd_ = INVALID_FHANDLE; - } - + : Fd_(other.Fd_) + { + other.Fd_ = INVALID_FHANDLE; + } + TFileHandle(const TString& fName, EOpenMode oMode) noexcept; inline ~TFileHandle() { @@ -221,5 +221,5 @@ private: TFile Duplicate(FILE*); TFile Duplicate(int); - + bool PosixDisableReadAhead(FHANDLE fileHandle, void* addr) noexcept; diff --git a/util/system/file_lock.cpp b/util/system/file_lock.cpp index 45d91282c5..a98018b9db 100644 --- a/util/system/file_lock.cpp +++ b/util/system/file_lock.cpp @@ -12,8 +12,8 @@ namespace { return LOCK_EX; case EFileLockType::Shared: return LOCK_SH; - default: - Y_UNREACHABLE(); + default: + Y_UNREACHABLE(); } Y_UNREACHABLE(); } diff --git a/util/system/filemap.cpp b/util/system/filemap.cpp index 7454a4cb94..fe39f8525f 100644 --- a/util/system/filemap.cpp +++ b/util/system/filemap.cpp @@ -37,10 +37,10 @@ #undef PAGE_SIZE #undef GRANULARITY -#ifdef _win_ +#ifdef _win_ #define MAP_FAILED ((void*)(LONG_PTR)-1) -#endif - +#endif + namespace { struct TSysInfo { inline TSysInfo() @@ -136,8 +136,8 @@ public: if (Mapping_ == nullptr) { ythrow yexception() << "Can't create file mapping of '" << DbgName_ << "': " << LastSystemErrorText(); } - } else { - Mapping_ = MAP_FAILED; + } else { + Mapping_ = MAP_FAILED; } #elif defined(_unix_) if (!(Mode_ & oNotGreedy)) { diff --git a/util/system/filemap.h b/util/system/filemap.h index 11be64bff4..94b1425b48 100644 --- a/util/system/filemap.h +++ b/util/system/filemap.h @@ -61,9 +61,9 @@ struct TMemoryMapCommon { Y_DECLARE_FLAGS(EOpenMode, EOpenModeFlag) /** - * Name that will be printed in exceptions if not specified. - * Overridden by name obtained from `TFile` if it's not empty. - */ + * Name that will be printed in exceptions if not specified. + * Overridden by name obtained from `TFile` if it's not empty. + */ static TString UnknownFileName(); }; Y_DECLARE_OPERATORS_FOR_FLAGS(TMemoryMapCommon::EOpenMode) diff --git a/util/system/fs.cpp b/util/system/fs.cpp index d2611a8ccc..81f0b15598 100644 --- a/util/system/fs.cpp +++ b/util/system/fs.cpp @@ -39,7 +39,7 @@ void NFs::RemoveRecursive(const TString& path) { TDirIterator dir(path); - for (auto it = dir.begin(); it != dir.end(); ++it) { + for (auto it = dir.begin(); it != dir.end(); ++it) { switch (it->fts_info) { case FTS_DOT: case FTS_D: diff --git a/util/system/fstat.cpp b/util/system/fstat.cpp index 81e98cbc6b..2550d66dcd 100644 --- a/util/system/fstat.cpp +++ b/util/system/fstat.cpp @@ -133,9 +133,9 @@ TFileStat::TFileStat(const char* fileName, bool nofollow) { } bool TFileStat::IsNull() const noexcept { - return *this == TFileStat(); -} - + return *this == TFileStat(); +} + bool TFileStat::IsFile() const noexcept { return S_ISREG(Mode); } @@ -157,12 +157,12 @@ bool operator==(const TFileStat& l, const TFileStat& r) noexcept { l.ATime == r.ATime && l.MTime == r.MTime && l.CTime == r.CTime; -} - +} + bool operator!=(const TFileStat& l, const TFileStat& r) noexcept { - return !(l == r); -} - + return !(l == r); +} + i64 GetFileLength(FHANDLE fd) { #if defined(_win_) LARGE_INTEGER pos; @@ -184,32 +184,32 @@ i64 GetFileLength(FHANDLE fd) { #error unsupported platform #endif } - -i64 GetFileLength(const char* name) { -#if defined(_win_) - WIN32_FIND_DATA fData; - HANDLE h = FindFirstFileA(name, &fData); - if (h == INVALID_HANDLE_VALUE) - return -1; - FindClose(h); - return (((i64)fData.nFileSizeHigh) * (i64(MAXDWORD) + 1)) + (i64)fData.nFileSizeLow; -#elif defined(_unix_) - struct stat buf; - int r = ::stat(name, &buf); + +i64 GetFileLength(const char* name) { +#if defined(_win_) + WIN32_FIND_DATA fData; + HANDLE h = FindFirstFileA(name, &fData); + if (h == INVALID_HANDLE_VALUE) + return -1; + FindClose(h); + return (((i64)fData.nFileSizeHigh) * (i64(MAXDWORD) + 1)) + (i64)fData.nFileSizeLow; +#elif defined(_unix_) + struct stat buf; + int r = ::stat(name, &buf); if (r == -1) { - return -1; + return -1; } if (!(buf.st_mode & (S_IFREG | S_IFBLK | S_IFCHR))) { // st_size only makes sense for regular files or devices errno = EINVAL; return -1; } - return (i64)buf.st_size; -#else + return (i64)buf.st_size; +#else #error unsupported platform -#endif -} - +#endif +} + i64 GetFileLength(const TString& name) { return GetFileLength(name.data()); -} +} diff --git a/util/system/fstat.h b/util/system/fstat.h index 64e79e1b55..8b61fb8c89 100644 --- a/util/system/fstat.h +++ b/util/system/fstat.h @@ -7,24 +7,24 @@ class TFile; class TFsPath; struct TFileStat { - ui32 Mode = 0; /* protection */ - ui32 Uid = 0; /* user ID of owner */ - ui32 Gid = 0; /* group ID of owner */ + ui32 Mode = 0; /* protection */ + ui32 Uid = 0; /* user ID of owner */ + ui32 Gid = 0; /* group ID of owner */ ui64 NLinks = 0; /* number of hard links */ ui64 Size = 0; /* total size, in bytes */ ui64 INode = 0; /* inode number */ ui64 AllocationSize = 0; /* number of bytes allocated on the disk */ - time_t ATime = 0; /* time of last access */ - time_t MTime = 0; /* time of last modification */ - time_t CTime = 0; /* time of last status change */ + time_t ATime = 0; /* time of last access */ + time_t MTime = 0; /* time of last modification */ + time_t CTime = 0; /* time of last status change */ public: TFileStat(); bool IsNull() const noexcept; - + bool IsFile() const noexcept; bool IsDir() const noexcept; bool IsSymlink() const noexcept; @@ -37,11 +37,11 @@ public: friend bool operator==(const TFileStat& l, const TFileStat& r) noexcept; friend bool operator!=(const TFileStat& l, const TFileStat& r) noexcept; - + private: void MakeFromFileName(const char* fileName, bool nofollow); }; i64 GetFileLength(FHANDLE fd); -i64 GetFileLength(const char* name); +i64 GetFileLength(const char* name); i64 GetFileLength(const TString& name); diff --git a/util/system/hostname.cpp b/util/system/hostname.cpp index 386f646d6b..8ae33d3d8c 100644 --- a/util/system/hostname.cpp +++ b/util/system/hostname.cpp @@ -89,7 +89,7 @@ bool IsFQDN(const TString& name) { try { // ResolveHost() can't be used since it is ipv4-only, port is not important TNetworkAddress addr(absName, 0); - } catch (const TNetworkResolutionError&) { + } catch (const TNetworkResolutionError&) { return false; } return true; diff --git a/util/system/interrupt_signals.h b/util/system/interrupt_signals.h index 9f9c2427f1..e869b471ea 100644 --- a/util/system/interrupt_signals.h +++ b/util/system/interrupt_signals.h @@ -10,13 +10,13 @@ #endif #endif -/** +/** * Set handler for interrupt signals. * * All OSes: SIGINT, SIGTERM (defined by C++ standard) * UNIX variants: Also SIGHUP * Windows: CTRL_C_EVENT handled as SIGINT, CTRL_BREAK_EVENT as SIGTERM, CTRL_CLOSE_EVENT as SIGHUP - * - * \param handler Signal handler to use. Pass nullptr to clear currently set handler. + * + * \param handler Signal handler to use. Pass nullptr to clear currently set handler. */ void SetInterruptSignalsHandler(void (*handler)(int signum)); diff --git a/util/system/mlock.cpp b/util/system/mlock.cpp index 435338c98f..df4891e219 100644 --- a/util/system/mlock.cpp +++ b/util/system/mlock.cpp @@ -52,18 +52,18 @@ void UnlockMemory(const void* addr, size_t len) { #endif } -void LockAllMemory(ELockAllMemoryFlags flags) { - Y_UNUSED(flags); +void LockAllMemory(ELockAllMemoryFlags flags) { + Y_UNUSED(flags); #if defined(_android_) // unimplemented #elif defined(_cygwin_) // unimplemented #elif defined(_unix_) int sys_flags = 0; - if (flags & LockCurrentMemory) { + if (flags & LockCurrentMemory) { sys_flags |= MCL_CURRENT; } - if (flags & LockFutureMemory) { + if (flags & LockFutureMemory) { sys_flags |= MCL_FUTURE; } if (flags & LockMemoryOnFault) { diff --git a/util/system/mlock.h b/util/system/mlock.h index f021c0fe67..06075c3f25 100644 --- a/util/system/mlock.h +++ b/util/system/mlock.h @@ -2,8 +2,8 @@ #include "defaults.h" -#include <util/generic/flags.h> - +#include <util/generic/flags.h> + //on some systems (not win, freebd, linux, but darwin (Mac OS X) //multiple mlock calls on the same address range //require the corresponding number of munlock calls to actually unlock the pages @@ -13,31 +13,31 @@ void LockMemory(const void* addr, size_t len); void UnlockMemory(const void* addr, size_t len); -enum ELockAllMemoryFlag { - /** Lock all pages which are currently mapped into the address space of the process. */ - LockCurrentMemory = 1, - - /** Lock all pages which will become mapped into the address space of the process in the future. */ - LockFutureMemory = 2, +enum ELockAllMemoryFlag { + /** Lock all pages which are currently mapped into the address space of the process. */ + LockCurrentMemory = 1, + + /** Lock all pages which will become mapped into the address space of the process in the future. */ + LockFutureMemory = 2, /** Since Linux 4.4, with LockCurrentMemory or LockFutureMemory or both, lock only pages that are or once they are present in memory. */ LockMemoryOnFault = 4, }; -Y_DECLARE_FLAGS(ELockAllMemoryFlags, ELockAllMemoryFlag) -Y_DECLARE_OPERATORS_FOR_FLAGS(ELockAllMemoryFlags) - -/** - * Performs provided locking operation. - * - * Does nothing on windows. - * - * \param flags Locking operation to perform. - */ -void LockAllMemory(ELockAllMemoryFlags flags); - -/** - * Unlocks whatever was locked with a previous call to `LockAllMemory`. - * - * Does nothing on windows. - */ +Y_DECLARE_FLAGS(ELockAllMemoryFlags, ELockAllMemoryFlag) +Y_DECLARE_OPERATORS_FOR_FLAGS(ELockAllMemoryFlags) + +/** + * Performs provided locking operation. + * + * Does nothing on windows. + * + * \param flags Locking operation to perform. + */ +void LockAllMemory(ELockAllMemoryFlags flags); + +/** + * Unlocks whatever was locked with a previous call to `LockAllMemory`. + * + * Does nothing on windows. + */ void UnlockAllMemory(); diff --git a/util/system/sem.cpp b/util/system/sem.cpp index 4a93b903b5..9a7392b845 100644 --- a/util/system/sem.cpp +++ b/util/system/sem.cpp @@ -239,7 +239,7 @@ bool TSemaphore::TryAcquire() noexcept { return Impl_->TryAcquire(); } -#if defined(_unix_) && !defined(_darwin_) +#if defined(_unix_) && !defined(_darwin_) class TFastSemaphore::TImpl: public TPosixSemaphore { public: inline TImpl(ui32 n) diff --git a/util/system/shellcommand.cpp b/util/system/shellcommand.cpp index b1989b5c8c..1cebdec47b 100644 --- a/util/system/shellcommand.cpp +++ b/util/system/shellcommand.cpp @@ -24,7 +24,7 @@ #include <grp.h> #include <sys/wait.h> -using TPid = pid_t; +using TPid = pid_t; using TWaitResult = pid_t; using TExitStatus = int; #define WAIT_PROCEED 0 @@ -39,7 +39,7 @@ using TGetGroupListGid = gid_t; #include "winint.h" -using TPid = HANDLE; +using TPid = HANDLE; using TWaitResult = DWORD; using TExitStatus = DWORD; #define WAIT_PROCEED WAIT_TIMEOUT @@ -190,7 +190,7 @@ using REALPIPEHANDLE = PIPEHANDLE; class TShellCommand::TImpl : public TAtomicRefCount<TShellCommand::TImpl> { private: - TPid Pid; + TPid Pid; TString Command; TList<TString> Arguments; TString WorkDir; @@ -367,11 +367,11 @@ public: } inline TProcessId GetPid() const { -#if defined(_win_) - return GetProcessId(Pid); -#else +#if defined(_win_) + return GetProcessId(Pid); +#else return Pid; -#endif +#endif } inline TFileHandle& GetInputHandle() { @@ -949,10 +949,10 @@ void TShellCommand::TImpl::Communicate(TProcessInfo* pi) { } /// @todo factor out (poll + wfmo) #if defined(_unix_) - bool haveIn = false; - bool haveOut = false; - bool haveErr = false; - + bool haveIn = false; + bool haveOut = false; + bool haveErr = false; + if (!input && pi->InputFd.IsOpen()) { DBG(Cerr << "closing input stream..." << Endl); pi->InputFd.Close(); diff --git a/util/system/shellcommand.h b/util/system/shellcommand.h index 8730627fe5..5abff75c10 100644 --- a/util/system/shellcommand.h +++ b/util/system/shellcommand.h @@ -408,7 +408,7 @@ public: /** * @brief get id of underlying process - * @note depends on os: pid_t on UNIX, HANDLE on win + * @note depends on os: pid_t on UNIX, HANDLE on win * * @return pid or handle */ diff --git a/util/thread/lfqueue_ut.cpp b/util/thread/lfqueue_ut.cpp index 83bca100cf..3211c6e97a 100644 --- a/util/thread/lfqueue_ut.cpp +++ b/util/thread/lfqueue_ut.cpp @@ -9,48 +9,48 @@ #include "lfqueue.h" -class TMoveTest { -public: - TMoveTest(int marker = 0, int value = 0) - : Marker_(marker) - , Value_(value) - { - } - - TMoveTest(const TMoveTest& other) { - *this = other; - } - - TMoveTest(TMoveTest&& other) { - *this = std::move(other); - } - - TMoveTest& operator=(const TMoveTest& other) { - Value_ = other.Value_; - Marker_ = other.Marker_ + 1024; - return *this; - } - - TMoveTest& operator=(TMoveTest&& other) { - Value_ = other.Value_; - Marker_ = other.Marker_; - other.Marker_ = 0; - return *this; - } - - int Marker() const { - return Marker_; - } - - int Value() const { - return Value_; - } - -private: - int Marker_ = 0; - int Value_ = 0; -}; - +class TMoveTest { +public: + TMoveTest(int marker = 0, int value = 0) + : Marker_(marker) + , Value_(value) + { + } + + TMoveTest(const TMoveTest& other) { + *this = other; + } + + TMoveTest(TMoveTest&& other) { + *this = std::move(other); + } + + TMoveTest& operator=(const TMoveTest& other) { + Value_ = other.Value_; + Marker_ = other.Marker_ + 1024; + return *this; + } + + TMoveTest& operator=(TMoveTest&& other) { + Value_ = other.Value_; + Marker_ = other.Marker_; + other.Marker_ = 0; + return *this; + } + + int Marker() const { + return Marker_; + } + + int Value() const { + return Value_; + } + +private: + int Marker_ = 0; + int Value_ = 0; +}; + class TOperationsChecker { public: TOperationsChecker() { @@ -104,23 +104,23 @@ int TOperationsChecker::CopyAssign_ = 0; Y_UNIT_TEST_SUITE(TLockFreeQueueTests) { Y_UNIT_TEST(TestMoveEnqueue) { - TMoveTest value(0xFF, 0xAA); - TMoveTest tmp; - - TLockFreeQueue<TMoveTest> queue; - - queue.Enqueue(value); - UNIT_ASSERT_VALUES_EQUAL(value.Marker(), 0xFF); - UNIT_ASSERT(queue.Dequeue(&tmp)); - UNIT_ASSERT_VALUES_UNEQUAL(tmp.Marker(), 0xFF); - UNIT_ASSERT_VALUES_EQUAL(tmp.Value(), 0xAA); - - queue.Enqueue(std::move(value)); - UNIT_ASSERT_VALUES_EQUAL(value.Marker(), 0); - UNIT_ASSERT(queue.Dequeue(&tmp)); - UNIT_ASSERT_VALUES_EQUAL(tmp.Value(), 0xAA); - } - + TMoveTest value(0xFF, 0xAA); + TMoveTest tmp; + + TLockFreeQueue<TMoveTest> queue; + + queue.Enqueue(value); + UNIT_ASSERT_VALUES_EQUAL(value.Marker(), 0xFF); + UNIT_ASSERT(queue.Dequeue(&tmp)); + UNIT_ASSERT_VALUES_UNEQUAL(tmp.Marker(), 0xFF); + UNIT_ASSERT_VALUES_EQUAL(tmp.Value(), 0xAA); + + queue.Enqueue(std::move(value)); + UNIT_ASSERT_VALUES_EQUAL(value.Marker(), 0); + UNIT_ASSERT(queue.Dequeue(&tmp)); + UNIT_ASSERT_VALUES_EQUAL(tmp.Value(), 0xAA); + } + Y_UNIT_TEST(TestSimpleEnqueueDequeue) { TLockFreeQueue<int> queue; @@ -146,11 +146,11 @@ Y_UNIT_TEST_SUITE(TLockFreeQueueTests) { UNIT_ASSERT_VALUES_EQUAL(13, i); UNIT_ASSERT(!queue.Dequeue(&i)); - - const int tmp = 100; - queue.Enqueue(tmp); - UNIT_ASSERT(queue.Dequeue(&i)); - UNIT_ASSERT_VALUES_EQUAL(i, tmp); + + const int tmp = 100; + queue.Enqueue(tmp); + UNIT_ASSERT(queue.Dequeue(&i)); + UNIT_ASSERT_VALUES_EQUAL(i, tmp); } Y_UNIT_TEST(TestSimpleEnqueueAllDequeue) { diff --git a/util/ya.make b/util/ya.make index 6ebe7e40cf..81145330fc 100644 --- a/util/ya.make +++ b/util/ya.make @@ -33,7 +33,7 @@ SRCS( datetime/parser.rl6 digest/city.cpp random/random.cpp - string/cast.cpp + string/cast.cpp ) IF (OS_WINDOWS) @@ -95,7 +95,7 @@ JOIN_SRCS( generic/deque.cpp generic/explicit_type.cpp generic/fastqueue.cpp - generic/flags.cpp + generic/flags.cpp generic/function.cpp generic/fwd.cpp generic/guid.cpp @@ -114,7 +114,7 @@ JOIN_SRCS( generic/maybe.cpp generic/mem_copy.cpp generic/noncopyable.cpp - generic/object_counter.cpp + generic/object_counter.cpp generic/overloaded.cpp generic/ptr.cpp generic/queue.cpp diff --git a/util/ysaveload.cpp b/util/ysaveload.cpp index 1ae3acb01e..77ac669f0c 100644 --- a/util/ysaveload.cpp +++ b/util/ysaveload.cpp @@ -1,17 +1,17 @@ #include "ysaveload.h" - -#include <util/generic/buffer.h> - + +#include <util/generic/buffer.h> + void TSerializer<TBuffer>::Save(IOutputStream* rh, const TBuffer& buf) { - ::SaveSize(rh, buf.Size()); - ::SavePodArray(rh, buf.Data(), buf.Size()); -} - + ::SaveSize(rh, buf.Size()); + ::SavePodArray(rh, buf.Data(), buf.Size()); +} + void TSerializer<TBuffer>::Load(IInputStream* rh, TBuffer& buf) { - const size_t s = ::LoadSize(rh); - buf.Resize(s); - ::LoadPodArray(rh, buf.Data(), buf.Size()); -} + const size_t s = ::LoadSize(rh); + buf.Resize(s); + ::LoadPodArray(rh, buf.Data(), buf.Size()); +} [[noreturn]] void NPrivate::ThrowLoadEOFException(size_t typeSize, size_t realSize, TStringBuf structName) { ythrow TLoadEOF() << "can not load " << structName << "(" << typeSize << ", " << realSize << " bytes)"; diff --git a/util/ysaveload.h b/util/ysaveload.h index 02efb4049b..d7fe51d88a 100644 --- a/util/ysaveload.h +++ b/util/ysaveload.h @@ -304,12 +304,12 @@ template <> class TSerializer<const char*> { public: static inline void Save(IOutputStream* rh, const char* s) { - size_t length = strlen(s); - ::SaveSize(rh, length); - ::SavePodArray(rh, s, length); + size_t length = strlen(s); + ::SaveSize(rh, length); + ::SavePodArray(rh, s, length); } - template <class Char, class TStorage> + template <class Char, class TStorage> static inline void Load(IInputStream* rh, Char*& s, TStorage& pool) { const size_t len = LoadSize(rh); @@ -326,7 +326,7 @@ class TVectorSerializer { public: static inline void Save(IOutputStream* rh, const TVec& v) { - ::SaveSize(rh, v.size()); + ::SaveSize(rh, v.size()); ::SaveRange(rh, v.begin(), v.end()); } @@ -350,18 +350,18 @@ template <class T, class A> class TSerializer<TVector<T, A>>: public TVectorSerializer<TVector<T, A>> { }; -template <class T, class A> +template <class T, class A> class TSerializer<std::vector<T, A>>: public TVectorSerializer<std::vector<T, A>> { -}; - -template <class T, class A> +}; + +template <class T, class A> class TSerializer<TList<T, A>>: public TVectorSerializer<TList<T, A>> { -}; - -template <class T, class A> +}; + +template <class T, class A> class TSerializer<std::list<T, A>>: public TVectorSerializer<std::list<T, A>> { -}; - +}; + template <> class TSerializer<TString>: public TVectorSerializer<TString> { }; @@ -378,10 +378,10 @@ template <class T, class A> class TSerializer<TDeque<T, A>>: public TVectorSerializer<TDeque<T, A>> { }; -template <class T, class A> +template <class T, class A> class TSerializer<std::deque<T, A>>: public TVectorSerializer<std::deque<T, A>> { -}; - +}; + template <class TArray> class TStdArraySerializer { public: @@ -585,10 +585,10 @@ template <class T1, class T2, class T3, class T4> class TSerializer<TMap<T1, T2, T3, T4>>: public TMapSerializer<TMap<T1, T2, T3, T4>, true> { }; -template <class K, class T, class C, class A> +template <class K, class T, class C, class A> class TSerializer<std::map<K, T, C, A>>: public TMapSerializer<std::map<K, T, C, A>, true> { -}; - +}; + template <class T1, class T2, class T3, class T4> class TSerializer<TMultiMap<T1, T2, T3, T4>>: public TMapSerializer<TMultiMap<T1, T2, T3, T4>, true> { }; @@ -609,10 +609,10 @@ template <class K, class C, class A> class TSerializer<TSet<K, C, A>>: public TSetSerializer<TSet<K, C, A>, true> { }; -template <class K, class C, class A> +template <class K, class C, class A> class TSerializer<std::set<K, C, A>>: public TSetSerializer<std::set<K, C, A>, true> { -}; - +}; + template <class T1, class T2, class T3, class T4> class TSerializer<THashSet<T1, T2, T3, T4>>: public TSetSerializer<THashSet<T1, T2, T3, T4>, false> { }; diff --git a/util/ysaveload_ut.cpp b/util/ysaveload_ut.cpp index 723c68f391..5166fc97c5 100644 --- a/util/ysaveload_ut.cpp +++ b/util/ysaveload_ut.cpp @@ -5,7 +5,7 @@ #include <util/memory/pool.h> #include <util/stream/buffer.h> #include <util/memory/blob.h> -#include <util/generic/list.h> +#include <util/generic/list.h> #include <util/generic/map.h> #include <util/generic/set.h> #include <util/generic/hash.h> @@ -26,7 +26,7 @@ class TSaveLoadTest: public TTestBase { UNIT_TEST(TestSaveLoad) UNIT_TEST(TestNewStyle) UNIT_TEST(TestNewNewStyle) - UNIT_TEST(TestList) + UNIT_TEST(TestList) UNIT_TEST(TestTuple) UNIT_TEST(TestVariant) UNIT_TEST(TestInheritNonVirtualClass) @@ -375,21 +375,21 @@ private: UNIT_ASSERT_EQUAL((++twoIter)->second, 22); } } - - void TestList() { - TBufferStream s; - + + void TestList() { + TBufferStream s; + TList<int> list = {0, 1, 10}; - Save(&s, list); - - list.clear(); - Load(&s, list); - - UNIT_ASSERT_VALUES_EQUAL(list.size(), 3); - UNIT_ASSERT_VALUES_EQUAL(*std::next(list.begin(), 0), 0); - UNIT_ASSERT_VALUES_EQUAL(*std::next(list.begin(), 1), 1); - UNIT_ASSERT_VALUES_EQUAL(*std::next(list.begin(), 2), 10); - } + Save(&s, list); + + list.clear(); + Load(&s, list); + + UNIT_ASSERT_VALUES_EQUAL(list.size(), 3); + UNIT_ASSERT_VALUES_EQUAL(*std::next(list.begin(), 0), 0); + UNIT_ASSERT_VALUES_EQUAL(*std::next(list.begin(), 1), 1); + UNIT_ASSERT_VALUES_EQUAL(*std::next(list.begin(), 2), 10); + } void TestTuple() { TBufferStream s; |