aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/erasure/reed_solomon.h
blob: a2e268e81d48dcb1719d4ffa7f0d335791443c44 (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
#pragma once

#include "helpers.h"

#include <algorithm>
#include <optional>

namespace NErasure {

template <int DataPartCount, int ParityPartCount, int WordSize, class TCodecTraits>
class TReedSolomonBase
    : public ICodec<typename TCodecTraits::TBlobType>
{
public:
    static constexpr ui64 RequiredDataAlignment = alignof(ui64);

    bool CanRepair(const TPartIndexList& erasedIndices) const final {
        return erasedIndices.size() <= ParityPartCount;
    }

    bool CanRepair(const TPartIndexSet& erasedIndices) const final {
        return erasedIndices.count() <= static_cast<size_t>(ParityPartCount);
    }

    std::optional<TPartIndexList> GetRepairIndices(const TPartIndexList& erasedIndices) const final {
        if (erasedIndices.empty()) {
            return TPartIndexList();
        }

        TPartIndexList indices = erasedIndices;
        std::sort(indices.begin(), indices.end());
        indices.erase(std::unique(indices.begin(), indices.end()), indices.end());

        if (indices.size() > static_cast<size_t>(ParityPartCount)) {
            return std::nullopt;
        }

        return Difference(0, DataPartCount + ParityPartCount, indices);
    }

    int GetDataPartCount() const final {
        return DataPartCount;
    }

    int GetParityPartCount() const final {
        return ParityPartCount;
    }

    int GetGuaranteedRepairablePartCount() const final {
        return ParityPartCount;
    }

    int GetWordSize() const final {
        return WordSize * sizeof(long);
    }

    virtual ~TReedSolomonBase() = default;
};

} // namespace NErasure