aboutsummaryrefslogtreecommitdiffstats
path: root/util/digest/sequence.h
blob: 712331007b8218f4d1455b94b1d9dbac3a51fb21 (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
#pragma once

#include "numeric.h"
#include <util/generic/hash.h>
#include <util/generic/array_ref.h>

template <typename ElementHash = void>
class TRangeHash {
private:
    template <typename ElementType>
    using TBase = std::conditional_t<
        !std::is_void<ElementHash>::value,
        ElementHash,
        THash<ElementType>>;

public:
    template <typename Range>
    size_t operator()(const Range& range) const {
        size_t accumulated = 0;
        for (const auto& element : range) {
            accumulated = CombineHashes(accumulated, TBase<typename Range::value_type>()(element));
        }
        return accumulated;
    }
};

using TSimpleRangeHash = TRangeHash<>;

template <typename RegionHash = void>
class TContiguousHash {
private:
    template <typename ElementType>
    using TBase = std::conditional_t<
        !std::is_void<RegionHash>::value,
        RegionHash,
        TRangeHash<ElementType>>;

public:
    template <typename ContainerType>
    auto operator()(const ContainerType& container) const {
        return operator()(MakeArrayRef(container));
    }

    template <typename ElementType>
    auto operator()(const TArrayRef<ElementType>& data) const {
        return TBase<ElementType>()(data);
    }
};