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

#include "unpackinfo.h"
#include <vespa/vespalib/stllike/asciistream.h>
#include <cassert>
#include <algorithm>

namespace search::queryeval {

UnpackInfo::UnpackInfo()
    : _size(0)
{
    memset(_unpack, 0, sizeof(_unpack));
}

UnpackInfo &
UnpackInfo::add(size_t index)
{
    if ((index <= max_index) && (_size < max_size)) {
        _unpack[_size++] = index;
        std::sort(&_unpack[0], &_unpack[_size]);
    } else {
        forceAll();
    }
    return *this;
}

UnpackInfo &
UnpackInfo::insert(size_t index, bool unpack)
{
    if (unpackAll()) {
        return *this;
    }
    for (size_t rp = 0; rp < _size; ++rp) {
        if (_unpack[rp] >= index) {
            if (_unpack[rp] == max_index) {
                forceAll();
                return *this;
            }
            ++_unpack[rp];
        }
    }
    if (unpack) {
        add(index);
    }
    return *this;
}

UnpackInfo &
UnpackInfo::remove(size_t index)
{
    if (unpackAll()) {
        return *this;
    }
    size_t wp = 0;
    bool found_index = false;
    for (size_t rp = 0; rp < _size; ++rp) {
        if (_unpack[rp] == index) {
            found_index = true;
        } else if (_unpack[rp] > index) {
            _unpack[wp++] = (_unpack[rp] - 1);
        } else {
            _unpack[wp++] = _unpack[rp];
        }
    }
    if (found_index) {
        --_size;
    }
    assert(wp == _size);
    return *this;
}

bool
UnpackInfo::needUnpack(size_t index) const
{
    if (unpackAll()) {
        return true;
    }
    for (size_t i = 0; i < _size; ++i) {
        if (_unpack[i] == index) {
            return true;
        }
    }
    return false;
}

vespalib::string 
UnpackInfo::toString() const
{
    vespalib::asciistream os;
    if (unpackAll()) {
        os << "full-unpack";
    } else if (empty()) {
        os << "no-unpack";
    } else {
        os << size_t(_unpack[0]);
        for (size_t i = 1; i < _size; ++i) {
            os << " " << size_t(_unpack[i]);
        }
    }
    return os.str();
}

}