aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/queryeval/simpleresult.cpp
blob: 543f05fa87a1d047208fe791eb20d20017636980 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "simpleresult.h"
#include <cassert>
#include <ostream>

namespace search::queryeval {

SimpleResult &
SimpleResult::addHit(uint32_t docid)
{
    _hits.push_back(docid);
    return *this;
}

void
SimpleResult::clear()
{
    std::vector<uint32_t> tmp;
    tmp.swap(_hits);
}

SimpleResult &
SimpleResult::search(SearchIterator &sb)
{
    clear();
    // assume strict toplevel search object located at start
    sb.initFullRange();
    for (sb.seek(1); !sb.isAtEnd(); sb.seek(sb.getDocId() + 1)) {
        sb.unpack(sb.getDocId());
        _hits.push_back(sb.getDocId());
    }
    return *this;
}

SimpleResult &
SimpleResult::searchStrict(SearchIterator &sb, uint32_t docIdLimit)
{
    clear();
    // assume strict toplevel search object located at start
    sb.initRange(1, docIdLimit);
    for (sb.seek(1); !sb.isAtEnd(); sb.seek(sb.getDocId() + 1)) {
        sb.unpack(sb.getDocId());
        _hits.push_back(sb.getDocId());
    }
    return *this;
}

SimpleResult &
SimpleResult::search(SearchIterator &sb, uint32_t docIdLimit)
{
    clear();
    // assume non-strict toplevel search object
    sb.initRange(1, docIdLimit);
    for (uint32_t docId = 1; !sb.isAtEnd(docId); ++docId) {
        if (sb.seek(docId)) {
            assert(docId == sb.getDocId());
            sb.unpack(docId);
            _hits.push_back(docId);
        }
    }
    return *this;
}

bool
SimpleResult::contains(const SimpleResult& subset) const
{
    auto hits_itr = _hits.begin();
    for (uint32_t i = 0; i < subset.getHitCount(); ++i) {
        uint32_t subset_hit = subset.getHit(i);
        while (hits_itr != _hits.end() && *hits_itr < subset_hit) {
            ++hits_itr;
        }
        if (hits_itr == _hits.end() || *hits_itr > subset_hit) {
            return false;
        }
    }
    return true;
}

std::ostream &
operator << (std::ostream &out, const SimpleResult &result)
{
    if (result.getHitCount() == 0) {
        out << std::endl << "empty" << std::endl;
    } else {
        out << std::endl;
        for (uint32_t i = 0; i < result.getHitCount(); ++i) {
            out << "{" << result.getHit(i) << "}" << std::endl;
        }
    }
    return out;
}

}