aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/erasure/helpers.cpp
blob: 74edeca52c02a13090d6ee98154aefefc0c9be02 (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
#include "helpers.h"

#include <algorithm>
#include <iterator>

namespace NErasure {

TPartIndexList MakeSegment(int begin, int end) {
    TPartIndexList result(end - begin);
    for (int i = begin; i < end; ++i) {
        result[i - begin] = i;
    }
    return result;
}

TPartIndexList MakeSingleton(int elem) {
    TPartIndexList result;
    result.push_back(elem);
    return result;
}

TPartIndexList Difference(int begin, int end, const TPartIndexList& subtrahend) {
    size_t pos = 0;
    TPartIndexList result;
    for (int i = begin; i < end; ++i) {
        while (pos < subtrahend.size() && subtrahend[pos] < i) {
            pos += 1;
        }
        if (pos == subtrahend.size() || subtrahend[pos] != i) {
            result.push_back(i);
        }
    }
    return result;
}

TPartIndexList Difference(const TPartIndexList& first, const TPartIndexList& second) {
    TPartIndexList result;
    std::set_difference(first.begin(), first.end(), second.begin(), second.end(), std::back_inserter(result));
    return result;
}

TPartIndexList Difference(const TPartIndexList& set, int subtrahend) {
    return Difference(set, MakeSingleton(subtrahend));
}

TPartIndexList Intersection(const TPartIndexList& first, const TPartIndexList& second) {
    TPartIndexList result;
    std::set_intersection(first.begin(), first.end(), second.begin(), second.end(), std::back_inserter(result));
    return result;
}

TPartIndexList Union(const TPartIndexList& first, const TPartIndexList& second) {
    TPartIndexList result;
    std::set_union(first.begin(), first.end(), second.begin(), second.end(), std::back_inserter(result));
    return result;
}

bool Contains(const TPartIndexList& set, int elem) {
    return std::binary_search(set.begin(), set.end(), elem);
}

TPartIndexList UniqueSortedIndices(const TPartIndexList& indices) {
    TPartIndexList copy = indices;
    std::sort(copy.begin(), copy.end());
    copy.erase(std::unique(copy.begin(), copy.end()), copy.end());
    return copy;
}

TPartIndexList ExtractRows(const TPartIndexList& matrix, int width, const TPartIndexList& rows) {
    Y_ASSERT(matrix.size() % width == 0);
    TPartIndexList result(width * rows.size());
    for (size_t i = 0; i < rows.size(); ++i) {
        auto start = matrix.begin() + rows[i] * width;
        std::copy(start, start + width, result.begin() + i * width);
    }
    return result;
}

} // namespace NErasure