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.
26 typedef struct _GSequenceNode GSequenceNode;
30 GSequenceNode * end_node;
31 GDestroyNotify data_destroy_notify;
32 gboolean access_prohibited;
34 /* The 'real_sequence' is used when temporary sequences are created
35 * to hold nodes that being rearranged. The 'real_sequence' of such
36 * a temporary sequence points to the sequence that is actually being
37 * manipulated. The only reason we need this is so that when the
38 * sort/sort_changed/search_iter() functions call out to the application
39 * g_sequence_iter_get_sequence() will return the correct sequence.
41 GSequence * real_sequence;
47 GSequenceNode * parent;
49 GSequenceNode * right;
50 gpointer data; /* For the end node, this field points
56 * Declaration of GSequenceNode methods
58 static GSequenceNode *node_new (gpointer data);
59 static GSequenceNode *node_get_first (GSequenceNode *node);
60 static GSequenceNode *node_get_last (GSequenceNode *node);
61 static GSequenceNode *node_get_prev (GSequenceNode *node);
62 static GSequenceNode *node_get_next (GSequenceNode *node);
63 static gint node_get_pos (GSequenceNode *node);
64 static GSequenceNode *node_get_by_pos (GSequenceNode *node,
66 static GSequenceNode *node_find_closest (GSequenceNode *haystack,
67 GSequenceNode *needle,
69 GSequenceIterCompareFunc cmp,
71 static gint node_get_length (GSequenceNode *node);
72 static void node_free (GSequenceNode *node,
74 static void node_cut (GSequenceNode *split);
75 static void node_insert_after (GSequenceNode *node,
76 GSequenceNode *second);
77 static void node_insert_before (GSequenceNode *node,
79 static void node_unlink (GSequenceNode *node);
80 static void node_insert_sorted (GSequenceNode *node,
83 GSequenceIterCompareFunc cmp_func,
87 * Various helper functions
90 check_seq_access (GSequence *seq)
92 if (G_UNLIKELY (seq->access_prohibited))
94 g_warning ("Accessing a sequence while it is "
95 "being sorted or searched is not allowed");
100 get_sequence (GSequenceNode *node)
102 return (GSequence *)node_get_last (node)->data;
106 check_iter_access (GSequenceIter *iter)
108 check_seq_access (get_sequence (iter));
112 is_end (GSequenceIter *iter)
114 GSequence *seq = get_sequence (iter);
116 return seq->end_node == iter;
121 GCompareDataFunc cmp_func;
123 GSequenceNode *end_node;
126 /* This function compares two iters using a normal compare
127 * function and user_data passed in in a SortInfo struct
130 iter_compare (GSequenceIter *node1,
131 GSequenceIter *node2,
134 const SortInfo *info = data;
137 if (node1 == info->end_node)
140 if (node2 == info->end_node)
143 retval = info->cmp_func (node1->data, node2->data, info->cmp_data);
154 * @data_destroy: a #GDestroyNotify function, or %NULL
156 * Creates a new GSequence. The @data_destroy function, if non-%NULL will
157 * be called on all items when the sequence is destroyed and on items that
158 * are removed from the sequence.
160 * Return value: a new #GSequence
165 g_sequence_new (GDestroyNotify data_destroy)
167 GSequence *seq = g_new (GSequence, 1);
168 seq->data_destroy_notify = data_destroy;
170 seq->end_node = node_new (seq);
172 seq->access_prohibited = FALSE;
174 seq->real_sequence = seq;
183 * Frees the memory allocated for @seq. If @seq has a data destroy
184 * function associated with it, that function is called on all items in
190 g_sequence_free (GSequence *seq)
192 g_return_if_fail (seq != NULL);
194 check_seq_access (seq);
196 node_free (seq->end_node, seq);
202 * g_sequence_foreach_range:
203 * @begin: a #GSequenceIter
204 * @end: a #GSequenceIter
206 * @user_data: user data passed to @func
208 * Calls @func for each item in the range (@begin, @end) passing
209 * @user_data to the function.
214 g_sequence_foreach_range (GSequenceIter *begin,
222 g_return_if_fail (func != NULL);
223 g_return_if_fail (begin != NULL);
224 g_return_if_fail (end != NULL);
226 seq = get_sequence (begin);
228 seq->access_prohibited = TRUE;
233 GSequenceIter *next = node_get_next (iter);
235 func (iter->data, user_data);
240 seq->access_prohibited = FALSE;
244 * g_sequence_foreach:
246 * @func: the function to call for each item in @seq
247 * @user_data: user data passed to @func
249 * Calls @func for each item in the sequence passing @user_data
255 g_sequence_foreach (GSequence *seq,
259 GSequenceIter *begin, *end;
261 check_seq_access (seq);
263 begin = g_sequence_get_begin_iter (seq);
264 end = g_sequence_get_end_iter (seq);
266 g_sequence_foreach_range (begin, end, func, user_data);
270 * g_sequence_range_get_midpoint:
271 * @begin: a #GSequenceIter
272 * @end: a #GSequenceIter
274 * Finds an iterator somewhere in the range (@begin, @end). This
275 * iterator will be close to the middle of the range, but is not
276 * guaranteed to be <emphasis>exactly</emphasis> in the middle.
278 * The @begin and @end iterators must both point to the same sequence and
279 * @begin must come before or be equal to @end in the sequence.
281 * Return value: A #GSequenceIter pointing somewhere in the
282 * (@begin, @end) range.
287 g_sequence_range_get_midpoint (GSequenceIter *begin,
290 int begin_pos, end_pos, mid_pos;
292 g_return_val_if_fail (begin != NULL, NULL);
293 g_return_val_if_fail (end != NULL, NULL);
294 g_return_val_if_fail (get_sequence (begin) == get_sequence (end), NULL);
296 begin_pos = node_get_pos (begin);
297 end_pos = node_get_pos (end);
299 g_return_val_if_fail (end_pos >= begin_pos, NULL);
301 mid_pos = begin_pos + (end_pos - begin_pos) / 2;
303 return node_get_by_pos (begin, mid_pos);
307 * g_sequence_iter_compare:
308 * @a: a #GSequenceIter
309 * @b: a #GSequenceIter
311 * Returns a negative number if @a comes before @b, 0 if they are equal,
312 * and a positive number if @a comes after @b.
314 * The @a and @b iterators must point into the same sequence.
316 * Return value: A negative number if @a comes before @b, 0 if they are
317 * equal, and a positive number if @a comes after @b.
322 g_sequence_iter_compare (GSequenceIter *a,
327 g_return_val_if_fail (a != NULL, 0);
328 g_return_val_if_fail (b != NULL, 0);
329 g_return_val_if_fail (get_sequence (a) == get_sequence (b), 0);
331 check_iter_access (a);
332 check_iter_access (b);
334 a_pos = node_get_pos (a);
335 b_pos = node_get_pos (b);
339 else if (a_pos > b_pos)
347 * @seq: a #GSequencePointer
348 * @data: the data for the new item
350 * Adds a new item to the end of @seq.
352 * Return value: an iterator pointing to the new item
357 g_sequence_append (GSequence *seq,
362 g_return_val_if_fail (seq != NULL, NULL);
364 check_seq_access (seq);
366 node = node_new (data);
367 node_insert_before (seq->end_node, node);
373 * g_sequence_prepend:
375 * @data: the data for the new item
377 * Adds a new item to the front of @seq
379 * Return value: an iterator pointing to the new item
384 g_sequence_prepend (GSequence *seq,
387 GSequenceNode *node, *first;
389 g_return_val_if_fail (seq != NULL, NULL);
391 check_seq_access (seq);
393 node = node_new (data);
394 first = node_get_first (seq->end_node);
396 node_insert_before (first, node);
402 * g_sequence_insert_before:
403 * @iter: a #GSequenceIter
404 * @data: the data for the new item
406 * Inserts a new item just before the item pointed to by @iter.
408 * Return value: an iterator pointing to the new item
413 g_sequence_insert_before (GSequenceIter *iter,
418 g_return_val_if_fail (iter != NULL, NULL);
420 check_iter_access (iter);
422 node = node_new (data);
424 node_insert_before (iter, node);
431 * @iter: a #GSequenceIter
433 * Removes the item pointed to by @iter. It is an error to pass the
434 * end iterator to this function.
436 * If the sequnce has a data destroy function associated with it, this
437 * function is called on the data for the removed item.
442 g_sequence_remove (GSequenceIter *iter)
446 g_return_if_fail (iter != NULL);
447 g_return_if_fail (!is_end (iter));
449 check_iter_access (iter);
451 seq = get_sequence (iter);
454 node_free (iter, seq);
458 * g_sequence_remove_range:
459 * @begin: a #GSequenceIter
460 * @end: a #GSequenceIter
462 * Removes all items in the (@begin, @end) range.
464 * If the sequence has a data destroy function associated with it, this
465 * function is called on the data for the removed items.
470 g_sequence_remove_range (GSequenceIter *begin,
473 g_return_if_fail (get_sequence (begin) == get_sequence (end));
475 check_iter_access (begin);
476 check_iter_access (end);
478 g_sequence_move_range (NULL, begin, end);
482 * g_sequence_move_range:
483 * @dest: a #GSequenceIter
484 * @begin: a #GSequenceIter
485 * @end: a #GSequenceIter
487 * Inserts the (@begin, @end) range at the destination pointed to by ptr.
488 * The @begin and @end iters must point into the same sequence. It is
489 * allowed for @dest to point to a different sequence than the one pointed
490 * into by @begin and @end.
492 * If @dest is NULL, the range indicated by @begin and @end is
493 * removed from the sequence. If @dest iter points to a place within
494 * the (@begin, @end) range, the range does not move.
499 g_sequence_move_range (GSequenceIter *dest,
500 GSequenceIter *begin,
504 GSequenceNode *first;
506 g_return_if_fail (begin != NULL);
507 g_return_if_fail (end != NULL);
509 check_iter_access (begin);
510 check_iter_access (end);
512 check_iter_access (dest);
514 src_seq = get_sequence (begin);
516 g_return_if_fail (src_seq == get_sequence (end));
518 /* Dest points to begin or end? */
519 if (dest == begin || dest == end)
522 /* begin comes after end? */
523 if (g_sequence_iter_compare (begin, end) >= 0)
526 /* dest points somewhere in the (begin, end) range? */
527 if (dest && get_sequence (dest) == src_seq &&
528 g_sequence_iter_compare (dest, begin) > 0 &&
529 g_sequence_iter_compare (dest, end) < 0)
534 src_seq = get_sequence (begin);
536 first = node_get_first (begin);
543 node_insert_after (node_get_last (first), end);
546 node_insert_before (dest, begin);
548 node_free (begin, src_seq);
554 * @cmp_func: the #GCompareDataFunc used to sort @seq. This function is
555 * passed two items of @seq and should return 0 if they are equal,
556 * a negative value fi the first comes before the second, and a
557 * positive value if the second comes before the first.
558 * @cmp_data: user data passed to @cmp_func
560 * Sorts @seq using @cmp_func.
565 g_sequence_sort (GSequence *seq,
566 GCompareDataFunc cmp_func,
569 SortInfo info = { cmp_func, cmp_data, seq->end_node };
571 check_seq_access (seq);
573 g_sequence_sort_iter (seq, iter_compare, &info);
577 * g_sequence_insert_sorted:
579 * @data: the data to insert
580 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
581 * is called with two items of the @seq and @user_data. It should
582 * return 0 if the items are equal, a negative value if the first
583 * item comes before the second, and a positive value if the second
584 * item comes before the first.
585 * @cmp_data: user data passed to @cmp_func.
587 * Inserts @data into @sequence using @func to determine the new position.
588 * The sequence must already be sorted according to @cmp_func; otherwise the
589 * new position of @data is undefined.
591 * Return value: a #GSequenceIter pointing to the new item.
596 g_sequence_insert_sorted (GSequence *seq,
598 GCompareDataFunc cmp_func,
601 SortInfo info = { cmp_func, cmp_data, NULL };
603 g_return_val_if_fail (seq != NULL, NULL);
604 g_return_val_if_fail (cmp_func != NULL, NULL);
606 info.end_node = seq->end_node;
607 check_seq_access (seq);
609 return g_sequence_insert_sorted_iter (seq, data, iter_compare, &info);
613 * g_sequence_sort_changed:
614 * @iter: A #GSequenceIter
615 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
616 * is called with two items of the @seq and @user_data. It should
617 * return 0 if the items are equal, a negative value if the first
618 * item comes before the second, and a positive value if the second
619 * item comes before the first.
620 * @cmp_data: user data passed to @cmp_func.
622 * Moves the data pointed to a new position as indicated by @cmp_func. This
623 * function should be called for items in a sequence already sorted according
624 * to @cmp_func whenever some aspect of an item changes so that @cmp_func
625 * may return different values for that item.
630 g_sequence_sort_changed (GSequenceIter *iter,
631 GCompareDataFunc cmp_func,
634 SortInfo info = { cmp_func, cmp_data, NULL };
636 g_return_if_fail (!is_end (iter));
638 info.end_node = get_sequence (iter)->end_node;
639 check_iter_access (iter);
641 g_sequence_sort_changed_iter (iter, iter_compare, &info);
647 * @data: data for the new item
648 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
649 * is called with two items of the @seq and @user_data. It should
650 * return 0 if the items are equal, a negative value if the first
651 * item comes before the second, and a positive value if the second
652 * item comes before the first.
653 * @cmp_data: user data passed to @cmp_func.
655 * Returns an iterator pointing to the position where @data would
656 * be inserted according to @cmp_func and @cmp_data.
658 * Return value: an #GSequenceIter pointing to the position where @data
659 * would have been inserted according to @cmp_func and @cmp_data.
664 g_sequence_search (GSequence *seq,
666 GCompareDataFunc cmp_func,
669 SortInfo info = { cmp_func, cmp_data, NULL };
671 g_return_val_if_fail (seq != NULL, NULL);
673 info.end_node = seq->end_node;
674 check_seq_access (seq);
676 return g_sequence_search_iter (seq, data, iter_compare, &info);
680 * g_sequence_sort_iter:
682 * @cmp_func: the #GSequenceItercompare used to compare iterators in the
683 * sequence. It is called with two iterators pointing into @seq. It should
684 * return 0 if the iterators are equal, a negative value if the first
685 * iterator comes before the second, and a positive value if the second
686 * iterator comes before the first.
687 * @cmp_data: user data passed to @cmp_func
689 * Like g_sequence_sort(), but uses a #GSequenceIterCompareFunc instead
690 * of a GCompareDataFunc as the compare function
695 g_sequence_sort_iter (GSequence *seq,
696 GSequenceIterCompareFunc cmp_func,
700 GSequenceNode *begin, *end;
702 g_return_if_fail (seq != NULL);
703 g_return_if_fail (cmp_func != NULL);
705 check_seq_access (seq);
707 begin = g_sequence_get_begin_iter (seq);
708 end = g_sequence_get_end_iter (seq);
710 tmp = g_sequence_new (NULL);
711 tmp->real_sequence = seq;
712 tmp->access_prohibited = TRUE;
714 seq->access_prohibited = TRUE;
716 g_sequence_move_range (g_sequence_get_begin_iter (tmp), begin, end);
718 while (g_sequence_get_length (tmp) > 0)
720 GSequenceNode *node = g_sequence_get_begin_iter (tmp);
722 node_insert_sorted (seq->end_node, node, seq->end_node,
726 tmp->access_prohibited = FALSE;
727 seq->access_prohibited = FALSE;
729 g_sequence_free (tmp);
733 * g_sequence_sort_changed_iter:
734 * @iter: a #GSequenceIter
735 * @iter_cmp: the #GSequenceItercompare used to compare iterators in the
736 * sequence. It is called with two iterators pointing into @seq. It should
737 * return 0 if the iterators are equal, a negative value if the first
738 * iterator comes before the second, and a positive value if the second
739 * iterator comes before the first.
740 * @cmp_data: user data passed to @cmp_func
742 * Like g_sequence_sort_changed(), but uses
743 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
744 * the compare function.
749 g_sequence_sort_changed_iter (GSequenceIter *iter,
750 GSequenceIterCompareFunc iter_cmp,
753 GSequence *seq, *tmp_seq;
754 GSequenceIter *next, *prev;
756 g_return_if_fail (iter != NULL);
757 g_return_if_fail (!is_end (iter));
758 g_return_if_fail (iter_cmp != NULL);
759 check_iter_access (iter);
761 /* If one of the neighbours is equal to iter, then
762 * don't move it. This ensures that sort_changed() is
763 * a stable operation.
766 next = node_get_next (iter);
767 prev = node_get_prev (iter);
769 if (prev != iter && iter_cmp (prev, iter, cmp_data) == 0)
772 if (!is_end (next) && iter_cmp (next, iter, cmp_data) == 0)
775 seq = get_sequence (iter);
777 seq->access_prohibited = TRUE;
779 tmp_seq = g_sequence_new (NULL);
780 tmp_seq->real_sequence = seq;
783 node_insert_before (tmp_seq->end_node, iter);
785 node_insert_sorted (seq->end_node, iter, seq->end_node,
788 g_sequence_free (tmp_seq);
790 seq->access_prohibited = FALSE;
794 * g_sequence_insert_sorted_iter:
796 * @data: data for the new item
797 * @iter_cmp: the #GSequenceItercompare used to compare iterators in the
798 * sequence. It is called with two iterators pointing into @seq. It should
799 * return 0 if the iterators are equal, a negative value if the first
800 * iterator comes before the second, and a positive value if the second
801 * iterator comes before the first.
802 * @cmp_data: user data passed to @cmp_func
804 * Like g_sequence_insert_sorted(), but uses
805 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
806 * the compare function.
808 * Return value: a #GSequenceIter pointing to the new item
813 g_sequence_insert_sorted_iter (GSequence *seq,
815 GSequenceIterCompareFunc iter_cmp,
818 GSequenceNode *new_node;
821 g_return_val_if_fail (seq != NULL, NULL);
822 g_return_val_if_fail (iter_cmp != NULL, NULL);
824 check_seq_access (seq);
826 seq->access_prohibited = TRUE;
828 /* Create a new temporary sequence and put the new node into
829 * that. The reason for this is that the user compare function
830 * will be called with the new node, and if it dereferences,
831 * "is_end" will be called on it. But that will crash if the
832 * node is not actually in a sequence.
834 * node_insert_sorted() makes sure the node is unlinked before
837 * The reason we need the "iter" versions at all is that that
838 * is the only kind of compare functions GtkTreeView can use.
840 tmp_seq = g_sequence_new (NULL);
841 tmp_seq->real_sequence = seq;
843 new_node = g_sequence_append (tmp_seq, data);
845 node_insert_sorted (seq->end_node, new_node,
846 seq->end_node, iter_cmp, cmp_data);
848 g_sequence_free (tmp_seq);
850 seq->access_prohibited = FALSE;
856 * g_sequence_search_iter:
858 * @data: data for the new item
859 * @iter_cmp: the #GSequenceIterCompare function used to compare iterators
860 * in the sequence. It is called with two iterators pointing into @seq.
861 * It should return 0 if the iterators are equal, a negative value if the
862 * first iterator comes before the second, and a positive value if the
863 * second iterator comes before the first.
864 * @cmp_data: user data passed to @iter_cmp
866 * Like g_sequence_search(), but uses
867 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
868 * the compare function.
870 * Return value: a #GSequenceIter pointing to the position in @seq
871 * where @data would have been inserted according to @iter_cmp and @cmp_data.
876 g_sequence_search_iter (GSequence *seq,
878 GSequenceIterCompareFunc iter_cmp,
882 GSequenceNode *dummy;
885 g_return_val_if_fail (seq != NULL, NULL);
887 check_seq_access (seq);
889 seq->access_prohibited = TRUE;
891 /* Create a new temporary sequence and put the dummy node into
892 * that. The reason for this is that the user compare function
893 * will be called with the new node, and if it dereferences,
894 * "is_end" will be called on it. But that will crash if the
895 * node is not actually in a sequence.
897 * node_insert_sorted() makes sure the node is unlinked before
900 * The reason we need the "iter" versions at all is that that
901 * is the only kind of compare functions GtkTreeView can use.
903 tmp_seq = g_sequence_new (NULL);
904 tmp_seq->real_sequence = seq;
906 dummy = g_sequence_append (tmp_seq, data);
908 node = node_find_closest (seq->end_node, dummy,
909 seq->end_node, iter_cmp, cmp_data);
911 g_sequence_free (tmp_seq);
913 seq->access_prohibited = FALSE;
919 * g_sequence_iter_get_sequence:
920 * @iter: a #GSequenceIter
922 * Returns the #GSequence that @iter points into.
924 * Return value: the #GSequence that @iter points into.
929 g_sequence_iter_get_sequence (GSequenceIter *iter)
933 g_return_val_if_fail (iter != NULL, NULL);
935 seq = get_sequence (iter);
937 /* For temporary sequences, this points to the sequence that
938 * is actually being manipulated
940 return seq->real_sequence;
945 * @iter: a #GSequenceIter
947 * Returns the data that @iter points to.
949 * Return value: the data that @iter points to
954 g_sequence_get (GSequenceIter *iter)
956 g_return_val_if_fail (iter != NULL, NULL);
957 g_return_val_if_fail (!is_end (iter), NULL);
964 * @iter: a #GSequenceIter
965 * @data: new data for the item
967 * Changes the data for the item pointed to by @iter to be @data. If
968 * the sequence has a data destroy function associated with it, that
969 * function is called on the existing data that @iter pointed to.
974 g_sequence_set (GSequenceIter *iter,
979 g_return_if_fail (iter != NULL);
980 g_return_if_fail (!is_end (iter));
982 seq = get_sequence (iter);
984 /* If @data is identical to iter->data, it is destroyed
985 * here. This will work right in case of ref-counted objects. Also
986 * it is similar to what ghashtables do.
988 * For non-refcounted data it's a little less convenient, but
989 * code relying on self-setting not destroying would be
990 * pretty dubious anyway ...
993 if (seq->data_destroy_notify)
994 seq->data_destroy_notify (iter->data);
1000 * g_sequence_get_length:
1001 * @seq: a #GSequence
1003 * Returns the length of @seq
1005 * Return value: the length of @seq
1010 g_sequence_get_length (GSequence *seq)
1012 return node_get_length (seq->end_node) - 1;
1016 * g_sequence_get_end_iter:
1017 * @seq: a #GSequence
1019 * Returns the end iterator for @seg
1021 * Return value: the end iterator for @seq
1026 g_sequence_get_end_iter (GSequence *seq)
1028 g_return_val_if_fail (seq != NULL, NULL);
1030 g_assert (is_end (seq->end_node));
1032 return seq->end_node;
1036 * g_sequence_get_begin_iter:
1037 * @seq: a #GSequence
1039 * Returns the begin iterator for @seq.
1041 * Return value: the begin iterator for @seq.
1046 g_sequence_get_begin_iter (GSequence *seq)
1048 g_return_val_if_fail (seq != NULL, NULL);
1049 return node_get_first (seq->end_node);
1053 clamp_position (GSequence *seq,
1056 gint len = g_sequence_get_length (seq);
1058 if (pos > len || pos < 0)
1065 * if pos > number of items or -1, will return end pointer
1068 * g_sequence_get_iter_at_pos:
1069 * @seq: a #GSequence
1070 * @pos: a position in @seq, or -1 for the end.
1072 * Returns the iterator at position @pos. If @pos is negative or larger
1073 * than the number of items in @seq, the end iterator is returned.
1075 * Return value: The #GSequenceIter at position @pos
1080 g_sequence_get_iter_at_pos (GSequence *seq,
1083 g_return_val_if_fail (seq != NULL, NULL);
1085 pos = clamp_position (seq, pos);
1087 return node_get_by_pos (seq->end_node, pos);
1092 * @src: a #GSequenceIter pointing to the item to move
1093 * @dest: a #GSequenceIter pointing to the position to which
1094 * the item is moved.
1096 * Moves the item pointed to by @src to the position indicated by @dest.
1097 * After calling this function @dest will point to the position immediately
1103 g_sequence_move (GSequenceIter *src,
1104 GSequenceIter *dest)
1106 g_return_if_fail (src != NULL);
1107 g_return_if_fail (dest != NULL);
1108 g_return_if_fail (!is_end (src));
1114 node_insert_before (dest, src);
1120 * g_sequence_iter_is_end:
1121 * @iter: a #GSequenceIter
1123 * Returns whether @iter is the end iterator
1125 * Return value: Whether @iter is the end iterator.
1130 g_sequence_iter_is_end (GSequenceIter *iter)
1132 g_return_val_if_fail (iter != NULL, FALSE);
1134 return is_end (iter);
1138 * g_sequence_iter_is_begin:
1139 * @iter: a #GSequenceIter
1141 * Returns whether @iter is the begin iterator
1143 * Return value: whether @iter is the begin iterator
1148 g_sequence_iter_is_begin (GSequenceIter *iter)
1150 g_return_val_if_fail (iter != NULL, FALSE);
1152 return (node_get_prev (iter) == iter);
1156 * g_sequence_iter_get_position:
1157 * @iter: a #GSequenceIter
1159 * Returns the position of @iter
1161 * Return value: the position of @iter
1166 g_sequence_iter_get_position (GSequenceIter *iter)
1168 g_return_val_if_fail (iter != NULL, -1);
1170 return node_get_pos (iter);
1174 * g_sequence_iter_next:
1175 * @iter: a #GSequenceIter
1177 * Returns an iterator pointing to the next position after @iter. If
1178 * @iter is the end iterator, the end iterator is returned.
1180 * Return value: a #GSequenceIter pointing to the next position after @iter.
1185 g_sequence_iter_next (GSequenceIter *iter)
1187 g_return_val_if_fail (iter != NULL, NULL);
1189 return node_get_next (iter);
1193 * g_sequence_iter_prev:
1194 * @iter: a #GSequenceIter
1196 * Returns an iterator pointing to the previous position before @iter. If
1197 * @iter is the begin iterator, the begin iterator is returned.
1199 * Return value: a #GSequenceIter pointing to the previous position before
1205 g_sequence_iter_prev (GSequenceIter *iter)
1207 g_return_val_if_fail (iter != NULL, NULL);
1209 return node_get_prev (iter);
1213 * g_sequence_iter_move:
1214 * @iter: a #GSequenceIter
1215 * @delta: A positive or negative number indicating how many positions away
1216 * from @iter the returned #GSequenceIter will be.
1218 * Returns the #GSequenceIter which is @delta positions away from @iter.
1219 * If @iter is closer than -@delta positions to the beginning of the sequence,
1220 * the begin iterator is returned. If @iter is closer than @delta positions
1221 * to the end of the sequence, the end iterator is returned.
1223 * Return value: a #GSequenceIter which is @delta positions away from @iter.
1228 g_sequence_iter_move (GSequenceIter *iter,
1233 g_return_val_if_fail (iter != NULL, NULL);
1235 new_pos = node_get_pos (iter) + delta;
1237 new_pos = clamp_position (get_sequence (iter), new_pos);
1239 return node_get_by_pos (iter, new_pos);
1244 * @a: a #GSequenceIter
1245 * @b: a #GSequenceIter
1247 * Swaps the items pointed to by @a and @b
1252 g_sequence_swap (GSequenceIter *a,
1255 GSequenceNode *leftmost, *rightmost, *rightmost_next;
1258 g_return_if_fail (!g_sequence_iter_is_end (a));
1259 g_return_if_fail (!g_sequence_iter_is_end (b));
1264 a_pos = g_sequence_iter_get_position (a);
1265 b_pos = g_sequence_iter_get_position (b);
1278 rightmost_next = node_get_next (rightmost);
1280 /* The situation is now like this:
1282 * ..., leftmost, ......., rightmost, rightmost_next, ...
1285 g_sequence_move (rightmost, leftmost);
1286 g_sequence_move (leftmost, rightmost_next);
1290 * Implementation of the splay tree.
1293 node_update_fields (GSequenceNode *node)
1295 g_assert (node != NULL);
1300 node->n_nodes += node->left->n_nodes;
1303 node->n_nodes += node->right->n_nodes;
1306 #define NODE_LEFT_CHILD(n) (((n)->parent) && ((n)->parent->left) == (n))
1307 #define NODE_RIGHT_CHILD(n) (((n)->parent) && ((n)->parent->right) == (n))
1310 node_rotate (GSequenceNode *node)
1312 GSequenceNode *tmp, *old;
1314 g_assert (node->parent);
1315 g_assert (node->parent != node);
1317 if (NODE_LEFT_CHILD (node))
1322 node->right = node->parent;
1323 node->parent = node->parent->parent;
1326 if (node->parent->left == node->right)
1327 node->parent->left = node;
1329 node->parent->right = node;
1332 g_assert (node->right);
1334 node->right->parent = node;
1335 node->right->left = tmp;
1337 if (node->right->left)
1338 node->right->left->parent = node->right;
1347 node->left = node->parent;
1348 node->parent = node->parent->parent;
1351 if (node->parent->right == node->left)
1352 node->parent->right = node;
1354 node->parent->left = node;
1357 g_assert (node->left);
1359 node->left->parent = node;
1360 node->left->right = tmp;
1362 if (node->left->right)
1363 node->left->right->parent = node->left;
1368 node_update_fields (old);
1369 node_update_fields (node);
1372 static GSequenceNode *
1373 splay (GSequenceNode *node)
1375 while (node->parent)
1377 if (!node->parent->parent)
1382 else if ((NODE_LEFT_CHILD (node) && NODE_LEFT_CHILD (node->parent)) ||
1383 (NODE_RIGHT_CHILD (node) && NODE_RIGHT_CHILD (node->parent)))
1386 node_rotate (node->parent);
1400 static GSequenceNode *
1401 node_new (gpointer data)
1403 GSequenceNode *node = g_slice_new0 (GSequenceNode);
1405 node->parent = NULL;
1406 node->parent = NULL;
1416 static GSequenceNode *
1417 find_min (GSequenceNode *node)
1427 static GSequenceNode *
1428 find_max (GSequenceNode *node)
1438 static GSequenceNode *
1439 node_get_first (GSequenceNode *node)
1441 return splay (find_min (node));
1444 static GSequenceNode *
1445 node_get_last (GSequenceNode *node)
1447 return splay (find_max (node));
1451 get_n_nodes (GSequenceNode *node)
1454 return node->n_nodes;
1459 static GSequenceNode *
1460 node_get_by_pos (GSequenceNode *node,
1465 g_assert (node != NULL);
1469 while ((i = get_n_nodes (node->left)) != pos)
1479 g_assert (node->parent != NULL);
1483 return splay (node);
1486 static GSequenceNode *
1487 node_get_prev (GSequenceNode *node)
1498 return splay (node);
1501 static GSequenceNode *
1502 node_get_next (GSequenceNode *node)
1513 return splay (node);
1517 node_get_pos (GSequenceNode *node)
1521 return get_n_nodes (node->left);
1524 /* Return closest node _strictly_ bigger than @needle. This node
1525 * always exists because the tree has an explicit end node).
1526 * This end node of @haystack must be passed in @end.
1528 static GSequenceNode *
1529 node_find_closest (GSequenceNode *haystack,
1530 GSequenceNode *needle,
1532 GSequenceIterCompareFunc iter_cmp,
1535 GSequenceNode *best;
1538 g_assert (haystack);
1540 haystack = splay (haystack);
1546 /* iter_cmp can't be passed the end node, since the function may
1549 if (haystack == end)
1552 c = iter_cmp (haystack, needle, cmp_data);
1554 /* In the following we don't break even if c == 0. Instaed we go on
1555 * searching along the 'bigger' nodes, so that we find the last one
1556 * that is equal to the needle.
1559 haystack = haystack->left;
1561 haystack = haystack->right;
1563 while (haystack != NULL);
1565 /* If the best node is smaller or equal to the data, then move one step
1566 * to the right to make sure the best one is strictly bigger than the data
1568 if (best != end && c <= 0)
1569 best = node_get_next (best);
1575 node_free (GSequenceNode *node,
1578 GPtrArray *stack = g_ptr_array_new ();
1582 g_ptr_array_add (stack, node);
1584 while (stack->len > 0)
1586 node = g_ptr_array_remove_index (stack, stack->len - 1);
1590 g_ptr_array_add (stack, node->right);
1591 g_ptr_array_add (stack, node->left);
1593 if (seq && seq->data_destroy_notify && node != seq->end_node)
1594 seq->data_destroy_notify (node->data);
1596 g_slice_free (GSequenceNode, node);
1600 g_ptr_array_free (stack, TRUE);
1603 /* Splits into two trees. @node will be part of the right tree
1606 node_cut (GSequenceNode *node)
1610 g_assert (node->parent == NULL);
1613 node->left->parent = NULL;
1616 node_update_fields (node);
1620 node_insert_before (GSequenceNode *node,
1623 g_assert (node != NULL);
1624 g_assert (new != NULL);
1628 new = splay (find_min (new));
1629 g_assert (new->left == NULL);
1632 node->left->parent = new;
1634 new->left = node->left;
1639 node_update_fields (new);
1640 node_update_fields (node);
1644 node_insert_after (GSequenceNode *node,
1647 g_assert (node != NULL);
1648 g_assert (new != NULL);
1652 new = splay (find_max (new));
1653 g_assert (new->right == NULL);
1654 g_assert (node->parent == NULL);
1657 node->right->parent = new;
1659 new->right = node->right;
1664 node_update_fields (new);
1665 node_update_fields (node);
1669 node_get_length (GSequenceNode *node)
1671 g_assert (node != NULL);
1674 return node->n_nodes;
1678 node_unlink (GSequenceNode *node)
1680 GSequenceNode *right, *left;
1685 right = node->right;
1687 node->parent = node->left = node->right = NULL;
1688 node_update_fields (node);
1692 right->parent = NULL;
1694 right = node_get_first (right);
1695 g_assert (right->left == NULL);
1700 left->parent = right;
1701 node_update_fields (right);
1706 left->parent = NULL;
1711 node_insert_sorted (GSequenceNode *node,
1714 GSequenceIterCompareFunc iter_cmp,
1717 GSequenceNode *closest;
1719 closest = node_find_closest (node, new, end, iter_cmp, cmp_data);
1723 node_insert_before (closest, new);
1727 node_calc_height (GSequenceNode *node)
1738 left_height = node_calc_height (node->left);
1741 right_height = node_calc_height (node->right);
1743 return MAX (left_height, right_height) + 1;
1749 /* Self-test function */
1751 check_node (GSequenceNode *node)
1755 g_assert (node->parent != node);
1756 g_assert (node->n_nodes ==
1757 1 + get_n_nodes (node->left) + get_n_nodes (node->right));
1758 check_node (node->left);
1759 check_node (node->right);
1764 g_sequence_self_test_internal_to_glib_dont_use (GSequence *seq)
1766 GSequenceNode *node = splay (seq->end_node);
1771 #define __G_SEQUENCE_C__
1772 #include "galiasdef.c"