aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/attribute/floatbase.cpp
blob: efd94283e8d1b4c6d98a89323d1730f6ec5681b3 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "floatbase.hpp"
#include "attributevector.hpp"
#include <vespa/document/fieldvalue/fieldvalue.h>

namespace search {

FloatingPointAttribute::FloatingPointAttribute(const vespalib::string & name, const Config & c) :
    NumericAttribute(name, c),
    _changes()
{
}

FloatingPointAttribute::~FloatingPointAttribute() = default;

uint32_t FloatingPointAttribute::clearDoc(DocId doc)
{
    uint32_t removed(0);
    if (hasMultiValue() && (doc < getNumDocs())) {
        removed = getValueCount(doc);
    }
    AttributeVector::clearDoc(_changes, doc);

    return removed;
}

uint32_t FloatingPointAttribute::get(DocId doc, WeightedString * s, uint32_t sz) const
{
    WeightedFloat * v = new WeightedFloat[sz];
    unsigned num(static_cast<const AttributeVector *>(this)->get(doc, v, sz));
    for(unsigned i(0); i < num; i++) {
        char tmp[32];
        snprintf(tmp, sizeof(tmp), "%g", v[i].getValue());
        s[i] = WeightedString(tmp, v[i].getWeight());
    }
    delete [] v;
    return num;
}

uint32_t FloatingPointAttribute::get(DocId doc, WeightedConstChar * v, uint32_t sz) const
{
    (void) doc;
    (void) v;
    (void) sz;
    return 0;
}

uint32_t FloatingPointAttribute::get(DocId doc, vespalib::string * s, uint32_t sz) const
{
    double * v = new double[sz];
    unsigned num(static_cast<const AttributeVector *>(this)->get(doc, v, sz));
    for(unsigned i(0); i < num; i++) {
        char tmp[32];
        snprintf(tmp, sizeof(tmp), "%g", v[i]);
        s[i] = tmp;
    }
    delete [] v;
    return num;
}

uint32_t FloatingPointAttribute::get(DocId doc, const char ** v, uint32_t sz) const
{
    (void) doc;
    (void) v;
    (void) sz;
    return 0;
}

bool FloatingPointAttribute::applyWeight(DocId doc, const FieldValue & fv, const ArithmeticValueUpdate & wAdjust)
{
    double v = fv.getAsDouble();
    return AttributeVector::adjustWeight(_changes, doc, NumericChangeData<double>(v), wAdjust);
}

bool FloatingPointAttribute::applyWeight(DocId doc, const FieldValue& fv, const document::AssignValueUpdate& wAdjust)
{
    double v = fv.getAsDouble();
    return AttributeVector::adjustWeight(_changes, doc, NumericChangeData<double>(v), wAdjust);
}

bool FloatingPointAttribute::apply(DocId doc, const ArithmeticValueUpdate & op)
{
    bool retval(doc < getNumDocs());
    if (retval) {
        retval = AttributeVector::applyArithmetic(_changes, doc, NumericChangeData<double>(0), op);
    }
    return retval;
}

vespalib::ConstArrayRef<char>
FloatingPointAttribute::get_raw(DocId) const
{
    return {};
}

vespalib::MemoryUsage
FloatingPointAttribute::getChangeVectorMemoryUsage() const
{
    return _changes.getMemoryUsage();
}

template class FloatingPointAttributeTemplate<float>;
template class FloatingPointAttributeTemplate<double>;

template bool AttributeVector::clearDoc(FloatingPointAttribute::ChangeVector& changes, DocId doc);
template bool AttributeVector::update(FloatingPointAttribute::ChangeVector& changes, DocId doc, const NumericChangeData<double>& v);
template bool AttributeVector::append(FloatingPointAttribute::ChangeVector& changes, DocId doc, const NumericChangeData<double>& v, int32_t w, bool doCount);
template bool AttributeVector::remove(FloatingPointAttribute::ChangeVector& changes, DocId doc, const NumericChangeData<double>& v, int32_t w);

}