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.1 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 * most of the operations (access, search, insertion, deletion, ...) on
34 * #GSequence are O(log(n)) in average and O(n) in worst case for time
35 * complexity. But, note that maintaining a balanced sorted list of n
36 * elements is done in time O(n log(n)).
37 * The data contained in each element can be either integer values, by using
38 * of the [Type Conversion Macros][glib-Type-Conversion-Macros], or simply
39 * pointers to any type of data.
41 * A #GSequence is accessed through "iterators", represented by a
42 * #GSequenceIter. An iterator represents a position between two
43 * elements of the sequence. For example, the "begin" iterator
44 * represents the gap immediately before the first element of the
45 * sequence, and the "end" iterator represents the gap immediately
46 * after the last element. In an empty sequence, the begin and end
47 * iterators are the same.
49 * Some methods on #GSequence operate on ranges of items. For example
50 * g_sequence_foreach_range() will call a user-specified function on
51 * each element with the given range. The range is delimited by the
52 * gaps represented by the passed-in iterators, so if you pass in the
53 * begin and end iterators, the range in question is the entire
56 * The function g_sequence_get() is used with an iterator to access the
57 * element immediately following the gap that the iterator represents.
58 * The iterator is said to "point" to that element.
60 * Iterators are stable across most operations on a #GSequence. For
61 * example an iterator pointing to some element of a sequence will
62 * continue to point to that element even after the sequence is sorted.
63 * Even moving an element to another sequence using for example
64 * g_sequence_move_range() will not invalidate the iterators pointing
65 * to it. The only operation that will invalidate an iterator is when
66 * the element it points to is removed from any sequence.
68 * To sort the data, either use g_sequence_insert_sorted() or
69 * g_sequence_insert_sorted_iter() to add data to the #GSequence or, if
70 * you want to add a large amount of data, it is more efficient to call
71 * g_sequence_sort() or g_sequence_sort_iter() after doing unsorted
78 * The #GSequenceIter struct is an opaque data type representing an
79 * iterator pointing into a #GSequence.
83 * GSequenceIterCompareFunc:
84 * @a: a #GSequenceIter
85 * @b: a #GSequenceIter
88 * A #GSequenceIterCompareFunc is a function used to compare iterators.
89 * It must return zero if the iterators compare equal, a negative value
90 * if @a comes before @b, and a positive value if @b comes before @a.
92 * Returns: zero if the iterators are equal, a negative value if @a
93 * comes before @b, and a positive value if @b comes before @a.
96 typedef struct _GSequenceNode GSequenceNode;
101 * The #GSequence struct is an opaque data type representing a
102 * [sequence][glib-Sequences] data type.
106 GSequenceNode * end_node;
107 GDestroyNotify data_destroy_notify;
108 gboolean access_prohibited;
110 /* The 'real_sequence' is used when temporary sequences are created
111 * to hold nodes that are being rearranged. The 'real_sequence' of such
112 * a temporary sequence points to the sequence that is actually being
113 * manipulated. The only reason we need this is so that when the
114 * sort/sort_changed/search_iter() functions call out to the application
115 * g_sequence_iter_get_sequence() will return the correct sequence.
117 GSequence * real_sequence;
120 struct _GSequenceNode
123 GSequenceNode * parent;
124 GSequenceNode * left;
125 GSequenceNode * right;
126 gpointer data; /* For the end node, this field points
132 * Declaration of GSequenceNode methods
134 static GSequenceNode *node_new (gpointer data);
135 static GSequenceNode *node_get_first (GSequenceNode *node);
136 static GSequenceNode *node_get_last (GSequenceNode *node);
137 static GSequenceNode *node_get_prev (GSequenceNode *node);
138 static GSequenceNode *node_get_next (GSequenceNode *node);
139 static gint node_get_pos (GSequenceNode *node);
140 static GSequenceNode *node_get_by_pos (GSequenceNode *node,
142 static GSequenceNode *node_find (GSequenceNode *haystack,
143 GSequenceNode *needle,
145 GSequenceIterCompareFunc cmp,
147 static GSequenceNode *node_find_closest (GSequenceNode *haystack,
148 GSequenceNode *needle,
150 GSequenceIterCompareFunc cmp,
152 static gint node_get_length (GSequenceNode *node);
153 static void node_free (GSequenceNode *node,
155 static void node_cut (GSequenceNode *split);
156 static void node_insert_before (GSequenceNode *node,
158 static void node_unlink (GSequenceNode *node);
159 static void node_join (GSequenceNode *left,
160 GSequenceNode *right);
161 static void node_insert_sorted (GSequenceNode *node,
164 GSequenceIterCompareFunc cmp_func,
169 * Various helper functions
172 check_seq_access (GSequence *seq)
174 if (G_UNLIKELY (seq->access_prohibited))
176 g_warning ("Accessing a sequence while it is "
177 "being sorted or searched is not allowed");
182 get_sequence (GSequenceNode *node)
184 return (GSequence *)node_get_last (node)->data;
188 seq_is_end (GSequence *seq,
191 return seq->end_node == iter;
195 is_end (GSequenceIter *iter)
197 GSequenceIter *parent = iter->parent;
205 while (parent->right == iter)
208 parent = iter->parent;
219 GCompareDataFunc cmp_func;
221 GSequenceNode *end_node;
224 /* This function compares two iters using a normal compare
225 * function and user_data passed in in a SortInfo struct
228 iter_compare (GSequenceIter *node1,
229 GSequenceIter *node2,
232 const SortInfo *info = data;
235 if (node1 == info->end_node)
238 if (node2 == info->end_node)
241 retval = info->cmp_func (node1->data, node2->data, info->cmp_data);
252 * @data_destroy: (nullable): a #GDestroyNotify function, or %NULL
254 * Creates a new GSequence. The @data_destroy function, if non-%NULL will
255 * be called on all items when the sequence is destroyed and on items that
256 * are removed from the sequence.
258 * Returns: (transfer full): a new #GSequence
263 g_sequence_new (GDestroyNotify data_destroy)
265 GSequence *seq = g_new (GSequence, 1);
266 seq->data_destroy_notify = data_destroy;
268 seq->end_node = node_new (seq);
270 seq->access_prohibited = FALSE;
272 seq->real_sequence = seq;
281 * Frees the memory allocated for @seq. If @seq has a data destroy
282 * function associated with it, that function is called on all items
288 g_sequence_free (GSequence *seq)
290 g_return_if_fail (seq != NULL);
292 check_seq_access (seq);
294 node_free (seq->end_node, seq);
300 * g_sequence_foreach_range:
301 * @begin: a #GSequenceIter
302 * @end: a #GSequenceIter
304 * @user_data: user data passed to @func
306 * Calls @func for each item in the range (@begin, @end) passing
307 * @user_data to the function. @func must not modify the sequence
313 g_sequence_foreach_range (GSequenceIter *begin,
321 g_return_if_fail (func != NULL);
322 g_return_if_fail (begin != NULL);
323 g_return_if_fail (end != NULL);
325 seq = get_sequence (begin);
327 seq->access_prohibited = TRUE;
332 GSequenceIter *next = node_get_next (iter);
334 func (iter->data, user_data);
339 seq->access_prohibited = FALSE;
343 * g_sequence_foreach:
345 * @func: the function to call for each item in @seq
346 * @user_data: user data passed to @func
348 * Calls @func for each item in the sequence passing @user_data
349 * to the function. @func must not modify the sequence itself.
354 g_sequence_foreach (GSequence *seq,
358 GSequenceIter *begin, *end;
360 check_seq_access (seq);
362 begin = g_sequence_get_begin_iter (seq);
363 end = g_sequence_get_end_iter (seq);
365 g_sequence_foreach_range (begin, end, func, user_data);
369 * g_sequence_range_get_midpoint:
370 * @begin: a #GSequenceIter
371 * @end: a #GSequenceIter
373 * Finds an iterator somewhere in the range (@begin, @end). This
374 * iterator will be close to the middle of the range, but is not
375 * guaranteed to be exactly in the middle.
377 * The @begin and @end iterators must both point to the same sequence
378 * and @begin must come before or be equal to @end in the sequence.
380 * Returns: (transfer none): a #GSequenceIter pointing somewhere in the
381 * (@begin, @end) range
386 g_sequence_range_get_midpoint (GSequenceIter *begin,
389 int begin_pos, end_pos, mid_pos;
391 g_return_val_if_fail (begin != NULL, NULL);
392 g_return_val_if_fail (end != NULL, NULL);
393 g_return_val_if_fail (get_sequence (begin) == get_sequence (end), NULL);
395 begin_pos = node_get_pos (begin);
396 end_pos = node_get_pos (end);
398 g_return_val_if_fail (end_pos >= begin_pos, NULL);
400 mid_pos = begin_pos + (end_pos - begin_pos) / 2;
402 return node_get_by_pos (begin, mid_pos);
406 * g_sequence_iter_compare:
407 * @a: a #GSequenceIter
408 * @b: a #GSequenceIter
410 * Returns a negative number if @a comes before @b, 0 if they are equal,
411 * and a positive number if @a comes after @b.
413 * The @a and @b iterators must point into the same sequence.
415 * Returns: a negative number if @a comes before @b, 0 if they are
416 * equal, and a positive number if @a comes after @b
421 g_sequence_iter_compare (GSequenceIter *a,
425 GSequence *seq_a, *seq_b;
427 g_return_val_if_fail (a != NULL, 0);
428 g_return_val_if_fail (b != NULL, 0);
430 seq_a = get_sequence (a);
431 seq_b = get_sequence (b);
432 g_return_val_if_fail (seq_a == seq_b, 0);
434 check_seq_access (seq_a);
435 check_seq_access (seq_b);
437 a_pos = node_get_pos (a);
438 b_pos = node_get_pos (b);
442 else if (a_pos > b_pos)
451 * @data: the data for the new item
453 * Adds a new item to the end of @seq.
455 * Returns: (transfer none): an iterator pointing to the new item
460 g_sequence_append (GSequence *seq,
465 g_return_val_if_fail (seq != NULL, NULL);
467 check_seq_access (seq);
469 node = node_new (data);
470 node_insert_before (seq->end_node, node);
476 * g_sequence_prepend:
478 * @data: the data for the new item
480 * Adds a new item to the front of @seq
482 * Returns: (transfer none): an iterator pointing to the new item
487 g_sequence_prepend (GSequence *seq,
490 GSequenceNode *node, *first;
492 g_return_val_if_fail (seq != NULL, NULL);
494 check_seq_access (seq);
496 node = node_new (data);
497 first = node_get_first (seq->end_node);
499 node_insert_before (first, node);
505 * g_sequence_insert_before:
506 * @iter: a #GSequenceIter
507 * @data: the data for the new item
509 * Inserts a new item just before the item pointed to by @iter.
511 * Returns: (transfer none): an iterator pointing to the new item
516 g_sequence_insert_before (GSequenceIter *iter,
522 g_return_val_if_fail (iter != NULL, NULL);
524 seq = get_sequence (iter);
525 check_seq_access (seq);
527 node = node_new (data);
529 node_insert_before (iter, node);
536 * @iter: a #GSequenceIter
538 * Removes the item pointed to by @iter. It is an error to pass the
539 * end iterator to this function.
541 * If the sequence has a data destroy function associated with it, this
542 * function is called on the data for the removed item.
547 g_sequence_remove (GSequenceIter *iter)
551 g_return_if_fail (iter != NULL);
553 seq = get_sequence (iter);
554 g_return_if_fail (!seq_is_end (seq, iter));
556 check_seq_access (seq);
559 node_free (iter, seq);
563 * g_sequence_remove_range:
564 * @begin: a #GSequenceIter
565 * @end: a #GSequenceIter
567 * Removes all items in the (@begin, @end) range.
569 * If the sequence has a data destroy function associated with it, this
570 * function is called on the data for the removed items.
575 g_sequence_remove_range (GSequenceIter *begin,
578 GSequence *seq_begin, *seq_end;
580 seq_begin = get_sequence (begin);
581 seq_end = get_sequence (end);
582 g_return_if_fail (seq_begin == seq_end);
583 /* check_seq_access() calls are done by g_sequence_move_range() */
585 g_sequence_move_range (NULL, begin, end);
589 * g_sequence_move_range:
590 * @dest: a #GSequenceIter
591 * @begin: a #GSequenceIter
592 * @end: a #GSequenceIter
594 * Inserts the (@begin, @end) range at the destination pointed to by @dest.
595 * The @begin and @end iters must point into the same sequence. It is
596 * allowed for @dest to point to a different sequence than the one pointed
597 * into by @begin and @end.
599 * If @dest is %NULL, the range indicated by @begin and @end is
600 * removed from the sequence. If @dest points to a place within
601 * the (@begin, @end) range, the range does not move.
606 g_sequence_move_range (GSequenceIter *dest,
607 GSequenceIter *begin,
610 GSequence *src_seq, *end_seq, *dest_seq;
611 GSequenceNode *first;
613 g_return_if_fail (begin != NULL);
614 g_return_if_fail (end != NULL);
616 src_seq = get_sequence (begin);
617 check_seq_access (src_seq);
619 end_seq = get_sequence (end);
620 check_seq_access (end_seq);
624 dest_seq = get_sequence (dest);
625 check_seq_access (dest_seq);
628 g_return_if_fail (src_seq == end_seq);
630 /* Dest points to begin or end? */
631 if (dest == begin || dest == end)
634 /* begin comes after end? */
635 if (g_sequence_iter_compare (begin, end) >= 0)
638 /* dest points somewhere in the (begin, end) range? */
639 if (dest && dest_seq == src_seq &&
640 g_sequence_iter_compare (dest, begin) > 0 &&
641 g_sequence_iter_compare (dest, end) < 0)
646 first = node_get_first (begin);
653 node_join (first, end);
657 first = node_get_first (dest);
661 node_join (begin, dest);
664 node_join (first, begin);
668 node_free (begin, src_seq);
675 * @cmp_func: the function used to sort the sequence
676 * @cmp_data: user data passed to @cmp_func
678 * Sorts @seq using @cmp_func.
680 * @cmp_func is passed two items of @seq and should
681 * return 0 if they are equal, a negative value if the
682 * first comes before the second, and a positive value
683 * if the second comes before the first.
688 g_sequence_sort (GSequence *seq,
689 GCompareDataFunc cmp_func,
694 info.cmp_func = cmp_func;
695 info.cmp_data = cmp_data;
696 info.end_node = seq->end_node;
698 check_seq_access (seq);
700 g_sequence_sort_iter (seq, iter_compare, &info);
704 * g_sequence_insert_sorted:
706 * @data: the data to insert
707 * @cmp_func: the function used to compare items in the sequence
708 * @cmp_data: user data passed to @cmp_func.
710 * Inserts @data into @seq using @cmp_func to determine the new
711 * position. The sequence must already be sorted according to @cmp_func;
712 * otherwise the new position of @data is undefined.
714 * @cmp_func is called with two items of the @seq, and @cmp_data.
715 * It should return 0 if the items are equal, a negative value
716 * if the first item comes before the second, and a positive value
717 * if the second item comes before the first.
719 * Note that when adding a large amount of data to a #GSequence,
720 * it is more efficient to do unsorted insertions and then call
721 * g_sequence_sort() or g_sequence_sort_iter().
723 * Returns: (transfer none): a #GSequenceIter pointing to the new item.
728 g_sequence_insert_sorted (GSequence *seq,
730 GCompareDataFunc cmp_func,
735 g_return_val_if_fail (seq != NULL, NULL);
736 g_return_val_if_fail (cmp_func != NULL, NULL);
738 info.cmp_func = cmp_func;
739 info.cmp_data = cmp_data;
740 info.end_node = seq->end_node;
741 check_seq_access (seq);
743 return g_sequence_insert_sorted_iter (seq, data, iter_compare, &info);
747 * g_sequence_sort_changed:
748 * @iter: A #GSequenceIter
749 * @cmp_func: the function used to compare items in the sequence
750 * @cmp_data: user data passed to @cmp_func.
752 * Moves the data pointed to by @iter to a new position as indicated by
754 * function should be called for items in a sequence already sorted according
755 * to @cmp_func whenever some aspect of an item changes so that @cmp_func
756 * may return different values for that item.
758 * @cmp_func is called with two items of the @seq, and @cmp_data.
759 * It should return 0 if the items are equal, a negative value if
760 * the first item comes before the second, and a positive value if
761 * the second item comes before the first.
766 g_sequence_sort_changed (GSequenceIter *iter,
767 GCompareDataFunc cmp_func,
773 g_return_if_fail (iter != NULL);
775 seq = get_sequence (iter);
776 /* check_seq_access() call is done by g_sequence_sort_changed_iter() */
777 g_return_if_fail (!seq_is_end (seq, iter));
779 info.cmp_func = cmp_func;
780 info.cmp_data = cmp_data;
781 info.end_node = seq->end_node;
783 g_sequence_sort_changed_iter (iter, iter_compare, &info);
789 * @data: data for the new item
790 * @cmp_func: the function used to compare items in the sequence
791 * @cmp_data: user data passed to @cmp_func
793 * Returns an iterator pointing to the position where @data would
794 * be inserted according to @cmp_func and @cmp_data.
796 * @cmp_func is called with two items of the @seq, and @cmp_data.
797 * It should return 0 if the items are equal, a negative value if
798 * the first item comes before the second, and a positive value if
799 * the second item comes before the first.
801 * If you are simply searching for an existing element of the sequence,
802 * consider using g_sequence_lookup().
804 * This function will fail if the data contained in the sequence is
807 * Returns: (transfer none): an #GSequenceIter pointing to the position where @data
808 * would have been inserted according to @cmp_func and @cmp_data
813 g_sequence_search (GSequence *seq,
815 GCompareDataFunc cmp_func,
820 g_return_val_if_fail (seq != NULL, NULL);
822 info.cmp_func = cmp_func;
823 info.cmp_data = cmp_data;
824 info.end_node = seq->end_node;
825 check_seq_access (seq);
827 return g_sequence_search_iter (seq, data, iter_compare, &info);
833 * @data: data to look up
834 * @cmp_func: the function used to compare items in the sequence
835 * @cmp_data: user data passed to @cmp_func
837 * Returns an iterator pointing to the position of the first item found
838 * equal to @data according to @cmp_func and @cmp_data. If more than one
839 * item is equal, it is not guaranteed that it is the first which is
840 * returned. In that case, you can use g_sequence_iter_next() and
841 * g_sequence_iter_prev() to get others.
843 * @cmp_func is called with two items of the @seq, and @cmp_data.
844 * It should return 0 if the items are equal, a negative value if
845 * the first item comes before the second, and a positive value if
846 * the second item comes before the first.
848 * This function will fail if the data contained in the sequence is
851 * Returns: (transfer none) (nullable): an #GSequenceIter pointing to the position of the
852 * first item found equal to @data according to @cmp_func and
853 * @cmp_data, or %NULL if no such item exists
858 g_sequence_lookup (GSequence *seq,
860 GCompareDataFunc cmp_func,
865 g_return_val_if_fail (seq != NULL, NULL);
867 info.cmp_func = cmp_func;
868 info.cmp_data = cmp_data;
869 info.end_node = seq->end_node;
870 check_seq_access (seq);
872 return g_sequence_lookup_iter (seq, data, iter_compare, &info);
876 * g_sequence_sort_iter:
878 * @cmp_func: the function used to compare iterators in the sequence
879 * @cmp_data: user data passed to @cmp_func
881 * Like g_sequence_sort(), but uses a #GSequenceIterCompareFunc instead
882 * of a #GCompareDataFunc as the compare function
884 * @cmp_func is called with two iterators pointing into @seq. It should
885 * return 0 if the iterators are equal, a negative value if the first
886 * iterator comes before the second, and a positive value if the second
887 * iterator comes before the first.
892 g_sequence_sort_iter (GSequence *seq,
893 GSequenceIterCompareFunc cmp_func,
897 GSequenceNode *begin, *end;
899 g_return_if_fail (seq != NULL);
900 g_return_if_fail (cmp_func != NULL);
902 check_seq_access (seq);
904 begin = g_sequence_get_begin_iter (seq);
905 end = g_sequence_get_end_iter (seq);
907 tmp = g_sequence_new (NULL);
908 tmp->real_sequence = seq;
910 g_sequence_move_range (g_sequence_get_begin_iter (tmp), begin, end);
912 seq->access_prohibited = TRUE;
913 tmp->access_prohibited = TRUE;
915 while (!g_sequence_is_empty (tmp))
917 GSequenceNode *node = g_sequence_get_begin_iter (tmp);
919 node_insert_sorted (seq->end_node, node, seq->end_node,
923 tmp->access_prohibited = FALSE;
924 seq->access_prohibited = FALSE;
926 g_sequence_free (tmp);
930 * g_sequence_sort_changed_iter:
931 * @iter: a #GSequenceIter
932 * @iter_cmp: the function used to compare iterators in the sequence
933 * @cmp_data: user data passed to @cmp_func
935 * Like g_sequence_sort_changed(), but uses
936 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
937 * the compare function.
939 * @iter_cmp is called with two iterators pointing into the #GSequence that
940 * @iter points into. It should
941 * return 0 if the iterators are equal, a negative value if the first
942 * iterator comes before the second, and a positive value if the second
943 * iterator comes before the first.
948 g_sequence_sort_changed_iter (GSequenceIter *iter,
949 GSequenceIterCompareFunc iter_cmp,
952 GSequence *seq, *tmp_seq;
953 GSequenceIter *next, *prev;
955 g_return_if_fail (iter != NULL);
956 g_return_if_fail (iter_cmp != NULL);
958 seq = get_sequence (iter);
959 g_return_if_fail (!seq_is_end (seq, iter));
961 check_seq_access (seq);
963 /* If one of the neighbours is equal to iter, then
964 * don't move it. This ensures that sort_changed() is
965 * a stable operation.
968 next = node_get_next (iter);
969 prev = node_get_prev (iter);
971 if (prev != iter && iter_cmp (prev, iter, cmp_data) == 0)
974 if (!is_end (next) && iter_cmp (next, iter, cmp_data) == 0)
977 seq->access_prohibited = TRUE;
979 tmp_seq = g_sequence_new (NULL);
980 tmp_seq->real_sequence = seq;
983 node_insert_before (tmp_seq->end_node, iter);
985 node_insert_sorted (seq->end_node, iter, seq->end_node,
988 g_sequence_free (tmp_seq);
990 seq->access_prohibited = FALSE;
994 * g_sequence_insert_sorted_iter:
996 * @data: data for the new item
997 * @iter_cmp: the function used to compare iterators in the sequence
998 * @cmp_data: user data passed to @iter_cmp
1000 * Like g_sequence_insert_sorted(), but uses
1001 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
1002 * the compare function.
1004 * @iter_cmp is called with two iterators pointing into @seq.
1005 * It should return 0 if the iterators are equal, a negative
1006 * value if the first iterator comes before the second, and a
1007 * positive value if the second iterator comes before the first.
1009 * Note that when adding a large amount of data to a #GSequence,
1010 * it is more efficient to do unsorted insertions and then call
1011 * g_sequence_sort() or g_sequence_sort_iter().
1013 * Returns: (transfer none): a #GSequenceIter pointing to the new item
1018 g_sequence_insert_sorted_iter (GSequence *seq,
1020 GSequenceIterCompareFunc iter_cmp,
1023 GSequenceNode *new_node;
1026 g_return_val_if_fail (seq != NULL, NULL);
1027 g_return_val_if_fail (iter_cmp != NULL, NULL);
1029 check_seq_access (seq);
1031 seq->access_prohibited = TRUE;
1033 /* Create a new temporary sequence and put the new node into
1034 * that. The reason for this is that the user compare function
1035 * will be called with the new node, and if it dereferences,
1036 * "is_end" will be called on it. But that will crash if the
1037 * node is not actually in a sequence.
1039 * node_insert_sorted() makes sure the node is unlinked before
1042 * The reason we need the "iter" versions at all is that that
1043 * is the only kind of compare functions GtkTreeView can use.
1045 tmp_seq = g_sequence_new (NULL);
1046 tmp_seq->real_sequence = seq;
1048 new_node = g_sequence_append (tmp_seq, data);
1050 node_insert_sorted (seq->end_node, new_node,
1051 seq->end_node, iter_cmp, cmp_data);
1053 g_sequence_free (tmp_seq);
1055 seq->access_prohibited = FALSE;
1061 * g_sequence_search_iter:
1062 * @seq: a #GSequence
1063 * @data: data for the new item
1064 * @iter_cmp: the function used to compare iterators in the sequence
1065 * @cmp_data: user data passed to @iter_cmp
1067 * Like g_sequence_search(), but uses a #GSequenceIterCompareFunc
1068 * instead of a #GCompareDataFunc as the compare function.
1070 * @iter_cmp is called with two iterators pointing into @seq.
1071 * It should return 0 if the iterators are equal, a negative value
1072 * if the first iterator comes before the second, and a positive
1073 * value if the second iterator comes before the first.
1075 * If you are simply searching for an existing element of the sequence,
1076 * consider using g_sequence_lookup_iter().
1078 * This function will fail if the data contained in the sequence is
1081 * Returns: (transfer none): a #GSequenceIter pointing to the position in @seq
1082 * where @data would have been inserted according to @iter_cmp
1088 g_sequence_search_iter (GSequence *seq,
1090 GSequenceIterCompareFunc iter_cmp,
1093 GSequenceNode *node;
1094 GSequenceNode *dummy;
1097 g_return_val_if_fail (seq != NULL, NULL);
1099 check_seq_access (seq);
1101 seq->access_prohibited = TRUE;
1103 tmp_seq = g_sequence_new (NULL);
1104 tmp_seq->real_sequence = seq;
1106 dummy = g_sequence_append (tmp_seq, data);
1108 node = node_find_closest (seq->end_node, dummy,
1109 seq->end_node, iter_cmp, cmp_data);
1111 g_sequence_free (tmp_seq);
1113 seq->access_prohibited = FALSE;
1119 * g_sequence_lookup_iter:
1120 * @seq: a #GSequence
1121 * @data: data to look up
1122 * @iter_cmp: the function used to compare iterators in the sequence
1123 * @cmp_data: user data passed to @iter_cmp
1125 * Like g_sequence_lookup(), but uses a #GSequenceIterCompareFunc
1126 * instead of a #GCompareDataFunc as the compare function.
1128 * @iter_cmp is called with two iterators pointing into @seq.
1129 * It should return 0 if the iterators are equal, a negative value
1130 * if the first iterator comes before the second, and a positive
1131 * value if the second iterator comes before the first.
1133 * This function will fail if the data contained in the sequence is
1136 * Returns: (transfer none) (nullable): an #GSequenceIter pointing to the position of
1137 * the first item found equal to @data according to @iter_cmp
1138 * and @cmp_data, or %NULL if no such item exists
1143 g_sequence_lookup_iter (GSequence *seq,
1145 GSequenceIterCompareFunc iter_cmp,
1148 GSequenceNode *node;
1149 GSequenceNode *dummy;
1152 g_return_val_if_fail (seq != NULL, NULL);
1154 check_seq_access (seq);
1156 seq->access_prohibited = TRUE;
1158 tmp_seq = g_sequence_new (NULL);
1159 tmp_seq->real_sequence = seq;
1161 dummy = g_sequence_append (tmp_seq, data);
1163 node = node_find (seq->end_node, dummy,
1164 seq->end_node, iter_cmp, cmp_data);
1166 g_sequence_free (tmp_seq);
1168 seq->access_prohibited = FALSE;
1174 * g_sequence_iter_get_sequence:
1175 * @iter: a #GSequenceIter
1177 * Returns the #GSequence that @iter points into.
1179 * Returns: (transfer none): the #GSequence that @iter points into
1184 g_sequence_iter_get_sequence (GSequenceIter *iter)
1188 g_return_val_if_fail (iter != NULL, NULL);
1190 seq = get_sequence (iter);
1192 /* For temporary sequences, this points to the sequence that
1193 * is actually being manipulated
1195 return seq->real_sequence;
1200 * @iter: a #GSequenceIter
1202 * Returns the data that @iter points to.
1204 * Returns: (transfer none): the data that @iter points to
1209 g_sequence_get (GSequenceIter *iter)
1211 g_return_val_if_fail (iter != NULL, NULL);
1212 g_return_val_if_fail (!is_end (iter), NULL);
1219 * @iter: a #GSequenceIter
1220 * @data: new data for the item
1222 * Changes the data for the item pointed to by @iter to be @data. If
1223 * the sequence has a data destroy function associated with it, that
1224 * function is called on the existing data that @iter pointed to.
1229 g_sequence_set (GSequenceIter *iter,
1234 g_return_if_fail (iter != NULL);
1236 seq = get_sequence (iter);
1237 g_return_if_fail (!seq_is_end (seq, iter));
1239 /* If @data is identical to iter->data, it is destroyed
1240 * here. This will work right in case of ref-counted objects. Also
1241 * it is similar to what ghashtables do.
1243 * For non-refcounted data it's a little less convenient, but
1244 * code relying on self-setting not destroying would be
1245 * pretty dubious anyway ...
1248 if (seq->data_destroy_notify)
1249 seq->data_destroy_notify (iter->data);
1255 * g_sequence_get_length:
1256 * @seq: a #GSequence
1258 * Returns the length of @seq. Note that this method is O(h) where `h' is the
1259 * height of the tree. It is thus more efficient to use g_sequence_is_empty()
1260 * when comparing the length to zero.
1262 * Returns: the length of @seq
1267 g_sequence_get_length (GSequence *seq)
1269 return node_get_length (seq->end_node) - 1;
1273 * g_sequence_is_empty:
1274 * @seq: a #GSequence
1276 * Returns %TRUE if the sequence contains zero items.
1278 * This function is functionally identical to checking the result of
1279 * g_sequence_get_length() being equal to zero. However this function is
1280 * implemented in O(1) running time.
1282 * Returns: %TRUE if the sequence is empty, otherwise %FALSE.
1287 g_sequence_is_empty (GSequence *seq)
1289 return (seq->end_node->parent == NULL) && (seq->end_node->left == NULL);
1293 * g_sequence_get_end_iter:
1294 * @seq: a #GSequence
1296 * Returns the end iterator for @seg
1298 * Returns: (transfer none): the end iterator for @seq
1303 g_sequence_get_end_iter (GSequence *seq)
1305 g_return_val_if_fail (seq != NULL, NULL);
1307 return seq->end_node;
1311 * g_sequence_get_begin_iter:
1312 * @seq: a #GSequence
1314 * Returns the begin iterator for @seq.
1316 * Returns: (transfer none): the begin iterator for @seq.
1321 g_sequence_get_begin_iter (GSequence *seq)
1323 g_return_val_if_fail (seq != NULL, NULL);
1325 return node_get_first (seq->end_node);
1329 clamp_position (GSequence *seq,
1332 gint len = g_sequence_get_length (seq);
1334 if (pos > len || pos < 0)
1341 * g_sequence_get_iter_at_pos:
1342 * @seq: a #GSequence
1343 * @pos: a position in @seq, or -1 for the end
1345 * Returns the iterator at position @pos. If @pos is negative or larger
1346 * than the number of items in @seq, the end iterator is returned.
1348 * Returns: (transfer none): The #GSequenceIter at position @pos
1353 g_sequence_get_iter_at_pos (GSequence *seq,
1356 g_return_val_if_fail (seq != NULL, NULL);
1358 pos = clamp_position (seq, pos);
1360 return node_get_by_pos (seq->end_node, pos);
1365 * @src: a #GSequenceIter pointing to the item to move
1366 * @dest: a #GSequenceIter pointing to the position to which
1369 * Moves the item pointed to by @src to the position indicated by @dest.
1370 * After calling this function @dest will point to the position immediately
1371 * after @src. It is allowed for @src and @dest to point into different
1377 g_sequence_move (GSequenceIter *src,
1378 GSequenceIter *dest)
1380 g_return_if_fail (src != NULL);
1381 g_return_if_fail (dest != NULL);
1382 g_return_if_fail (!is_end (src));
1388 node_insert_before (dest, src);
1394 * g_sequence_iter_is_end:
1395 * @iter: a #GSequenceIter
1397 * Returns whether @iter is the end iterator
1399 * Returns: Whether @iter is the end iterator
1404 g_sequence_iter_is_end (GSequenceIter *iter)
1406 g_return_val_if_fail (iter != NULL, FALSE);
1408 return is_end (iter);
1412 * g_sequence_iter_is_begin:
1413 * @iter: a #GSequenceIter
1415 * Returns whether @iter is the begin iterator
1417 * Returns: whether @iter is the begin iterator
1422 g_sequence_iter_is_begin (GSequenceIter *iter)
1424 g_return_val_if_fail (iter != NULL, FALSE);
1426 return (node_get_prev (iter) == iter);
1430 * g_sequence_iter_get_position:
1431 * @iter: a #GSequenceIter
1433 * Returns the position of @iter
1435 * Returns: the position of @iter
1440 g_sequence_iter_get_position (GSequenceIter *iter)
1442 g_return_val_if_fail (iter != NULL, -1);
1444 return node_get_pos (iter);
1448 * g_sequence_iter_next:
1449 * @iter: a #GSequenceIter
1451 * Returns an iterator pointing to the next position after @iter.
1452 * If @iter is the end iterator, the end iterator is returned.
1454 * Returns: (transfer none): a #GSequenceIter pointing to the next position after @iter
1459 g_sequence_iter_next (GSequenceIter *iter)
1461 g_return_val_if_fail (iter != NULL, NULL);
1463 return node_get_next (iter);
1467 * g_sequence_iter_prev:
1468 * @iter: a #GSequenceIter
1470 * Returns an iterator pointing to the previous position before @iter.
1471 * If @iter is the begin iterator, the begin iterator is returned.
1473 * Returns: (transfer none): a #GSequenceIter pointing to the previous position
1479 g_sequence_iter_prev (GSequenceIter *iter)
1481 g_return_val_if_fail (iter != NULL, NULL);
1483 return node_get_prev (iter);
1487 * g_sequence_iter_move:
1488 * @iter: a #GSequenceIter
1489 * @delta: A positive or negative number indicating how many positions away
1490 * from @iter the returned #GSequenceIter will be
1492 * Returns the #GSequenceIter which is @delta positions away from @iter.
1493 * If @iter is closer than -@delta positions to the beginning of the sequence,
1494 * the begin iterator is returned. If @iter is closer than @delta positions
1495 * to the end of the sequence, the end iterator is returned.
1497 * Returns: (transfer none): a #GSequenceIter which is @delta positions away from @iter
1502 g_sequence_iter_move (GSequenceIter *iter,
1508 g_return_val_if_fail (iter != NULL, NULL);
1510 len = g_sequence_get_length (get_sequence (iter));
1512 new_pos = node_get_pos (iter) + delta;
1516 else if (new_pos > len)
1519 return node_get_by_pos (iter, new_pos);
1524 * @a: a #GSequenceIter
1525 * @b: a #GSequenceIter
1527 * Swaps the items pointed to by @a and @b. It is allowed for @a and @b
1528 * to point into difference sequences.
1533 g_sequence_swap (GSequenceIter *a,
1536 GSequenceNode *leftmost, *rightmost, *rightmost_next;
1539 g_return_if_fail (!g_sequence_iter_is_end (a));
1540 g_return_if_fail (!g_sequence_iter_is_end (b));
1545 a_pos = g_sequence_iter_get_position (a);
1546 b_pos = g_sequence_iter_get_position (b);
1559 rightmost_next = node_get_next (rightmost);
1561 /* The situation is now like this:
1563 * ..., leftmost, ......., rightmost, rightmost_next, ...
1566 g_sequence_move (rightmost, leftmost);
1567 g_sequence_move (leftmost, rightmost_next);
1571 * Implementation of a treap
1576 get_priority (GSequenceNode *node)
1578 guint key = GPOINTER_TO_UINT (node);
1580 /* This hash function is based on one found on Thomas Wang's
1583 * http://www.concentric.net/~Ttwang/tech/inthash.htm
1586 key = (key << 15) - key - 1;
1587 key = key ^ (key >> 12);
1588 key = key + (key << 2);
1589 key = key ^ (key >> 4);
1590 key = key + (key << 3) + (key << 11);
1591 key = key ^ (key >> 16);
1593 /* We rely on 0 being less than all other priorities */
1594 return key? key : 1;
1597 static GSequenceNode *
1598 find_root (GSequenceNode *node)
1600 while (node->parent)
1601 node = node->parent;
1606 static GSequenceNode *
1607 node_new (gpointer data)
1609 GSequenceNode *node = g_slice_new0 (GSequenceNode);
1615 node->parent = NULL;
1620 static GSequenceNode *
1621 node_get_first (GSequenceNode *node)
1623 node = find_root (node);
1631 static GSequenceNode *
1632 node_get_last (GSequenceNode *node)
1634 node = find_root (node);
1642 #define NODE_LEFT_CHILD(n) (((n)->parent) && ((n)->parent->left) == (n))
1643 #define NODE_RIGHT_CHILD(n) (((n)->parent) && ((n)->parent->right) == (n))
1645 static GSequenceNode *
1646 node_get_next (GSequenceNode *node)
1648 GSequenceNode *n = node;
1658 while (NODE_RIGHT_CHILD (n))
1670 static GSequenceNode *
1671 node_get_prev (GSequenceNode *node)
1673 GSequenceNode *n = node;
1683 while (NODE_LEFT_CHILD (n))
1695 #define N_NODES(n) ((n)? (n)->n_nodes : 0)
1698 node_get_pos (GSequenceNode *node)
1703 n_smaller = node->left->n_nodes;
1707 if (NODE_RIGHT_CHILD (node))
1708 n_smaller += N_NODES (node->parent->left) + 1;
1710 node = node->parent;
1716 static GSequenceNode *
1717 node_get_by_pos (GSequenceNode *node,
1722 node = find_root (node);
1724 while ((i = N_NODES (node->left)) != pos)
1740 static GSequenceNode *
1741 node_find (GSequenceNode *haystack,
1742 GSequenceNode *needle,
1744 GSequenceIterCompareFunc iter_cmp,
1749 haystack = find_root (haystack);
1753 /* iter_cmp can't be passed the end node, since the function may
1756 if (haystack == end)
1759 c = iter_cmp (haystack, needle, cmp_data);
1765 haystack = haystack->left;
1767 haystack = haystack->right;
1769 while (haystack != NULL);
1774 static GSequenceNode *
1775 node_find_closest (GSequenceNode *haystack,
1776 GSequenceNode *needle,
1778 GSequenceIterCompareFunc iter_cmp,
1781 GSequenceNode *best;
1784 haystack = find_root (haystack);
1790 /* iter_cmp can't be passed the end node, since the function may
1793 if (haystack == end)
1796 c = iter_cmp (haystack, needle, cmp_data);
1798 /* In the following we don't break even if c == 0. Instead we go on
1799 * searching along the 'bigger' nodes, so that we find the last one
1800 * that is equal to the needle.
1803 haystack = haystack->left;
1805 haystack = haystack->right;
1807 while (haystack != NULL);
1809 /* If the best node is smaller or equal to the data, then move one step
1810 * to the right to make sure the best one is strictly bigger than the data
1812 if (best != end && c <= 0)
1813 best = node_get_next (best);
1819 node_get_length (GSequenceNode *node)
1821 node = find_root (node);
1823 return node->n_nodes;
1827 real_node_free (GSequenceNode *node,
1832 real_node_free (node->left, seq);
1833 real_node_free (node->right, seq);
1835 if (seq && seq->data_destroy_notify && node != seq->end_node)
1836 seq->data_destroy_notify (node->data);
1838 g_slice_free (GSequenceNode, node);
1843 node_free (GSequenceNode *node,
1846 node = find_root (node);
1848 real_node_free (node, seq);
1852 node_update_fields (GSequenceNode *node)
1856 n_nodes += N_NODES (node->left);
1857 n_nodes += N_NODES (node->right);
1859 node->n_nodes = n_nodes;
1863 node_rotate (GSequenceNode *node)
1865 GSequenceNode *tmp, *old;
1867 g_assert (node->parent);
1868 g_assert (node->parent != node);
1870 if (NODE_LEFT_CHILD (node))
1875 node->right = node->parent;
1876 node->parent = node->parent->parent;
1879 if (node->parent->left == node->right)
1880 node->parent->left = node;
1882 node->parent->right = node;
1885 g_assert (node->right);
1887 node->right->parent = node;
1888 node->right->left = tmp;
1890 if (node->right->left)
1891 node->right->left->parent = node->right;
1900 node->left = node->parent;
1901 node->parent = node->parent->parent;
1904 if (node->parent->right == node->left)
1905 node->parent->right = node;
1907 node->parent->left = node;
1910 g_assert (node->left);
1912 node->left->parent = node;
1913 node->left->right = tmp;
1915 if (node->left->right)
1916 node->left->right->parent = node->left;
1921 node_update_fields (old);
1922 node_update_fields (node);
1926 node_update_fields_deep (GSequenceNode *node)
1930 node_update_fields (node);
1932 node_update_fields_deep (node->parent);
1937 rotate_down (GSequenceNode *node,
1942 left = node->left ? get_priority (node->left) : 0;
1943 right = node->right ? get_priority (node->right) : 0;
1945 while (priority < left || priority < right)
1948 node_rotate (node->left);
1950 node_rotate (node->right);
1952 left = node->left ? get_priority (node->left) : 0;
1953 right = node->right ? get_priority (node->right) : 0;
1958 node_cut (GSequenceNode *node)
1960 while (node->parent)
1964 node->left->parent = NULL;
1967 node_update_fields (node);
1969 rotate_down (node, get_priority (node));
1973 node_join (GSequenceNode *left,
1974 GSequenceNode *right)
1976 GSequenceNode *fake = node_new (NULL);
1978 fake->left = find_root (left);
1979 fake->right = find_root (right);
1980 fake->left->parent = fake;
1981 fake->right->parent = fake;
1983 node_update_fields (fake);
1987 node_free (fake, NULL);
1991 node_insert_before (GSequenceNode *node,
1994 new->left = node->left;
1996 new->left->parent = new;
2001 node_update_fields_deep (new);
2003 while (new->parent && get_priority (new) > get_priority (new->parent))
2006 rotate_down (new, get_priority (new));
2010 node_unlink (GSequenceNode *node)
2012 rotate_down (node, 0);
2014 if (NODE_RIGHT_CHILD (node))
2015 node->parent->right = NULL;
2016 else if (NODE_LEFT_CHILD (node))
2017 node->parent->left = NULL;
2020 node_update_fields_deep (node->parent);
2022 node->parent = NULL;
2026 node_insert_sorted (GSequenceNode *node,
2029 GSequenceIterCompareFunc iter_cmp,
2032 GSequenceNode *closest;
2034 closest = node_find_closest (node, new, end, iter_cmp, cmp_data);
2038 node_insert_before (closest, new);