~shnatsel/slingshot/update-cmake-modules

« back to all changes in this revision

Viewing changes to lib/synapse-core/result-set.vala

  • Committer: RabbitBot
  • Author(s): Tom Beckmann
  • Date: 2014-06-28 05:00:49 UTC
  • mfrom: (421.1.19 slingshot)
  • Revision ID: rabbitbot-20140628050049-pi540b7grh00lzcv
Copy the sources of Synapse over and implements a new searchview using libsynapse

Copying is currently necessary as synapse's library is internal. I hope we can change this soon, although I don't consider it very urgent, as synapse is almost unmaintained at the moment, so we most likely won't miss out on important updates.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2010 Michal Hruby <michal.mhr@gmail.com>
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Lesser General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2.1 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General Public License
 
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 *
 
17
 * Authored by Michal Hruby <michal.mhr@gmail.com>
 
18
 *
 
19
 */
 
20
 
 
21
namespace Synapse
 
22
{
 
23
  public class ResultSet : Object, Gee.Traversable<Match>, Gee.Iterable <Gee.Map.Entry <Match, int>>
 
24
  {
 
25
    protected Gee.Map<Match, int> matches;
 
26
    protected Gee.Set<unowned string> uris;
 
27
 
 
28
    public ResultSet ()
 
29
    {
 
30
      Object ();
 
31
    }
 
32
 
 
33
    construct
 
34
    {
 
35
      matches = new Gee.HashMap<Match, int> ();
 
36
      // Match.uri is not owned, so we can optimize here
 
37
      uris = new Gee.HashSet<unowned string> ();
 
38
    }
 
39
 
 
40
    public Type element_type
 
41
    {
 
42
      get { return matches.element_type; }
 
43
    }
 
44
 
 
45
    public int size
 
46
    {
 
47
      get { return matches.size; }
 
48
    }
 
49
 
 
50
    public Gee.Set<Match> keys
 
51
    {
 
52
      owned get { return matches.keys; }
 
53
    }
 
54
 
 
55
    public Gee.Set<Gee.Map.Entry <Match, int>> entries
 
56
    {
 
57
      owned get { return matches.entries; }
 
58
    }
 
59
 
 
60
    public Gee.Iterator<Gee.Map.Entry <Match, int>?> iterator ()
 
61
    {
 
62
      return matches.iterator ();
 
63
    }
 
64
 
 
65
    public bool foreach (Gee.ForallFunc<Match> func)
 
66
    {
 
67
        return matches.keys.foreach (func);
 
68
    }
 
69
 
 
70
    public void add (Match match, int relevancy)
 
71
    {
 
72
      matches.set (match, relevancy);
 
73
 
 
74
      if (match is UriMatch)
 
75
      {
 
76
        unowned string uri = (match as UriMatch).uri;
 
77
        if (uri != null && uri != "")
 
78
        {
 
79
          uris.add (uri);
 
80
        }
 
81
      }
 
82
    }
 
83
 
 
84
    public void add_all (ResultSet? rs)
 
85
    {
 
86
      if (rs == null) return;
 
87
      matches.set_all (rs.matches);
 
88
      uris.add_all (rs.uris);
 
89
    }
 
90
 
 
91
    public bool contains_uri (string uri)
 
92
    {
 
93
      return uri in uris;
 
94
    }
 
95
 
 
96
    public Gee.List<Match> get_sorted_list ()
 
97
    {
 
98
      var l = new Gee.ArrayList<Gee.Map.Entry<Match, int>> ();
 
99
      l.add_all (matches.entries);
 
100
 
 
101
      l.sort ((a, b) => 
 
102
      {
 
103
        unowned Gee.Map.Entry<Match, int> e1 = (Gee.Map.Entry<Match, int>) a;
 
104
        unowned Gee.Map.Entry<Match, int> e2 = (Gee.Map.Entry<Match, int>) b;
 
105
        int relevancy_delta = e2.value - e1.value;
 
106
        if (relevancy_delta != 0) return relevancy_delta;
 
107
        // FIXME: utf8 compare!
 
108
        else return e1.key.title.ascii_casecmp (e2.key.title);
 
109
      });
 
110
 
 
111
      var sorted_list = new Gee.ArrayList<Match> ();
 
112
      foreach (Gee.Map.Entry<Match, int> m in l)
 
113
      {
 
114
        sorted_list.add (m.key);
 
115
      }
 
116
 
 
117
      return sorted_list;
 
118
    }
 
119
  }
 
120
}
 
121