aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/protobuf/util/repeated_field_utils.h
blob: 9d53a797d8ed1c1d2c3fa5552de689e8155a1ade (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#pragma once 
 
#include <google/protobuf/repeated_field.h>
#include <util/generic/vector.h>
 
template <typename T> 
void RemoveRepeatedPtrFieldElement(google::protobuf::RepeatedPtrField<T>* repeated, unsigned index) { 
    google::protobuf::RepeatedPtrField<T> r; 
    Y_ASSERT(index < (unsigned)repeated->size());
    for (unsigned i = 0; i < (unsigned)repeated->size(); ++i) {
        if (i == index) { 
            continue; 
        } 
        r.Add()->Swap(repeated->Mutable(i)); 
    } 
    r.Swap(repeated); 
} 

namespace NProtoBuf {
    /// Move item to specified position
    template <typename TRepeated>
    static void MoveRepeatedFieldItem(TRepeated* field, size_t indexFrom, size_t indexTo) {
        if (!field->size() || indexFrom >= static_cast<size_t>(field->size()) || indexFrom == indexTo)
            return;
        if (indexTo >= static_cast<size_t>(field->size()))
            indexTo = field->size() - 1;
        if (indexFrom > indexTo) {
            for (size_t i = indexFrom; i > indexTo; --i)
                field->SwapElements(i, i - 1);
        } else {
            for (size_t i = indexFrom; i < indexTo; ++i)
                field->SwapElements(i, i + 1);
        }
    }

    template <typename T>
    static T* InsertRepeatedFieldItem(NProtoBuf::RepeatedPtrField<T>* field, size_t index) {
        T* ret = field->Add();
        MoveRepeatedFieldItem(field, field->size() - 1, index);
        return ret;
    }

    template <typename TRepeated> // suitable both for RepeatedField and RepeatedPtrField
    static void RemoveRepeatedFieldItem(TRepeated* field, size_t index) {
        if ((int)index >= field->size())
            return;

        for (int i = index + 1; i < field->size(); ++i)
            field->SwapElements(i - 1, i);

        field->RemoveLast();
    }

    template <typename TRepeated, typename TPred> // suitable both for RepeatedField and RepeatedPtrField
    static void RemoveRepeatedFieldItemIf(TRepeated* repeated, TPred p) {
        auto last = std::remove_if(repeated->begin(), repeated->end(), p);
        if (last != repeated->end()) {
            size_t countToRemove = repeated->end() - last;
            while (countToRemove--)
                repeated->RemoveLast();
        }
    }

    namespace NImpl {
        template <typename TRepeated>
        static void ShiftLeft(TRepeated* field, int begIndex, int endIndex, size_t shiftSize) {
            Y_ASSERT(begIndex <= field->size());
            Y_ASSERT(endIndex <= field->size());
            size_t shiftIndex = (int)shiftSize < begIndex ? begIndex - shiftSize : 0;
            for (int i = begIndex; i < endIndex; ++i, ++shiftIndex)
                field->SwapElements(shiftIndex, i);
        }
    }

    // Remove several items at once, could be more efficient compared to calling RemoveRepeatedFieldItem several times
    template <typename TRepeated>
    static void RemoveRepeatedFieldItems(TRepeated* field, const TVector<size_t>& sortedIndices) {
        if (sortedIndices.empty())
            return;

        size_t shift = 1;
        for (size_t i = 1; i < sortedIndices.size(); ++i, ++shift)
            NImpl::ShiftLeft(field, sortedIndices[i - 1] + 1, sortedIndices[i], shift);
        NImpl::ShiftLeft(field, sortedIndices.back() + 1, field->size(), shift);

        for (; shift > 0; --shift)
            field->RemoveLast();
    }

    template <typename TRepeated>
    static void ReverseRepeatedFieldItems(TRepeated* field) {
        for (int i1 = 0, i2 = field->size() - 1; i1 < i2; ++i1, --i2)
            field->SwapElements(i1, i2);
    }

}