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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
28 * @short_description: scalable lists
30 * The #GSequence data structure has the API of a list, but is
31 * implemented internally with a balanced binary tree. This means that
32 * it is possible to maintain a sorted list of n elements in time O(n
33 * log n). The data contained in each element can be either integer
34 * values, by using of the <link
35 * linkend="glib-Type-Conversion-Macros">Type Conversion Macros</link>,
36 * or simply pointers to any type of data.
38 * A #GSequence is accessed through <firstterm>iterators</firstterm>,
39 * represented by a #GSequenceIter. An iterator represents a position
40 * between two elements of the sequence. For example, the
41 * <firstterm>begin</firstterm> iterator represents the gap immediately
42 * before the first element of the sequence, and the
43 * <firstterm>end</firstterm> 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 <firstterm>point</firstterm> 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
79 * @Returns: zero if the iterators are equal, a negative value if @a
80 * comes before @b, and a positive value if @b comes before
83 * A #GSequenceIterCompareFunc is a function used to compare iterators.
84 * It must return zero if the iterators compare equal, a negative value
85 * if @a 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_closest (GSequenceNode *haystack,
135 GSequenceNode *needle,
137 GSequenceIterCompareFunc cmp,
139 static gint node_get_length (GSequenceNode *node);
140 static void node_free (GSequenceNode *node,
142 static void node_cut (GSequenceNode *split);
143 static void node_insert_before (GSequenceNode *node,
145 static void node_unlink (GSequenceNode *node);
146 static void node_join (GSequenceNode *left,
147 GSequenceNode *right);
148 static void node_insert_sorted (GSequenceNode *node,
151 GSequenceIterCompareFunc cmp_func,
156 * Various helper functions
159 check_seq_access (GSequence *seq)
161 if (G_UNLIKELY (seq->access_prohibited))
163 g_warning ("Accessing a sequence while it is "
164 "being sorted or searched is not allowed");
169 get_sequence (GSequenceNode *node)
171 return (GSequence *)node_get_last (node)->data;
175 check_iter_access (GSequenceIter *iter)
177 check_seq_access (get_sequence (iter));
181 is_end (GSequenceIter *iter)
191 if (iter->parent->right != iter)
194 seq = get_sequence (iter);
196 return seq->end_node == iter;
201 GCompareDataFunc cmp_func;
203 GSequenceNode *end_node;
206 /* This function compares two iters using a normal compare
207 * function and user_data passed in in a SortInfo struct
210 iter_compare (GSequenceIter *node1,
211 GSequenceIter *node2,
214 const SortInfo *info = data;
217 if (node1 == info->end_node)
220 if (node2 == info->end_node)
223 retval = info->cmp_func (node1->data, node2->data, info->cmp_data);
234 * @data_destroy: a #GDestroyNotify function, or %NULL
236 * Creates a new GSequence. The @data_destroy function, if non-%NULL will
237 * be called on all items when the sequence is destroyed and on items that
238 * are removed from the sequence.
240 * Return value: a new #GSequence
245 g_sequence_new (GDestroyNotify data_destroy)
247 GSequence *seq = g_new (GSequence, 1);
248 seq->data_destroy_notify = data_destroy;
250 seq->end_node = node_new (seq);
252 seq->access_prohibited = FALSE;
254 seq->real_sequence = seq;
263 * Frees the memory allocated for @seq. If @seq has a data destroy
264 * function associated with it, that function is called on all items in
270 g_sequence_free (GSequence *seq)
272 g_return_if_fail (seq != NULL);
274 check_seq_access (seq);
276 node_free (seq->end_node, seq);
282 * g_sequence_foreach_range:
283 * @begin: a #GSequenceIter
284 * @end: a #GSequenceIter
286 * @user_data: user data passed to @func
288 * Calls @func for each item in the range (@begin, @end) passing
289 * @user_data to the function.
294 g_sequence_foreach_range (GSequenceIter *begin,
302 g_return_if_fail (func != NULL);
303 g_return_if_fail (begin != NULL);
304 g_return_if_fail (end != NULL);
306 seq = get_sequence (begin);
308 seq->access_prohibited = TRUE;
313 GSequenceIter *next = node_get_next (iter);
315 func (iter->data, user_data);
320 seq->access_prohibited = FALSE;
324 * g_sequence_foreach:
326 * @func: the function to call for each item in @seq
327 * @user_data: user data passed to @func
329 * Calls @func for each item in the sequence passing @user_data
335 g_sequence_foreach (GSequence *seq,
339 GSequenceIter *begin, *end;
341 check_seq_access (seq);
343 begin = g_sequence_get_begin_iter (seq);
344 end = g_sequence_get_end_iter (seq);
346 g_sequence_foreach_range (begin, end, func, user_data);
350 * g_sequence_range_get_midpoint:
351 * @begin: a #GSequenceIter
352 * @end: a #GSequenceIter
354 * Finds an iterator somewhere in the range (@begin, @end). This
355 * iterator will be close to the middle of the range, but is not
356 * guaranteed to be <emphasis>exactly</emphasis> in the middle.
358 * The @begin and @end iterators must both point to the same sequence and
359 * @begin must come before or be equal to @end in the sequence.
361 * Return value: A #GSequenceIter pointing somewhere in the
362 * (@begin, @end) range.
367 g_sequence_range_get_midpoint (GSequenceIter *begin,
370 int begin_pos, end_pos, mid_pos;
372 g_return_val_if_fail (begin != NULL, NULL);
373 g_return_val_if_fail (end != NULL, NULL);
374 g_return_val_if_fail (get_sequence (begin) == get_sequence (end), NULL);
376 begin_pos = node_get_pos (begin);
377 end_pos = node_get_pos (end);
379 g_return_val_if_fail (end_pos >= begin_pos, NULL);
381 mid_pos = begin_pos + (end_pos - begin_pos) / 2;
383 return node_get_by_pos (begin, mid_pos);
387 * g_sequence_iter_compare:
388 * @a: a #GSequenceIter
389 * @b: a #GSequenceIter
391 * Returns a negative number if @a comes before @b, 0 if they are equal,
392 * and a positive number if @a comes after @b.
394 * The @a and @b iterators must point into the same sequence.
396 * Return value: A negative number if @a comes before @b, 0 if they are
397 * equal, and a positive number if @a comes after @b.
402 g_sequence_iter_compare (GSequenceIter *a,
407 g_return_val_if_fail (a != NULL, 0);
408 g_return_val_if_fail (b != NULL, 0);
409 g_return_val_if_fail (get_sequence (a) == get_sequence (b), 0);
411 check_iter_access (a);
412 check_iter_access (b);
414 a_pos = node_get_pos (a);
415 b_pos = node_get_pos (b);
419 else if (a_pos > b_pos)
427 * @seq: a #GSequencePointer
428 * @data: the data for the new item
430 * Adds a new item to the end of @seq.
432 * Return value: an iterator pointing to the new item
437 g_sequence_append (GSequence *seq,
442 g_return_val_if_fail (seq != NULL, NULL);
444 check_seq_access (seq);
446 node = node_new (data);
447 node_insert_before (seq->end_node, node);
453 * g_sequence_prepend:
455 * @data: the data for the new item
457 * Adds a new item to the front of @seq
459 * Return value: an iterator pointing to the new item
464 g_sequence_prepend (GSequence *seq,
467 GSequenceNode *node, *first;
469 g_return_val_if_fail (seq != NULL, NULL);
471 check_seq_access (seq);
473 node = node_new (data);
474 first = node_get_first (seq->end_node);
476 node_insert_before (first, node);
482 * g_sequence_insert_before:
483 * @iter: a #GSequenceIter
484 * @data: the data for the new item
486 * Inserts a new item just before the item pointed to by @iter.
488 * Return value: an iterator pointing to the new item
493 g_sequence_insert_before (GSequenceIter *iter,
498 g_return_val_if_fail (iter != NULL, NULL);
500 check_iter_access (iter);
502 node = node_new (data);
504 node_insert_before (iter, node);
511 * @iter: a #GSequenceIter
513 * Removes the item pointed to by @iter. It is an error to pass the
514 * end iterator to this function.
516 * If the sequnce has a data destroy function associated with it, this
517 * function is called on the data for the removed item.
522 g_sequence_remove (GSequenceIter *iter)
526 g_return_if_fail (iter != NULL);
527 g_return_if_fail (!is_end (iter));
529 check_iter_access (iter);
531 seq = get_sequence (iter);
534 node_free (iter, seq);
538 * g_sequence_remove_range:
539 * @begin: a #GSequenceIter
540 * @end: a #GSequenceIter
542 * Removes all items in the (@begin, @end) range.
544 * If the sequence has a data destroy function associated with it, this
545 * function is called on the data for the removed items.
550 g_sequence_remove_range (GSequenceIter *begin,
553 g_return_if_fail (get_sequence (begin) == get_sequence (end));
555 check_iter_access (begin);
556 check_iter_access (end);
558 g_sequence_move_range (NULL, begin, end);
562 * g_sequence_move_range:
563 * @dest: a #GSequenceIter
564 * @begin: a #GSequenceIter
565 * @end: a #GSequenceIter
567 * Inserts the (@begin, @end) range at the destination pointed to by ptr.
568 * The @begin and @end iters must point into the same sequence. It is
569 * allowed for @dest to point to a different sequence than the one pointed
570 * into by @begin and @end.
572 * If @dest is NULL, the range indicated by @begin and @end is
573 * removed from the sequence. If @dest iter points to a place within
574 * the (@begin, @end) range, the range does not move.
579 g_sequence_move_range (GSequenceIter *dest,
580 GSequenceIter *begin,
584 GSequenceNode *first;
586 g_return_if_fail (begin != NULL);
587 g_return_if_fail (end != NULL);
589 check_iter_access (begin);
590 check_iter_access (end);
592 check_iter_access (dest);
594 src_seq = get_sequence (begin);
596 g_return_if_fail (src_seq == get_sequence (end));
598 /* Dest points to begin or end? */
599 if (dest == begin || dest == end)
602 /* begin comes after end? */
603 if (g_sequence_iter_compare (begin, end) >= 0)
606 /* dest points somewhere in the (begin, end) range? */
607 if (dest && get_sequence (dest) == src_seq &&
608 g_sequence_iter_compare (dest, begin) > 0 &&
609 g_sequence_iter_compare (dest, end) < 0)
614 src_seq = get_sequence (begin);
616 first = node_get_first (begin);
623 node_join (first, end);
627 first = node_get_first (dest);
631 node_join (begin, dest);
634 node_join (first, begin);
638 node_free (begin, src_seq);
645 * @cmp_func: the #GCompareDataFunc used to sort @seq. This function is
646 * passed two items of @seq and should return 0 if they are equal,
647 * a negative value if the first comes before the second, and a
648 * positive value if the second comes before the first.
649 * @cmp_data: user data passed to @cmp_func
651 * Sorts @seq using @cmp_func.
656 g_sequence_sort (GSequence *seq,
657 GCompareDataFunc cmp_func,
662 info.cmp_func = cmp_func;
663 info.cmp_data = cmp_data;
664 info.end_node = seq->end_node;
666 check_seq_access (seq);
668 g_sequence_sort_iter (seq, iter_compare, &info);
672 * g_sequence_insert_sorted:
674 * @data: the data to insert
675 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
676 * is called with two items of the @seq and @user_data. It should
677 * return 0 if the items are equal, a negative value if the first
678 * item comes before the second, and a positive value if the second
679 * item comes before the first.
680 * @cmp_data: user data passed to @cmp_func.
682 * Inserts @data into @sequence using @func to determine the new position.
683 * The sequence must already be sorted according to @cmp_func; otherwise the
684 * new position of @data is undefined.
686 * Return value: a #GSequenceIter pointing to the new item.
691 g_sequence_insert_sorted (GSequence *seq,
693 GCompareDataFunc cmp_func,
698 g_return_val_if_fail (seq != NULL, NULL);
699 g_return_val_if_fail (cmp_func != NULL, NULL);
701 info.cmp_func = cmp_func;
702 info.cmp_data = cmp_data;
703 info.end_node = seq->end_node;
704 check_seq_access (seq);
706 return g_sequence_insert_sorted_iter (seq, data, iter_compare, &info);
710 * g_sequence_sort_changed:
711 * @iter: A #GSequenceIter
712 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
713 * is called with two items of the @seq and @user_data. It should
714 * return 0 if the items are equal, a negative value if the first
715 * item comes before the second, and a positive value if the second
716 * item comes before the first.
717 * @cmp_data: user data passed to @cmp_func.
719 * Moves the data pointed to a new position as indicated by @cmp_func. This
720 * function should be called for items in a sequence already sorted according
721 * to @cmp_func whenever some aspect of an item changes so that @cmp_func
722 * may return different values for that item.
727 g_sequence_sort_changed (GSequenceIter *iter,
728 GCompareDataFunc cmp_func,
733 g_return_if_fail (!is_end (iter));
735 info.cmp_func = cmp_func;
736 info.cmp_data = cmp_data;
737 info.end_node = get_sequence (iter)->end_node;
738 check_iter_access (iter);
740 g_sequence_sort_changed_iter (iter, iter_compare, &info);
746 * @data: data for the new item
747 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
748 * is called with two items of the @seq and @user_data. It should
749 * return 0 if the items are equal, a negative value if the first
750 * item comes before the second, and a positive value if the second
751 * item comes before the first.
752 * @cmp_data: user data passed to @cmp_func.
754 * Returns an iterator pointing to the position where @data would
755 * be inserted according to @cmp_func and @cmp_data.
757 * Return value: an #GSequenceIter pointing to the position where @data
758 * would have been inserted according to @cmp_func and @cmp_data.
763 g_sequence_search (GSequence *seq,
765 GCompareDataFunc cmp_func,
770 g_return_val_if_fail (seq != NULL, NULL);
772 info.cmp_func = cmp_func;
773 info.cmp_data = cmp_data;
774 info.end_node = seq->end_node;
775 check_seq_access (seq);
777 return g_sequence_search_iter (seq, data, iter_compare, &info);
781 * g_sequence_sort_iter:
783 * @cmp_func: the #GSequenceItercompare used to compare iterators in the
784 * sequence. It is called with two iterators pointing into @seq. It should
785 * return 0 if the iterators are equal, a negative value if the first
786 * iterator comes before the second, and a positive value if the second
787 * iterator comes before the first.
788 * @cmp_data: user data passed to @cmp_func
790 * Like g_sequence_sort(), but uses a #GSequenceIterCompareFunc instead
791 * of a GCompareDataFunc as the compare function
796 g_sequence_sort_iter (GSequence *seq,
797 GSequenceIterCompareFunc cmp_func,
801 GSequenceNode *begin, *end;
803 g_return_if_fail (seq != NULL);
804 g_return_if_fail (cmp_func != NULL);
806 check_seq_access (seq);
808 begin = g_sequence_get_begin_iter (seq);
809 end = g_sequence_get_end_iter (seq);
811 tmp = g_sequence_new (NULL);
812 tmp->real_sequence = seq;
814 g_sequence_move_range (g_sequence_get_begin_iter (tmp), begin, end);
816 seq->access_prohibited = TRUE;
817 tmp->access_prohibited = TRUE;
819 while (g_sequence_get_length (tmp) > 0)
821 GSequenceNode *node = g_sequence_get_begin_iter (tmp);
823 node_insert_sorted (seq->end_node, node, seq->end_node,
827 tmp->access_prohibited = FALSE;
828 seq->access_prohibited = FALSE;
830 g_sequence_free (tmp);
834 * g_sequence_sort_changed_iter:
835 * @iter: a #GSequenceIter
836 * @iter_cmp: the #GSequenceItercompare used to compare iterators in the
837 * sequence. It is called with two iterators pointing into @seq. It should
838 * return 0 if the iterators are equal, a negative value if the first
839 * iterator comes before the second, and a positive value if the second
840 * iterator comes before the first.
841 * @cmp_data: user data passed to @cmp_func
843 * Like g_sequence_sort_changed(), but uses
844 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
845 * the compare function.
850 g_sequence_sort_changed_iter (GSequenceIter *iter,
851 GSequenceIterCompareFunc iter_cmp,
854 GSequence *seq, *tmp_seq;
855 GSequenceIter *next, *prev;
857 g_return_if_fail (iter != NULL);
858 g_return_if_fail (!is_end (iter));
859 g_return_if_fail (iter_cmp != NULL);
860 check_iter_access (iter);
862 /* If one of the neighbours is equal to iter, then
863 * don't move it. This ensures that sort_changed() is
864 * a stable operation.
867 next = node_get_next (iter);
868 prev = node_get_prev (iter);
870 if (prev != iter && iter_cmp (prev, iter, cmp_data) == 0)
873 if (!is_end (next) && iter_cmp (next, iter, cmp_data) == 0)
876 seq = get_sequence (iter);
878 seq->access_prohibited = TRUE;
880 tmp_seq = g_sequence_new (NULL);
881 tmp_seq->real_sequence = seq;
884 node_insert_before (tmp_seq->end_node, iter);
886 node_insert_sorted (seq->end_node, iter, seq->end_node,
889 g_sequence_free (tmp_seq);
891 seq->access_prohibited = FALSE;
895 * g_sequence_insert_sorted_iter:
897 * @data: data for the new item
898 * @iter_cmp: the #GSequenceItercompare used to compare iterators in the
899 * sequence. It is called with two iterators pointing into @seq. It should
900 * return 0 if the iterators are equal, a negative value if the first
901 * iterator comes before the second, and a positive value if the second
902 * iterator comes before the first.
903 * @cmp_data: user data passed to @cmp_func
905 * Like g_sequence_insert_sorted(), but uses
906 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
907 * the compare function.
909 * Return value: a #GSequenceIter pointing to the new item
914 g_sequence_insert_sorted_iter (GSequence *seq,
916 GSequenceIterCompareFunc iter_cmp,
919 GSequenceNode *new_node;
922 g_return_val_if_fail (seq != NULL, NULL);
923 g_return_val_if_fail (iter_cmp != NULL, NULL);
925 check_seq_access (seq);
927 seq->access_prohibited = TRUE;
929 /* Create a new temporary sequence and put the new node into
930 * that. The reason for this is that the user compare function
931 * will be called with the new node, and if it dereferences,
932 * "is_end" will be called on it. But that will crash if the
933 * node is not actually in a sequence.
935 * node_insert_sorted() makes sure the node is unlinked before
938 * The reason we need the "iter" versions at all is that that
939 * is the only kind of compare functions GtkTreeView can use.
941 tmp_seq = g_sequence_new (NULL);
942 tmp_seq->real_sequence = seq;
944 new_node = g_sequence_append (tmp_seq, data);
946 node_insert_sorted (seq->end_node, new_node,
947 seq->end_node, iter_cmp, cmp_data);
949 g_sequence_free (tmp_seq);
951 seq->access_prohibited = FALSE;
957 * g_sequence_search_iter:
959 * @data: data for the new item
960 * @iter_cmp: the #GSequenceIterCompare function used to compare iterators
961 * in the sequence. It is called with two iterators pointing into @seq.
962 * It should return 0 if the iterators are equal, a negative value if the
963 * first iterator comes before the second, and a positive value if the
964 * second iterator comes before the first.
965 * @cmp_data: user data passed to @iter_cmp
967 * Like g_sequence_search(), but uses
968 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
969 * the compare function.
971 * Return value: a #GSequenceIter pointing to the position in @seq
972 * where @data would have been inserted according to @iter_cmp and @cmp_data.
977 g_sequence_search_iter (GSequence *seq,
979 GSequenceIterCompareFunc iter_cmp,
983 GSequenceNode *dummy;
986 g_return_val_if_fail (seq != NULL, NULL);
988 check_seq_access (seq);
990 seq->access_prohibited = TRUE;
992 tmp_seq = g_sequence_new (NULL);
993 tmp_seq->real_sequence = seq;
995 dummy = g_sequence_append (tmp_seq, data);
997 node = node_find_closest (seq->end_node, dummy,
998 seq->end_node, iter_cmp, cmp_data);
1000 g_sequence_free (tmp_seq);
1002 seq->access_prohibited = FALSE;
1008 * g_sequence_iter_get_sequence:
1009 * @iter: a #GSequenceIter
1011 * Returns the #GSequence that @iter points into.
1013 * Return value: the #GSequence that @iter points into.
1018 g_sequence_iter_get_sequence (GSequenceIter *iter)
1022 g_return_val_if_fail (iter != NULL, NULL);
1024 seq = get_sequence (iter);
1026 /* For temporary sequences, this points to the sequence that
1027 * is actually being manipulated
1029 return seq->real_sequence;
1034 * @iter: a #GSequenceIter
1036 * Returns the data that @iter points to.
1038 * Return value: the data that @iter points to
1043 g_sequence_get (GSequenceIter *iter)
1045 g_return_val_if_fail (iter != NULL, NULL);
1046 g_return_val_if_fail (!is_end (iter), NULL);
1053 * @iter: a #GSequenceIter
1054 * @data: new data for the item
1056 * Changes the data for the item pointed to by @iter to be @data. If
1057 * the sequence has a data destroy function associated with it, that
1058 * function is called on the existing data that @iter pointed to.
1063 g_sequence_set (GSequenceIter *iter,
1068 g_return_if_fail (iter != NULL);
1069 g_return_if_fail (!is_end (iter));
1071 seq = get_sequence (iter);
1073 /* If @data is identical to iter->data, it is destroyed
1074 * here. This will work right in case of ref-counted objects. Also
1075 * it is similar to what ghashtables do.
1077 * For non-refcounted data it's a little less convenient, but
1078 * code relying on self-setting not destroying would be
1079 * pretty dubious anyway ...
1082 if (seq->data_destroy_notify)
1083 seq->data_destroy_notify (iter->data);
1089 * g_sequence_get_length:
1090 * @seq: a #GSequence
1092 * Returns the length of @seq
1094 * Return value: the length of @seq
1099 g_sequence_get_length (GSequence *seq)
1101 return node_get_length (seq->end_node) - 1;
1105 * g_sequence_get_end_iter:
1106 * @seq: a #GSequence
1108 * Returns the end iterator for @seg
1110 * Return value: the end iterator for @seq
1115 g_sequence_get_end_iter (GSequence *seq)
1117 g_return_val_if_fail (seq != NULL, NULL);
1119 return seq->end_node;
1123 * g_sequence_get_begin_iter:
1124 * @seq: a #GSequence
1126 * Returns the begin iterator for @seq.
1128 * Return value: the begin iterator for @seq.
1133 g_sequence_get_begin_iter (GSequence *seq)
1135 g_return_val_if_fail (seq != NULL, NULL);
1137 return node_get_first (seq->end_node);
1141 clamp_position (GSequence *seq,
1144 gint len = g_sequence_get_length (seq);
1146 if (pos > len || pos < 0)
1153 * if pos > number of items or -1, will return end pointer
1156 * g_sequence_get_iter_at_pos:
1157 * @seq: a #GSequence
1158 * @pos: a position in @seq, or -1 for the end.
1160 * Returns the iterator at position @pos. If @pos is negative or larger
1161 * than the number of items in @seq, the end iterator is returned.
1163 * Return value: The #GSequenceIter at position @pos
1168 g_sequence_get_iter_at_pos (GSequence *seq,
1171 g_return_val_if_fail (seq != NULL, NULL);
1173 pos = clamp_position (seq, pos);
1175 return node_get_by_pos (seq->end_node, pos);
1180 * @src: a #GSequenceIter pointing to the item to move
1181 * @dest: a #GSequenceIter pointing to the position to which
1182 * the item is moved.
1184 * Moves the item pointed to by @src to the position indicated by @dest.
1185 * After calling this function @dest will point to the position immediately
1186 * after @src. It is allowed for @src and @dest to point into different
1192 g_sequence_move (GSequenceIter *src,
1193 GSequenceIter *dest)
1195 g_return_if_fail (src != NULL);
1196 g_return_if_fail (dest != NULL);
1197 g_return_if_fail (!is_end (src));
1203 node_insert_before (dest, src);
1209 * g_sequence_iter_is_end:
1210 * @iter: a #GSequenceIter
1212 * Returns whether @iter is the end iterator
1214 * Return value: Whether @iter is the end iterator.
1219 g_sequence_iter_is_end (GSequenceIter *iter)
1221 g_return_val_if_fail (iter != NULL, FALSE);
1223 return is_end (iter);
1227 * g_sequence_iter_is_begin:
1228 * @iter: a #GSequenceIter
1230 * Returns whether @iter is the begin iterator
1232 * Return value: whether @iter is the begin iterator
1237 g_sequence_iter_is_begin (GSequenceIter *iter)
1239 g_return_val_if_fail (iter != NULL, FALSE);
1241 return (node_get_prev (iter) == iter);
1245 * g_sequence_iter_get_position:
1246 * @iter: a #GSequenceIter
1248 * Returns the position of @iter
1250 * Return value: the position of @iter
1255 g_sequence_iter_get_position (GSequenceIter *iter)
1257 g_return_val_if_fail (iter != NULL, -1);
1259 return node_get_pos (iter);
1263 * g_sequence_iter_next:
1264 * @iter: a #GSequenceIter
1266 * Returns an iterator pointing to the next position after @iter. If
1267 * @iter is the end iterator, the end iterator is returned.
1269 * Return value: a #GSequenceIter pointing to the next position after @iter.
1274 g_sequence_iter_next (GSequenceIter *iter)
1276 g_return_val_if_fail (iter != NULL, NULL);
1278 return node_get_next (iter);
1282 * g_sequence_iter_prev:
1283 * @iter: a #GSequenceIter
1285 * Returns an iterator pointing to the previous position before @iter. If
1286 * @iter is the begin iterator, the begin iterator is returned.
1288 * Return value: a #GSequenceIter pointing to the previous position before
1294 g_sequence_iter_prev (GSequenceIter *iter)
1296 g_return_val_if_fail (iter != NULL, NULL);
1298 return node_get_prev (iter);
1302 * g_sequence_iter_move:
1303 * @iter: a #GSequenceIter
1304 * @delta: A positive or negative number indicating how many positions away
1305 * from @iter the returned #GSequenceIter will be.
1307 * Returns the #GSequenceIter which is @delta positions away from @iter.
1308 * If @iter is closer than -@delta positions to the beginning of the sequence,
1309 * the begin iterator is returned. If @iter is closer than @delta positions
1310 * to the end of the sequence, the end iterator is returned.
1312 * Return value: a #GSequenceIter which is @delta positions away from @iter.
1317 g_sequence_iter_move (GSequenceIter *iter,
1322 g_return_val_if_fail (iter != NULL, NULL);
1324 new_pos = node_get_pos (iter) + delta;
1326 new_pos = clamp_position (get_sequence (iter), new_pos);
1328 return node_get_by_pos (iter, new_pos);
1333 * @a: a #GSequenceIter
1334 * @b: a #GSequenceIter
1336 * Swaps the items pointed to by @a and @b. It is allowed for @a and @b
1337 * to point into difference sequences.
1342 g_sequence_swap (GSequenceIter *a,
1345 GSequenceNode *leftmost, *rightmost, *rightmost_next;
1348 g_return_if_fail (!g_sequence_iter_is_end (a));
1349 g_return_if_fail (!g_sequence_iter_is_end (b));
1354 a_pos = g_sequence_iter_get_position (a);
1355 b_pos = g_sequence_iter_get_position (b);
1368 rightmost_next = node_get_next (rightmost);
1370 /* The situation is now like this:
1372 * ..., leftmost, ......., rightmost, rightmost_next, ...
1375 g_sequence_move (rightmost, leftmost);
1376 g_sequence_move (leftmost, rightmost_next);
1380 * Implementation of a treap
1385 get_priority (GSequenceNode *node)
1387 guint key = GPOINTER_TO_UINT (node);
1389 /* This hash function is based on one found on Thomas Wang's
1392 * http://www.concentric.net/~Ttwang/tech/inthash.htm
1395 key = (key << 15) - key - 1;
1396 key = key ^ (key >> 12);
1397 key = key + (key << 2);
1398 key = key ^ (key >> 4);
1399 key = key + (key << 3) + (key << 11);
1400 key = key ^ (key >> 16);
1402 /* We rely on 0 being less than all other priorities */
1403 return key? key : 1;
1406 static GSequenceNode *
1407 find_root (GSequenceNode *node)
1409 while (node->parent)
1410 node = node->parent;
1415 static GSequenceNode *
1416 node_new (gpointer data)
1418 GSequenceNode *node = g_slice_new0 (GSequenceNode);
1424 node->parent = NULL;
1429 static GSequenceNode *
1430 node_get_first (GSequenceNode *node)
1432 node = find_root (node);
1440 static GSequenceNode *
1441 node_get_last (GSequenceNode *node)
1443 node = find_root (node);
1451 #define NODE_LEFT_CHILD(n) (((n)->parent) && ((n)->parent->left) == (n))
1452 #define NODE_RIGHT_CHILD(n) (((n)->parent) && ((n)->parent->right) == (n))
1454 static GSequenceNode *
1455 node_get_next (GSequenceNode *node)
1457 GSequenceNode *n = node;
1467 while (NODE_RIGHT_CHILD (n))
1479 static GSequenceNode *
1480 node_get_prev (GSequenceNode *node)
1482 GSequenceNode *n = node;
1492 while (NODE_LEFT_CHILD (n))
1504 #define N_NODES(n) ((n)? (n)->n_nodes : 0)
1507 node_get_pos (GSequenceNode *node)
1512 n_smaller = node->left->n_nodes;
1516 if (NODE_RIGHT_CHILD (node))
1517 n_smaller += N_NODES (node->parent->left) + 1;
1519 node = node->parent;
1525 static GSequenceNode *
1526 node_get_by_pos (GSequenceNode *node,
1531 node = find_root (node);
1533 while ((i = N_NODES (node->left)) != pos)
1549 static GSequenceNode *
1550 node_find_closest (GSequenceNode *haystack,
1551 GSequenceNode *needle,
1553 GSequenceIterCompareFunc iter_cmp,
1556 GSequenceNode *best;
1559 haystack = find_root (haystack);
1565 /* iter_cmp can't be passed the end node, since the function may
1568 if (haystack == end)
1571 c = iter_cmp (haystack, needle, cmp_data);
1573 /* In the following we don't break even if c == 0. Instaed we go on
1574 * searching along the 'bigger' nodes, so that we find the last one
1575 * that is equal to the needle.
1578 haystack = haystack->left;
1580 haystack = haystack->right;
1582 while (haystack != NULL);
1584 /* If the best node is smaller or equal to the data, then move one step
1585 * to the right to make sure the best one is strictly bigger than the data
1587 if (best != end && c <= 0)
1588 best = node_get_next (best);
1594 node_get_length (GSequenceNode *node)
1596 node = find_root (node);
1598 return node->n_nodes;
1602 real_node_free (GSequenceNode *node,
1607 real_node_free (node->left, seq);
1608 real_node_free (node->right, seq);
1610 if (seq && seq->data_destroy_notify && node != seq->end_node)
1611 seq->data_destroy_notify (node->data);
1613 g_slice_free (GSequenceNode, node);
1618 node_free (GSequenceNode *node,
1621 node = find_root (node);
1623 real_node_free (node, seq);
1627 node_update_fields (GSequenceNode *node)
1631 n_nodes += N_NODES (node->left);
1632 n_nodes += N_NODES (node->right);
1634 node->n_nodes = n_nodes;
1638 node_rotate (GSequenceNode *node)
1640 GSequenceNode *tmp, *old;
1642 g_assert (node->parent);
1643 g_assert (node->parent != node);
1645 if (NODE_LEFT_CHILD (node))
1650 node->right = node->parent;
1651 node->parent = node->parent->parent;
1654 if (node->parent->left == node->right)
1655 node->parent->left = node;
1657 node->parent->right = node;
1660 g_assert (node->right);
1662 node->right->parent = node;
1663 node->right->left = tmp;
1665 if (node->right->left)
1666 node->right->left->parent = node->right;
1675 node->left = node->parent;
1676 node->parent = node->parent->parent;
1679 if (node->parent->right == node->left)
1680 node->parent->right = node;
1682 node->parent->left = node;
1685 g_assert (node->left);
1687 node->left->parent = node;
1688 node->left->right = tmp;
1690 if (node->left->right)
1691 node->left->right->parent = node->left;
1696 node_update_fields (old);
1697 node_update_fields (node);
1701 node_update_fields_deep (GSequenceNode *node)
1705 node_update_fields (node);
1707 node_update_fields_deep (node->parent);
1712 rotate_down (GSequenceNode *node,
1717 left = node->left ? get_priority (node->left) : 0;
1718 right = node->right ? get_priority (node->right) : 0;
1720 while (priority < left || priority < right)
1723 node_rotate (node->left);
1725 node_rotate (node->right);
1727 left = node->left ? get_priority (node->left) : 0;
1728 right = node->right ? get_priority (node->right) : 0;
1733 node_cut (GSequenceNode *node)
1735 while (node->parent)
1739 node->left->parent = NULL;
1742 node_update_fields (node);
1744 rotate_down (node, get_priority (node));
1748 node_join (GSequenceNode *left,
1749 GSequenceNode *right)
1751 GSequenceNode *fake = node_new (NULL);
1753 fake->left = find_root (left);
1754 fake->right = find_root (right);
1755 fake->left->parent = fake;
1756 fake->right->parent = fake;
1758 node_update_fields (fake);
1762 node_free (fake, NULL);
1766 node_insert_before (GSequenceNode *node,
1769 new->left = node->left;
1771 new->left->parent = new;
1776 node_update_fields_deep (new);
1778 while (new->parent && get_priority (new) > get_priority (new->parent))
1781 rotate_down (new, get_priority (new));
1785 node_unlink (GSequenceNode *node)
1787 rotate_down (node, 0);
1789 if (NODE_RIGHT_CHILD (node))
1790 node->parent->right = NULL;
1791 else if (NODE_LEFT_CHILD (node))
1792 node->parent->left = NULL;
1795 node_update_fields_deep (node->parent);
1797 node->parent = NULL;
1801 node_insert_sorted (GSequenceNode *node,
1804 GSequenceIterCompareFunc iter_cmp,
1807 GSequenceNode *closest;
1809 closest = node_find_closest (node, new, end, iter_cmp, cmp_data);
1813 node_insert_before (closest, new);