shared_range.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "shared_range.h"
  2. #include "new.h"
  3. namespace NYT {
  4. ////////////////////////////////////////////////////////////////////////////////
  5. TSharedRangeHolderPtr TSharedRangeHolder::Clone(const TSharedRangeHolderCloneOptions& /*options*/)
  6. {
  7. return this;
  8. }
  9. std::optional<size_t> TSharedRangeHolder::GetTotalByteSize() const
  10. {
  11. return std::nullopt;
  12. }
  13. ////////////////////////////////////////////////////////////////////////////////
  14. TSharedRangeHolderPtr MakeCompositeSharedRangeHolder(std::vector<TSharedRangeHolderPtr> holders)
  15. {
  16. struct THolder
  17. : public TSharedRangeHolder
  18. {
  19. std::vector<TSharedRangeHolderPtr> Subholders;
  20. TSharedRangeHolderPtr Clone(const TSharedRangeHolderCloneOptions& options) override
  21. {
  22. auto newHolder = New<THolder>();
  23. newHolder->Subholders.reserve(Subholders.size());
  24. for (const auto& subholder : Subholders) {
  25. if (!subholder) {
  26. continue;
  27. }
  28. if (auto clonedSubholder = subholder->Clone(options)) {
  29. newHolder->Subholders.push_back(clonedSubholder);
  30. }
  31. }
  32. return newHolder;
  33. }
  34. std::optional<size_t> GetTotalByteSize() const override
  35. {
  36. size_t result = 0;
  37. for (const auto& subholder : Subholders) {
  38. if (!subholder) {
  39. continue;
  40. }
  41. auto subsize = subholder->GetTotalByteSize();
  42. if (!subsize) {
  43. return std::nullopt;
  44. }
  45. result += *subsize;
  46. }
  47. return result;
  48. }
  49. };
  50. auto holder = New<THolder>();
  51. holder->Subholders = std::move(holders);
  52. return holder;
  53. }
  54. ////////////////////////////////////////////////////////////////////////////////
  55. } // namespace NYT