2015-11-10 10:17:52 -05:00
|
|
|
/*
|
2013-08-25 10:33:48 -04:00
|
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
|
|
*/
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/filter.h>
|
2015-01-30 10:03:18 -05:00
|
|
|
#include <core/string.h>
|
2013-08-25 10:33:48 -04:00
|
|
|
|
2016-04-29 07:57:06 -04:00
|
|
|
static GHashTable *filter_index = NULL;
|
2013-08-25 10:33:48 -04:00
|
|
|
|
2015-09-29 16:25:16 -04:00
|
|
|
|
2016-04-29 10:17:54 -04:00
|
|
|
void __filter_copy(const struct set *lhs, struct set *rhs)
|
|
|
|
{
|
|
|
|
GHashTableIter iter;
|
|
|
|
gpointer data;
|
|
|
|
|
|
|
|
g_hash_table_iter_init(&iter, lhs->s_set);
|
|
|
|
while (g_hash_table_iter_next(&iter, &data, NULL))
|
|
|
|
set_insert(rhs, GPOINTER_TO_UINT(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
void __filter_inline_intersect(const struct set *lhs, struct set *rhs)
|
|
|
|
{
|
|
|
|
GHashTableIter iter;
|
|
|
|
gpointer data;
|
|
|
|
|
|
|
|
g_hash_table_iter_init(&iter, rhs->s_set);
|
|
|
|
while (g_hash_table_iter_next(&iter, &data, NULL)) {
|
|
|
|
if (!set_has(lhs, GPOINTER_TO_UINT(data)))
|
|
|
|
g_hash_table_iter_remove(&iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-29 07:57:06 -04:00
|
|
|
void filter_init()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2013-08-25 10:33:48 -04:00
|
|
|
|
2016-04-29 09:03:20 -04:00
|
|
|
void filter_add(const gchar *text, void *data)
|
2013-08-25 10:33:48 -04:00
|
|
|
{
|
2015-11-10 09:36:38 -05:00
|
|
|
const gchar *c = g_utf8_next_char(text);
|
|
|
|
glong begin, end;
|
2016-04-29 07:57:06 -04:00
|
|
|
struct set *set;
|
2015-11-10 09:36:38 -05:00
|
|
|
gchar *substr;
|
2014-03-15 20:44:07 -04:00
|
|
|
|
2015-11-10 09:36:38 -05:00
|
|
|
for (begin = 0, end = 1; end <= g_utf8_strlen(text, -1); end++) {
|
|
|
|
substr = g_utf8_substring(text, begin, end);
|
2016-04-29 07:57:06 -04:00
|
|
|
set = g_hash_table_lookup(filter_index, substr);
|
|
|
|
|
|
|
|
if (!set) {
|
|
|
|
set = set_alloc();
|
|
|
|
g_hash_table_insert(filter_index, substr, set);
|
|
|
|
}
|
|
|
|
|
2016-04-29 09:03:20 -04:00
|
|
|
set_insert(set, GPOINTER_TO_UINT(data));
|
2015-09-08 09:14:39 -04:00
|
|
|
|
2015-11-10 09:36:38 -05:00
|
|
|
if (g_unichar_isspace(g_utf8_get_char(c))) {
|
|
|
|
c = g_utf8_next_char(c);
|
2015-01-30 10:03:18 -05:00
|
|
|
begin = ++end;
|
2015-11-10 09:36:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
c = g_utf8_next_char(c);
|
2015-01-30 10:03:18 -05:00
|
|
|
}
|
2013-08-25 10:33:48 -04:00
|
|
|
}
|
|
|
|
|
2016-04-29 09:03:20 -04:00
|
|
|
void filter_remove(const gchar *text, void *data)
|
2016-04-29 08:51:58 -04:00
|
|
|
{
|
|
|
|
const gchar *c = g_utf8_next_char(text);
|
|
|
|
glong begin, end;
|
|
|
|
struct set *set;
|
|
|
|
gchar *substr;
|
|
|
|
|
|
|
|
for (begin = 0, end = 1; end <= g_utf8_strlen(text, -1); end++) {
|
|
|
|
substr = g_utf8_substring(text, begin, end);
|
|
|
|
set = g_hash_table_lookup(filter_index, substr);
|
|
|
|
|
|
|
|
if (set)
|
2016-04-29 09:03:20 -04:00
|
|
|
set_remove(set, GPOINTER_TO_UINT(data));
|
2016-04-29 08:51:58 -04:00
|
|
|
|
|
|
|
if (g_unichar_isspace(g_utf8_get_char(c))) {
|
|
|
|
c = g_utf8_next_char(c);
|
|
|
|
begin = ++end;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = g_utf8_next_char(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-10 10:12:20 -05:00
|
|
|
void filter_search(const gchar *text, struct set *res)
|
2013-08-25 10:33:48 -04:00
|
|
|
{
|
2015-11-10 10:12:20 -05:00
|
|
|
gchar *lower = string_lowercase(text);
|
2016-04-29 07:57:06 -04:00
|
|
|
struct set *found;
|
2015-11-10 10:12:20 -05:00
|
|
|
glong begin, end;
|
|
|
|
gchar *c, *substr;
|
2013-08-25 10:33:48 -04:00
|
|
|
|
2015-10-21 15:57:21 -04:00
|
|
|
set_clear(res);
|
2015-11-10 10:12:20 -05:00
|
|
|
c = lower;
|
|
|
|
|
|
|
|
for (begin = 0, end = 1; end <= g_utf8_strlen(lower, -1); end++) {
|
|
|
|
c = g_utf8_next_char(c);
|
|
|
|
if ((*c != '\0') && !g_unichar_isspace(g_utf8_get_char(c)))
|
|
|
|
continue;
|
2014-11-06 08:24:13 -05:00
|
|
|
|
2015-11-10 10:12:20 -05:00
|
|
|
substr = g_utf8_substring(lower, begin, end);
|
2016-04-29 07:57:06 -04:00
|
|
|
found = g_hash_table_lookup(filter_index, substr);
|
2015-11-10 10:12:20 -05:00
|
|
|
g_free(substr);
|
2015-01-30 10:52:15 -05:00
|
|
|
|
|
|
|
if (!found) {
|
2015-10-21 15:57:21 -04:00
|
|
|
set_clear(res);
|
2015-11-10 10:12:20 -05:00
|
|
|
break;
|
2015-01-30 10:52:15 -05:00
|
|
|
}
|
2013-08-25 10:33:48 -04:00
|
|
|
|
2015-01-30 10:52:15 -05:00
|
|
|
if (begin == 0)
|
2016-04-29 10:17:54 -04:00
|
|
|
__filter_copy(found, res);
|
2015-01-30 10:52:15 -05:00
|
|
|
else
|
2016-04-29 10:17:54 -04:00
|
|
|
__filter_inline_intersect(found, res);
|
2013-08-25 10:33:48 -04:00
|
|
|
|
2015-11-10 10:12:20 -05:00
|
|
|
c = g_utf8_next_char(c);
|
2015-01-30 10:52:15 -05:00
|
|
|
begin = ++end;
|
|
|
|
}
|
2015-11-10 10:12:20 -05:00
|
|
|
|
|
|
|
g_free(lower);
|
2013-08-25 10:33:48 -04:00
|
|
|
}
|