aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/fef/symmetrictable.h
blob: 2c7f8b6445159cd5cd469d4b5df116e71629e897 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "table.h"
#include <vector>

namespace search::fef {

/**
 * This class represents a rank table with double values. It takes both negative and positive indexes.
 * The content of a table is typically a pre-computed function that is used by a feature executor.
 * Values in the negative index range are negated values of corresponding positive value.
 **/
class SymmetricTable
{
private:
    std::vector<double> _backingTable;
    int                 _size;
    double *            _table;
    double              _max;

public:
    using SP = std::shared_ptr<SymmetricTable>;

    SymmetricTable();
    /**
     * Creates a symmetric table based on the real one.
     **/
    SymmetricTable(const Table & table);
    SymmetricTable(const SymmetricTable & table);
    ~SymmetricTable();

    SymmetricTable & operator =(const SymmetricTable & table);
    void swap(SymmetricTable & rhs) {
        _backingTable.swap(rhs._backingTable);
        std::swap(_size, rhs._size);
        std::swap(_table, rhs._table);
        std::swap(_max, rhs._max);
    }
    /**
     * Returns the element at the given position.
     **/
    double operator[](int i) const { return _table[i]; }

    /**
     * Retrives the element at the given position or the last element if i is outside the range.
     **/
    double get(int i) const {
        return (i<-_size) ? _table[-_size] : ((i>_size) ? _table[_size] : _table[i]);
    };
    double max() const { return _max; }
};

}