aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/memory/shared_range.cpp
blob: 4d4bd40d18044268c8a62ab828d11bdace981b09 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "shared_range.h"
#include "new.h"

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

TSharedRangeHolderPtr TSharedRangeHolder::Clone(const TSharedRangeHolderCloneOptions& /*options*/)
{
    return this;
}

std::optional<size_t> TSharedRangeHolder::GetTotalByteSize() const
{
    return std::nullopt;
}

////////////////////////////////////////////////////////////////////////////////

TSharedRangeHolderPtr MakeCompositeSharedRangeHolder(std::vector<TSharedRangeHolderPtr> holders)
{
    struct THolder
        : public TSharedRangeHolder
    {
        std::vector<TSharedRangeHolderPtr> Subholders;

        TSharedRangeHolderPtr Clone(const TSharedRangeHolderCloneOptions& options) override
        {
            auto newHolder = New<THolder>();
            newHolder->Subholders.reserve(Subholders.size());
            for (const auto& subholder : Subholders) {
                if (!subholder) {
                    continue;
                }
                if (auto clonedSubholder = subholder->Clone(options)) {
                    newHolder->Subholders.push_back(clonedSubholder);
                }
            }
            return newHolder;
        }

        std::optional<size_t> GetTotalByteSize() const override
        {
            size_t result = 0;
            for (const auto& subholder : Subholders) {
                if (!subholder) {
                    continue;
                }
                auto subsize = subholder->GetTotalByteSize();
                if (!subsize) {
                    return std::nullopt;
                }
                result += *subsize;
            }
            return result;
        }
    };

    auto holder = New<THolder>();
    holder->Subholders = std::move(holders);
    return holder;
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT