Add support for g_auto[s]list(Type)
[glib.git] / glib / gsequence.c
blob2dac88a9453167c4e8fabb715ad9d6f42318f1e0
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/>.
19 #include "config.h"
21 #include "gsequence.h"
23 #include "gmem.h"
24 #include "gtestutils.h"
25 #include "gslice.h"
26 /**
27 * SECTION:sequence
28 * @title: Sequences
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 log n).
34 * The data contained in each element can be either integer values, by using
35 * of the [Type Conversion Macros][glib-Type-Conversion-Macros], or simply
36 * pointers to any type of data.
38 * A #GSequence is accessed through "iterators", represented by a
39 * #GSequenceIter. An iterator represents a position between two
40 * elements of the sequence. For example, the "begin" iterator
41 * represents the gap immediately before the first element of the
42 * sequence, and the "end" iterator represents the gap immediately
43 * after the last element. In an empty sequence, the begin and end
44 * iterators are the same.
46 * Some methods on #GSequence operate on ranges of items. For example
47 * g_sequence_foreach_range() will call a user-specified function on
48 * each element with the given range. The range is delimited by the
49 * gaps represented by the passed-in iterators, so if you pass in the
50 * begin and end iterators, the range in question is the entire
51 * sequence.
53 * The function g_sequence_get() is used with an iterator to access the
54 * element immediately following the gap that the iterator represents.
55 * The iterator is said to "point" to that element.
57 * Iterators are stable across most operations on a #GSequence. For
58 * example an iterator pointing to some element of a sequence will
59 * continue to point to that element even after the sequence is sorted.
60 * Even moving an element to another sequence using for example
61 * g_sequence_move_range() will not invalidate the iterators pointing
62 * to it. The only operation that will invalidate an iterator is when
63 * the element it points to is removed from any sequence.
66 /**
67 * GSequenceIter:
69 * The #GSequenceIter struct is an opaque data type representing an
70 * iterator pointing into a #GSequence.
73 /**
74 * GSequenceIterCompareFunc:
75 * @a: a #GSequenceIter
76 * @b: a #GSequenceIter
77 * @data: user data
79 * A #GSequenceIterCompareFunc is a function used to compare iterators.
80 * It must return zero if the iterators compare equal, a negative value
81 * if @a comes before @b, and a positive value if @b comes before @a.
83 * Returns: zero if the iterators are equal, a negative value if @a
84 * comes before @b, and a positive value if @b comes before @a.
87 typedef struct _GSequenceNode GSequenceNode;
89 /**
90 * GSequence:
92 * The #GSequence struct is an opaque data type representing a
93 * [sequence][glib-Sequences] data type.
95 struct _GSequence
97 GSequenceNode * end_node;
98 GDestroyNotify data_destroy_notify;
99 gboolean access_prohibited;
101 /* The 'real_sequence' is used when temporary sequences are created
102 * to hold nodes that are being rearranged. The 'real_sequence' of such
103 * a temporary sequence points to the sequence that is actually being
104 * manipulated. The only reason we need this is so that when the
105 * sort/sort_changed/search_iter() functions call out to the application
106 * g_sequence_iter_get_sequence() will return the correct sequence.
108 GSequence * real_sequence;
111 struct _GSequenceNode
113 gint n_nodes;
114 GSequenceNode * parent;
115 GSequenceNode * left;
116 GSequenceNode * right;
117 gpointer data; /* For the end node, this field points
118 * to the sequence
123 * Declaration of GSequenceNode methods
125 static GSequenceNode *node_new (gpointer data);
126 static GSequenceNode *node_get_first (GSequenceNode *node);
127 static GSequenceNode *node_get_last (GSequenceNode *node);
128 static GSequenceNode *node_get_prev (GSequenceNode *node);
129 static GSequenceNode *node_get_next (GSequenceNode *node);
130 static gint node_get_pos (GSequenceNode *node);
131 static GSequenceNode *node_get_by_pos (GSequenceNode *node,
132 gint pos);
133 static GSequenceNode *node_find (GSequenceNode *haystack,
134 GSequenceNode *needle,
135 GSequenceNode *end,
136 GSequenceIterCompareFunc cmp,
137 gpointer user_data);
138 static GSequenceNode *node_find_closest (GSequenceNode *haystack,
139 GSequenceNode *needle,
140 GSequenceNode *end,
141 GSequenceIterCompareFunc cmp,
142 gpointer user_data);
143 static gint node_get_length (GSequenceNode *node);
144 static void node_free (GSequenceNode *node,
145 GSequence *seq);
146 static void node_cut (GSequenceNode *split);
147 static void node_insert_before (GSequenceNode *node,
148 GSequenceNode *new);
149 static void node_unlink (GSequenceNode *node);
150 static void node_join (GSequenceNode *left,
151 GSequenceNode *right);
152 static void node_insert_sorted (GSequenceNode *node,
153 GSequenceNode *new,
154 GSequenceNode *end,
155 GSequenceIterCompareFunc cmp_func,
156 gpointer cmp_data);
160 * Various helper functions
162 static void
163 check_seq_access (GSequence *seq)
165 if (G_UNLIKELY (seq->access_prohibited))
167 g_warning ("Accessing a sequence while it is "
168 "being sorted or searched is not allowed");
172 static GSequence *
173 get_sequence (GSequenceNode *node)
175 return (GSequence *)node_get_last (node)->data;
178 static void
179 check_iter_access (GSequenceIter *iter)
181 check_seq_access (get_sequence (iter));
184 static gboolean
185 is_end (GSequenceIter *iter)
187 GSequenceIter *parent = iter->parent;
189 if (iter->right)
190 return FALSE;
192 if (!parent)
193 return TRUE;
195 while (parent->right == iter)
197 iter = parent;
198 parent = iter->parent;
200 if (!parent)
201 return TRUE;
204 return FALSE;
207 typedef struct
209 GCompareDataFunc cmp_func;
210 gpointer cmp_data;
211 GSequenceNode *end_node;
212 } SortInfo;
214 /* This function compares two iters using a normal compare
215 * function and user_data passed in in a SortInfo struct
217 static gint
218 iter_compare (GSequenceIter *node1,
219 GSequenceIter *node2,
220 gpointer data)
222 const SortInfo *info = data;
223 gint retval;
225 if (node1 == info->end_node)
226 return 1;
228 if (node2 == info->end_node)
229 return -1;
231 retval = info->cmp_func (node1->data, node2->data, info->cmp_data);
233 return retval;
237 * Public API
241 * g_sequence_new:
242 * @data_destroy: (nullable): a #GDestroyNotify function, or %NULL
244 * Creates a new GSequence. The @data_destroy function, if non-%NULL will
245 * be called on all items when the sequence is destroyed and on items that
246 * are removed from the sequence.
248 * Returns: (transfer full): a new #GSequence
250 * Since: 2.14
252 GSequence *
253 g_sequence_new (GDestroyNotify data_destroy)
255 GSequence *seq = g_new (GSequence, 1);
256 seq->data_destroy_notify = data_destroy;
258 seq->end_node = node_new (seq);
260 seq->access_prohibited = FALSE;
262 seq->real_sequence = seq;
264 return seq;
268 * g_sequence_free:
269 * @seq: a #GSequence
271 * Frees the memory allocated for @seq. If @seq has a data destroy
272 * function associated with it, that function is called on all items
273 * in @seq.
275 * Since: 2.14
277 void
278 g_sequence_free (GSequence *seq)
280 g_return_if_fail (seq != NULL);
282 check_seq_access (seq);
284 node_free (seq->end_node, seq);
286 g_free (seq);
290 * g_sequence_foreach_range:
291 * @begin: a #GSequenceIter
292 * @end: a #GSequenceIter
293 * @func: a #GFunc
294 * @user_data: user data passed to @func
296 * Calls @func for each item in the range (@begin, @end) passing
297 * @user_data to the function. @func must not modify the sequence
298 * itself.
300 * Since: 2.14
302 void
303 g_sequence_foreach_range (GSequenceIter *begin,
304 GSequenceIter *end,
305 GFunc func,
306 gpointer user_data)
308 GSequence *seq;
309 GSequenceIter *iter;
311 g_return_if_fail (func != NULL);
312 g_return_if_fail (begin != NULL);
313 g_return_if_fail (end != NULL);
315 seq = get_sequence (begin);
317 seq->access_prohibited = TRUE;
319 iter = begin;
320 while (iter != end)
322 GSequenceIter *next = node_get_next (iter);
324 func (iter->data, user_data);
326 iter = next;
329 seq->access_prohibited = FALSE;
333 * g_sequence_foreach:
334 * @seq: a #GSequence
335 * @func: the function to call for each item in @seq
336 * @user_data: user data passed to @func
338 * Calls @func for each item in the sequence passing @user_data
339 * to the function. @func must not modify the sequence itself.
341 * Since: 2.14
343 void
344 g_sequence_foreach (GSequence *seq,
345 GFunc func,
346 gpointer user_data)
348 GSequenceIter *begin, *end;
350 check_seq_access (seq);
352 begin = g_sequence_get_begin_iter (seq);
353 end = g_sequence_get_end_iter (seq);
355 g_sequence_foreach_range (begin, end, func, user_data);
359 * g_sequence_range_get_midpoint:
360 * @begin: a #GSequenceIter
361 * @end: a #GSequenceIter
363 * Finds an iterator somewhere in the range (@begin, @end). This
364 * iterator will be close to the middle of the range, but is not
365 * guaranteed to be exactly in the middle.
367 * The @begin and @end iterators must both point to the same sequence
368 * and @begin must come before or be equal to @end in the sequence.
370 * Returns: (transfer none): a #GSequenceIter pointing somewhere in the
371 * (@begin, @end) range
373 * Since: 2.14
375 GSequenceIter *
376 g_sequence_range_get_midpoint (GSequenceIter *begin,
377 GSequenceIter *end)
379 int begin_pos, end_pos, mid_pos;
381 g_return_val_if_fail (begin != NULL, NULL);
382 g_return_val_if_fail (end != NULL, NULL);
383 g_return_val_if_fail (get_sequence (begin) == get_sequence (end), NULL);
385 begin_pos = node_get_pos (begin);
386 end_pos = node_get_pos (end);
388 g_return_val_if_fail (end_pos >= begin_pos, NULL);
390 mid_pos = begin_pos + (end_pos - begin_pos) / 2;
392 return node_get_by_pos (begin, mid_pos);
396 * g_sequence_iter_compare:
397 * @a: a #GSequenceIter
398 * @b: a #GSequenceIter
400 * Returns a negative number if @a comes before @b, 0 if they are equal,
401 * and a positive number if @a comes after @b.
403 * The @a and @b iterators must point into the same sequence.
405 * Returns: a negative number if @a comes before @b, 0 if they are
406 * equal, and a positive number if @a comes after @b
408 * Since: 2.14
410 gint
411 g_sequence_iter_compare (GSequenceIter *a,
412 GSequenceIter *b)
414 gint a_pos, b_pos;
416 g_return_val_if_fail (a != NULL, 0);
417 g_return_val_if_fail (b != NULL, 0);
418 g_return_val_if_fail (get_sequence (a) == get_sequence (b), 0);
420 check_iter_access (a);
421 check_iter_access (b);
423 a_pos = node_get_pos (a);
424 b_pos = node_get_pos (b);
426 if (a_pos == b_pos)
427 return 0;
428 else if (a_pos > b_pos)
429 return 1;
430 else
431 return -1;
435 * g_sequence_append:
436 * @seq: a #GSequence
437 * @data: the data for the new item
439 * Adds a new item to the end of @seq.
441 * Returns: (transfer none): an iterator pointing to the new item
443 * Since: 2.14
445 GSequenceIter *
446 g_sequence_append (GSequence *seq,
447 gpointer data)
449 GSequenceNode *node;
451 g_return_val_if_fail (seq != NULL, NULL);
453 check_seq_access (seq);
455 node = node_new (data);
456 node_insert_before (seq->end_node, node);
458 return node;
462 * g_sequence_prepend:
463 * @seq: a #GSequence
464 * @data: the data for the new item
466 * Adds a new item to the front of @seq
468 * Returns: (transfer none): an iterator pointing to the new item
470 * Since: 2.14
472 GSequenceIter *
473 g_sequence_prepend (GSequence *seq,
474 gpointer data)
476 GSequenceNode *node, *first;
478 g_return_val_if_fail (seq != NULL, NULL);
480 check_seq_access (seq);
482 node = node_new (data);
483 first = node_get_first (seq->end_node);
485 node_insert_before (first, node);
487 return node;
491 * g_sequence_insert_before:
492 * @iter: a #GSequenceIter
493 * @data: the data for the new item
495 * Inserts a new item just before the item pointed to by @iter.
497 * Returns: (transfer none): an iterator pointing to the new item
499 * Since: 2.14
501 GSequenceIter *
502 g_sequence_insert_before (GSequenceIter *iter,
503 gpointer data)
505 GSequenceNode *node;
507 g_return_val_if_fail (iter != NULL, NULL);
509 check_iter_access (iter);
511 node = node_new (data);
513 node_insert_before (iter, node);
515 return node;
519 * g_sequence_remove:
520 * @iter: a #GSequenceIter
522 * Removes the item pointed to by @iter. It is an error to pass the
523 * end iterator to this function.
525 * If the sequence has a data destroy function associated with it, this
526 * function is called on the data for the removed item.
528 * Since: 2.14
530 void
531 g_sequence_remove (GSequenceIter *iter)
533 GSequence *seq;
535 g_return_if_fail (iter != NULL);
536 g_return_if_fail (!is_end (iter));
538 check_iter_access (iter);
540 seq = get_sequence (iter);
542 node_unlink (iter);
543 node_free (iter, seq);
547 * g_sequence_remove_range:
548 * @begin: a #GSequenceIter
549 * @end: a #GSequenceIter
551 * Removes all items in the (@begin, @end) range.
553 * If the sequence has a data destroy function associated with it, this
554 * function is called on the data for the removed items.
556 * Since: 2.14
558 void
559 g_sequence_remove_range (GSequenceIter *begin,
560 GSequenceIter *end)
562 g_return_if_fail (get_sequence (begin) == get_sequence (end));
564 check_iter_access (begin);
565 check_iter_access (end);
567 g_sequence_move_range (NULL, begin, end);
571 * g_sequence_move_range:
572 * @dest: a #GSequenceIter
573 * @begin: a #GSequenceIter
574 * @end: a #GSequenceIter
576 * Inserts the (@begin, @end) range at the destination pointed to by ptr.
577 * The @begin and @end iters must point into the same sequence. It is
578 * allowed for @dest to point to a different sequence than the one pointed
579 * into by @begin and @end.
581 * If @dest is NULL, the range indicated by @begin and @end is
582 * removed from the sequence. If @dest iter points to a place within
583 * the (@begin, @end) range, the range does not move.
585 * Since: 2.14
587 void
588 g_sequence_move_range (GSequenceIter *dest,
589 GSequenceIter *begin,
590 GSequenceIter *end)
592 GSequence *src_seq;
593 GSequenceNode *first;
595 g_return_if_fail (begin != NULL);
596 g_return_if_fail (end != NULL);
598 check_iter_access (begin);
599 check_iter_access (end);
600 if (dest)
601 check_iter_access (dest);
603 src_seq = get_sequence (begin);
605 g_return_if_fail (src_seq == get_sequence (end));
607 /* Dest points to begin or end? */
608 if (dest == begin || dest == end)
609 return;
611 /* begin comes after end? */
612 if (g_sequence_iter_compare (begin, end) >= 0)
613 return;
615 /* dest points somewhere in the (begin, end) range? */
616 if (dest && get_sequence (dest) == src_seq &&
617 g_sequence_iter_compare (dest, begin) > 0 &&
618 g_sequence_iter_compare (dest, end) < 0)
620 return;
623 src_seq = get_sequence (begin);
625 first = node_get_first (begin);
627 node_cut (begin);
629 node_cut (end);
631 if (first != begin)
632 node_join (first, end);
634 if (dest)
636 first = node_get_first (dest);
638 node_cut (dest);
640 node_join (begin, dest);
642 if (dest != first)
643 node_join (first, begin);
645 else
647 node_free (begin, src_seq);
652 * g_sequence_sort:
653 * @seq: a #GSequence
654 * @cmp_func: the function used to sort the sequence
655 * @cmp_data: user data passed to @cmp_func
657 * Sorts @seq using @cmp_func.
659 * @cmp_func is passed two items of @seq and should
660 * return 0 if they are equal, a negative value if the
661 * first comes before the second, and a positive value
662 * if the second comes before the first.
664 * Since: 2.14
666 void
667 g_sequence_sort (GSequence *seq,
668 GCompareDataFunc cmp_func,
669 gpointer cmp_data)
671 SortInfo info;
673 info.cmp_func = cmp_func;
674 info.cmp_data = cmp_data;
675 info.end_node = seq->end_node;
677 check_seq_access (seq);
679 g_sequence_sort_iter (seq, iter_compare, &info);
683 * g_sequence_insert_sorted:
684 * @seq: a #GSequence
685 * @data: the data to insert
686 * @cmp_func: the function used to compare items in the sequence
687 * @cmp_data: user data passed to @cmp_func.
689 * Inserts @data into @sequence using @func to determine the new
690 * position. The sequence must already be sorted according to @cmp_func;
691 * otherwise the new position of @data is undefined.
693 * @cmp_func is called with two items of the @seq and @user_data.
694 * It should return 0 if the items are equal, a negative value
695 * if the first item comes before the second, and a positive value
696 * if the second item comes before the first.
698 * Returns: (transfer none): a #GSequenceIter pointing to the new item.
700 * Since: 2.14
702 GSequenceIter *
703 g_sequence_insert_sorted (GSequence *seq,
704 gpointer data,
705 GCompareDataFunc cmp_func,
706 gpointer cmp_data)
708 SortInfo info;
710 g_return_val_if_fail (seq != NULL, NULL);
711 g_return_val_if_fail (cmp_func != NULL, NULL);
713 info.cmp_func = cmp_func;
714 info.cmp_data = cmp_data;
715 info.end_node = seq->end_node;
716 check_seq_access (seq);
718 return g_sequence_insert_sorted_iter (seq, data, iter_compare, &info);
722 * g_sequence_sort_changed:
723 * @iter: A #GSequenceIter
724 * @cmp_func: the function used to compare items in the sequence
725 * @cmp_data: user data passed to @cmp_func.
727 * Moves the data pointed to a new position as indicated by @cmp_func. This
728 * function should be called for items in a sequence already sorted according
729 * to @cmp_func whenever some aspect of an item changes so that @cmp_func
730 * may return different values for that item.
732 * @cmp_func is called with two items of the @seq and @user_data.
733 * It should return 0 if the items are equal, a negative value if
734 * the first item comes before the second, and a positive value if
735 * the second item comes before the first.
737 * Since: 2.14
739 void
740 g_sequence_sort_changed (GSequenceIter *iter,
741 GCompareDataFunc cmp_func,
742 gpointer cmp_data)
744 SortInfo info;
746 g_return_if_fail (!is_end (iter));
748 info.cmp_func = cmp_func;
749 info.cmp_data = cmp_data;
750 info.end_node = get_sequence (iter)->end_node;
751 check_iter_access (iter);
753 g_sequence_sort_changed_iter (iter, iter_compare, &info);
757 * g_sequence_search:
758 * @seq: a #GSequence
759 * @data: data for the new item
760 * @cmp_func: the function used to compare items in the sequence
761 * @cmp_data: user data passed to @cmp_func
763 * Returns an iterator pointing to the position where @data would
764 * be inserted according to @cmp_func and @cmp_data.
766 * @cmp_func is called with two items of the @seq and @user_data.
767 * It should return 0 if the items are equal, a negative value if
768 * the first item comes before the second, and a positive value if
769 * the second item comes before the first.
771 * If you are simply searching for an existing element of the sequence,
772 * consider using g_sequence_lookup().
774 * This function will fail if the data contained in the sequence is
775 * unsorted. Use g_sequence_insert_sorted() or
776 * g_sequence_insert_sorted_iter() to add data to your sequence or, if
777 * you want to add a large amount of data, call g_sequence_sort() after
778 * doing unsorted insertions.
780 * Returns: (transfer none): an #GSequenceIter pointing to the position where @data
781 * would have been inserted according to @cmp_func and @cmp_data
783 * Since: 2.14
785 GSequenceIter *
786 g_sequence_search (GSequence *seq,
787 gpointer data,
788 GCompareDataFunc cmp_func,
789 gpointer cmp_data)
791 SortInfo info;
793 g_return_val_if_fail (seq != NULL, NULL);
795 info.cmp_func = cmp_func;
796 info.cmp_data = cmp_data;
797 info.end_node = seq->end_node;
798 check_seq_access (seq);
800 return g_sequence_search_iter (seq, data, iter_compare, &info);
804 * g_sequence_lookup:
805 * @seq: a #GSequence
806 * @data: data to lookup
807 * @cmp_func: the function used to compare items in the sequence
808 * @cmp_data: user data passed to @cmp_func
810 * Returns an iterator pointing to the position of the first item found
811 * equal to @data according to @cmp_func and @cmp_data. If more than one
812 * item is equal, it is not guaranteed that it is the first which is
813 * returned. In that case, you can use g_sequence_iter_next() and
814 * g_sequence_iter_prev() to get others.
816 * @cmp_func is called with two items of the @seq and @user_data.
817 * It should return 0 if the items are equal, a negative value if
818 * the first item comes before the second, and a positive value if
819 * the second item comes before the first.
821 * This function will fail if the data contained in the sequence is
822 * unsorted. Use g_sequence_insert_sorted() or
823 * g_sequence_insert_sorted_iter() to add data to your sequence or, if
824 * you want to add a large amount of data, call g_sequence_sort() after
825 * doing unsorted insertions.
827 * Returns: (transfer none) (nullable): an #GSequenceIter pointing to the position of the
828 * first item found equal to @data according to @cmp_func and
829 * @cmp_data, or %NULL if no such item exists
831 * Since: 2.28
833 GSequenceIter *
834 g_sequence_lookup (GSequence *seq,
835 gpointer data,
836 GCompareDataFunc cmp_func,
837 gpointer cmp_data)
839 SortInfo info;
841 g_return_val_if_fail (seq != NULL, NULL);
843 info.cmp_func = cmp_func;
844 info.cmp_data = cmp_data;
845 info.end_node = seq->end_node;
846 check_seq_access (seq);
848 return g_sequence_lookup_iter (seq, data, iter_compare, &info);
852 * g_sequence_sort_iter:
853 * @seq: a #GSequence
854 * @cmp_func: the function used to compare iterators in the sequence
855 * @cmp_data: user data passed to @cmp_func
857 * Like g_sequence_sort(), but uses a #GSequenceIterCompareFunc instead
858 * of a GCompareDataFunc as the compare function
860 * @cmp_func is called with two iterators pointing into @seq. It should
861 * return 0 if the iterators are equal, a negative value if the first
862 * iterator comes before the second, and a positive value if the second
863 * iterator comes before the first.
865 * Since: 2.14
867 void
868 g_sequence_sort_iter (GSequence *seq,
869 GSequenceIterCompareFunc cmp_func,
870 gpointer cmp_data)
872 GSequence *tmp;
873 GSequenceNode *begin, *end;
875 g_return_if_fail (seq != NULL);
876 g_return_if_fail (cmp_func != NULL);
878 check_seq_access (seq);
880 begin = g_sequence_get_begin_iter (seq);
881 end = g_sequence_get_end_iter (seq);
883 tmp = g_sequence_new (NULL);
884 tmp->real_sequence = seq;
886 g_sequence_move_range (g_sequence_get_begin_iter (tmp), begin, end);
888 seq->access_prohibited = TRUE;
889 tmp->access_prohibited = TRUE;
891 while (!g_sequence_is_empty (tmp))
893 GSequenceNode *node = g_sequence_get_begin_iter (tmp);
895 node_insert_sorted (seq->end_node, node, seq->end_node,
896 cmp_func, cmp_data);
899 tmp->access_prohibited = FALSE;
900 seq->access_prohibited = FALSE;
902 g_sequence_free (tmp);
906 * g_sequence_sort_changed_iter:
907 * @iter: a #GSequenceIter
908 * @iter_cmp: the function used to compare iterators in the sequence
909 * @cmp_data: user data passed to @cmp_func
911 * Like g_sequence_sort_changed(), but uses
912 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
913 * the compare function.
915 * @iter_cmp is called with two iterators pointing into @seq. It should
916 * return 0 if the iterators are equal, a negative value if the first
917 * iterator comes before the second, and a positive value if the second
918 * iterator comes before the first.
920 * Since: 2.14
922 void
923 g_sequence_sort_changed_iter (GSequenceIter *iter,
924 GSequenceIterCompareFunc iter_cmp,
925 gpointer cmp_data)
927 GSequence *seq, *tmp_seq;
928 GSequenceIter *next, *prev;
930 g_return_if_fail (iter != NULL);
931 g_return_if_fail (!is_end (iter));
932 g_return_if_fail (iter_cmp != NULL);
933 check_iter_access (iter);
935 /* If one of the neighbours is equal to iter, then
936 * don't move it. This ensures that sort_changed() is
937 * a stable operation.
940 next = node_get_next (iter);
941 prev = node_get_prev (iter);
943 if (prev != iter && iter_cmp (prev, iter, cmp_data) == 0)
944 return;
946 if (!is_end (next) && iter_cmp (next, iter, cmp_data) == 0)
947 return;
949 seq = get_sequence (iter);
951 seq->access_prohibited = TRUE;
953 tmp_seq = g_sequence_new (NULL);
954 tmp_seq->real_sequence = seq;
956 node_unlink (iter);
957 node_insert_before (tmp_seq->end_node, iter);
959 node_insert_sorted (seq->end_node, iter, seq->end_node,
960 iter_cmp, cmp_data);
962 g_sequence_free (tmp_seq);
964 seq->access_prohibited = FALSE;
968 * g_sequence_insert_sorted_iter:
969 * @seq: a #GSequence
970 * @data: data for the new item
971 * @iter_cmp: the function used to compare iterators in the sequence
972 * @cmp_data: user data passed to @cmp_func
974 * Like g_sequence_insert_sorted(), but uses
975 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
976 * the compare function.
978 * @iter_cmp is called with two iterators pointing into @seq.
979 * It should return 0 if the iterators are equal, a negative
980 * value if the first iterator comes before the second, and a
981 * positive value if the second iterator comes before the first.
983 * It is called with two iterators pointing into @seq. It should
984 * return 0 if the iterators are equal, a negative value if the
985 * first iterator comes before the second, and a positive value
986 * if the second iterator comes before the first.
988 * Returns: (transfer none): a #GSequenceIter pointing to the new item
990 * Since: 2.14
992 GSequenceIter *
993 g_sequence_insert_sorted_iter (GSequence *seq,
994 gpointer data,
995 GSequenceIterCompareFunc iter_cmp,
996 gpointer cmp_data)
998 GSequenceNode *new_node;
999 GSequence *tmp_seq;
1001 g_return_val_if_fail (seq != NULL, NULL);
1002 g_return_val_if_fail (iter_cmp != NULL, NULL);
1004 check_seq_access (seq);
1006 seq->access_prohibited = TRUE;
1008 /* Create a new temporary sequence and put the new node into
1009 * that. The reason for this is that the user compare function
1010 * will be called with the new node, and if it dereferences,
1011 * "is_end" will be called on it. But that will crash if the
1012 * node is not actually in a sequence.
1014 * node_insert_sorted() makes sure the node is unlinked before
1015 * it is inserted.
1017 * The reason we need the "iter" versions at all is that that
1018 * is the only kind of compare functions GtkTreeView can use.
1020 tmp_seq = g_sequence_new (NULL);
1021 tmp_seq->real_sequence = seq;
1023 new_node = g_sequence_append (tmp_seq, data);
1025 node_insert_sorted (seq->end_node, new_node,
1026 seq->end_node, iter_cmp, cmp_data);
1028 g_sequence_free (tmp_seq);
1030 seq->access_prohibited = FALSE;
1032 return new_node;
1036 * g_sequence_search_iter:
1037 * @seq: a #GSequence
1038 * @data: data for the new item
1039 * @iter_cmp: the function used to compare iterators in the sequence
1040 * @cmp_data: user data passed to @iter_cmp
1042 * Like g_sequence_search(), but uses a #GSequenceIterCompareFunc
1043 * instead of a #GCompareDataFunc as the compare function.
1045 * @iter_cmp is called with two iterators pointing into @seq.
1046 * It should return 0 if the iterators are equal, a negative value
1047 * if the first iterator comes before the second, and a positive
1048 * value if the second iterator comes before the first.
1050 * If you are simply searching for an existing element of the sequence,
1051 * consider using g_sequence_lookup_iter().
1053 * This function will fail if the data contained in the sequence is
1054 * unsorted. Use g_sequence_insert_sorted() or
1055 * g_sequence_insert_sorted_iter() to add data to your sequence or, if
1056 * you want to add a large amount of data, call g_sequence_sort() after
1057 * doing unsorted insertions.
1059 * Returns: (transfer none): a #GSequenceIter pointing to the position in @seq
1060 * where @data would have been inserted according to @iter_cmp
1061 * and @cmp_data
1063 * Since: 2.14
1065 GSequenceIter *
1066 g_sequence_search_iter (GSequence *seq,
1067 gpointer data,
1068 GSequenceIterCompareFunc iter_cmp,
1069 gpointer cmp_data)
1071 GSequenceNode *node;
1072 GSequenceNode *dummy;
1073 GSequence *tmp_seq;
1075 g_return_val_if_fail (seq != NULL, NULL);
1077 check_seq_access (seq);
1079 seq->access_prohibited = TRUE;
1081 tmp_seq = g_sequence_new (NULL);
1082 tmp_seq->real_sequence = seq;
1084 dummy = g_sequence_append (tmp_seq, data);
1086 node = node_find_closest (seq->end_node, dummy,
1087 seq->end_node, iter_cmp, cmp_data);
1089 g_sequence_free (tmp_seq);
1091 seq->access_prohibited = FALSE;
1093 return node;
1097 * g_sequence_lookup_iter:
1098 * @seq: a #GSequence
1099 * @data: data to lookup
1100 * @iter_cmp: the function used to compare iterators in the sequence
1101 * @cmp_data: user data passed to @iter_cmp
1103 * Like g_sequence_lookup(), but uses a #GSequenceIterCompareFunc
1104 * instead of a #GCompareDataFunc as the compare function.
1106 * @iter_cmp is called with two iterators pointing into @seq.
1107 * It should return 0 if the iterators are equal, a negative value
1108 * if the first iterator comes before the second, and a positive
1109 * value if the second iterator comes before the first.
1111 * This function will fail if the data contained in the sequence is
1112 * unsorted. Use g_sequence_insert_sorted() or
1113 * g_sequence_insert_sorted_iter() to add data to your sequence or, if
1114 * you want to add a large amount of data, call g_sequence_sort() after
1115 * doing unsorted insertions.
1117 * Returns: (transfer none) (nullable): an #GSequenceIter pointing to the position of
1118 * the first item found equal to @data according to @cmp_func
1119 * and @cmp_data, or %NULL if no such item exists
1121 * Since: 2.28
1123 GSequenceIter *
1124 g_sequence_lookup_iter (GSequence *seq,
1125 gpointer data,
1126 GSequenceIterCompareFunc iter_cmp,
1127 gpointer cmp_data)
1129 GSequenceNode *node;
1130 GSequenceNode *dummy;
1131 GSequence *tmp_seq;
1133 g_return_val_if_fail (seq != NULL, NULL);
1135 check_seq_access (seq);
1137 seq->access_prohibited = TRUE;
1139 tmp_seq = g_sequence_new (NULL);
1140 tmp_seq->real_sequence = seq;
1142 dummy = g_sequence_append (tmp_seq, data);
1144 node = node_find (seq->end_node, dummy,
1145 seq->end_node, iter_cmp, cmp_data);
1147 g_sequence_free (tmp_seq);
1149 seq->access_prohibited = FALSE;
1151 return node;
1155 * g_sequence_iter_get_sequence:
1156 * @iter: a #GSequenceIter
1158 * Returns the #GSequence that @iter points into.
1160 * Returns: (transfer none): the #GSequence that @iter points into
1162 * Since: 2.14
1164 GSequence *
1165 g_sequence_iter_get_sequence (GSequenceIter *iter)
1167 GSequence *seq;
1169 g_return_val_if_fail (iter != NULL, NULL);
1171 seq = get_sequence (iter);
1173 /* For temporary sequences, this points to the sequence that
1174 * is actually being manipulated
1176 return seq->real_sequence;
1180 * g_sequence_get:
1181 * @iter: a #GSequenceIter
1183 * Returns the data that @iter points to.
1185 * Returns: (transfer none): the data that @iter points to
1187 * Since: 2.14
1189 gpointer
1190 g_sequence_get (GSequenceIter *iter)
1192 g_return_val_if_fail (iter != NULL, NULL);
1193 g_return_val_if_fail (!is_end (iter), NULL);
1195 return iter->data;
1199 * g_sequence_set:
1200 * @iter: a #GSequenceIter
1201 * @data: new data for the item
1203 * Changes the data for the item pointed to by @iter to be @data. If
1204 * the sequence has a data destroy function associated with it, that
1205 * function is called on the existing data that @iter pointed to.
1207 * Since: 2.14
1209 void
1210 g_sequence_set (GSequenceIter *iter,
1211 gpointer data)
1213 GSequence *seq;
1215 g_return_if_fail (iter != NULL);
1216 g_return_if_fail (!is_end (iter));
1218 seq = get_sequence (iter);
1220 /* If @data is identical to iter->data, it is destroyed
1221 * here. This will work right in case of ref-counted objects. Also
1222 * it is similar to what ghashtables do.
1224 * For non-refcounted data it's a little less convenient, but
1225 * code relying on self-setting not destroying would be
1226 * pretty dubious anyway ...
1229 if (seq->data_destroy_notify)
1230 seq->data_destroy_notify (iter->data);
1232 iter->data = data;
1236 * g_sequence_get_length:
1237 * @seq: a #GSequence
1239 * Returns the length of @seq. Note that this method is O(h) where `h' is the
1240 * height of the tree. It is thus more efficient to use g_sequence_is_empty()
1241 * when comparing the length to zero.
1243 * Returns: the length of @seq
1245 * Since: 2.14
1247 gint
1248 g_sequence_get_length (GSequence *seq)
1250 return node_get_length (seq->end_node) - 1;
1254 * g_sequence_is_empty:
1255 * @seq: a #GSequence
1257 * Returns %TRUE if the sequence contains zero items.
1259 * This function is functionally identical to checking the result of
1260 * g_sequence_get_length() being equal to zero. However this function is
1261 * implemented in O(1) running time.
1263 * Returns: %TRUE if the sequence is empty, otherwise %FALSE.
1265 * Since: 2.48
1267 gboolean
1268 g_sequence_is_empty (GSequence *seq)
1270 return (seq->end_node->parent == NULL) && (seq->end_node->left == NULL);
1274 * g_sequence_get_end_iter:
1275 * @seq: a #GSequence
1277 * Returns the end iterator for @seg
1279 * Returns: (transfer none): the end iterator for @seq
1281 * Since: 2.14
1283 GSequenceIter *
1284 g_sequence_get_end_iter (GSequence *seq)
1286 g_return_val_if_fail (seq != NULL, NULL);
1288 return seq->end_node;
1292 * g_sequence_get_begin_iter:
1293 * @seq: a #GSequence
1295 * Returns the begin iterator for @seq.
1297 * Returns: (transfer none): the begin iterator for @seq.
1299 * Since: 2.14
1301 GSequenceIter *
1302 g_sequence_get_begin_iter (GSequence *seq)
1304 g_return_val_if_fail (seq != NULL, NULL);
1306 return node_get_first (seq->end_node);
1309 static int
1310 clamp_position (GSequence *seq,
1311 int pos)
1313 gint len = g_sequence_get_length (seq);
1315 if (pos > len || pos < 0)
1316 pos = len;
1318 return pos;
1322 * if pos > number of items or -1, will return end pointer
1325 * g_sequence_get_iter_at_pos:
1326 * @seq: a #GSequence
1327 * @pos: a position in @seq, or -1 for the end
1329 * Returns the iterator at position @pos. If @pos is negative or larger
1330 * than the number of items in @seq, the end iterator is returned.
1332 * Returns: (transfer none): The #GSequenceIter at position @pos
1334 * Since: 2.14
1336 GSequenceIter *
1337 g_sequence_get_iter_at_pos (GSequence *seq,
1338 gint pos)
1340 g_return_val_if_fail (seq != NULL, NULL);
1342 pos = clamp_position (seq, pos);
1344 return node_get_by_pos (seq->end_node, pos);
1348 * g_sequence_move:
1349 * @src: a #GSequenceIter pointing to the item to move
1350 * @dest: a #GSequenceIter pointing to the position to which
1351 * the item is moved
1353 * Moves the item pointed to by @src to the position indicated by @dest.
1354 * After calling this function @dest will point to the position immediately
1355 * after @src. It is allowed for @src and @dest to point into different
1356 * sequences.
1358 * Since: 2.14
1360 void
1361 g_sequence_move (GSequenceIter *src,
1362 GSequenceIter *dest)
1364 g_return_if_fail (src != NULL);
1365 g_return_if_fail (dest != NULL);
1366 g_return_if_fail (!is_end (src));
1368 if (src == dest)
1369 return;
1371 node_unlink (src);
1372 node_insert_before (dest, src);
1375 /* GSequenceIter */
1378 * g_sequence_iter_is_end:
1379 * @iter: a #GSequenceIter
1381 * Returns whether @iter is the end iterator
1383 * Returns: Whether @iter is the end iterator
1385 * Since: 2.14
1387 gboolean
1388 g_sequence_iter_is_end (GSequenceIter *iter)
1390 g_return_val_if_fail (iter != NULL, FALSE);
1392 return is_end (iter);
1396 * g_sequence_iter_is_begin:
1397 * @iter: a #GSequenceIter
1399 * Returns whether @iter is the begin iterator
1401 * Returns: whether @iter is the begin iterator
1403 * Since: 2.14
1405 gboolean
1406 g_sequence_iter_is_begin (GSequenceIter *iter)
1408 g_return_val_if_fail (iter != NULL, FALSE);
1410 return (node_get_prev (iter) == iter);
1414 * g_sequence_iter_get_position:
1415 * @iter: a #GSequenceIter
1417 * Returns the position of @iter
1419 * Returns: the position of @iter
1421 * Since: 2.14
1423 gint
1424 g_sequence_iter_get_position (GSequenceIter *iter)
1426 g_return_val_if_fail (iter != NULL, -1);
1428 return node_get_pos (iter);
1432 * g_sequence_iter_next:
1433 * @iter: a #GSequenceIter
1435 * Returns an iterator pointing to the next position after @iter.
1436 * If @iter is the end iterator, the end iterator is returned.
1438 * Returns: (transfer none): a #GSequenceIter pointing to the next position after @iter
1440 * Since: 2.14
1442 GSequenceIter *
1443 g_sequence_iter_next (GSequenceIter *iter)
1445 g_return_val_if_fail (iter != NULL, NULL);
1447 return node_get_next (iter);
1451 * g_sequence_iter_prev:
1452 * @iter: a #GSequenceIter
1454 * Returns an iterator pointing to the previous position before @iter.
1455 * If @iter is the begin iterator, the begin iterator is returned.
1457 * Returns: (transfer none): a #GSequenceIter pointing to the previous position
1458 * before @iter
1460 * Since: 2.14
1462 GSequenceIter *
1463 g_sequence_iter_prev (GSequenceIter *iter)
1465 g_return_val_if_fail (iter != NULL, NULL);
1467 return node_get_prev (iter);
1471 * g_sequence_iter_move:
1472 * @iter: a #GSequenceIter
1473 * @delta: A positive or negative number indicating how many positions away
1474 * from @iter the returned #GSequenceIter will be
1476 * Returns the #GSequenceIter which is @delta positions away from @iter.
1477 * If @iter is closer than -@delta positions to the beginning of the sequence,
1478 * the begin iterator is returned. If @iter is closer than @delta positions
1479 * to the end of the sequence, the end iterator is returned.
1481 * Returns: (transfer none): a #GSequenceIter which is @delta positions away from @iter
1483 * Since: 2.14
1485 GSequenceIter *
1486 g_sequence_iter_move (GSequenceIter *iter,
1487 gint delta)
1489 gint new_pos;
1490 gint len;
1492 g_return_val_if_fail (iter != NULL, NULL);
1494 len = g_sequence_get_length (get_sequence (iter));
1496 new_pos = node_get_pos (iter) + delta;
1498 if (new_pos < 0)
1499 new_pos = 0;
1500 else if (new_pos > len)
1501 new_pos = len;
1503 return node_get_by_pos (iter, new_pos);
1507 * g_sequence_swap:
1508 * @a: a #GSequenceIter
1509 * @b: a #GSequenceIter
1511 * Swaps the items pointed to by @a and @b. It is allowed for @a and @b
1512 * to point into difference sequences.
1514 * Since: 2.14
1516 void
1517 g_sequence_swap (GSequenceIter *a,
1518 GSequenceIter *b)
1520 GSequenceNode *leftmost, *rightmost, *rightmost_next;
1521 int a_pos, b_pos;
1523 g_return_if_fail (!g_sequence_iter_is_end (a));
1524 g_return_if_fail (!g_sequence_iter_is_end (b));
1526 if (a == b)
1527 return;
1529 a_pos = g_sequence_iter_get_position (a);
1530 b_pos = g_sequence_iter_get_position (b);
1532 if (a_pos > b_pos)
1534 leftmost = b;
1535 rightmost = a;
1537 else
1539 leftmost = a;
1540 rightmost = b;
1543 rightmost_next = node_get_next (rightmost);
1545 /* The situation is now like this:
1547 * ..., leftmost, ......., rightmost, rightmost_next, ...
1550 g_sequence_move (rightmost, leftmost);
1551 g_sequence_move (leftmost, rightmost_next);
1555 * Implementation of a treap
1559 static guint
1560 get_priority (GSequenceNode *node)
1562 guint key = GPOINTER_TO_UINT (node);
1564 /* This hash function is based on one found on Thomas Wang's
1565 * web page at
1567 * http://www.concentric.net/~Ttwang/tech/inthash.htm
1570 key = (key << 15) - key - 1;
1571 key = key ^ (key >> 12);
1572 key = key + (key << 2);
1573 key = key ^ (key >> 4);
1574 key = key + (key << 3) + (key << 11);
1575 key = key ^ (key >> 16);
1577 /* We rely on 0 being less than all other priorities */
1578 return key? key : 1;
1581 static GSequenceNode *
1582 find_root (GSequenceNode *node)
1584 while (node->parent)
1585 node = node->parent;
1587 return node;
1590 static GSequenceNode *
1591 node_new (gpointer data)
1593 GSequenceNode *node = g_slice_new0 (GSequenceNode);
1595 node->n_nodes = 1;
1596 node->data = data;
1597 node->left = NULL;
1598 node->right = NULL;
1599 node->parent = NULL;
1601 return node;
1604 static GSequenceNode *
1605 node_get_first (GSequenceNode *node)
1607 node = find_root (node);
1609 while (node->left)
1610 node = node->left;
1612 return node;
1615 static GSequenceNode *
1616 node_get_last (GSequenceNode *node)
1618 node = find_root (node);
1620 while (node->right)
1621 node = node->right;
1623 return node;
1626 #define NODE_LEFT_CHILD(n) (((n)->parent) && ((n)->parent->left) == (n))
1627 #define NODE_RIGHT_CHILD(n) (((n)->parent) && ((n)->parent->right) == (n))
1629 static GSequenceNode *
1630 node_get_next (GSequenceNode *node)
1632 GSequenceNode *n = node;
1634 if (n->right)
1636 n = n->right;
1637 while (n->left)
1638 n = n->left;
1640 else
1642 while (NODE_RIGHT_CHILD (n))
1643 n = n->parent;
1645 if (n->parent)
1646 n = n->parent;
1647 else
1648 n = node;
1651 return n;
1654 static GSequenceNode *
1655 node_get_prev (GSequenceNode *node)
1657 GSequenceNode *n = node;
1659 if (n->left)
1661 n = n->left;
1662 while (n->right)
1663 n = n->right;
1665 else
1667 while (NODE_LEFT_CHILD (n))
1668 n = n->parent;
1670 if (n->parent)
1671 n = n->parent;
1672 else
1673 n = node;
1676 return n;
1679 #define N_NODES(n) ((n)? (n)->n_nodes : 0)
1681 static gint
1682 node_get_pos (GSequenceNode *node)
1684 int n_smaller = 0;
1686 if (node->left)
1687 n_smaller = node->left->n_nodes;
1689 while (node)
1691 if (NODE_RIGHT_CHILD (node))
1692 n_smaller += N_NODES (node->parent->left) + 1;
1694 node = node->parent;
1697 return n_smaller;
1700 static GSequenceNode *
1701 node_get_by_pos (GSequenceNode *node,
1702 gint pos)
1704 int i;
1706 node = find_root (node);
1708 while ((i = N_NODES (node->left)) != pos)
1710 if (i < pos)
1712 node = node->right;
1713 pos -= (i + 1);
1715 else
1717 node = node->left;
1721 return node;
1724 static GSequenceNode *
1725 node_find (GSequenceNode *haystack,
1726 GSequenceNode *needle,
1727 GSequenceNode *end,
1728 GSequenceIterCompareFunc iter_cmp,
1729 gpointer cmp_data)
1731 gint c;
1733 haystack = find_root (haystack);
1737 /* iter_cmp can't be passed the end node, since the function may
1738 * be user-supplied
1740 if (haystack == end)
1741 c = 1;
1742 else
1743 c = iter_cmp (haystack, needle, cmp_data);
1745 if (c == 0)
1746 break;
1748 if (c > 0)
1749 haystack = haystack->left;
1750 else
1751 haystack = haystack->right;
1753 while (haystack != NULL);
1755 return haystack;
1758 static GSequenceNode *
1759 node_find_closest (GSequenceNode *haystack,
1760 GSequenceNode *needle,
1761 GSequenceNode *end,
1762 GSequenceIterCompareFunc iter_cmp,
1763 gpointer cmp_data)
1765 GSequenceNode *best;
1766 gint c;
1768 haystack = find_root (haystack);
1772 best = haystack;
1774 /* iter_cmp can't be passed the end node, since the function may
1775 * be user-supplied
1777 if (haystack == end)
1778 c = 1;
1779 else
1780 c = iter_cmp (haystack, needle, cmp_data);
1782 /* In the following we don't break even if c == 0. Instead we go on
1783 * searching along the 'bigger' nodes, so that we find the last one
1784 * that is equal to the needle.
1786 if (c > 0)
1787 haystack = haystack->left;
1788 else
1789 haystack = haystack->right;
1791 while (haystack != NULL);
1793 /* If the best node is smaller or equal to the data, then move one step
1794 * to the right to make sure the best one is strictly bigger than the data
1796 if (best != end && c <= 0)
1797 best = node_get_next (best);
1799 return best;
1802 static gint
1803 node_get_length (GSequenceNode *node)
1805 node = find_root (node);
1807 return node->n_nodes;
1810 static void
1811 real_node_free (GSequenceNode *node,
1812 GSequence *seq)
1814 if (node)
1816 real_node_free (node->left, seq);
1817 real_node_free (node->right, seq);
1819 if (seq && seq->data_destroy_notify && node != seq->end_node)
1820 seq->data_destroy_notify (node->data);
1822 g_slice_free (GSequenceNode, node);
1826 static void
1827 node_free (GSequenceNode *node,
1828 GSequence *seq)
1830 node = find_root (node);
1832 real_node_free (node, seq);
1835 static void
1836 node_update_fields (GSequenceNode *node)
1838 int n_nodes = 1;
1840 n_nodes += N_NODES (node->left);
1841 n_nodes += N_NODES (node->right);
1843 node->n_nodes = n_nodes;
1846 static void
1847 node_rotate (GSequenceNode *node)
1849 GSequenceNode *tmp, *old;
1851 g_assert (node->parent);
1852 g_assert (node->parent != node);
1854 if (NODE_LEFT_CHILD (node))
1856 /* rotate right */
1857 tmp = node->right;
1859 node->right = node->parent;
1860 node->parent = node->parent->parent;
1861 if (node->parent)
1863 if (node->parent->left == node->right)
1864 node->parent->left = node;
1865 else
1866 node->parent->right = node;
1869 g_assert (node->right);
1871 node->right->parent = node;
1872 node->right->left = tmp;
1874 if (node->right->left)
1875 node->right->left->parent = node->right;
1877 old = node->right;
1879 else
1881 /* rotate left */
1882 tmp = node->left;
1884 node->left = node->parent;
1885 node->parent = node->parent->parent;
1886 if (node->parent)
1888 if (node->parent->right == node->left)
1889 node->parent->right = node;
1890 else
1891 node->parent->left = node;
1894 g_assert (node->left);
1896 node->left->parent = node;
1897 node->left->right = tmp;
1899 if (node->left->right)
1900 node->left->right->parent = node->left;
1902 old = node->left;
1905 node_update_fields (old);
1906 node_update_fields (node);
1909 static void
1910 node_update_fields_deep (GSequenceNode *node)
1912 if (node)
1914 node_update_fields (node);
1916 node_update_fields_deep (node->parent);
1920 static void
1921 rotate_down (GSequenceNode *node,
1922 guint priority)
1924 guint left, right;
1926 left = node->left ? get_priority (node->left) : 0;
1927 right = node->right ? get_priority (node->right) : 0;
1929 while (priority < left || priority < right)
1931 if (left > right)
1932 node_rotate (node->left);
1933 else
1934 node_rotate (node->right);
1936 left = node->left ? get_priority (node->left) : 0;
1937 right = node->right ? get_priority (node->right) : 0;
1941 static void
1942 node_cut (GSequenceNode *node)
1944 while (node->parent)
1945 node_rotate (node);
1947 if (node->left)
1948 node->left->parent = NULL;
1950 node->left = NULL;
1951 node_update_fields (node);
1953 rotate_down (node, get_priority (node));
1956 static void
1957 node_join (GSequenceNode *left,
1958 GSequenceNode *right)
1960 GSequenceNode *fake = node_new (NULL);
1962 fake->left = find_root (left);
1963 fake->right = find_root (right);
1964 fake->left->parent = fake;
1965 fake->right->parent = fake;
1967 node_update_fields (fake);
1969 node_unlink (fake);
1971 node_free (fake, NULL);
1974 static void
1975 node_insert_before (GSequenceNode *node,
1976 GSequenceNode *new)
1978 new->left = node->left;
1979 if (new->left)
1980 new->left->parent = new;
1982 new->parent = node;
1983 node->left = new;
1985 node_update_fields_deep (new);
1987 while (new->parent && get_priority (new) > get_priority (new->parent))
1988 node_rotate (new);
1990 rotate_down (new, get_priority (new));
1993 static void
1994 node_unlink (GSequenceNode *node)
1996 rotate_down (node, 0);
1998 if (NODE_RIGHT_CHILD (node))
1999 node->parent->right = NULL;
2000 else if (NODE_LEFT_CHILD (node))
2001 node->parent->left = NULL;
2003 if (node->parent)
2004 node_update_fields_deep (node->parent);
2006 node->parent = NULL;
2009 static void
2010 node_insert_sorted (GSequenceNode *node,
2011 GSequenceNode *new,
2012 GSequenceNode *end,
2013 GSequenceIterCompareFunc iter_cmp,
2014 gpointer cmp_data)
2016 GSequenceNode *closest;
2018 closest = node_find_closest (node, new, end, iter_cmp, cmp_data);
2020 node_unlink (new);
2022 node_insert_before (closest, new);