From: Soren Sandmann Date: Sun, 4 Feb 2007 03:39:57 +0000 (+0000) Subject: Add a new 'real_sequence' field. (g_sequence_new): Initialize X-Git-Tag: GLIB_2_13_0~47 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=576a5d41270c6a1ee2fd7a423998b92d808183be;p=platform%2Fupstream%2Fglib.git Add a new 'real_sequence' field. (g_sequence_new): Initialize 2007-02-03 Soren Sandmann * glib/gsequence.c (struct _GSequence): Add a new 'real_sequence' field. (g_sequence_new): Initialize real_sequence to the sequence (g_sequence_sort_iter): Set real_sequence of the temporary sequence to the real sequence. (g_sequence_sort_changed_iter): Same (g_sequence_insert_sorted_iter): Same (g_sequence_search_iter): Same (g_sequence_iter_get_sequence): Return real_sequence * tests/sequence-test.c (compare_iters): Insert assertions that the iters point to the sequence being manipulated. svn path=/trunk/; revision=5323 --- diff --git a/ChangeLog b/ChangeLog index 4bd7301..ee1777d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,20 @@ 2007-02-03 Soren Sandmann + * glib/gsequence.c (struct _GSequence): Add a new 'real_sequence' + field. + (g_sequence_new): Initialize real_sequence to the sequence + (g_sequence_sort_iter): Set real_sequence of the temporary + sequence to the real sequence. + (g_sequence_sort_changed_iter): Same + (g_sequence_insert_sorted_iter): Same + (g_sequence_search_iter): Same + (g_sequence_iter_get_sequence): Return real_sequence + + * tests/sequence-test.c (compare_iters): Insert assertions that + the iters point to the sequence being manipulated. + +2007-02-03 Soren Sandmann + * glib/gsequence.[ch]: New files implementing GSequence, a list implemented using a binary tree. * glib/glib.h, glib/glib.symbols: Update for GSequence. diff --git a/glib/gsequence.c b/glib/gsequence.c index 5ecd464..c8a073f 100644 --- a/glib/gsequence.c +++ b/glib/gsequence.c @@ -30,6 +30,15 @@ struct _GSequence GSequenceNode * end_node; GDestroyNotify data_destroy_notify; gboolean access_prohibited; + + /* The 'real_sequence' is used when temporary sequences are created + * to hold nodes that being rearranged. The 'real_sequence' of such + * a temporary sequence points to the sequence that is actually being + * manipulated. The only reason we need this is so that when the + * sort/sort_changed/search_iter() functions call out to the application + * g_sequence_iter_get_sequence() will return the correct sequence. + */ + GSequence * real_sequence; }; struct _GSequenceNode @@ -161,6 +170,8 @@ g_sequence_new (GDestroyNotify data_destroy) seq->end_node = node_new (seq); seq->access_prohibited = FALSE; + + seq->real_sequence = seq; return seq; } @@ -697,12 +708,13 @@ g_sequence_sort_iter (GSequence *seq, end = g_sequence_get_end_iter (seq); tmp = g_sequence_new (NULL); - - g_sequence_move_range (g_sequence_get_begin_iter (tmp), begin, end); - + tmp->real_sequence = seq; tmp->access_prohibited = TRUE; + seq->access_prohibited = TRUE; + g_sequence_move_range (g_sequence_get_begin_iter (tmp), begin, end); + while (g_sequence_get_length (tmp) > 0) { GSequenceNode *node = g_sequence_get_begin_iter (tmp); @@ -765,6 +777,8 @@ g_sequence_sort_changed_iter (GSequenceIter *iter, seq->access_prohibited = TRUE; tmp_seq = g_sequence_new (NULL); + tmp_seq->real_sequence = seq; + node_unlink (iter); node_insert_before (tmp_seq->end_node, iter); @@ -824,6 +838,8 @@ g_sequence_insert_sorted_iter (GSequence *seq, * is the only kind of compare functions GtkTreeView can use. */ tmp_seq = g_sequence_new (NULL); + tmp_seq->real_sequence = seq; + new_node = g_sequence_append (tmp_seq, data); node_insert_sorted (seq->end_node, new_node, @@ -885,6 +901,8 @@ g_sequence_search_iter (GSequence *seq, * is the only kind of compare functions GtkTreeView can use. */ tmp_seq = g_sequence_new (NULL); + tmp_seq->real_sequence = seq; + dummy = g_sequence_append (tmp_seq, data); node = node_find_closest (seq->end_node, dummy, @@ -910,9 +928,16 @@ g_sequence_search_iter (GSequence *seq, GSequence * g_sequence_iter_get_sequence (GSequenceIter *iter) { - g_return_val_if_fail (iter != NULL, NULL); + GSequence *seq; - return get_sequence (iter); + g_return_val_if_fail (iter != NULL, NULL); + + seq = get_sequence (iter); + + /* For temporary sequences, this points to the sequence that + * is actually being manipulated + */ + return seq->real_sequence; } /** diff --git a/tests/sequence-test.c b/tests/sequence-test.c index 026dddf..af8a133 100644 --- a/tests/sequence-test.c +++ b/tests/sequence-test.c @@ -127,7 +127,7 @@ compare_items (gconstpointer a, { const Item *item_a = fix_pointer (a); const Item *item_b = fix_pointer (b); - + if (item_a->number < item_b->number) return -1; else if (item_a->number == item_b->number) @@ -169,12 +169,19 @@ compare_iters (gconstpointer a, gconstpointer b, gpointer data) { + GSequence *seq = data; GSequenceIter *iter_a = (GSequenceIter *)a; GSequenceIter *iter_b = (GSequenceIter *)b; /* compare_items() will fix up the pointers */ Item *item_a = g_sequence_get (iter_a); Item *item_b = g_sequence_get (iter_b); + if (seq) + { + g_assert (g_sequence_iter_get_sequence (iter_a) == seq); + g_assert (g_sequence_iter_get_sequence (iter_b) == seq); + } + return compare_items (item_a, item_b, data); } @@ -372,7 +379,7 @@ run_random_tests (guint32 seed) case SORT_ITER: { g_sequence_sort_iter (seq->sequence, - (GSequenceIterCompareFunc)compare_iters, NULL); + (GSequenceIterCompareFunc)compare_iters, seq->sequence); g_queue_sort (seq->queue, compare_iters, NULL); check_sorted (seq); } @@ -538,7 +545,7 @@ run_random_tests (guint32 seed) iter = g_sequence_insert_sorted_iter (seq->sequence, new_item (seq), (GSequenceIterCompareFunc)compare_iters, - NULL); + seq->sequence); g_queue_insert_sorted (seq->queue, iter, compare_iters, NULL); } @@ -593,7 +600,7 @@ run_random_tests (guint32 seed) { g_sequence_set (iter, new_item (seq)); g_sequence_sort_changed_iter (iter, - (GSequenceIterCompareFunc)compare_iters, NULL); + (GSequenceIterCompareFunc)compare_iters, seq->sequence); g_queue_delete_link (seq->queue, link); g_queue_insert_sorted (seq->queue, iter, compare_iters, NULL); @@ -737,7 +744,7 @@ run_random_tests (guint32 seed) item = new_item (seq); search_iter = g_sequence_search_iter (seq->sequence, item, - (GSequenceIterCompareFunc)compare_iters, NULL); + (GSequenceIterCompareFunc)compare_iters, seq->sequence); insert_iter = g_sequence_insert_sorted (seq->sequence, item, compare_items, NULL); @@ -792,13 +799,11 @@ run_random_tests (guint32 seed) if (g_sequence_get_length (seq->sequence) > 0) { - g_assert (!g_sequence_iter_is_begin ( - g_sequence_get_end_iter (seq->sequence))); + g_assert (!g_sequence_iter_is_begin (g_sequence_get_end_iter (seq->sequence))); } else { - g_assert (g_sequence_iter_is_begin ( - g_sequence_get_end_iter (seq->sequence))); + g_assert (g_sequence_iter_is_begin (g_sequence_get_end_iter (seq->sequence))); } g_assert (g_sequence_iter_is_begin (g_sequence_get_begin_iter (seq->sequence))); @@ -815,13 +820,11 @@ run_random_tests (guint32 seed) if (len > 0) { - g_assert (!g_sequence_iter_is_end ( - g_sequence_get_begin_iter (seq->sequence))); + g_assert (!g_sequence_iter_is_end (g_sequence_get_begin_iter (seq->sequence))); } else { - g_assert (g_sequence_iter_is_end ( - g_sequence_get_begin_iter (seq->sequence))); + g_assert (g_sequence_iter_is_end (g_sequence_get_begin_iter (seq->sequence))); } g_assert (g_sequence_iter_is_end (g_sequence_get_end_iter (seq->sequence))); @@ -1083,12 +1086,10 @@ test_insert_sorted_non_pointer (void) for (j = 0; j < 10000; j++) { - g_sequence_insert_sorted ( - seq, GINT_TO_POINTER (g_random_int()), + g_sequence_insert_sorted (seq, GINT_TO_POINTER (g_random_int()), compare, NULL); - g_sequence_insert_sorted_iter ( - seq, GINT_TO_POINTER (g_random_int()), + g_sequence_insert_sorted_iter (seq, GINT_TO_POINTER (g_random_int()), compare_iter, NULL); }