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

#pragma once

#include "postingdata.h"
#include <vespa/vespalib/btree/minmaxaggregated.h>
#include <algorithm>

namespace search {

/**
 * Inner attribute iterator used for temporary posting lists (range searches).
 */
template <typename P>
class ArrayIterator
{
public:
    ArrayIterator() : _cur(nullptr), _end(nullptr), _begin(nullptr) { }

    const P * operator->() const { return _cur; }

    ArrayIterator & operator++() {
        ++_cur;
        return *this;
    }

    bool valid() const { return _cur != _end; }

    void linearSeek(uint32_t docId) {
        while (_cur != _end && _cur->_key < docId) {
            ++_cur;
        }
    }

    uint32_t getKey() const { return _cur->_key; }
    inline int32_t getData() const { return _cur->getData(); }

    void set(const P *begin, const P *end) {
        _cur = begin;
        _end = end;
        _begin = begin;
    }

    void lower_bound(uint32_t docId) {
        P keyWrap;
        keyWrap._key = docId;
        _cur = std::lower_bound<const P *, P>(_begin, _end, keyWrap);
    }

    void swap(ArrayIterator &rhs) {
        std::swap(_cur, rhs._cur);
        std::swap(_end, rhs._end);
        std::swap(_begin, rhs._begin);
    }
protected:
    const P *_cur;
    const P *_end;
    const P *_begin;
};

template <>
inline int32_t
ArrayIterator<AttributePosting>::getData() const
{
    return 1;   // default weight 1 for single value attributes
}


/**
 * Inner attribute iterator used for short posting lists (8 or less
 * documents).
 */

template <typename P>
class DocIdMinMaxIterator : public ArrayIterator<P>
{
public:
    DocIdMinMaxIterator()
        : ArrayIterator<P>()
    { }
    inline vespalib::btree::MinMaxAggregated getAggregated() const { return vespalib::btree::MinMaxAggregated(1, 1); }
};


template<>
inline vespalib::btree::MinMaxAggregated
DocIdMinMaxIterator<AttributeWeightPosting>::getAggregated() const
{
    vespalib::btree::MinMaxAggregated a;
    for (const AttributeWeightPosting *cur = _cur, *end = _end; cur != end; ++cur) {
        a.add(cur->getData());
    }
    return a;
};

} // namespace search