core/filter: Switch from using Index to a GHashTable

This is more straightforward than using my custom index code without
adding too much work.

Implements #43: Filter code can use a GHashTable directly
Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2016-04-29 07:57:06 -04:00 committed by Anna Schumaker
parent ee4bbacf81
commit 04a175d3e3
1 changed files with 24 additions and 11 deletions

View File

@ -1,26 +1,40 @@
/* /*
* Copyright 2013 (c) Anna Schumaker. * Copyright 2013 (c) Anna Schumaker.
*/ */
#include <core/containers/index.h>
#include <core/filter.h> #include <core/filter.h>
#include <core/string.h> #include <core/string.h>
static GHashTable *filter_index = NULL;
static struct database filter_index;
void filter_init() { index_init(&filter_index, "", false); } void filter_init()
void filter_deinit() { db_deinit(&filter_index); } {
filter_index = g_hash_table_new_full(g_str_hash, g_str_equal,
g_free, set_free);
}
void filter_deinit()
{
g_hash_table_destroy(filter_index);
}
void filter_add(const gchar *text, unsigned int index) void filter_add(const gchar *text, unsigned int index)
{ {
const gchar *c = g_utf8_next_char(text); const gchar *c = g_utf8_next_char(text);
glong begin, end; glong begin, end;
struct set *set;
gchar *substr; gchar *substr;
for (begin = 0, end = 1; end <= g_utf8_strlen(text, -1); end++) { for (begin = 0, end = 1; end <= g_utf8_strlen(text, -1); end++) {
substr = g_utf8_substring(text, begin, end); substr = g_utf8_substring(text, begin, end);
index_insert(&filter_index, substr, index); set = g_hash_table_lookup(filter_index, substr);
if (!set) {
set = set_alloc();
g_hash_table_insert(filter_index, substr, set);
}
set_insert(set, index);
if (g_unichar_isspace(g_utf8_get_char(c))) { if (g_unichar_isspace(g_utf8_get_char(c))) {
c = g_utf8_next_char(c); c = g_utf8_next_char(c);
@ -28,14 +42,13 @@ void filter_add(const gchar *text, unsigned int index)
} }
c = g_utf8_next_char(c); c = g_utf8_next_char(c);
g_free(substr);
} }
} }
void filter_search(const gchar *text, struct set *res) void filter_search(const gchar *text, struct set *res)
{ {
gchar *lower = string_lowercase(text); gchar *lower = string_lowercase(text);
struct index_entry *found; struct set *found;
glong begin, end; glong begin, end;
gchar *c, *substr; gchar *c, *substr;
@ -48,7 +61,7 @@ void filter_search(const gchar *text, struct set *res)
continue; continue;
substr = g_utf8_substring(lower, begin, end); substr = g_utf8_substring(lower, begin, end);
found = INDEX_ENTRY(db_get(&filter_index, substr)); found = g_hash_table_lookup(filter_index, substr);
g_free(substr); g_free(substr);
if (!found) { if (!found) {
@ -57,9 +70,9 @@ void filter_search(const gchar *text, struct set *res)
} }
if (begin == 0) if (begin == 0)
set_copy(&found->ie_set, res); set_copy(found, res);
else else
set_inline_intersect(&found->ie_set, res); set_inline_intersect(found, res);
c = g_utf8_next_char(c); c = g_utf8_next_char(c);
begin = ++end; begin = ++end;