aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/attribute/numeric_sort_blob_writer.cpp
blob: 187424f5bd703d4d47e85f0418a8af45aba6c15b (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "numeric_sort_blob_writer.h"
#include <vespa/vespalib/util/sort.h>
#include <cmath>
#include <type_traits>

namespace search::attribute {

template <typename T, bool asc>
NumericSortBlobWriter<T, asc>::NumericSortBlobWriter() noexcept
: _best()
{
}

template <typename T, bool asc>
NumericSortBlobWriter<T, asc>::~NumericSortBlobWriter() noexcept = default;

template <typename T, bool asc>
void
NumericSortBlobWriter<T, asc>::candidate(T val)
{
    if constexpr (std::disjunction_v<std::is_same<T,float>,std::is_same<T, double>>) {
        if (std::isnan(val)) {
            return;
        }
    }
    if (_best.has_value()) {
        if constexpr (asc) {
            if (_best.value() <= val) {
                return;
            }
        } else {
            if (_best.value() >= val) {
                return;
            }
        }
    }
    _best = val;
}

template <typename T, bool asc>
long
NumericSortBlobWriter<T, asc>::write(void *serTo, size_t available)
{
    auto dst = static_cast<unsigned char*>(serTo);
    if (_best.has_value()) {
        if (available < 1 + sizeof(T)) {
            return -1;
        }
        *dst = has_value;
        auto ret = vespalib::serializeForSort<vespalib::convertForSort<T, asc>>(_best.value(), dst + 1, available - 1);
        return (ret >= 0) ? (ret + 1) : -1;
    } else {
        if (available < 1) {
            return -1;
        }
        *dst = missing_value;
        return 1;
    }
}

template class NumericSortBlobWriter<int8_t, true>;
template class NumericSortBlobWriter<int16_t, true>;
template class NumericSortBlobWriter<int32_t, true>;
template class NumericSortBlobWriter<int64_t, true>;
template class NumericSortBlobWriter<float, true>;
template class NumericSortBlobWriter<double, true>;

template class NumericSortBlobWriter<int8_t, false>;
template class NumericSortBlobWriter<int16_t, false>;
template class NumericSortBlobWriter<int32_t, false>;
template class NumericSortBlobWriter<int64_t, false>;
template class NumericSortBlobWriter<float, false>;
template class NumericSortBlobWriter<double, false>;

}