repeated_field_utils.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #pragma once
  2. #include <google/protobuf/repeated_field.h>
  3. #include <util/generic/vector.h>
  4. template <typename T>
  5. void RemoveRepeatedPtrFieldElement(google::protobuf::RepeatedPtrField<T>* repeated, unsigned index) {
  6. google::protobuf::RepeatedPtrField<T> r;
  7. Y_ASSERT(index < (unsigned)repeated->size());
  8. for (unsigned i = 0; i < (unsigned)repeated->size(); ++i) {
  9. if (i == index) {
  10. continue;
  11. }
  12. r.Add()->Swap(repeated->Mutable(i));
  13. }
  14. r.Swap(repeated);
  15. }
  16. namespace NProtoBuf {
  17. /// Move item to specified position
  18. template <typename TRepeated>
  19. static void MoveRepeatedFieldItem(TRepeated* field, size_t indexFrom, size_t indexTo) {
  20. if (!field->size() || indexFrom >= static_cast<size_t>(field->size()) || indexFrom == indexTo)
  21. return;
  22. if (indexTo >= static_cast<size_t>(field->size()))
  23. indexTo = field->size() - 1;
  24. if (indexFrom > indexTo) {
  25. for (size_t i = indexFrom; i > indexTo; --i)
  26. field->SwapElements(i, i - 1);
  27. } else {
  28. for (size_t i = indexFrom; i < indexTo; ++i)
  29. field->SwapElements(i, i + 1);
  30. }
  31. }
  32. template <typename T>
  33. static T* InsertRepeatedFieldItem(NProtoBuf::RepeatedPtrField<T>* field, size_t index) {
  34. T* ret = field->Add();
  35. MoveRepeatedFieldItem(field, field->size() - 1, index);
  36. return ret;
  37. }
  38. template <typename TRepeated> // suitable both for RepeatedField and RepeatedPtrField
  39. static void RemoveRepeatedFieldItem(TRepeated* field, size_t index) {
  40. if ((int)index >= field->size())
  41. return;
  42. for (int i = index + 1; i < field->size(); ++i)
  43. field->SwapElements(i - 1, i);
  44. field->RemoveLast();
  45. }
  46. template <typename TRepeated, typename TPred> // suitable both for RepeatedField and RepeatedPtrField
  47. static void RemoveRepeatedFieldItemIf(TRepeated* repeated, TPred p) {
  48. auto last = std::remove_if(repeated->begin(), repeated->end(), p);
  49. if (last != repeated->end()) {
  50. size_t countToRemove = repeated->end() - last;
  51. while (countToRemove--)
  52. repeated->RemoveLast();
  53. }
  54. }
  55. namespace NImpl {
  56. template <typename TRepeated>
  57. static void ShiftLeft(TRepeated* field, int begIndex, int endIndex, size_t shiftSize) {
  58. Y_ASSERT(begIndex <= field->size());
  59. Y_ASSERT(endIndex <= field->size());
  60. size_t shiftIndex = (int)shiftSize < begIndex ? begIndex - shiftSize : 0;
  61. for (int i = begIndex; i < endIndex; ++i, ++shiftIndex)
  62. field->SwapElements(shiftIndex, i);
  63. }
  64. }
  65. // Remove several items at once, could be more efficient compared to calling RemoveRepeatedFieldItem several times
  66. template <typename TRepeated>
  67. static void RemoveRepeatedFieldItems(TRepeated* field, const TVector<size_t>& sortedIndices) {
  68. if (sortedIndices.empty())
  69. return;
  70. size_t shift = 1;
  71. for (size_t i = 1; i < sortedIndices.size(); ++i, ++shift)
  72. NImpl::ShiftLeft(field, sortedIndices[i - 1] + 1, sortedIndices[i], shift);
  73. NImpl::ShiftLeft(field, sortedIndices.back() + 1, field->size(), shift);
  74. for (; shift > 0; --shift)
  75. field->RemoveLast();
  76. }
  77. template <typename TRepeated>
  78. static void ReverseRepeatedFieldItems(TRepeated* field) {
  79. for (int i1 = 0, i2 = field->size() - 1; i1 < i2; ++i1, --i2)
  80. field->SwapElements(i1, i2);
  81. }
  82. }