~marcustomlinson/unity-scopes-shell/lp-1552082

« back to all changes in this revision

Viewing changes to src/Unity/resultsmap.cpp

  • Committer: Pawel Stolowski
  • Date: 2015-11-30 09:23:32 UTC
  • mfrom: (278 unity-scopes-shell)
  • mto: This revision was merged to the branch mainline in revision 279.
  • Revision ID: pawel.stolowski@canonical.com-20151130092332-io500es46ww36vfc
Merged trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2015 Canonical, Ltd.
 
3
 *
 
4
 * Authors:
 
5
 *  Pawel Stolowski <pawel.stolowski@canonical.com>
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; version 3.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
#include "resultsmap.h"
 
21
#include <cassert>
 
22
 
 
23
ResultsMap::ResultsMap(QList<std::shared_ptr<unity::scopes::Result>> const &results)
 
24
{
 
25
    rebuild(results);
 
26
}
 
27
 
 
28
ResultsMap::ResultsMap(QList<std::shared_ptr<unity::scopes::CategorisedResult>> const &results)
 
29
{
 
30
    int pos = 0;
 
31
    for (auto result: results) {
 
32
        std::shared_ptr<unity::scopes::Result> res = result;
 
33
        assert(res);
 
34
        const ResultPos rpos { res, pos++ };
 
35
        m_results.insert({result->uri(), rpos });
 
36
    }
 
37
}
 
38
 
 
39
void ResultsMap::rebuild(QList<std::shared_ptr<unity::scopes::Result>> const &results)
 
40
{
 
41
    m_results.clear();
 
42
    int pos = 0;
 
43
    for (auto result: results) {
 
44
        assert(result);
 
45
        const ResultPos rpos { result, pos++ };
 
46
        m_results.insert({result->uri(), rpos });
 
47
    }
 
48
}
 
49
 
 
50
int ResultsMap::find(std::shared_ptr<unity::scopes::Result> const& result) const
 
51
{
 
52
    assert(result);
 
53
    auto it = m_results.find(result->uri());
 
54
    if (it != m_results.end()) {
 
55
        assert(it->second.result);
 
56
        while (it != m_results.end() && it->second.result->uri() == result->uri())
 
57
        {
 
58
            if (*(it->second.result) == *result) {
 
59
                return it->second.index;
 
60
            }
 
61
            ++it;
 
62
        }
 
63
    }
 
64
    return -1;
 
65
}