~desrt/indicator-appmenu/emit-unregister-signal

« back to all changes in this revision

Viewing changes to src/hudresult.c

  • Committer: Charles Kerr
  • Date: 2012-04-03 20:31:54 UTC
  • mfrom: (192.1.21 indicator-appmenu)
  • Revision ID: charles.kerr@canonical.com-20120403203154-2qacfj0nmpm56pnr
merge lp:~desrt/indicator-appmenu/hud-performance to improve hud-service performance and search result quality by substantially reworking the core distance algorithm and the needle/haystack assignment algorithm.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include <string.h>
22
22
 
23
23
#include "hudsettings.h"
24
 
#include "distance.h"
 
24
#include "hudtoken.h"
25
25
 
26
26
/**
27
27
 * SECTION:hudresult
48
48
  HudItem *item;
49
49
 
50
50
  guint   distance;
51
 
  gchar **matched;
 
51
  const gchar **matched;
52
52
  gchar  *description;
53
53
};
54
54
 
55
55
G_DEFINE_TYPE (HudResult, hud_result, G_TYPE_OBJECT)
56
56
 
57
 
static guint
58
 
calculate_distance_from_list (const gchar   *search_string,
59
 
                              HudStringList *list,
60
 
                              GStrv         *matched)
61
 
{
62
 
  HudStringList *iter;
63
 
  const gchar **strv;
64
 
  guint distance;
65
 
  gint i = 0;
66
 
 
67
 
  for (iter = list; iter; iter = hud_string_list_get_tail (iter))
68
 
    i++;
69
 
 
70
 
  strv = g_new (const char *, i + 1);
71
 
  strv[i] = NULL;
72
 
 
73
 
  for (iter = list; iter; iter = hud_string_list_get_tail (iter))
74
 
    strv[--i] = hud_string_list_get_head (iter);
75
 
 
76
 
  distance = calculate_distance (search_string, (char **) strv, matched);
77
 
 
78
 
  g_free (strv);
79
 
 
80
 
 
81
 
  return distance;
82
 
}
83
 
 
84
57
static void
85
58
hud_result_finalize (GObject *object)
86
59
{
87
60
  HudResult *result = HUD_RESULT (object);
88
61
 
89
62
  g_object_unref (result->item);
90
 
  g_strfreev (result->matched);
 
63
  g_free (result->matched);
91
64
  g_free (result->description);
92
65
 
93
66
  G_OBJECT_CLASS (hud_result_parent_class)
126
99
 * Returns: a new #HudResult, or %NULL in event of a poor match
127
100
 **/
128
101
HudResult *
129
 
hud_result_get_if_matched (HudItem     *item,
130
 
                           const gchar *search_string,
131
 
                           guint        penalty)
 
102
hud_result_get_if_matched (HudItem      *item,
 
103
                           HudTokenList *search_tokens,
 
104
                           guint         penalty)
132
105
{
133
106
  if (!hud_item_get_enabled (item))
134
107
    return NULL;
135
108
 
136
109
  /* ignore the penalty in the max-distance calculation */
137
 
  if (calculate_distance_from_list (search_string, hud_item_get_tokens (item), NULL) <= hud_settings.max_distance)
138
 
    return hud_result_new (item, search_string, penalty);
 
110
  if (hud_token_list_distance (hud_item_get_token_list (item), search_tokens, NULL) <= hud_settings.max_distance)
 
111
    return hud_result_new (item, search_tokens, penalty);
139
112
  else
140
113
    return NULL;
141
114
}
212
185
 * Returns: the new #HudResult
213
186
 **/
214
187
HudResult *
215
 
hud_result_new (HudItem     *item,
216
 
                const gchar *search_string,
217
 
                guint        penalty)
 
188
hud_result_new (HudItem      *item,
 
189
                HudTokenList *search_tokens,
 
190
                guint         penalty)
218
191
{
219
192
  HudResult *result;
220
193
 
221
194
  g_return_val_if_fail (HUD_IS_ITEM (item), NULL);
222
 
  g_return_val_if_fail (search_string != NULL, NULL);
 
195
  g_return_val_if_fail (search_tokens != NULL, NULL);
223
196
 
224
197
  result = g_object_new (HUD_TYPE_RESULT, NULL);
225
198
  result->item = g_object_ref (item);
226
 
  result->distance = calculate_distance_from_list (search_string, hud_item_get_tokens (item), &result->matched);
 
199
  result->distance = hud_token_list_distance (hud_item_get_token_list (item), search_tokens, &result->matched);
227
200
  hud_result_format_description (result);
228
201
 
229
202
  result->distance += (result->distance * penalty) / 100;