aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/test/weighted_type_test_utils.h
blob: 09b9eecb9646c29a8a074d5e2c0b715980453dd8 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/searchcommon/attribute/iattributevector.h>
#include <type_traits>

template <typename T> struct IsWeightedType : std::false_type {};
template <typename T> struct IsWeightedType<search::attribute::WeightedType<T>> : std::true_type {};

struct value_then_weight_order {
    template <typename T>
    bool operator()(const T& lhs, const T& rhs) const noexcept {
        if (lhs.getValue() != rhs.getValue()) {
            return (lhs.getValue() < rhs.getValue());
        }
        return (lhs.getWeight() < rhs.getWeight());
    }
};

struct order_by_value {
    template <typename T>
    bool operator()(const T& lhs, const T& rhs) const noexcept {
        if constexpr (IsWeightedType<T>::value) {
            return (lhs.getValue() < rhs.getValue());
        } else {
            return (lhs < rhs);
        }
    }
};


struct order_by_weight {
    template <typename T>
    bool operator()(const search::attribute::WeightedType<T>& lhs,
                    const search::attribute::WeightedType<T>& rhs) const noexcept {
        return (lhs.getWeight() < rhs.getWeight());
    }
};