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

#include "zcurve.h"
#include <vespa/vespalib/geo/zcurve.h>

using vespalib::Serializer;
using vespalib::Deserializer;

namespace search::expression {

IMPLEMENT_EXPRESSIONNODE(ZCurveFunctionNode, UnaryFunctionNode);

ZCurveFunctionNode::~ZCurveFunctionNode() {}

ZCurveFunctionNode::ZCurveFunctionNode(const ZCurveFunctionNode & rhs) :
    UnaryFunctionNode(rhs),
    _dim(rhs._dim),
    _handler()
{
}

ZCurveFunctionNode & ZCurveFunctionNode::operator = (const ZCurveFunctionNode & rhs)
{
    if (this != &rhs) {
        UnaryFunctionNode::operator =(rhs);
        _dim = rhs._dim;
        _handler.reset();
    }
    return *this;
}

void ZCurveFunctionNode::onPrepareResult()
{
    if (getArg().getResult()->inherits(ResultNodeVector::classId)) {
        setResultType(std::make_unique<IntegerResultNodeVector>());
        _handler = std::make_unique<MultiValueHandler>(*this);
    } else {
        setResultType(std::make_unique<Int64ResultNode>());
        _handler = std::make_unique<SingleValueHandler>(*this);
    }
}

int32_t ZCurveFunctionNode::Handler::getXorY(uint64_t z) const
{
    int32_t x, y;
    vespalib::geo::ZCurve::decode(z, &x, &y);
    return (_dim==X) ? x : y;
}

bool ZCurveFunctionNode::onExecute() const
{
    getArg().execute();
    _handler->handle(*getArg().getResult());
    return true;
}

void ZCurveFunctionNode::SingleValueHandler::handle(const ResultNode & arg)
{
    handleOne(arg, _result);
}

void ZCurveFunctionNode::MultiValueHandler::handle(const ResultNode & arg)
{
    const ResultNodeVector & v(static_cast<const ResultNodeVector &>(arg));
   _result.getVector().resize(v.size());
    for(size_t i(0), m(_result.getVector().size()); i < m; i++) {
        handleOne(v.get(i), _result.getVector()[i]);
    }
}

Serializer & ZCurveFunctionNode::onSerialize(Serializer & os) const
{
    UnaryFunctionNode::onSerialize(os);
    uint8_t code(_dim);
    return os << code;
}

Deserializer & ZCurveFunctionNode::onDeserialize(Deserializer & is)
{
    UnaryFunctionNode::onDeserialize(is);
    uint8_t code(0);
    is >> code;
    _dim = static_cast<Dimension>(code);
    return is;
}

}

// this function was added by ../../forcelink.sh
void forcelink_file_searchlib_expression_zcurve() {}