1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007
3 * Soeren Sandmann (sandmann@daimi.au.dk)
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "gsequence.h"
24 #include "gtestutils.h"
29 * @short_description: scalable lists
31 * The #GSequence data structure has the API of a list, but is
32 * implemented internally with a balanced binary tree. This means that
33 * it is possible to maintain a sorted list of n elements in time O(n
34 * log n). The data contained in each element can be either integer
35 * values, by using of the <link
36 * linkend="glib-Type-Conversion-Macros">Type Conversion Macros</link>,
37 * or simply pointers to any type of data.
39 * A #GSequence is accessed through "iterators", represented by a
40 * #GSequenceIter. An iterator represents a position between two
41 * elements of the sequence. For example, the "begin" iterator
42 * represents the gap immediately before the first element of the
43 * sequence, and the "end" iterator represents the gap immediately
44 * after the last element. In an empty sequence, the begin and end
45 * iterators are the same.
47 * Some methods on #GSequence operate on ranges of items. For example
48 * g_sequence_foreach_range() will call a user-specified function on
49 * each element with the given range. The range is delimited by the
50 * gaps represented by the passed-in iterators, so if you pass in the
51 * begin and end iterators, the range in question is the entire
54 * The function g_sequence_get() is used with an iterator to access the
55 * element immediately following the gap that the iterator represents.
56 * The iterator is said to "point" to that element.
58 * Iterators are stable across most operations on a #GSequence. For
59 * example an iterator pointing to some element of a sequence will
60 * continue to point to that element even after the sequence is sorted.
61 * Even moving an element to another sequence using for example
62 * g_sequence_move_range() will not invalidate the iterators pointing
63 * to it. The only operation that will invalidate an iterator is when
64 * the element it points to is removed from any sequence.
70 * The #GSequenceIter struct is an opaque data type representing an
71 * iterator pointing into a #GSequence.
75 * GSequenceIterCompareFunc:
76 * @a: a #GSequenceIter
77 * @b: a #GSequenceIter
80 * A #GSequenceIterCompareFunc is a function used to compare iterators.
81 * It must return zero if the iterators compare equal, a negative value
82 * if @a comes before @b, and a positive value if @b comes before @a.
84 * Returns: zero if the iterators are equal, a negative value if @a
85 * comes before @b, and a positive value if @b comes before @a.
88 typedef struct _GSequenceNode GSequenceNode;
93 * The #GSequence struct is an opaque data type representing a
94 * <link linkend="glib-Sequences">Sequence</link> data type.
98 GSequenceNode * end_node;
99 GDestroyNotify data_destroy_notify;
100 gboolean access_prohibited;
102 /* The 'real_sequence' is used when temporary sequences are created
103 * to hold nodes that are being rearranged. The 'real_sequence' of such
104 * a temporary sequence points to the sequence that is actually being
105 * manipulated. The only reason we need this is so that when the
106 * sort/sort_changed/search_iter() functions call out to the application
107 * g_sequence_iter_get_sequence() will return the correct sequence.
109 GSequence * real_sequence;
112 struct _GSequenceNode
115 GSequenceNode * parent;
116 GSequenceNode * left;
117 GSequenceNode * right;
118 gpointer data; /* For the end node, this field points
124 * Declaration of GSequenceNode methods
126 static GSequenceNode *node_new (gpointer data);
127 static GSequenceNode *node_get_first (GSequenceNode *node);
128 static GSequenceNode *node_get_last (GSequenceNode *node);
129 static GSequenceNode *node_get_prev (GSequenceNode *node);
130 static GSequenceNode *node_get_next (GSequenceNode *node);
131 static gint node_get_pos (GSequenceNode *node);
132 static GSequenceNode *node_get_by_pos (GSequenceNode *node,
134 static GSequenceNode *node_find (GSequenceNode *haystack,
135 GSequenceNode *needle,
137 GSequenceIterCompareFunc cmp,
139 static GSequenceNode *node_find_closest (GSequenceNode *haystack,
140 GSequenceNode *needle,
142 GSequenceIterCompareFunc cmp,
144 static gint node_get_length (GSequenceNode *node);
145 static void node_free (GSequenceNode *node,
147 static void node_cut (GSequenceNode *split);
148 static void node_insert_before (GSequenceNode *node,
150 static void node_unlink (GSequenceNode *node);
151 static void node_join (GSequenceNode *left,
152 GSequenceNode *right);
153 static void node_insert_sorted (GSequenceNode *node,
156 GSequenceIterCompareFunc cmp_func,
161 * Various helper functions
164 check_seq_access (GSequence *seq)
166 if (G_UNLIKELY (seq->access_prohibited))
168 g_warning ("Accessing a sequence while it is "
169 "being sorted or searched is not allowed");
174 get_sequence (GSequenceNode *node)
176 return (GSequence *)node_get_last (node)->data;
180 check_iter_access (GSequenceIter *iter)
182 check_seq_access (get_sequence (iter));
186 is_end (GSequenceIter *iter)
196 if (iter->parent->right != iter)
199 seq = get_sequence (iter);
201 return seq->end_node == iter;
206 GCompareDataFunc cmp_func;
208 GSequenceNode *end_node;
211 /* This function compares two iters using a normal compare
212 * function and user_data passed in in a SortInfo struct
215 iter_compare (GSequenceIter *node1,
216 GSequenceIter *node2,
219 const SortInfo *info = data;
222 if (node1 == info->end_node)
225 if (node2 == info->end_node)
228 retval = info->cmp_func (node1->data, node2->data, info->cmp_data);
239 * @data_destroy: (allow-none): a #GDestroyNotify function, or %NULL
241 * Creates a new GSequence. The @data_destroy function, if non-%NULL will
242 * be called on all items when the sequence is destroyed and on items that
243 * are removed from the sequence.
245 * Return value: a new #GSequence
250 g_sequence_new (GDestroyNotify data_destroy)
252 GSequence *seq = g_new (GSequence, 1);
253 seq->data_destroy_notify = data_destroy;
255 seq->end_node = node_new (seq);
257 seq->access_prohibited = FALSE;
259 seq->real_sequence = seq;
268 * Frees the memory allocated for @seq. If @seq has a data destroy
269 * function associated with it, that function is called on all items
275 g_sequence_free (GSequence *seq)
277 g_return_if_fail (seq != NULL);
279 check_seq_access (seq);
281 node_free (seq->end_node, seq);
287 * g_sequence_foreach_range:
288 * @begin: a #GSequenceIter
289 * @end: a #GSequenceIter
291 * @user_data: user data passed to @func
293 * Calls @func for each item in the range (@begin, @end) passing
294 * @user_data to the function.
299 g_sequence_foreach_range (GSequenceIter *begin,
307 g_return_if_fail (func != NULL);
308 g_return_if_fail (begin != NULL);
309 g_return_if_fail (end != NULL);
311 seq = get_sequence (begin);
313 seq->access_prohibited = TRUE;
318 GSequenceIter *next = node_get_next (iter);
320 func (iter->data, user_data);
325 seq->access_prohibited = FALSE;
329 * g_sequence_foreach:
331 * @func: the function to call for each item in @seq
332 * @user_data: user data passed to @func
334 * Calls @func for each item in the sequence passing @user_data
340 g_sequence_foreach (GSequence *seq,
344 GSequenceIter *begin, *end;
346 check_seq_access (seq);
348 begin = g_sequence_get_begin_iter (seq);
349 end = g_sequence_get_end_iter (seq);
351 g_sequence_foreach_range (begin, end, func, user_data);
355 * g_sequence_range_get_midpoint:
356 * @begin: a #GSequenceIter
357 * @end: a #GSequenceIter
359 * Finds an iterator somewhere in the range (@begin, @end). This
360 * iterator will be close to the middle of the range, but is not
361 * guaranteed to be exactly in the middle.
363 * The @begin and @end iterators must both point to the same sequence
364 * and @begin must come before or be equal to @end in the sequence.
366 * Return value: a #GSequenceIter pointing somewhere in the
367 * (@begin, @end) range
372 g_sequence_range_get_midpoint (GSequenceIter *begin,
375 int begin_pos, end_pos, mid_pos;
377 g_return_val_if_fail (begin != NULL, NULL);
378 g_return_val_if_fail (end != NULL, NULL);
379 g_return_val_if_fail (get_sequence (begin) == get_sequence (end), NULL);
381 begin_pos = node_get_pos (begin);
382 end_pos = node_get_pos (end);
384 g_return_val_if_fail (end_pos >= begin_pos, NULL);
386 mid_pos = begin_pos + (end_pos - begin_pos) / 2;
388 return node_get_by_pos (begin, mid_pos);
392 * g_sequence_iter_compare:
393 * @a: a #GSequenceIter
394 * @b: a #GSequenceIter
396 * Returns a negative number if @a comes before @b, 0 if they are equal,
397 * and a positive number if @a comes after @b.
399 * The @a and @b iterators must point into the same sequence.
401 * Return value: a negative number if @a comes before @b, 0 if they are
402 * equal, and a positive number if @a comes after @b
407 g_sequence_iter_compare (GSequenceIter *a,
412 g_return_val_if_fail (a != NULL, 0);
413 g_return_val_if_fail (b != NULL, 0);
414 g_return_val_if_fail (get_sequence (a) == get_sequence (b), 0);
416 check_iter_access (a);
417 check_iter_access (b);
419 a_pos = node_get_pos (a);
420 b_pos = node_get_pos (b);
424 else if (a_pos > b_pos)
433 * @data: the data for the new item
435 * Adds a new item to the end of @seq.
437 * Return value: an iterator pointing to the new item
442 g_sequence_append (GSequence *seq,
447 g_return_val_if_fail (seq != NULL, NULL);
449 check_seq_access (seq);
451 node = node_new (data);
452 node_insert_before (seq->end_node, node);
458 * g_sequence_prepend:
460 * @data: the data for the new item
462 * Adds a new item to the front of @seq
464 * Return value: an iterator pointing to the new item
469 g_sequence_prepend (GSequence *seq,
472 GSequenceNode *node, *first;
474 g_return_val_if_fail (seq != NULL, NULL);
476 check_seq_access (seq);
478 node = node_new (data);
479 first = node_get_first (seq->end_node);
481 node_insert_before (first, node);
487 * g_sequence_insert_before:
488 * @iter: a #GSequenceIter
489 * @data: the data for the new item
491 * Inserts a new item just before the item pointed to by @iter.
493 * Return value: an iterator pointing to the new item
498 g_sequence_insert_before (GSequenceIter *iter,
503 g_return_val_if_fail (iter != NULL, NULL);
505 check_iter_access (iter);
507 node = node_new (data);
509 node_insert_before (iter, node);
516 * @iter: a #GSequenceIter
518 * Removes the item pointed to by @iter. It is an error to pass the
519 * end iterator to this function.
521 * If the sequence has a data destroy function associated with it, this
522 * function is called on the data for the removed item.
527 g_sequence_remove (GSequenceIter *iter)
531 g_return_if_fail (iter != NULL);
532 g_return_if_fail (!is_end (iter));
534 check_iter_access (iter);
536 seq = get_sequence (iter);
539 node_free (iter, seq);
543 * g_sequence_remove_range:
544 * @begin: a #GSequenceIter
545 * @end: a #GSequenceIter
547 * Removes all items in the (@begin, @end) range.
549 * If the sequence has a data destroy function associated with it, this
550 * function is called on the data for the removed items.
555 g_sequence_remove_range (GSequenceIter *begin,
558 g_return_if_fail (get_sequence (begin) == get_sequence (end));
560 check_iter_access (begin);
561 check_iter_access (end);
563 g_sequence_move_range (NULL, begin, end);
567 * g_sequence_move_range:
568 * @dest: a #GSequenceIter
569 * @begin: a #GSequenceIter
570 * @end: a #GSequenceIter
572 * Inserts the (@begin, @end) range at the destination pointed to by ptr.
573 * The @begin and @end iters must point into the same sequence. It is
574 * allowed for @dest to point to a different sequence than the one pointed
575 * into by @begin and @end.
577 * If @dest is NULL, the range indicated by @begin and @end is
578 * removed from the sequence. If @dest iter points to a place within
579 * the (@begin, @end) range, the range does not move.
584 g_sequence_move_range (GSequenceIter *dest,
585 GSequenceIter *begin,
589 GSequenceNode *first;
591 g_return_if_fail (begin != NULL);
592 g_return_if_fail (end != NULL);
594 check_iter_access (begin);
595 check_iter_access (end);
597 check_iter_access (dest);
599 src_seq = get_sequence (begin);
601 g_return_if_fail (src_seq == get_sequence (end));
603 /* Dest points to begin or end? */
604 if (dest == begin || dest == end)
607 /* begin comes after end? */
608 if (g_sequence_iter_compare (begin, end) >= 0)
611 /* dest points somewhere in the (begin, end) range? */
612 if (dest && get_sequence (dest) == src_seq &&
613 g_sequence_iter_compare (dest, begin) > 0 &&
614 g_sequence_iter_compare (dest, end) < 0)
619 src_seq = get_sequence (begin);
621 first = node_get_first (begin);
628 node_join (first, end);
632 first = node_get_first (dest);
636 node_join (begin, dest);
639 node_join (first, begin);
643 node_free (begin, src_seq);
650 * @cmp_func: the function used to sort the sequence
651 * @cmp_data: user data passed to @cmp_func
653 * Sorts @seq using @cmp_func.
655 * @cmp_func is passed two items of @seq and should
656 * return 0 if they are equal, a negative value if the
657 * first comes before the second, and a positive value
658 * if the second comes before the first.
663 g_sequence_sort (GSequence *seq,
664 GCompareDataFunc cmp_func,
669 info.cmp_func = cmp_func;
670 info.cmp_data = cmp_data;
671 info.end_node = seq->end_node;
673 check_seq_access (seq);
675 g_sequence_sort_iter (seq, iter_compare, &info);
679 * g_sequence_insert_sorted:
681 * @data: the data to insert
682 * @cmp_func: the function used to compare items in the sequence
683 * @cmp_data: user data passed to @cmp_func.
685 * Inserts @data into @sequence using @func to determine the new
686 * position. The sequence must already be sorted according to @cmp_func;
687 * otherwise the new position of @data is undefined.
689 * @cmp_func is called with two items of the @seq and @user_data.
690 * It should return 0 if the items are equal, a negative value
691 * if the first item comes before the second, and a positive value
692 * if the second item comes before the first.
694 * Return value: a #GSequenceIter pointing to the new item.
699 g_sequence_insert_sorted (GSequence *seq,
701 GCompareDataFunc cmp_func,
706 g_return_val_if_fail (seq != NULL, NULL);
707 g_return_val_if_fail (cmp_func != NULL, NULL);
709 info.cmp_func = cmp_func;
710 info.cmp_data = cmp_data;
711 info.end_node = seq->end_node;
712 check_seq_access (seq);
714 return g_sequence_insert_sorted_iter (seq, data, iter_compare, &info);
718 * g_sequence_sort_changed:
719 * @iter: A #GSequenceIter
720 * @cmp_func: the function used to compare items in the sequence
721 * @cmp_data: user data passed to @cmp_func.
723 * Moves the data pointed to a new position as indicated by @cmp_func. This
724 * function should be called for items in a sequence already sorted according
725 * to @cmp_func whenever some aspect of an item changes so that @cmp_func
726 * may return different values for that item.
728 * @cmp_func is called with two items of the @seq and @user_data.
729 * It should return 0 if the items are equal, a negative value if
730 * the first item comes before the second, and a positive value if
731 * the second item comes before the first.
736 g_sequence_sort_changed (GSequenceIter *iter,
737 GCompareDataFunc cmp_func,
742 g_return_if_fail (!is_end (iter));
744 info.cmp_func = cmp_func;
745 info.cmp_data = cmp_data;
746 info.end_node = get_sequence (iter)->end_node;
747 check_iter_access (iter);
749 g_sequence_sort_changed_iter (iter, iter_compare, &info);
755 * @data: data for the new item
756 * @cmp_func: the function used to compare items in the sequence
757 * @cmp_data: user data passed to @cmp_func
759 * Returns an iterator pointing to the position where @data would
760 * be inserted according to @cmp_func and @cmp_data.
762 * @cmp_func is called with two items of the @seq and @user_data.
763 * It should return 0 if the items are equal, a negative value if
764 * the first item comes before the second, and a positive value if
765 * the second item comes before the first.
767 * If you are simply searching for an existing element of the sequence,
768 * consider using g_sequence_lookup().
770 * This function will fail if the data contained in the sequence is
771 * unsorted. Use g_sequence_insert_sorted() or
772 * g_sequence_insert_sorted_iter() to add data to your sequence or, if
773 * you want to add a large amount of data, call g_sequence_sort() after
774 * doing unsorted insertions.
776 * Return value: an #GSequenceIter pointing to the position where @data
777 * would have been inserted according to @cmp_func and @cmp_data
782 g_sequence_search (GSequence *seq,
784 GCompareDataFunc cmp_func,
789 g_return_val_if_fail (seq != NULL, NULL);
791 info.cmp_func = cmp_func;
792 info.cmp_data = cmp_data;
793 info.end_node = seq->end_node;
794 check_seq_access (seq);
796 return g_sequence_search_iter (seq, data, iter_compare, &info);
802 * @data: data to lookup
803 * @cmp_func: the function used to compare items in the sequence
804 * @cmp_data: user data passed to @cmp_func
806 * Returns an iterator pointing to the position of the first item found
807 * equal to @data according to @cmp_func and @cmp_data. If more than one
808 * item is equal, it is not guaranteed that it is the first which is
809 * returned. In that case, you can use g_sequence_iter_next() and
810 * g_sequence_iter_prev() to get others.
812 * @cmp_func is called with two items of the @seq and @user_data.
813 * It should return 0 if the items are equal, a negative value if
814 * the first item comes before the second, and a positive value if
815 * the second item comes before the first.
817 * This function will fail if the data contained in the sequence is
818 * unsorted. Use g_sequence_insert_sorted() or
819 * g_sequence_insert_sorted_iter() to add data to your sequence or, if
820 * you want to add a large amount of data, call g_sequence_sort() after
821 * doing unsorted insertions.
823 * Return value: an #GSequenceIter pointing to the position of the
824 * first item found equal to @data according to @cmp_func and
825 * @cmp_data, or %NULL if no such item exists
830 g_sequence_lookup (GSequence *seq,
832 GCompareDataFunc cmp_func,
837 g_return_val_if_fail (seq != NULL, NULL);
839 info.cmp_func = cmp_func;
840 info.cmp_data = cmp_data;
841 info.end_node = seq->end_node;
842 check_seq_access (seq);
844 return g_sequence_lookup_iter (seq, data, iter_compare, &info);
848 * g_sequence_sort_iter:
850 * @cmp_func: the function used to compare iterators in the sequence
851 * @cmp_data: user data passed to @cmp_func
853 * Like g_sequence_sort(), but uses a #GSequenceIterCompareFunc instead
854 * of a GCompareDataFunc as the compare function
856 * @cmp_func is called with two iterators pointing into @seq. It should
857 * return 0 if the iterators are equal, a negative value if the first
858 * iterator comes before the second, and a positive value if the second
859 * iterator comes before the first.
864 g_sequence_sort_iter (GSequence *seq,
865 GSequenceIterCompareFunc cmp_func,
869 GSequenceNode *begin, *end;
871 g_return_if_fail (seq != NULL);
872 g_return_if_fail (cmp_func != NULL);
874 check_seq_access (seq);
876 begin = g_sequence_get_begin_iter (seq);
877 end = g_sequence_get_end_iter (seq);
879 tmp = g_sequence_new (NULL);
880 tmp->real_sequence = seq;
882 g_sequence_move_range (g_sequence_get_begin_iter (tmp), begin, end);
884 seq->access_prohibited = TRUE;
885 tmp->access_prohibited = TRUE;
887 while (g_sequence_get_length (tmp) > 0)
889 GSequenceNode *node = g_sequence_get_begin_iter (tmp);
891 node_insert_sorted (seq->end_node, node, seq->end_node,
895 tmp->access_prohibited = FALSE;
896 seq->access_prohibited = FALSE;
898 g_sequence_free (tmp);
902 * g_sequence_sort_changed_iter:
903 * @iter: a #GSequenceIter
904 * @iter_cmp: the function used to compare iterators in the sequence
905 * @cmp_data: user data passed to @cmp_func
907 * Like g_sequence_sort_changed(), but uses
908 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
909 * the compare function.
911 * @iter_cmp is called with two iterators pointing into @seq. It should
912 * return 0 if the iterators are equal, a negative value if the first
913 * iterator comes before the second, and a positive value if the second
914 * iterator comes before the first.
919 g_sequence_sort_changed_iter (GSequenceIter *iter,
920 GSequenceIterCompareFunc iter_cmp,
923 GSequence *seq, *tmp_seq;
924 GSequenceIter *next, *prev;
926 g_return_if_fail (iter != NULL);
927 g_return_if_fail (!is_end (iter));
928 g_return_if_fail (iter_cmp != NULL);
929 check_iter_access (iter);
931 /* If one of the neighbours is equal to iter, then
932 * don't move it. This ensures that sort_changed() is
933 * a stable operation.
936 next = node_get_next (iter);
937 prev = node_get_prev (iter);
939 if (prev != iter && iter_cmp (prev, iter, cmp_data) == 0)
942 if (!is_end (next) && iter_cmp (next, iter, cmp_data) == 0)
945 seq = get_sequence (iter);
947 seq->access_prohibited = TRUE;
949 tmp_seq = g_sequence_new (NULL);
950 tmp_seq->real_sequence = seq;
953 node_insert_before (tmp_seq->end_node, iter);
955 node_insert_sorted (seq->end_node, iter, seq->end_node,
958 g_sequence_free (tmp_seq);
960 seq->access_prohibited = FALSE;
964 * g_sequence_insert_sorted_iter:
966 * @data: data for the new item
967 * @iter_cmp: the function used to compare iterators in the sequence
968 * @cmp_data: user data passed to @cmp_func
970 * Like g_sequence_insert_sorted(), but uses
971 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
972 * the compare function.
974 * @iter_cmp is called with two iterators pointing into @seq.
975 * It should return 0 if the iterators are equal, a negative
976 * value if the first iterator comes before the second, and a
977 * positive value if the second iterator comes before the first.
979 * It is called with two iterators pointing into @seq. It should
980 * return 0 if the iterators are equal, a negative value if the
981 * first iterator comes before the second, and a positive value
982 * if the second iterator comes before the first.
984 * Return value: a #GSequenceIter pointing to the new item
989 g_sequence_insert_sorted_iter (GSequence *seq,
991 GSequenceIterCompareFunc iter_cmp,
994 GSequenceNode *new_node;
997 g_return_val_if_fail (seq != NULL, NULL);
998 g_return_val_if_fail (iter_cmp != NULL, NULL);
1000 check_seq_access (seq);
1002 seq->access_prohibited = TRUE;
1004 /* Create a new temporary sequence and put the new node into
1005 * that. The reason for this is that the user compare function
1006 * will be called with the new node, and if it dereferences,
1007 * "is_end" will be called on it. But that will crash if the
1008 * node is not actually in a sequence.
1010 * node_insert_sorted() makes sure the node is unlinked before
1013 * The reason we need the "iter" versions at all is that that
1014 * is the only kind of compare functions GtkTreeView can use.
1016 tmp_seq = g_sequence_new (NULL);
1017 tmp_seq->real_sequence = seq;
1019 new_node = g_sequence_append (tmp_seq, data);
1021 node_insert_sorted (seq->end_node, new_node,
1022 seq->end_node, iter_cmp, cmp_data);
1024 g_sequence_free (tmp_seq);
1026 seq->access_prohibited = FALSE;
1032 * g_sequence_search_iter:
1033 * @seq: a #GSequence
1034 * @data: data for the new item
1035 * @iter_cmp: the function used to compare iterators in the sequence
1036 * @cmp_data: user data passed to @iter_cmp
1038 * Like g_sequence_search(), but uses a #GSequenceIterCompareFunc
1039 * instead of a #GCompareDataFunc as the compare function.
1041 * @iter_cmp is called with two iterators pointing into @seq.
1042 * It should return 0 if the iterators are equal, a negative value
1043 * if the first iterator comes before the second, and a positive
1044 * value if the second iterator comes before the first.
1046 * If you are simply searching for an existing element of the sequence,
1047 * consider using g_sequence_lookup_iter().
1049 * This function will fail if the data contained in the sequence is
1050 * unsorted. Use g_sequence_insert_sorted() or
1051 * g_sequence_insert_sorted_iter() to add data to your sequence or, if
1052 * you want to add a large amount of data, call g_sequence_sort() after
1053 * doing unsorted insertions.
1055 * Return value: a #GSequenceIter pointing to the position in @seq
1056 * where @data would have been inserted according to @iter_cmp
1062 g_sequence_search_iter (GSequence *seq,
1064 GSequenceIterCompareFunc iter_cmp,
1067 GSequenceNode *node;
1068 GSequenceNode *dummy;
1071 g_return_val_if_fail (seq != NULL, NULL);
1073 check_seq_access (seq);
1075 seq->access_prohibited = TRUE;
1077 tmp_seq = g_sequence_new (NULL);
1078 tmp_seq->real_sequence = seq;
1080 dummy = g_sequence_append (tmp_seq, data);
1082 node = node_find_closest (seq->end_node, dummy,
1083 seq->end_node, iter_cmp, cmp_data);
1085 g_sequence_free (tmp_seq);
1087 seq->access_prohibited = FALSE;
1093 * g_sequence_lookup_iter:
1094 * @seq: a #GSequence
1095 * @data: data to lookup
1096 * @iter_cmp: the function used to compare iterators in the sequence
1097 * @cmp_data: user data passed to @iter_cmp
1099 * Like g_sequence_lookup(), but uses a #GSequenceIterCompareFunc
1100 * instead of a #GCompareDataFunc as the compare function.
1102 * @iter_cmp is called with two iterators pointing into @seq.
1103 * It should return 0 if the iterators are equal, a negative value
1104 * if the first iterator comes before the second, and a positive
1105 * value if the second iterator comes before the first.
1107 * This function will fail if the data contained in the sequence is
1108 * unsorted. Use g_sequence_insert_sorted() or
1109 * g_sequence_insert_sorted_iter() to add data to your sequence or, if
1110 * you want to add a large amount of data, call g_sequence_sort() after
1111 * doing unsorted insertions.
1113 * Return value: an #GSequenceIter pointing to the position of
1114 * the first item found equal to @data according to @cmp_func
1115 * and @cmp_data, or %NULL if no such item exists
1120 g_sequence_lookup_iter (GSequence *seq,
1122 GSequenceIterCompareFunc iter_cmp,
1125 GSequenceNode *node;
1126 GSequenceNode *dummy;
1129 g_return_val_if_fail (seq != NULL, NULL);
1131 check_seq_access (seq);
1133 seq->access_prohibited = TRUE;
1135 tmp_seq = g_sequence_new (NULL);
1136 tmp_seq->real_sequence = seq;
1138 dummy = g_sequence_append (tmp_seq, data);
1140 node = node_find (seq->end_node, dummy,
1141 seq->end_node, iter_cmp, cmp_data);
1143 g_sequence_free (tmp_seq);
1145 seq->access_prohibited = FALSE;
1151 * g_sequence_iter_get_sequence:
1152 * @iter: a #GSequenceIter
1154 * Returns the #GSequence that @iter points into.
1156 * Return value: the #GSequence that @iter points into
1161 g_sequence_iter_get_sequence (GSequenceIter *iter)
1165 g_return_val_if_fail (iter != NULL, NULL);
1167 seq = get_sequence (iter);
1169 /* For temporary sequences, this points to the sequence that
1170 * is actually being manipulated
1172 return seq->real_sequence;
1177 * @iter: a #GSequenceIter
1179 * Returns the data that @iter points to.
1181 * Return value: the data that @iter points to
1186 g_sequence_get (GSequenceIter *iter)
1188 g_return_val_if_fail (iter != NULL, NULL);
1189 g_return_val_if_fail (!is_end (iter), NULL);
1196 * @iter: a #GSequenceIter
1197 * @data: new data for the item
1199 * Changes the data for the item pointed to by @iter to be @data. If
1200 * the sequence has a data destroy function associated with it, that
1201 * function is called on the existing data that @iter pointed to.
1206 g_sequence_set (GSequenceIter *iter,
1211 g_return_if_fail (iter != NULL);
1212 g_return_if_fail (!is_end (iter));
1214 seq = get_sequence (iter);
1216 /* If @data is identical to iter->data, it is destroyed
1217 * here. This will work right in case of ref-counted objects. Also
1218 * it is similar to what ghashtables do.
1220 * For non-refcounted data it's a little less convenient, but
1221 * code relying on self-setting not destroying would be
1222 * pretty dubious anyway ...
1225 if (seq->data_destroy_notify)
1226 seq->data_destroy_notify (iter->data);
1232 * g_sequence_get_length:
1233 * @seq: a #GSequence
1235 * Returns the length of @seq
1237 * Return value: the length of @seq
1242 g_sequence_get_length (GSequence *seq)
1244 return node_get_length (seq->end_node) - 1;
1248 * g_sequence_get_end_iter:
1249 * @seq: a #GSequence
1251 * Returns the end iterator for @seg
1253 * Return value: the end iterator for @seq
1258 g_sequence_get_end_iter (GSequence *seq)
1260 g_return_val_if_fail (seq != NULL, NULL);
1262 return seq->end_node;
1266 * g_sequence_get_begin_iter:
1267 * @seq: a #GSequence
1269 * Returns the begin iterator for @seq.
1271 * Return value: the begin iterator for @seq.
1276 g_sequence_get_begin_iter (GSequence *seq)
1278 g_return_val_if_fail (seq != NULL, NULL);
1280 return node_get_first (seq->end_node);
1284 clamp_position (GSequence *seq,
1287 gint len = g_sequence_get_length (seq);
1289 if (pos > len || pos < 0)
1296 * if pos > number of items or -1, will return end pointer
1299 * g_sequence_get_iter_at_pos:
1300 * @seq: a #GSequence
1301 * @pos: a position in @seq, or -1 for the end
1303 * Returns the iterator at position @pos. If @pos is negative or larger
1304 * than the number of items in @seq, the end iterator is returned.
1306 * Return value: The #GSequenceIter at position @pos
1311 g_sequence_get_iter_at_pos (GSequence *seq,
1314 g_return_val_if_fail (seq != NULL, NULL);
1316 pos = clamp_position (seq, pos);
1318 return node_get_by_pos (seq->end_node, pos);
1323 * @src: a #GSequenceIter pointing to the item to move
1324 * @dest: a #GSequenceIter pointing to the position to which
1327 * Moves the item pointed to by @src to the position indicated by @dest.
1328 * After calling this function @dest will point to the position immediately
1329 * after @src. It is allowed for @src and @dest to point into different
1335 g_sequence_move (GSequenceIter *src,
1336 GSequenceIter *dest)
1338 g_return_if_fail (src != NULL);
1339 g_return_if_fail (dest != NULL);
1340 g_return_if_fail (!is_end (src));
1346 node_insert_before (dest, src);
1352 * g_sequence_iter_is_end:
1353 * @iter: a #GSequenceIter
1355 * Returns whether @iter is the end iterator
1357 * Return value: Whether @iter is the end iterator
1362 g_sequence_iter_is_end (GSequenceIter *iter)
1364 g_return_val_if_fail (iter != NULL, FALSE);
1366 return is_end (iter);
1370 * g_sequence_iter_is_begin:
1371 * @iter: a #GSequenceIter
1373 * Returns whether @iter is the begin iterator
1375 * Return value: whether @iter is the begin iterator
1380 g_sequence_iter_is_begin (GSequenceIter *iter)
1382 g_return_val_if_fail (iter != NULL, FALSE);
1384 return (node_get_prev (iter) == iter);
1388 * g_sequence_iter_get_position:
1389 * @iter: a #GSequenceIter
1391 * Returns the position of @iter
1393 * Return value: the position of @iter
1398 g_sequence_iter_get_position (GSequenceIter *iter)
1400 g_return_val_if_fail (iter != NULL, -1);
1402 return node_get_pos (iter);
1406 * g_sequence_iter_next:
1407 * @iter: a #GSequenceIter
1409 * Returns an iterator pointing to the next position after @iter.
1410 * If @iter is the end iterator, the end iterator is returned.
1412 * Return value: a #GSequenceIter pointing to the next position after @iter
1417 g_sequence_iter_next (GSequenceIter *iter)
1419 g_return_val_if_fail (iter != NULL, NULL);
1421 return node_get_next (iter);
1425 * g_sequence_iter_prev:
1426 * @iter: a #GSequenceIter
1428 * Returns an iterator pointing to the previous position before @iter.
1429 * If @iter is the begin iterator, the begin iterator is returned.
1431 * Return value: a #GSequenceIter pointing to the previous position
1437 g_sequence_iter_prev (GSequenceIter *iter)
1439 g_return_val_if_fail (iter != NULL, NULL);
1441 return node_get_prev (iter);
1445 * g_sequence_iter_move:
1446 * @iter: a #GSequenceIter
1447 * @delta: A positive or negative number indicating how many positions away
1448 * from @iter the returned #GSequenceIter will be
1450 * Returns the #GSequenceIter which is @delta positions away from @iter.
1451 * If @iter is closer than -@delta positions to the beginning of the sequence,
1452 * the begin iterator is returned. If @iter is closer than @delta positions
1453 * to the end of the sequence, the end iterator is returned.
1455 * Return value: a #GSequenceIter which is @delta positions away from @iter
1460 g_sequence_iter_move (GSequenceIter *iter,
1466 g_return_val_if_fail (iter != NULL, NULL);
1468 len = g_sequence_get_length (get_sequence (iter));
1470 new_pos = node_get_pos (iter) + delta;
1474 else if (new_pos > len)
1477 return node_get_by_pos (iter, new_pos);
1482 * @a: a #GSequenceIter
1483 * @b: a #GSequenceIter
1485 * Swaps the items pointed to by @a and @b. It is allowed for @a and @b
1486 * to point into difference sequences.
1491 g_sequence_swap (GSequenceIter *a,
1494 GSequenceNode *leftmost, *rightmost, *rightmost_next;
1497 g_return_if_fail (!g_sequence_iter_is_end (a));
1498 g_return_if_fail (!g_sequence_iter_is_end (b));
1503 a_pos = g_sequence_iter_get_position (a);
1504 b_pos = g_sequence_iter_get_position (b);
1517 rightmost_next = node_get_next (rightmost);
1519 /* The situation is now like this:
1521 * ..., leftmost, ......., rightmost, rightmost_next, ...
1524 g_sequence_move (rightmost, leftmost);
1525 g_sequence_move (leftmost, rightmost_next);
1529 * Implementation of a treap
1534 get_priority (GSequenceNode *node)
1536 guint key = GPOINTER_TO_UINT (node);
1538 /* This hash function is based on one found on Thomas Wang's
1541 * http://www.concentric.net/~Ttwang/tech/inthash.htm
1544 key = (key << 15) - key - 1;
1545 key = key ^ (key >> 12);
1546 key = key + (key << 2);
1547 key = key ^ (key >> 4);
1548 key = key + (key << 3) + (key << 11);
1549 key = key ^ (key >> 16);
1551 /* We rely on 0 being less than all other priorities */
1552 return key? key : 1;
1555 static GSequenceNode *
1556 find_root (GSequenceNode *node)
1558 while (node->parent)
1559 node = node->parent;
1564 static GSequenceNode *
1565 node_new (gpointer data)
1567 GSequenceNode *node = g_slice_new0 (GSequenceNode);
1573 node->parent = NULL;
1578 static GSequenceNode *
1579 node_get_first (GSequenceNode *node)
1581 node = find_root (node);
1589 static GSequenceNode *
1590 node_get_last (GSequenceNode *node)
1592 node = find_root (node);
1600 #define NODE_LEFT_CHILD(n) (((n)->parent) && ((n)->parent->left) == (n))
1601 #define NODE_RIGHT_CHILD(n) (((n)->parent) && ((n)->parent->right) == (n))
1603 static GSequenceNode *
1604 node_get_next (GSequenceNode *node)
1606 GSequenceNode *n = node;
1616 while (NODE_RIGHT_CHILD (n))
1628 static GSequenceNode *
1629 node_get_prev (GSequenceNode *node)
1631 GSequenceNode *n = node;
1641 while (NODE_LEFT_CHILD (n))
1653 #define N_NODES(n) ((n)? (n)->n_nodes : 0)
1656 node_get_pos (GSequenceNode *node)
1661 n_smaller = node->left->n_nodes;
1665 if (NODE_RIGHT_CHILD (node))
1666 n_smaller += N_NODES (node->parent->left) + 1;
1668 node = node->parent;
1674 static GSequenceNode *
1675 node_get_by_pos (GSequenceNode *node,
1680 node = find_root (node);
1682 while ((i = N_NODES (node->left)) != pos)
1698 static GSequenceNode *
1699 node_find (GSequenceNode *haystack,
1700 GSequenceNode *needle,
1702 GSequenceIterCompareFunc iter_cmp,
1707 haystack = find_root (haystack);
1711 /* iter_cmp can't be passed the end node, since the function may
1714 if (haystack == end)
1717 c = iter_cmp (haystack, needle, cmp_data);
1723 haystack = haystack->left;
1725 haystack = haystack->right;
1727 while (haystack != NULL);
1732 static GSequenceNode *
1733 node_find_closest (GSequenceNode *haystack,
1734 GSequenceNode *needle,
1736 GSequenceIterCompareFunc iter_cmp,
1739 GSequenceNode *best;
1742 haystack = find_root (haystack);
1748 /* iter_cmp can't be passed the end node, since the function may
1751 if (haystack == end)
1754 c = iter_cmp (haystack, needle, cmp_data);
1756 /* In the following we don't break even if c == 0. Instead we go on
1757 * searching along the 'bigger' nodes, so that we find the last one
1758 * that is equal to the needle.
1761 haystack = haystack->left;
1763 haystack = haystack->right;
1765 while (haystack != NULL);
1767 /* If the best node is smaller or equal to the data, then move one step
1768 * to the right to make sure the best one is strictly bigger than the data
1770 if (best != end && c <= 0)
1771 best = node_get_next (best);
1777 node_get_length (GSequenceNode *node)
1779 node = find_root (node);
1781 return node->n_nodes;
1785 real_node_free (GSequenceNode *node,
1790 real_node_free (node->left, seq);
1791 real_node_free (node->right, seq);
1793 if (seq && seq->data_destroy_notify && node != seq->end_node)
1794 seq->data_destroy_notify (node->data);
1796 g_slice_free (GSequenceNode, node);
1801 node_free (GSequenceNode *node,
1804 node = find_root (node);
1806 real_node_free (node, seq);
1810 node_update_fields (GSequenceNode *node)
1814 n_nodes += N_NODES (node->left);
1815 n_nodes += N_NODES (node->right);
1817 node->n_nodes = n_nodes;
1821 node_rotate (GSequenceNode *node)
1823 GSequenceNode *tmp, *old;
1825 g_assert (node->parent);
1826 g_assert (node->parent != node);
1828 if (NODE_LEFT_CHILD (node))
1833 node->right = node->parent;
1834 node->parent = node->parent->parent;
1837 if (node->parent->left == node->right)
1838 node->parent->left = node;
1840 node->parent->right = node;
1843 g_assert (node->right);
1845 node->right->parent = node;
1846 node->right->left = tmp;
1848 if (node->right->left)
1849 node->right->left->parent = node->right;
1858 node->left = node->parent;
1859 node->parent = node->parent->parent;
1862 if (node->parent->right == node->left)
1863 node->parent->right = node;
1865 node->parent->left = node;
1868 g_assert (node->left);
1870 node->left->parent = node;
1871 node->left->right = tmp;
1873 if (node->left->right)
1874 node->left->right->parent = node->left;
1879 node_update_fields (old);
1880 node_update_fields (node);
1884 node_update_fields_deep (GSequenceNode *node)
1888 node_update_fields (node);
1890 node_update_fields_deep (node->parent);
1895 rotate_down (GSequenceNode *node,
1900 left = node->left ? get_priority (node->left) : 0;
1901 right = node->right ? get_priority (node->right) : 0;
1903 while (priority < left || priority < right)
1906 node_rotate (node->left);
1908 node_rotate (node->right);
1910 left = node->left ? get_priority (node->left) : 0;
1911 right = node->right ? get_priority (node->right) : 0;
1916 node_cut (GSequenceNode *node)
1918 while (node->parent)
1922 node->left->parent = NULL;
1925 node_update_fields (node);
1927 rotate_down (node, get_priority (node));
1931 node_join (GSequenceNode *left,
1932 GSequenceNode *right)
1934 GSequenceNode *fake = node_new (NULL);
1936 fake->left = find_root (left);
1937 fake->right = find_root (right);
1938 fake->left->parent = fake;
1939 fake->right->parent = fake;
1941 node_update_fields (fake);
1945 node_free (fake, NULL);
1949 node_insert_before (GSequenceNode *node,
1952 new->left = node->left;
1954 new->left->parent = new;
1959 node_update_fields_deep (new);
1961 while (new->parent && get_priority (new) > get_priority (new->parent))
1964 rotate_down (new, get_priority (new));
1968 node_unlink (GSequenceNode *node)
1970 rotate_down (node, 0);
1972 if (NODE_RIGHT_CHILD (node))
1973 node->parent->right = NULL;
1974 else if (NODE_LEFT_CHILD (node))
1975 node->parent->left = NULL;
1978 node_update_fields_deep (node->parent);
1980 node->parent = NULL;
1984 node_insert_sorted (GSequenceNode *node,
1987 GSequenceIterCompareFunc iter_cmp,
1990 GSequenceNode *closest;
1992 closest = node_find_closest (node, new, end, iter_cmp, cmp_data);
1996 node_insert_before (closest, new);