Kouhei Sutou
kous****@users*****
2006年 11月 28日 (火) 13:10:02 JST
Index: tomoe/lib/tomoe-context.c diff -u tomoe/lib/tomoe-context.c:1.38 tomoe/lib/tomoe-context.c:1.39 --- tomoe/lib/tomoe-context.c:1.38 Mon Nov 27 16:49:17 2006 +++ tomoe/lib/tomoe-context.c Tue Nov 28 13:10:01 2006 @@ -17,7 +17,7 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe-context.c,v 1.38 2006/11/27 07:49:17 kous Exp $ + * $Id: tomoe-context.c,v 1.39 2006/11/28 04:10:01 kous Exp $ */ #include "tomoe-dict.h" @@ -148,6 +148,7 @@ GList *matched = NULL; if (!context) return matched; + if (!input) return matched; priv = TOMOE_CONTEXT_GET_PRIVATE (context); shelf = priv->shelf; @@ -173,7 +174,7 @@ } static GList * -tomoe_context_search_by_reading (TomoeContext *context, TomoeReading *reading) +tomoe_context_search_by_dict (TomoeContext *context, TomoeQuery *query) { TomoeContextPrivate *priv; TomoeShelf *shelf; @@ -192,8 +193,7 @@ for (name = names; name; name = name->next) { TomoeDict *dict; dict = tomoe_shelf_get_dict(shelf, name->data); - results = g_list_concat (tomoe_dict_search_by_reading (dict, reading), - results); + results = g_list_concat (tomoe_dict_search (dict, query), results); } results = g_list_sort (results, _candidate_compare_func); @@ -203,18 +203,13 @@ GList * tomoe_context_search (TomoeContext *context, TomoeQuery *query) { - const GList *readings; TomoeWriting *writing; writing = tomoe_query_get_writing (query); if (writing) return tomoe_context_search_by_strokes (context, writing); - - readings = tomoe_query_get_readings (query); - if (readings) - return tomoe_context_search_by_reading (context, readings->data); - - return NULL; + else + return tomoe_context_search_by_dict (context, query); } /* Index: tomoe/lib/tomoe-dict.c diff -u tomoe/lib/tomoe-dict.c:1.121 tomoe/lib/tomoe-dict.c:1.122 --- tomoe/lib/tomoe-dict.c:1.121 Tue Nov 28 11:27:59 2006 +++ tomoe/lib/tomoe-dict.c Tue Nov 28 13:10:01 2006 @@ -21,7 +21,7 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe-dict.c,v 1.121 2006/11/28 02:27:59 makeinu Exp $ + * $Id: tomoe-dict.c,v 1.122 2006/11/28 04:10:01 kous Exp $ */ #include <stdio.h> @@ -55,9 +55,7 @@ }; typedef struct _TomoeDictSearchContext { - gint min_n_strokes; - gint max_n_strokes; - TomoeReading *reading; + TomoeQuery *query; GList *results; } TomoeDictSearchContext; @@ -368,41 +366,22 @@ return NULL; } -static void -tomoe_dict_collect_chars_by_n_strokes (gpointer data, gpointer user_data) +static gboolean +tomoe_dict_does_match_char_with_n_strokes (TomoeChar *chr, gint min, gint max) { - TomoeChar *chr = data; - TomoeDictSearchContext *context = user_data; TomoeWriting *writing; gint n_strokes; + if (min < 0 && max < 0) + return TRUE; + writing = tomoe_char_get_writing (chr); - if (!writing) return; + if (!writing) + return FALSE; n_strokes = tomoe_writing_get_n_strokes (writing); - if ((context->min_n_strokes < 0 || context->min_n_strokes <= n_strokes) && - (context->max_n_strokes < 0 || context->max_n_strokes >= n_strokes)) { - context->results = g_list_prepend (context->results, - tomoe_candidate_new (chr)); - } -} - -GList * -tomoe_dict_search_by_n_strokes (TomoeDict *dict, gint min, gint max) -{ - TomoeDictPrivate *priv; - TomoeDictSearchContext context; - - context.min_n_strokes = min; - context.max_n_strokes = max; - context.results = NULL; - - priv = TOMOE_DICT_GET_PRIVATE(dict); - g_ptr_array_foreach_reverse (priv->chars, - tomoe_dict_collect_chars_by_n_strokes, - &context); - - return context.results; + return ((min < 0 || min <= n_strokes) && + (max < 0 || max >= n_strokes)); } static gint @@ -416,31 +395,59 @@ tomoe_reading_get_reading(searched_reading)); } +static gboolean +tomoe_dict_does_match_char_with_readings (TomoeChar *chr, TomoeReading *reading) +{ + if (!reading) + return TRUE; + + if (g_list_find_custom ((GList *)tomoe_char_get_readings (chr), + reading, tomoe_dict_compare_reading)) + return TRUE; + else + return FALSE; +} + static void -tomoe_dict_collect_chars_by_reading (gpointer data, gpointer user_data) +tomoe_dict_collect_chars_by_query (gpointer data, gpointer user_data) { TomoeChar *chr = data; TomoeDictSearchContext *context = user_data; + TomoeQuery *query; + TomoeReading *reading; + gint min_n_strokes, max_n_strokes; - if (g_list_find_custom ((GList *)tomoe_char_get_readings (chr), - context->reading, tomoe_dict_compare_reading)) - context->results = g_list_prepend (context->results, - tomoe_candidate_new (chr)); + query = context->query; + + min_n_strokes = tomoe_query_get_min_n_strokes (query); + max_n_strokes = tomoe_query_get_max_n_strokes (query); + if (!tomoe_dict_does_match_char_with_n_strokes (chr, + min_n_strokes, + max_n_strokes)) + return; + + reading = g_list_nth_data ((GList *)tomoe_query_get_readings (query), 0); + if (!tomoe_dict_does_match_char_with_readings (chr, reading)) + return; + + context->results = g_list_prepend (context->results, + tomoe_candidate_new (chr)); } GList * -tomoe_dict_search_by_reading (TomoeDict* dict, TomoeReading *reading) +tomoe_dict_search (TomoeDict *dict, TomoeQuery *query) { TomoeDictPrivate *priv; TomoeDictSearchContext context; - context.reading = reading; + context.query = g_object_ref (query); context.results = NULL; priv = TOMOE_DICT_GET_PRIVATE(dict); g_ptr_array_foreach_reverse (priv->chars, - tomoe_dict_collect_chars_by_reading, + tomoe_dict_collect_chars_by_query, &context); + g_object_unref (context.query); return context.results; } Index: tomoe/lib/tomoe-dict.h diff -u tomoe/lib/tomoe-dict.h:1.47 tomoe/lib/tomoe-dict.h:1.48 --- tomoe/lib/tomoe-dict.h:1.47 Mon Nov 27 18:17:30 2006 +++ tomoe/lib/tomoe-dict.h Tue Nov 28 13:10:01 2006 @@ -21,7 +21,7 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe-dict.h,v 1.47 2006/11/27 09:17:30 kous Exp $ + * $Id: tomoe-dict.h,v 1.48 2006/11/28 04:10:01 kous Exp $ */ /** @@ -37,6 +37,7 @@ G_BEGIN_DECLS #include "tomoe-char.h" +#include "tomoe-query.h" #define TOMOE_TYPE_DICT (tomoe_dict_get_type ()) #define TOMOE_DICT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TOMOE_TYPE_DICT, TomoeDict)) @@ -130,28 +131,9 @@ TomoeChar *tomoe_dict_get_char (TomoeDict *dict, const gchar *utf8); -/* search methods */ -/** - * @brief Match number of strokes of TomoeChar with range. - * @param dict - Pointer to the TomoeDict struct. - * @param min - Minimum value of the number of strokes. Use - * -1 for no limit. - * @param max - Maximum value of the number of strokes. Use - * -1 for no limit. - * @return The GList of TomoeCandidate. - */ -GList *tomoe_dict_search_by_n_strokes (TomoeDict *dict, - gint min, - gint max); - -/** - * @brief Match reading of TomoeChar with input. - * @param dict - Pointer to the TomoeDict object. - * @param reading - Pointer to string matchkey - * @return The GList of TomoeCandidate. - */ -GList *tomoe_dict_search_by_reading (TomoeDict *dict, - TomoeReading *reading); +/* search method */ +GList *tomoe_dict_search (TomoeDict *dict, + TomoeQuery *query); G_END_DECLS Index: tomoe/lib/tomoe-query.c diff -u tomoe/lib/tomoe-query.c:1.2 tomoe/lib/tomoe-query.c:1.3 --- tomoe/lib/tomoe-query.c:1.2 Mon Nov 27 18:11:09 2006 +++ tomoe/lib/tomoe-query.c Tue Nov 28 13:10:01 2006 @@ -17,7 +17,7 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe-query.c,v 1.2 2006/11/27 09:11:09 kous Exp $ + * $Id: tomoe-query.c,v 1.3 2006/11/28 04:10:01 kous Exp $ */ #include <stdlib.h> @@ -38,7 +38,8 @@ GList *readings; GList *radicals; GList *variants; - gint n_strokes; + gint min_n_strokes; + gint max_n_strokes; TomoeWriting *writing; }; @@ -48,7 +49,8 @@ PROP_READINGS, PROP_RADICALS, PROP_VARIANTS, - PROP_N_STROKES, + PROP_MIN_N_STROKES, + PROP_MAX_N_STROKES, PROP_WRITING }; @@ -86,7 +88,8 @@ priv->readings = NULL; priv->radicals = NULL; priv->variants = NULL; - priv->n_strokes = -1; + priv->min_n_strokes = -1; + priv->max_n_strokes = -1; priv->writing = NULL; } @@ -120,7 +123,8 @@ priv->readings = NULL; priv->radicals = NULL; priv->variants = NULL; - priv->n_strokes = -1; + priv->min_n_strokes = -1; + priv->max_n_strokes = -1; priv->writing = NULL; G_OBJECT_CLASS (tomoe_query_parent_class)->dispose (object); @@ -185,6 +189,49 @@ return priv->readings; } +void +tomoe_query_set_min_n_strokes (TomoeQuery *query, gint n_strokes) +{ + TomoeQueryPrivate *priv; + + g_return_if_fail (TOMOE_IS_QUERY (query)); + + priv = TOMOE_QUERY_GET_PRIVATE (query); + priv->min_n_strokes = n_strokes; +} + +gint +tomoe_query_get_min_n_strokes (TomoeQuery *query) +{ + TomoeQueryPrivate *priv; + + g_return_val_if_fail (TOMOE_IS_QUERY (query), -1); + + priv = TOMOE_QUERY_GET_PRIVATE (query); + return priv->min_n_strokes; +} + +void +tomoe_query_set_max_n_strokes (TomoeQuery *query, gint n_strokes) +{ + TomoeQueryPrivate *priv; + + g_return_if_fail (TOMOE_IS_QUERY (query)); + + priv = TOMOE_QUERY_GET_PRIVATE (query); + priv->max_n_strokes = n_strokes; +} + +gint +tomoe_query_get_max_n_strokes (TomoeQuery *query) +{ + TomoeQueryPrivate *priv; + + g_return_val_if_fail (TOMOE_IS_QUERY (query), -1); + + priv = TOMOE_QUERY_GET_PRIVATE (query); + return priv->max_n_strokes; +} void tomoe_query_set_writing (TomoeQuery* query, TomoeWriting *writing) Index: tomoe/lib/tomoe-query.h diff -u tomoe/lib/tomoe-query.h:1.2 tomoe/lib/tomoe-query.h:1.3 --- tomoe/lib/tomoe-query.h:1.2 Mon Nov 27 18:11:09 2006 +++ tomoe/lib/tomoe-query.h Tue Nov 28 13:10:01 2006 @@ -17,7 +17,7 @@ * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * - * $Id: tomoe-query.h,v 1.2 2006/11/27 09:11:09 kous Exp $ + * $Id: tomoe-query.h,v 1.3 2006/11/28 04:10:01 kous Exp $ */ /** @@ -71,8 +71,12 @@ TomoeChar *chr); void tomoe_query_add_variant (TomoeQuery *query, TomoeChar *chr); -void tomoe_query_set_n_strokes (TomoeQuery *query, - gint num); +void tomoe_query_set_min_n_strokes (TomoeQuery *query, + gint n_strokes); +gint tomoe_query_get_min_n_strokes (TomoeQuery *query); +void tomoe_query_set_max_n_strokes (TomoeQuery *query, + gint n_strokes); +gint tomoe_query_get_max_n_strokes (TomoeQuery *query); void tomoe_query_set_writing (TomoeQuery *query, TomoeWriting *writing); TomoeWriting *tomoe_query_get_writing (TomoeQuery *query);