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.
29 * @short_description: scalable lists
31 * The #GSequence data structure has the API of a list, but is
32 * implemented internally with a balanced binary tree. This means that
33 * it is possible to maintain a sorted list of n elements in time O(n
34 * log n). The data contained in each element can be either integer
35 * values, by using of the <link
36 * linkend="glib-Type-Conversion-Macros">Type Conversion Macros</link>,
37 * or simply pointers to any type of data.
39 * A #GSequence is accessed through <firstterm>iterators</firstterm>,
40 * represented by a #GSequenceIter. An iterator represents a position
41 * between two elements of the sequence. For example, the
42 * <firstterm>begin</firstterm> iterator represents the gap immediately
43 * before the first element of the sequence, and the
44 * <firstterm>end</firstterm> iterator represents the gap immediately
45 * after the last element. In an empty sequence, the begin and end
46 * iterators are the same.
48 * Some methods on #GSequence operate on ranges of items. For example
49 * g_sequence_foreach_range() will call a user-specified function on
50 * each element with the given range. The range is delimited by the
51 * gaps represented by the passed-in iterators, so if you pass in the
52 * begin and end iterators, the range in question is the entire
55 * The function g_sequence_get() is used with an iterator to access the
56 * element immediately following the gap that the iterator represents.
57 * The iterator is said to <firstterm>point</firstterm> to that element.
59 * Iterators are stable across most operations on a #GSequence. For
60 * example an iterator pointing to some element of a sequence will
61 * continue to point to that element even after the sequence is sorted.
62 * Even moving an element to another sequence using for example
63 * g_sequence_move_range() will not invalidate the iterators pointing
64 * to it. The only operation that will invalidate an iterator is when
65 * the element it points to is removed from any sequence.
71 * The #GSequenceIter struct is an opaque data type representing an
72 * iterator pointing into a #GSequence.
76 * GSequenceIterCompareFunc:
77 * @a: a #GSequenceIter
78 * @b: a #GSequenceIter
80 * @Returns: zero if the iterators are equal, a negative value if @a
81 * comes before @b, and a positive value if @b comes before
84 * A #GSequenceIterCompareFunc is a function used to compare iterators.
85 * It must return zero if the iterators compare equal, a negative value
86 * if @a comes before @b, and a positive value if @b comes before @a.
89 typedef struct _GSequenceNode GSequenceNode;
94 * The #GSequence struct is an opaque data type representing a
95 * <link linkend="glib-Sequences">Sequence</link> data type.
99 GSequenceNode * end_node;
100 GDestroyNotify data_destroy_notify;
101 gboolean access_prohibited;
103 /* The 'real_sequence' is used when temporary sequences are created
104 * to hold nodes that are being rearranged. The 'real_sequence' of such
105 * a temporary sequence points to the sequence that is actually being
106 * manipulated. The only reason we need this is so that when the
107 * sort/sort_changed/search_iter() functions call out to the application
108 * g_sequence_iter_get_sequence() will return the correct sequence.
110 GSequence * real_sequence;
113 struct _GSequenceNode
116 GSequenceNode * parent;
117 GSequenceNode * left;
118 GSequenceNode * right;
119 gpointer data; /* For the end node, this field points
125 * Declaration of GSequenceNode methods
127 static GSequenceNode *node_new (gpointer data);
128 static GSequenceNode *node_get_first (GSequenceNode *node);
129 static GSequenceNode *node_get_last (GSequenceNode *node);
130 static GSequenceNode *node_get_prev (GSequenceNode *node);
131 static GSequenceNode *node_get_next (GSequenceNode *node);
132 static gint node_get_pos (GSequenceNode *node);
133 static GSequenceNode *node_get_by_pos (GSequenceNode *node,
135 static GSequenceNode *node_find_closest (GSequenceNode *haystack,
136 GSequenceNode *needle,
138 GSequenceIterCompareFunc cmp,
140 static gint node_get_length (GSequenceNode *node);
141 static void node_free (GSequenceNode *node,
143 static void node_cut (GSequenceNode *split);
144 static void node_insert_before (GSequenceNode *node,
146 static void node_unlink (GSequenceNode *node);
147 static void node_join (GSequenceNode *left,
148 GSequenceNode *right);
149 static void node_insert_sorted (GSequenceNode *node,
152 GSequenceIterCompareFunc cmp_func,
157 * Various helper functions
160 check_seq_access (GSequence *seq)
162 if (G_UNLIKELY (seq->access_prohibited))
164 g_warning ("Accessing a sequence while it is "
165 "being sorted or searched is not allowed");
170 get_sequence (GSequenceNode *node)
172 return (GSequence *)node_get_last (node)->data;
176 check_iter_access (GSequenceIter *iter)
178 check_seq_access (get_sequence (iter));
182 is_end (GSequenceIter *iter)
192 if (iter->parent->right != iter)
195 seq = get_sequence (iter);
197 return seq->end_node == iter;
202 GCompareDataFunc cmp_func;
204 GSequenceNode *end_node;
207 /* This function compares two iters using a normal compare
208 * function and user_data passed in in a SortInfo struct
211 iter_compare (GSequenceIter *node1,
212 GSequenceIter *node2,
215 const SortInfo *info = data;
218 if (node1 == info->end_node)
221 if (node2 == info->end_node)
224 retval = info->cmp_func (node1->data, node2->data, info->cmp_data);
235 * @data_destroy: a #GDestroyNotify function, or %NULL
237 * Creates a new GSequence. The @data_destroy function, if non-%NULL will
238 * be called on all items when the sequence is destroyed and on items that
239 * are removed from the sequence.
241 * Return value: a new #GSequence
246 g_sequence_new (GDestroyNotify data_destroy)
248 GSequence *seq = g_new (GSequence, 1);
249 seq->data_destroy_notify = data_destroy;
251 seq->end_node = node_new (seq);
253 seq->access_prohibited = FALSE;
255 seq->real_sequence = seq;
264 * Frees the memory allocated for @seq. If @seq has a data destroy
265 * function associated with it, that function is called on all items in
271 g_sequence_free (GSequence *seq)
273 g_return_if_fail (seq != NULL);
275 check_seq_access (seq);
277 node_free (seq->end_node, seq);
283 * g_sequence_foreach_range:
284 * @begin: a #GSequenceIter
285 * @end: a #GSequenceIter
287 * @user_data: user data passed to @func
289 * Calls @func for each item in the range (@begin, @end) passing
290 * @user_data to the function.
295 g_sequence_foreach_range (GSequenceIter *begin,
303 g_return_if_fail (func != NULL);
304 g_return_if_fail (begin != NULL);
305 g_return_if_fail (end != NULL);
307 seq = get_sequence (begin);
309 seq->access_prohibited = TRUE;
314 GSequenceIter *next = node_get_next (iter);
316 func (iter->data, user_data);
321 seq->access_prohibited = FALSE;
325 * g_sequence_foreach:
327 * @func: the function to call for each item in @seq
328 * @user_data: user data passed to @func
330 * Calls @func for each item in the sequence passing @user_data
336 g_sequence_foreach (GSequence *seq,
340 GSequenceIter *begin, *end;
342 check_seq_access (seq);
344 begin = g_sequence_get_begin_iter (seq);
345 end = g_sequence_get_end_iter (seq);
347 g_sequence_foreach_range (begin, end, func, user_data);
351 * g_sequence_range_get_midpoint:
352 * @begin: a #GSequenceIter
353 * @end: a #GSequenceIter
355 * Finds an iterator somewhere in the range (@begin, @end). This
356 * iterator will be close to the middle of the range, but is not
357 * guaranteed to be <emphasis>exactly</emphasis> in the middle.
359 * The @begin and @end iterators must both point to the same sequence and
360 * @begin must come before or be equal to @end in the sequence.
362 * Return value: A #GSequenceIter pointing somewhere in the
363 * (@begin, @end) range.
368 g_sequence_range_get_midpoint (GSequenceIter *begin,
371 int begin_pos, end_pos, mid_pos;
373 g_return_val_if_fail (begin != NULL, NULL);
374 g_return_val_if_fail (end != NULL, NULL);
375 g_return_val_if_fail (get_sequence (begin) == get_sequence (end), NULL);
377 begin_pos = node_get_pos (begin);
378 end_pos = node_get_pos (end);
380 g_return_val_if_fail (end_pos >= begin_pos, NULL);
382 mid_pos = begin_pos + (end_pos - begin_pos) / 2;
384 return node_get_by_pos (begin, mid_pos);
388 * g_sequence_iter_compare:
389 * @a: a #GSequenceIter
390 * @b: a #GSequenceIter
392 * Returns a negative number if @a comes before @b, 0 if they are equal,
393 * and a positive number if @a comes after @b.
395 * The @a and @b iterators must point into the same sequence.
397 * Return value: A negative number if @a comes before @b, 0 if they are
398 * equal, and a positive number if @a comes after @b.
403 g_sequence_iter_compare (GSequenceIter *a,
408 g_return_val_if_fail (a != NULL, 0);
409 g_return_val_if_fail (b != NULL, 0);
410 g_return_val_if_fail (get_sequence (a) == get_sequence (b), 0);
412 check_iter_access (a);
413 check_iter_access (b);
415 a_pos = node_get_pos (a);
416 b_pos = node_get_pos (b);
420 else if (a_pos > b_pos)
428 * @seq: a #GSequencePointer
429 * @data: the data for the new item
431 * Adds a new item to the end of @seq.
433 * Return value: an iterator pointing to the new item
438 g_sequence_append (GSequence *seq,
443 g_return_val_if_fail (seq != NULL, NULL);
445 check_seq_access (seq);
447 node = node_new (data);
448 node_insert_before (seq->end_node, node);
454 * g_sequence_prepend:
456 * @data: the data for the new item
458 * Adds a new item to the front of @seq
460 * Return value: an iterator pointing to the new item
465 g_sequence_prepend (GSequence *seq,
468 GSequenceNode *node, *first;
470 g_return_val_if_fail (seq != NULL, NULL);
472 check_seq_access (seq);
474 node = node_new (data);
475 first = node_get_first (seq->end_node);
477 node_insert_before (first, node);
483 * g_sequence_insert_before:
484 * @iter: a #GSequenceIter
485 * @data: the data for the new item
487 * Inserts a new item just before the item pointed to by @iter.
489 * Return value: an iterator pointing to the new item
494 g_sequence_insert_before (GSequenceIter *iter,
499 g_return_val_if_fail (iter != NULL, NULL);
501 check_iter_access (iter);
503 node = node_new (data);
505 node_insert_before (iter, node);
512 * @iter: a #GSequenceIter
514 * Removes the item pointed to by @iter. It is an error to pass the
515 * end iterator to this function.
517 * If the sequnce has a data destroy function associated with it, this
518 * function is called on the data for the removed item.
523 g_sequence_remove (GSequenceIter *iter)
527 g_return_if_fail (iter != NULL);
528 g_return_if_fail (!is_end (iter));
530 check_iter_access (iter);
532 seq = get_sequence (iter);
535 node_free (iter, seq);
539 * g_sequence_remove_range:
540 * @begin: a #GSequenceIter
541 * @end: a #GSequenceIter
543 * Removes all items in the (@begin, @end) range.
545 * If the sequence has a data destroy function associated with it, this
546 * function is called on the data for the removed items.
551 g_sequence_remove_range (GSequenceIter *begin,
554 g_return_if_fail (get_sequence (begin) == get_sequence (end));
556 check_iter_access (begin);
557 check_iter_access (end);
559 g_sequence_move_range (NULL, begin, end);
563 * g_sequence_move_range:
564 * @dest: a #GSequenceIter
565 * @begin: a #GSequenceIter
566 * @end: a #GSequenceIter
568 * Inserts the (@begin, @end) range at the destination pointed to by ptr.
569 * The @begin and @end iters must point into the same sequence. It is
570 * allowed for @dest to point to a different sequence than the one pointed
571 * into by @begin and @end.
573 * If @dest is NULL, the range indicated by @begin and @end is
574 * removed from the sequence. If @dest iter points to a place within
575 * the (@begin, @end) range, the range does not move.
580 g_sequence_move_range (GSequenceIter *dest,
581 GSequenceIter *begin,
585 GSequenceNode *first;
587 g_return_if_fail (begin != NULL);
588 g_return_if_fail (end != NULL);
590 check_iter_access (begin);
591 check_iter_access (end);
593 check_iter_access (dest);
595 src_seq = get_sequence (begin);
597 g_return_if_fail (src_seq == get_sequence (end));
599 /* Dest points to begin or end? */
600 if (dest == begin || dest == end)
603 /* begin comes after end? */
604 if (g_sequence_iter_compare (begin, end) >= 0)
607 /* dest points somewhere in the (begin, end) range? */
608 if (dest && get_sequence (dest) == src_seq &&
609 g_sequence_iter_compare (dest, begin) > 0 &&
610 g_sequence_iter_compare (dest, end) < 0)
615 src_seq = get_sequence (begin);
617 first = node_get_first (begin);
624 node_join (first, end);
628 first = node_get_first (dest);
632 node_join (begin, dest);
635 node_join (first, begin);
639 node_free (begin, src_seq);
646 * @cmp_func: the #GCompareDataFunc used to sort @seq. This function is
647 * passed two items of @seq and should return 0 if they are equal,
648 * a negative value if the first comes before the second, and a
649 * positive value if the second comes before the first.
650 * @cmp_data: user data passed to @cmp_func
652 * Sorts @seq using @cmp_func.
657 g_sequence_sort (GSequence *seq,
658 GCompareDataFunc cmp_func,
663 info.cmp_func = cmp_func;
664 info.cmp_data = cmp_data;
665 info.end_node = seq->end_node;
667 check_seq_access (seq);
669 g_sequence_sort_iter (seq, iter_compare, &info);
673 * g_sequence_insert_sorted:
675 * @data: the data to insert
676 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
677 * is called with two items of the @seq and @user_data. It should
678 * return 0 if the items are equal, a negative value if the first
679 * item comes before the second, and a positive value if the second
680 * item comes before the first.
681 * @cmp_data: user data passed to @cmp_func.
683 * Inserts @data into @sequence using @func to determine the new position.
684 * The sequence must already be sorted according to @cmp_func; otherwise the
685 * new position of @data is undefined.
687 * Return value: a #GSequenceIter pointing to the new item.
692 g_sequence_insert_sorted (GSequence *seq,
694 GCompareDataFunc cmp_func,
699 g_return_val_if_fail (seq != NULL, NULL);
700 g_return_val_if_fail (cmp_func != NULL, NULL);
702 info.cmp_func = cmp_func;
703 info.cmp_data = cmp_data;
704 info.end_node = seq->end_node;
705 check_seq_access (seq);
707 return g_sequence_insert_sorted_iter (seq, data, iter_compare, &info);
711 * g_sequence_sort_changed:
712 * @iter: A #GSequenceIter
713 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
714 * is called with two items of the @seq and @user_data. It should
715 * return 0 if the items are equal, a negative value if the first
716 * item comes before the second, and a positive value if the second
717 * item comes before the first.
718 * @cmp_data: user data passed to @cmp_func.
720 * Moves the data pointed to a new position as indicated by @cmp_func. This
721 * function should be called for items in a sequence already sorted according
722 * to @cmp_func whenever some aspect of an item changes so that @cmp_func
723 * may return different values for that item.
728 g_sequence_sort_changed (GSequenceIter *iter,
729 GCompareDataFunc cmp_func,
734 g_return_if_fail (!is_end (iter));
736 info.cmp_func = cmp_func;
737 info.cmp_data = cmp_data;
738 info.end_node = get_sequence (iter)->end_node;
739 check_iter_access (iter);
741 g_sequence_sort_changed_iter (iter, iter_compare, &info);
747 * @data: data for the new item
748 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
749 * is called with two items of the @seq and @user_data. It should
750 * return 0 if the items are equal, a negative value if the first
751 * item comes before the second, and a positive value if the second
752 * item comes before the first.
753 * @cmp_data: user data passed to @cmp_func.
755 * Returns an iterator pointing to the position where @data would
756 * be inserted according to @cmp_func and @cmp_data.
758 * Return value: an #GSequenceIter pointing to the position where @data
759 * would have been inserted according to @cmp_func and @cmp_data.
764 g_sequence_search (GSequence *seq,
766 GCompareDataFunc cmp_func,
771 g_return_val_if_fail (seq != NULL, NULL);
773 info.cmp_func = cmp_func;
774 info.cmp_data = cmp_data;
775 info.end_node = seq->end_node;
776 check_seq_access (seq);
778 return g_sequence_search_iter (seq, data, iter_compare, &info);
782 * g_sequence_sort_iter:
784 * @cmp_func: the #GSequenceItercompare used to compare iterators in the
785 * sequence. It is called with two iterators pointing into @seq. It should
786 * return 0 if the iterators are equal, a negative value if the first
787 * iterator comes before the second, and a positive value if the second
788 * iterator comes before the first.
789 * @cmp_data: user data passed to @cmp_func
791 * Like g_sequence_sort(), but uses a #GSequenceIterCompareFunc instead
792 * of a GCompareDataFunc as the compare function
797 g_sequence_sort_iter (GSequence *seq,
798 GSequenceIterCompareFunc cmp_func,
802 GSequenceNode *begin, *end;
804 g_return_if_fail (seq != NULL);
805 g_return_if_fail (cmp_func != NULL);
807 check_seq_access (seq);
809 begin = g_sequence_get_begin_iter (seq);
810 end = g_sequence_get_end_iter (seq);
812 tmp = g_sequence_new (NULL);
813 tmp->real_sequence = seq;
815 g_sequence_move_range (g_sequence_get_begin_iter (tmp), begin, end);
817 seq->access_prohibited = TRUE;
818 tmp->access_prohibited = TRUE;
820 while (g_sequence_get_length (tmp) > 0)
822 GSequenceNode *node = g_sequence_get_begin_iter (tmp);
824 node_insert_sorted (seq->end_node, node, seq->end_node,
828 tmp->access_prohibited = FALSE;
829 seq->access_prohibited = FALSE;
831 g_sequence_free (tmp);
835 * g_sequence_sort_changed_iter:
836 * @iter: a #GSequenceIter
837 * @iter_cmp: the #GSequenceItercompare used to compare iterators in the
838 * sequence. It is called with two iterators pointing into @seq. It should
839 * return 0 if the iterators are equal, a negative value if the first
840 * iterator comes before the second, and a positive value if the second
841 * iterator comes before the first.
842 * @cmp_data: user data passed to @cmp_func
844 * Like g_sequence_sort_changed(), but uses
845 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
846 * the compare function.
851 g_sequence_sort_changed_iter (GSequenceIter *iter,
852 GSequenceIterCompareFunc iter_cmp,
855 GSequence *seq, *tmp_seq;
856 GSequenceIter *next, *prev;
858 g_return_if_fail (iter != NULL);
859 g_return_if_fail (!is_end (iter));
860 g_return_if_fail (iter_cmp != NULL);
861 check_iter_access (iter);
863 /* If one of the neighbours is equal to iter, then
864 * don't move it. This ensures that sort_changed() is
865 * a stable operation.
868 next = node_get_next (iter);
869 prev = node_get_prev (iter);
871 if (prev != iter && iter_cmp (prev, iter, cmp_data) == 0)
874 if (!is_end (next) && iter_cmp (next, iter, cmp_data) == 0)
877 seq = get_sequence (iter);
879 seq->access_prohibited = TRUE;
881 tmp_seq = g_sequence_new (NULL);
882 tmp_seq->real_sequence = seq;
885 node_insert_before (tmp_seq->end_node, iter);
887 node_insert_sorted (seq->end_node, iter, seq->end_node,
890 g_sequence_free (tmp_seq);
892 seq->access_prohibited = FALSE;
896 * g_sequence_insert_sorted_iter:
898 * @data: data for the new item
899 * @iter_cmp: the #GSequenceItercompare used to compare iterators in the
900 * sequence. It is called with two iterators pointing into @seq. It should
901 * return 0 if the iterators are equal, a negative value if the first
902 * iterator comes before the second, and a positive value if the second
903 * iterator comes before the first.
904 * @cmp_data: user data passed to @cmp_func
906 * Like g_sequence_insert_sorted(), but uses
907 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
908 * the compare function.
910 * Return value: a #GSequenceIter pointing to the new item
915 g_sequence_insert_sorted_iter (GSequence *seq,
917 GSequenceIterCompareFunc iter_cmp,
920 GSequenceNode *new_node;
923 g_return_val_if_fail (seq != NULL, NULL);
924 g_return_val_if_fail (iter_cmp != NULL, NULL);
926 check_seq_access (seq);
928 seq->access_prohibited = TRUE;
930 /* Create a new temporary sequence and put the new node into
931 * that. The reason for this is that the user compare function
932 * will be called with the new node, and if it dereferences,
933 * "is_end" will be called on it. But that will crash if the
934 * node is not actually in a sequence.
936 * node_insert_sorted() makes sure the node is unlinked before
939 * The reason we need the "iter" versions at all is that that
940 * is the only kind of compare functions GtkTreeView can use.
942 tmp_seq = g_sequence_new (NULL);
943 tmp_seq->real_sequence = seq;
945 new_node = g_sequence_append (tmp_seq, data);
947 node_insert_sorted (seq->end_node, new_node,
948 seq->end_node, iter_cmp, cmp_data);
950 g_sequence_free (tmp_seq);
952 seq->access_prohibited = FALSE;
958 * g_sequence_search_iter:
960 * @data: data for the new item
961 * @iter_cmp: the #GSequenceIterCompare function used to compare iterators
962 * in the sequence. It is called with two iterators pointing into @seq.
963 * It should return 0 if the iterators are equal, a negative value if the
964 * first iterator comes before the second, and a positive value if the
965 * second iterator comes before the first.
966 * @cmp_data: user data passed to @iter_cmp
968 * Like g_sequence_search(), but uses
969 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
970 * the compare function.
972 * Return value: a #GSequenceIter pointing to the position in @seq
973 * where @data would have been inserted according to @iter_cmp and @cmp_data.
978 g_sequence_search_iter (GSequence *seq,
980 GSequenceIterCompareFunc iter_cmp,
984 GSequenceNode *dummy;
987 g_return_val_if_fail (seq != NULL, NULL);
989 check_seq_access (seq);
991 seq->access_prohibited = TRUE;
993 tmp_seq = g_sequence_new (NULL);
994 tmp_seq->real_sequence = seq;
996 dummy = g_sequence_append (tmp_seq, data);
998 node = node_find_closest (seq->end_node, dummy,
999 seq->end_node, iter_cmp, cmp_data);
1001 g_sequence_free (tmp_seq);
1003 seq->access_prohibited = FALSE;
1009 * g_sequence_iter_get_sequence:
1010 * @iter: a #GSequenceIter
1012 * Returns the #GSequence that @iter points into.
1014 * Return value: the #GSequence that @iter points into.
1019 g_sequence_iter_get_sequence (GSequenceIter *iter)
1023 g_return_val_if_fail (iter != NULL, NULL);
1025 seq = get_sequence (iter);
1027 /* For temporary sequences, this points to the sequence that
1028 * is actually being manipulated
1030 return seq->real_sequence;
1035 * @iter: a #GSequenceIter
1037 * Returns the data that @iter points to.
1039 * Return value: the data that @iter points to
1044 g_sequence_get (GSequenceIter *iter)
1046 g_return_val_if_fail (iter != NULL, NULL);
1047 g_return_val_if_fail (!is_end (iter), NULL);
1054 * @iter: a #GSequenceIter
1055 * @data: new data for the item
1057 * Changes the data for the item pointed to by @iter to be @data. If
1058 * the sequence has a data destroy function associated with it, that
1059 * function is called on the existing data that @iter pointed to.
1064 g_sequence_set (GSequenceIter *iter,
1069 g_return_if_fail (iter != NULL);
1070 g_return_if_fail (!is_end (iter));
1072 seq = get_sequence (iter);
1074 /* If @data is identical to iter->data, it is destroyed
1075 * here. This will work right in case of ref-counted objects. Also
1076 * it is similar to what ghashtables do.
1078 * For non-refcounted data it's a little less convenient, but
1079 * code relying on self-setting not destroying would be
1080 * pretty dubious anyway ...
1083 if (seq->data_destroy_notify)
1084 seq->data_destroy_notify (iter->data);
1090 * g_sequence_get_length:
1091 * @seq: a #GSequence
1093 * Returns the length of @seq
1095 * Return value: the length of @seq
1100 g_sequence_get_length (GSequence *seq)
1102 return node_get_length (seq->end_node) - 1;
1106 * g_sequence_get_end_iter:
1107 * @seq: a #GSequence
1109 * Returns the end iterator for @seg
1111 * Return value: the end iterator for @seq
1116 g_sequence_get_end_iter (GSequence *seq)
1118 g_return_val_if_fail (seq != NULL, NULL);
1120 return seq->end_node;
1124 * g_sequence_get_begin_iter:
1125 * @seq: a #GSequence
1127 * Returns the begin iterator for @seq.
1129 * Return value: the begin iterator for @seq.
1134 g_sequence_get_begin_iter (GSequence *seq)
1136 g_return_val_if_fail (seq != NULL, NULL);
1138 return node_get_first (seq->end_node);
1142 clamp_position (GSequence *seq,
1145 gint len = g_sequence_get_length (seq);
1147 if (pos > len || pos < 0)
1154 * if pos > number of items or -1, will return end pointer
1157 * g_sequence_get_iter_at_pos:
1158 * @seq: a #GSequence
1159 * @pos: a position in @seq, or -1 for the end.
1161 * Returns the iterator at position @pos. If @pos is negative or larger
1162 * than the number of items in @seq, the end iterator is returned.
1164 * Return value: The #GSequenceIter at position @pos
1169 g_sequence_get_iter_at_pos (GSequence *seq,
1172 g_return_val_if_fail (seq != NULL, NULL);
1174 pos = clamp_position (seq, pos);
1176 return node_get_by_pos (seq->end_node, pos);
1181 * @src: a #GSequenceIter pointing to the item to move
1182 * @dest: a #GSequenceIter pointing to the position to which
1183 * the item is moved.
1185 * Moves the item pointed to by @src to the position indicated by @dest.
1186 * After calling this function @dest will point to the position immediately
1187 * after @src. It is allowed for @src and @dest to point into different
1193 g_sequence_move (GSequenceIter *src,
1194 GSequenceIter *dest)
1196 g_return_if_fail (src != NULL);
1197 g_return_if_fail (dest != NULL);
1198 g_return_if_fail (!is_end (src));
1204 node_insert_before (dest, src);
1210 * g_sequence_iter_is_end:
1211 * @iter: a #GSequenceIter
1213 * Returns whether @iter is the end iterator
1215 * Return value: Whether @iter is the end iterator.
1220 g_sequence_iter_is_end (GSequenceIter *iter)
1222 g_return_val_if_fail (iter != NULL, FALSE);
1224 return is_end (iter);
1228 * g_sequence_iter_is_begin:
1229 * @iter: a #GSequenceIter
1231 * Returns whether @iter is the begin iterator
1233 * Return value: whether @iter is the begin iterator
1238 g_sequence_iter_is_begin (GSequenceIter *iter)
1240 g_return_val_if_fail (iter != NULL, FALSE);
1242 return (node_get_prev (iter) == iter);
1246 * g_sequence_iter_get_position:
1247 * @iter: a #GSequenceIter
1249 * Returns the position of @iter
1251 * Return value: the position of @iter
1256 g_sequence_iter_get_position (GSequenceIter *iter)
1258 g_return_val_if_fail (iter != NULL, -1);
1260 return node_get_pos (iter);
1264 * g_sequence_iter_next:
1265 * @iter: a #GSequenceIter
1267 * Returns an iterator pointing to the next position after @iter. If
1268 * @iter is the end iterator, the end iterator is returned.
1270 * Return value: a #GSequenceIter pointing to the next position after @iter.
1275 g_sequence_iter_next (GSequenceIter *iter)
1277 g_return_val_if_fail (iter != NULL, NULL);
1279 return node_get_next (iter);
1283 * g_sequence_iter_prev:
1284 * @iter: a #GSequenceIter
1286 * Returns an iterator pointing to the previous position before @iter. If
1287 * @iter is the begin iterator, the begin iterator is returned.
1289 * Return value: a #GSequenceIter pointing to the previous position before
1295 g_sequence_iter_prev (GSequenceIter *iter)
1297 g_return_val_if_fail (iter != NULL, NULL);
1299 return node_get_prev (iter);
1303 * g_sequence_iter_move:
1304 * @iter: a #GSequenceIter
1305 * @delta: A positive or negative number indicating how many positions away
1306 * from @iter the returned #GSequenceIter will be.
1308 * Returns the #GSequenceIter which is @delta positions away from @iter.
1309 * If @iter is closer than -@delta positions to the beginning of the sequence,
1310 * the begin iterator is returned. If @iter is closer than @delta positions
1311 * to the end of the sequence, the end iterator is returned.
1313 * Return value: a #GSequenceIter which is @delta positions away from @iter.
1318 g_sequence_iter_move (GSequenceIter *iter,
1323 g_return_val_if_fail (iter != NULL, NULL);
1325 new_pos = node_get_pos (iter) + delta;
1327 new_pos = clamp_position (get_sequence (iter), new_pos);
1329 return node_get_by_pos (iter, new_pos);
1334 * @a: a #GSequenceIter
1335 * @b: a #GSequenceIter
1337 * Swaps the items pointed to by @a and @b. It is allowed for @a and @b
1338 * to point into difference sequences.
1343 g_sequence_swap (GSequenceIter *a,
1346 GSequenceNode *leftmost, *rightmost, *rightmost_next;
1349 g_return_if_fail (!g_sequence_iter_is_end (a));
1350 g_return_if_fail (!g_sequence_iter_is_end (b));
1355 a_pos = g_sequence_iter_get_position (a);
1356 b_pos = g_sequence_iter_get_position (b);
1369 rightmost_next = node_get_next (rightmost);
1371 /* The situation is now like this:
1373 * ..., leftmost, ......., rightmost, rightmost_next, ...
1376 g_sequence_move (rightmost, leftmost);
1377 g_sequence_move (leftmost, rightmost_next);
1381 * Implementation of a treap
1386 get_priority (GSequenceNode *node)
1388 guint key = GPOINTER_TO_UINT (node);
1390 /* This hash function is based on one found on Thomas Wang's
1393 * http://www.concentric.net/~Ttwang/tech/inthash.htm
1396 key = (key << 15) - key - 1;
1397 key = key ^ (key >> 12);
1398 key = key + (key << 2);
1399 key = key ^ (key >> 4);
1400 key = key + (key << 3) + (key << 11);
1401 key = key ^ (key >> 16);
1403 /* We rely on 0 being less than all other priorities */
1404 return key? key : 1;
1407 static GSequenceNode *
1408 find_root (GSequenceNode *node)
1410 while (node->parent)
1411 node = node->parent;
1416 static GSequenceNode *
1417 node_new (gpointer data)
1419 GSequenceNode *node = g_slice_new0 (GSequenceNode);
1425 node->parent = NULL;
1430 static GSequenceNode *
1431 node_get_first (GSequenceNode *node)
1433 node = find_root (node);
1441 static GSequenceNode *
1442 node_get_last (GSequenceNode *node)
1444 node = find_root (node);
1452 #define NODE_LEFT_CHILD(n) (((n)->parent) && ((n)->parent->left) == (n))
1453 #define NODE_RIGHT_CHILD(n) (((n)->parent) && ((n)->parent->right) == (n))
1455 static GSequenceNode *
1456 node_get_next (GSequenceNode *node)
1458 GSequenceNode *n = node;
1468 while (NODE_RIGHT_CHILD (n))
1480 static GSequenceNode *
1481 node_get_prev (GSequenceNode *node)
1483 GSequenceNode *n = node;
1493 while (NODE_LEFT_CHILD (n))
1505 #define N_NODES(n) ((n)? (n)->n_nodes : 0)
1508 node_get_pos (GSequenceNode *node)
1513 n_smaller = node->left->n_nodes;
1517 if (NODE_RIGHT_CHILD (node))
1518 n_smaller += N_NODES (node->parent->left) + 1;
1520 node = node->parent;
1526 static GSequenceNode *
1527 node_get_by_pos (GSequenceNode *node,
1532 node = find_root (node);
1534 while ((i = N_NODES (node->left)) != pos)
1550 static GSequenceNode *
1551 node_find_closest (GSequenceNode *haystack,
1552 GSequenceNode *needle,
1554 GSequenceIterCompareFunc iter_cmp,
1557 GSequenceNode *best;
1560 haystack = find_root (haystack);
1566 /* iter_cmp can't be passed the end node, since the function may
1569 if (haystack == end)
1572 c = iter_cmp (haystack, needle, cmp_data);
1574 /* In the following we don't break even if c == 0. Instaed we go on
1575 * searching along the 'bigger' nodes, so that we find the last one
1576 * that is equal to the needle.
1579 haystack = haystack->left;
1581 haystack = haystack->right;
1583 while (haystack != NULL);
1585 /* If the best node is smaller or equal to the data, then move one step
1586 * to the right to make sure the best one is strictly bigger than the data
1588 if (best != end && c <= 0)
1589 best = node_get_next (best);
1595 node_get_length (GSequenceNode *node)
1597 node = find_root (node);
1599 return node->n_nodes;
1603 real_node_free (GSequenceNode *node,
1608 real_node_free (node->left, seq);
1609 real_node_free (node->right, seq);
1611 if (seq && seq->data_destroy_notify && node != seq->end_node)
1612 seq->data_destroy_notify (node->data);
1614 g_slice_free (GSequenceNode, node);
1619 node_free (GSequenceNode *node,
1622 node = find_root (node);
1624 real_node_free (node, seq);
1628 node_update_fields (GSequenceNode *node)
1632 n_nodes += N_NODES (node->left);
1633 n_nodes += N_NODES (node->right);
1635 node->n_nodes = n_nodes;
1639 node_rotate (GSequenceNode *node)
1641 GSequenceNode *tmp, *old;
1643 g_assert (node->parent);
1644 g_assert (node->parent != node);
1646 if (NODE_LEFT_CHILD (node))
1651 node->right = node->parent;
1652 node->parent = node->parent->parent;
1655 if (node->parent->left == node->right)
1656 node->parent->left = node;
1658 node->parent->right = node;
1661 g_assert (node->right);
1663 node->right->parent = node;
1664 node->right->left = tmp;
1666 if (node->right->left)
1667 node->right->left->parent = node->right;
1676 node->left = node->parent;
1677 node->parent = node->parent->parent;
1680 if (node->parent->right == node->left)
1681 node->parent->right = node;
1683 node->parent->left = node;
1686 g_assert (node->left);
1688 node->left->parent = node;
1689 node->left->right = tmp;
1691 if (node->left->right)
1692 node->left->right->parent = node->left;
1697 node_update_fields (old);
1698 node_update_fields (node);
1702 node_update_fields_deep (GSequenceNode *node)
1706 node_update_fields (node);
1708 node_update_fields_deep (node->parent);
1713 rotate_down (GSequenceNode *node,
1718 left = node->left ? get_priority (node->left) : 0;
1719 right = node->right ? get_priority (node->right) : 0;
1721 while (priority < left || priority < right)
1724 node_rotate (node->left);
1726 node_rotate (node->right);
1728 left = node->left ? get_priority (node->left) : 0;
1729 right = node->right ? get_priority (node->right) : 0;
1734 node_cut (GSequenceNode *node)
1736 while (node->parent)
1740 node->left->parent = NULL;
1743 node_update_fields (node);
1745 rotate_down (node, get_priority (node));
1749 node_join (GSequenceNode *left,
1750 GSequenceNode *right)
1752 GSequenceNode *fake = node_new (NULL);
1754 fake->left = find_root (left);
1755 fake->right = find_root (right);
1756 fake->left->parent = fake;
1757 fake->right->parent = fake;
1759 node_update_fields (fake);
1763 node_free (fake, NULL);
1767 node_insert_before (GSequenceNode *node,
1770 new->left = node->left;
1772 new->left->parent = new;
1777 node_update_fields_deep (new);
1779 while (new->parent && get_priority (new) > get_priority (new->parent))
1782 rotate_down (new, get_priority (new));
1786 node_unlink (GSequenceNode *node)
1788 rotate_down (node, 0);
1790 if (NODE_RIGHT_CHILD (node))
1791 node->parent->right = NULL;
1792 else if (NODE_LEFT_CHILD (node))
1793 node->parent->left = NULL;
1796 node_update_fields_deep (node->parent);
1798 node->parent = NULL;
1802 node_insert_sorted (GSequenceNode *node,
1805 GSequenceIterCompareFunc iter_cmp,
1808 GSequenceNode *closest;
1810 closest = node_find_closest (node, new, end, iter_cmp, cmp_data);
1814 node_insert_before (closest, new);
1818 #define __G_SEQUENCE_C__
1819 #include "galiasdef.c"