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;
38 GSequenceNode * parent;
40 GSequenceNode * right;
41 gpointer data; /* For the end node, this field points
47 * Declaration of GSequenceNode methods
49 static GSequenceNode *node_new (gpointer data);
50 static GSequenceNode *node_get_first (GSequenceNode *node);
51 static GSequenceNode *node_get_last (GSequenceNode *node);
52 static GSequenceNode *node_get_prev (GSequenceNode *node);
53 static GSequenceNode *node_get_next (GSequenceNode *node);
54 static gint node_get_pos (GSequenceNode *node);
55 static GSequenceNode *node_get_by_pos (GSequenceNode *node,
57 static GSequenceNode *node_find_closest (GSequenceNode *haystack,
58 GSequenceNode *needle,
60 GSequenceIterCompareFunc cmp,
62 static gint node_get_length (GSequenceNode *node);
63 static void node_free (GSequenceNode *node,
65 static void node_cut (GSequenceNode *split);
66 static void node_insert_after (GSequenceNode *node,
67 GSequenceNode *second);
68 static void node_insert_before (GSequenceNode *node,
70 static void node_unlink (GSequenceNode *node);
71 static void node_insert_sorted (GSequenceNode *node,
74 GSequenceIterCompareFunc cmp_func,
78 * Various helper functions
81 check_seq_access (GSequence *seq)
83 if (G_UNLIKELY (seq->access_prohibited))
85 g_warning ("Accessing a sequence while it is "
86 "being sorted or searched is not allowed");
91 get_sequence (GSequenceNode *node)
93 return (GSequence *)node_get_last (node)->data;
97 check_iter_access (GSequenceIter *iter)
99 check_seq_access (get_sequence (iter));
103 is_end (GSequenceIter *iter)
105 GSequence *seq = get_sequence (iter);
107 return seq->end_node == iter;
112 GCompareDataFunc cmp_func;
114 GSequenceNode *end_node;
117 /* This function compares two iters using a normal compare
118 * function and user_data passed in in a SortInfo struct
121 iter_compare (GSequenceIter *node1,
122 GSequenceIter *node2,
125 const SortInfo *info = data;
128 if (node1 == info->end_node)
131 if (node2 == info->end_node)
134 retval = info->cmp_func (node1->data, node2->data, info->cmp_data);
145 * @data_destroy: a #GDestroyNotify function, or %NULL
147 * Creates a new GSequence. The @data_destroy function, if non-%NULL will
148 * be called on all items when the sequence is destroyed and on items that
149 * are removed from the sequence.
151 * Return value: a new #GSequence
156 g_sequence_new (GDestroyNotify data_destroy)
158 GSequence *seq = g_new (GSequence, 1);
159 seq->data_destroy_notify = data_destroy;
161 seq->end_node = node_new (seq);
163 seq->access_prohibited = FALSE;
172 * Frees the memory allocated for @seq. If @seq has a data destroy
173 * function associated with it, that function is called on all items in
179 g_sequence_free (GSequence *seq)
181 g_return_if_fail (seq != NULL);
183 check_seq_access (seq);
185 node_free (seq->end_node, seq);
191 * g_sequence_foreach_range:
192 * @begin: a #GSequenceIter
193 * @end: a #GSequenceIter
195 * @user_data: user data passed to @func
197 * Calls @func for each item in the range (@begin, @end) passing
198 * @user_data to the function.
203 g_sequence_foreach_range (GSequenceIter *begin,
211 g_return_if_fail (func != NULL);
212 g_return_if_fail (begin != NULL);
213 g_return_if_fail (end != NULL);
215 seq = get_sequence (begin);
217 seq->access_prohibited = TRUE;
222 GSequenceIter *next = node_get_next (iter);
224 func (iter->data, user_data);
229 seq->access_prohibited = FALSE;
233 * g_sequence_foreach:
235 * @func: the function to call for each item in @seq
236 * @user_data: user data passed to @func
238 * Calls @func for each item in the sequence passing @user_data
244 g_sequence_foreach (GSequence *seq,
248 GSequenceIter *begin, *end;
250 check_seq_access (seq);
252 begin = g_sequence_get_begin_iter (seq);
253 end = g_sequence_get_end_iter (seq);
255 g_sequence_foreach_range (begin, end, func, user_data);
259 * g_sequence_range_get_midpoint:
260 * @begin: a #GSequenceIter
261 * @end: a #GSequenceIter
263 * Finds an iterator somewhere in the range (@begin, @end). This
264 * iterator will be close to the middle of the range, but is not
265 * guaranteed to be <emphasis>exactly</emphasis> in the middle.
267 * The @begin and @end iterators must both point to the same sequence and
268 * @begin must come before or be equal to @end in the sequence.
270 * Return value: A #GSequenceIter pointing somewhere in the
271 * (@begin, @end) range.
276 g_sequence_range_get_midpoint (GSequenceIter *begin,
279 int begin_pos, end_pos, mid_pos;
281 g_return_val_if_fail (begin != NULL, NULL);
282 g_return_val_if_fail (end != NULL, NULL);
283 g_return_val_if_fail (get_sequence (begin) == get_sequence (end), NULL);
285 begin_pos = node_get_pos (begin);
286 end_pos = node_get_pos (end);
288 g_return_val_if_fail (end_pos >= begin_pos, NULL);
290 mid_pos = begin_pos + (end_pos - begin_pos) / 2;
292 return node_get_by_pos (begin, mid_pos);
296 * g_sequence_iter_compare:
297 * @a: a #GSequenceIter
298 * @b: a #GSequenceIter
300 * Returns a negative number if @a comes before @b, 0 if they are equal,
301 * and a positive number if @a comes after @b.
303 * The @a and @b iterators must point into the same sequence.
305 * Return value: A negative number if @a comes before @b, 0 if they are
306 * equal, and a positive number if @a comes after @b.
311 g_sequence_iter_compare (GSequenceIter *a,
316 g_return_val_if_fail (a != NULL, 0);
317 g_return_val_if_fail (b != NULL, 0);
318 g_return_val_if_fail (get_sequence (a) == get_sequence (b), 0);
320 check_iter_access (a);
321 check_iter_access (b);
323 a_pos = node_get_pos (a);
324 b_pos = node_get_pos (b);
328 else if (a_pos > b_pos)
336 * @seq: a #GSequencePointer
337 * @data: the data for the new item
339 * Adds a new item to the end of @seq.
341 * Return value: an iterator pointing to the new item
346 g_sequence_append (GSequence *seq,
351 g_return_val_if_fail (seq != NULL, NULL);
353 check_seq_access (seq);
355 node = node_new (data);
356 node_insert_before (seq->end_node, node);
362 * g_sequence_prepend:
364 * @data: the data for the new item
366 * Adds a new item to the front of @seq
368 * Return value: an iterator pointing to the new item
373 g_sequence_prepend (GSequence *seq,
376 GSequenceNode *node, *first;
378 g_return_val_if_fail (seq != NULL, NULL);
380 check_seq_access (seq);
382 node = node_new (data);
383 first = node_get_first (seq->end_node);
385 node_insert_before (first, node);
391 * g_sequence_insert_before:
392 * @iter: a #GSequenceIter
393 * @data: the data for the new item
395 * Inserts a new item just before the item pointed to by @iter.
397 * Return value: an iterator pointing to the new item
402 g_sequence_insert_before (GSequenceIter *iter,
407 g_return_val_if_fail (iter != NULL, NULL);
409 check_iter_access (iter);
411 node = node_new (data);
413 node_insert_before (iter, node);
420 * @iter: a #GSequenceIter
422 * Removes the item pointed to by @iter. It is an error to pass the
423 * end iterator to this function.
425 * If the sequnce has a data destroy function associated with it, this
426 * function is called on the data for the removed item.
431 g_sequence_remove (GSequenceIter *iter)
435 g_return_if_fail (iter != NULL);
436 g_return_if_fail (!is_end (iter));
438 check_iter_access (iter);
440 seq = get_sequence (iter);
443 node_free (iter, seq);
447 * g_sequence_remove_range:
448 * @begin: a #GSequenceIter
449 * @end: a #GSequenceIter
451 * Removes all items in the (@begin, @end) range.
453 * If the sequence has a data destroy function associated with it, this
454 * function is called on the data for the removed items.
459 g_sequence_remove_range (GSequenceIter *begin,
462 g_return_if_fail (get_sequence (begin) == get_sequence (end));
464 check_iter_access (begin);
465 check_iter_access (end);
467 g_sequence_move_range (NULL, begin, end);
471 * g_sequence_move_range:
472 * @dest: a #GSequenceIter
473 * @begin: a #GSequenceIter
474 * @end: a #GSequenceIter
476 * Inserts the (@begin, @end) range at the destination pointed to by ptr.
477 * The @begin and @end iters must point into the same sequence. It is
478 * allowed for @dest to point to a different sequence than the one pointed
479 * into by @begin and @end.
481 * If @dest is NULL, the range indicated by @begin and @end is
482 * removed from the sequence. If @dest iter points to a place within
483 * the (@begin, @end) range, the range does not move.
488 g_sequence_move_range (GSequenceIter *dest,
489 GSequenceIter *begin,
493 GSequenceNode *first;
495 g_return_if_fail (begin != NULL);
496 g_return_if_fail (end != NULL);
498 check_iter_access (begin);
499 check_iter_access (end);
501 check_iter_access (dest);
503 src_seq = get_sequence (begin);
505 g_return_if_fail (src_seq == get_sequence (end));
507 /* Dest points to begin or end? */
508 if (dest == begin || dest == end)
511 /* begin comes after end? */
512 if (g_sequence_iter_compare (begin, end) >= 0)
515 /* dest points somewhere in the (begin, end) range? */
516 if (dest && get_sequence (dest) == src_seq &&
517 g_sequence_iter_compare (dest, begin) > 0 &&
518 g_sequence_iter_compare (dest, end) < 0)
523 src_seq = get_sequence (begin);
525 first = node_get_first (begin);
532 node_insert_after (node_get_last (first), end);
535 node_insert_before (dest, begin);
537 node_free (begin, src_seq);
543 * @cmp_func: the #GCompareDataFunc used to sort @seq. This function is
544 * passed two items of @seq and should return 0 if they are equal,
545 * a negative value fi the first comes before the second, and a
546 * positive value if the second comes before the first.
547 * @cmp_data: user data passed to @cmp_func
549 * Sorts @seq using @cmp_func.
554 g_sequence_sort (GSequence *seq,
555 GCompareDataFunc cmp_func,
558 SortInfo info = { cmp_func, cmp_data, seq->end_node };
560 check_seq_access (seq);
562 g_sequence_sort_iter (seq, iter_compare, &info);
566 * g_sequence_insert_sorted:
568 * @data: the data to insert
569 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
570 * is called with two items of the @seq and @user_data. It should
571 * return 0 if the items are equal, a negative value if the first
572 * item comes before the second, and a positive value if the second
573 * item comes before the first.
574 * @cmp_data: user data passed to @cmp_func.
576 * Inserts @data into @sequence using @func to determine the new position.
577 * The sequence must already be sorted according to @cmp_func; otherwise the
578 * new position of @data is undefined.
580 * Return value: a #GSequenceIter pointing to the new item.
585 g_sequence_insert_sorted (GSequence *seq,
587 GCompareDataFunc cmp_func,
590 SortInfo info = { cmp_func, cmp_data, NULL };
592 g_return_val_if_fail (seq != NULL, NULL);
593 g_return_val_if_fail (cmp_func != NULL, NULL);
595 info.end_node = seq->end_node;
596 check_seq_access (seq);
598 return g_sequence_insert_sorted_iter (seq, data, iter_compare, &info);
602 * g_sequence_sort_changed:
603 * @iter: A #GSequenceIter
604 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
605 * is called with two items of the @seq and @user_data. It should
606 * return 0 if the items are equal, a negative value if the first
607 * item comes before the second, and a positive value if the second
608 * item comes before the first.
609 * @cmp_data: user data passed to @cmp_func.
611 * Moves the data pointed to a new position as indicated by @cmp_func. This
612 * function should be called for items in a sequence already sorted according
613 * to @cmp_func whenever some aspect of an item changes so that @cmp_func
614 * may return different values for that item.
619 g_sequence_sort_changed (GSequenceIter *iter,
620 GCompareDataFunc cmp_func,
623 SortInfo info = { cmp_func, cmp_data, NULL };
625 g_return_if_fail (!is_end (iter));
627 info.end_node = get_sequence (iter)->end_node;
628 check_iter_access (iter);
630 g_sequence_sort_changed_iter (iter, iter_compare, &info);
636 * @data: data for the new item
637 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
638 * is called with two items of the @seq and @user_data. It should
639 * return 0 if the items are equal, a negative value if the first
640 * item comes before the second, and a positive value if the second
641 * item comes before the first.
642 * @cmp_data: user data passed to @cmp_func.
644 * Returns an iterator pointing to the position where @data would
645 * be inserted according to @cmp_func and @cmp_data.
647 * Return value: an #GSequenceIter pointing to the position where @data
648 * would have been inserted according to @cmp_func and @cmp_data.
653 g_sequence_search (GSequence *seq,
655 GCompareDataFunc cmp_func,
658 SortInfo info = { cmp_func, cmp_data, NULL };
660 g_return_val_if_fail (seq != NULL, NULL);
662 info.end_node = seq->end_node;
663 check_seq_access (seq);
665 return g_sequence_search_iter (seq, data, iter_compare, &info);
669 * g_sequence_sort_iter:
671 * @cmp_func: the #GSequenceItercompare used to compare iterators in the
672 * sequence. It is called with two iterators pointing into @seq. It should
673 * return 0 if the iterators are equal, a negative value if the first
674 * iterator comes before the second, and a positive value if the second
675 * iterator comes before the first.
676 * @cmp_data: user data passed to @cmp_func
678 * Like g_sequence_sort(), but uses a #GSequenceIterCompareFunc instead
679 * of a GCompareDataFunc as the compare function
684 g_sequence_sort_iter (GSequence *seq,
685 GSequenceIterCompareFunc cmp_func,
689 GSequenceNode *begin, *end;
691 g_return_if_fail (seq != NULL);
692 g_return_if_fail (cmp_func != NULL);
694 check_seq_access (seq);
696 begin = g_sequence_get_begin_iter (seq);
697 end = g_sequence_get_end_iter (seq);
699 tmp = g_sequence_new (NULL);
701 g_sequence_move_range (g_sequence_get_begin_iter (tmp), begin, end);
703 tmp->access_prohibited = TRUE;
704 seq->access_prohibited = TRUE;
706 while (g_sequence_get_length (tmp) > 0)
708 GSequenceNode *node = g_sequence_get_begin_iter (tmp);
710 node_insert_sorted (seq->end_node, node, seq->end_node,
714 tmp->access_prohibited = FALSE;
715 seq->access_prohibited = FALSE;
717 g_sequence_free (tmp);
721 * g_sequence_sort_changed_iter:
722 * @iter: a #GSequenceIter
723 * @iter_cmp: the #GSequenceItercompare used to compare iterators in the
724 * sequence. It is called with two iterators pointing into @seq. It should
725 * return 0 if the iterators are equal, a negative value if the first
726 * iterator comes before the second, and a positive value if the second
727 * iterator comes before the first.
728 * @cmp_data: user data passed to @cmp_func
730 * Like g_sequence_sort_changed(), but uses
731 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
732 * the compare function.
737 g_sequence_sort_changed_iter (GSequenceIter *iter,
738 GSequenceIterCompareFunc iter_cmp,
741 GSequence *seq, *tmp_seq;
742 GSequenceIter *next, *prev;
744 g_return_if_fail (iter != NULL);
745 g_return_if_fail (!is_end (iter));
746 g_return_if_fail (iter_cmp != NULL);
747 check_iter_access (iter);
749 /* If one of the neighbours is equal to iter, then
750 * don't move it. This ensures that sort_changed() is
751 * a stable operation.
754 next = node_get_next (iter);
755 prev = node_get_prev (iter);
757 if (prev != iter && iter_cmp (prev, iter, cmp_data) == 0)
760 if (!is_end (next) && iter_cmp (next, iter, cmp_data) == 0)
763 seq = get_sequence (iter);
765 seq->access_prohibited = TRUE;
767 tmp_seq = g_sequence_new (NULL);
769 node_insert_before (tmp_seq->end_node, iter);
771 node_insert_sorted (seq->end_node, iter, seq->end_node,
774 g_sequence_free (tmp_seq);
776 seq->access_prohibited = FALSE;
780 * g_sequence_insert_sorted_iter:
782 * @data: data for the new item
783 * @iter_cmp: 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_insert_sorted(), but uses
791 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
792 * the compare function.
794 * Return value: a #GSequenceIter pointing to the new item
799 g_sequence_insert_sorted_iter (GSequence *seq,
801 GSequenceIterCompareFunc iter_cmp,
804 GSequenceNode *new_node;
807 g_return_val_if_fail (seq != NULL, NULL);
808 g_return_val_if_fail (iter_cmp != NULL, NULL);
810 check_seq_access (seq);
812 seq->access_prohibited = TRUE;
814 /* Create a new temporary sequence and put the new node into
815 * that. The reason for this is that the user compare function
816 * will be called with the new node, and if it dereferences,
817 * "is_end" will be called on it. But that will crash if the
818 * node is not actually in a sequence.
820 * node_insert_sorted() makes sure the node is unlinked before
823 * The reason we need the "iter" versions at all is that that
824 * is the only kind of compare functions GtkTreeView can use.
826 tmp_seq = g_sequence_new (NULL);
827 new_node = g_sequence_append (tmp_seq, data);
829 node_insert_sorted (seq->end_node, new_node,
830 seq->end_node, iter_cmp, cmp_data);
832 g_sequence_free (tmp_seq);
834 seq->access_prohibited = FALSE;
840 * g_sequence_search_iter:
842 * @data: data for the new item
843 * @iter_cmp: the #GSequenceIterCompare function used to compare iterators
844 * in the sequence. It is called with two iterators pointing into @seq.
845 * It should return 0 if the iterators are equal, a negative value if the
846 * first iterator comes before the second, and a positive value if the
847 * second iterator comes before the first.
848 * @cmp_data: user data passed to @iter_cmp
850 * Like g_sequence_search(), but uses
851 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
852 * the compare function.
854 * Return value: a #GSequenceIter pointing to the position in @seq
855 * where @data would have been inserted according to @iter_cmp and @cmp_data.
860 g_sequence_search_iter (GSequence *seq,
862 GSequenceIterCompareFunc iter_cmp,
866 GSequenceNode *dummy;
869 g_return_val_if_fail (seq != NULL, NULL);
871 check_seq_access (seq);
873 seq->access_prohibited = TRUE;
875 /* Create a new temporary sequence and put the dummy node into
876 * that. The reason for this is that the user compare function
877 * will be called with the new node, and if it dereferences,
878 * "is_end" will be called on it. But that will crash if the
879 * node is not actually in a sequence.
881 * node_insert_sorted() makes sure the node is unlinked before
884 * The reason we need the "iter" versions at all is that that
885 * is the only kind of compare functions GtkTreeView can use.
887 tmp_seq = g_sequence_new (NULL);
888 dummy = g_sequence_append (tmp_seq, data);
890 node = node_find_closest (seq->end_node, dummy,
891 seq->end_node, iter_cmp, cmp_data);
893 g_sequence_free (tmp_seq);
895 seq->access_prohibited = FALSE;
901 * g_sequence_iter_get_sequence:
902 * @iter: a #GSequenceIter
904 * Returns the #GSequence that @iter points into.
906 * Return value: the #GSequence that @iter points into.
911 g_sequence_iter_get_sequence (GSequenceIter *iter)
913 g_return_val_if_fail (iter != NULL, NULL);
915 return get_sequence (iter);
920 * @iter: a #GSequenceIter
922 * Returns the data that @iter points to.
924 * Return value: the data that @iter points to
929 g_sequence_get (GSequenceIter *iter)
931 g_return_val_if_fail (iter != NULL, NULL);
932 g_return_val_if_fail (!is_end (iter), NULL);
939 * @iter: a #GSequenceIter
940 * @data: new data for the item
942 * Changes the data for the item pointed to by @iter to be @data. If
943 * the sequence has a data destroy function associated with it, that
944 * function is called on the existing data that @iter pointed to.
949 g_sequence_set (GSequenceIter *iter,
954 g_return_if_fail (iter != NULL);
955 g_return_if_fail (!is_end (iter));
957 seq = get_sequence (iter);
959 /* If @data is identical to iter->data, it is destroyed
960 * here. This will work right in case of ref-counted objects. Also
961 * it is similar to what ghashtables do.
963 * For non-refcounted data it's a little less convenient, but
964 * code relying on self-setting not destroying would be
965 * pretty dubious anyway ...
968 if (seq->data_destroy_notify)
969 seq->data_destroy_notify (iter->data);
975 * g_sequence_get_length:
978 * Returns the length of @seq
980 * Return value: the length of @seq
985 g_sequence_get_length (GSequence *seq)
987 return node_get_length (seq->end_node) - 1;
991 * g_sequence_get_end_iter:
994 * Returns the end iterator for @seg
996 * Return value: the end iterator for @seq
1001 g_sequence_get_end_iter (GSequence *seq)
1003 g_return_val_if_fail (seq != NULL, NULL);
1005 g_assert (is_end (seq->end_node));
1007 return seq->end_node;
1011 * g_sequence_get_begin_iter:
1012 * @seq: a #GSequence
1014 * Returns the begin iterator for @seq.
1016 * Return value: the begin iterator for @seq.
1021 g_sequence_get_begin_iter (GSequence *seq)
1023 g_return_val_if_fail (seq != NULL, NULL);
1024 return node_get_first (seq->end_node);
1028 clamp_position (GSequence *seq,
1031 gint len = g_sequence_get_length (seq);
1033 if (pos > len || pos < 0)
1040 * if pos > number of items or -1, will return end pointer
1043 * g_sequence_get_iter_at_pos:
1044 * @seq: a #GSequence
1045 * @pos: a position in @seq, or -1 for the end.
1047 * Returns the iterator at position @pos. If @pos is negative or larger
1048 * than the number of items in @seq, the end iterator is returned.
1050 * Return value: The #GSequenceIter at position @pos
1055 g_sequence_get_iter_at_pos (GSequence *seq,
1058 g_return_val_if_fail (seq != NULL, NULL);
1060 pos = clamp_position (seq, pos);
1062 return node_get_by_pos (seq->end_node, pos);
1067 * @src: a #GSequenceIter pointing to the item to move
1068 * @dest: a #GSequenceIter pointing to the position to which
1069 * the item is moved.
1071 * Moves the item pointed to by @src to the position indicated by @dest.
1072 * After calling this function @dest will point to the position immediately
1078 g_sequence_move (GSequenceIter *src,
1079 GSequenceIter *dest)
1081 g_return_if_fail (src != NULL);
1082 g_return_if_fail (dest != NULL);
1083 g_return_if_fail (!is_end (src));
1089 node_insert_before (dest, src);
1095 * g_sequence_iter_is_end:
1096 * @iter: a #GSequenceIter
1098 * Returns whether @iter is the end iterator
1100 * Return value: Whether @iter is the end iterator.
1105 g_sequence_iter_is_end (GSequenceIter *iter)
1107 g_return_val_if_fail (iter != NULL, FALSE);
1109 return is_end (iter);
1113 * g_sequence_iter_is_begin:
1114 * @iter: a #GSequenceIter
1116 * Returns whether @iter is the begin iterator
1118 * Return value: whether @iter is the begin iterator
1123 g_sequence_iter_is_begin (GSequenceIter *iter)
1125 g_return_val_if_fail (iter != NULL, FALSE);
1127 return (node_get_prev (iter) == iter);
1131 * g_sequence_iter_get_position:
1132 * @iter: a #GSequenceIter
1134 * Returns the position of @iter
1136 * Return value: the position of @iter
1141 g_sequence_iter_get_position (GSequenceIter *iter)
1143 g_return_val_if_fail (iter != NULL, -1);
1145 return node_get_pos (iter);
1149 * g_sequence_iter_next:
1150 * @iter: a #GSequenceIter
1152 * Returns an iterator pointing to the next position after @iter. If
1153 * @iter is the end iterator, the end iterator is returned.
1155 * Return value: a #GSequenceIter pointing to the next position after @iter.
1160 g_sequence_iter_next (GSequenceIter *iter)
1162 g_return_val_if_fail (iter != NULL, NULL);
1164 return node_get_next (iter);
1168 * g_sequence_iter_prev:
1169 * @iter: a #GSequenceIter
1171 * Returns an iterator pointing to the previous position before @iter. If
1172 * @iter is the begin iterator, the begin iterator is returned.
1174 * Return value: a #GSequenceIter pointing to the previous position before
1180 g_sequence_iter_prev (GSequenceIter *iter)
1182 g_return_val_if_fail (iter != NULL, NULL);
1184 return node_get_prev (iter);
1188 * g_sequence_iter_move:
1189 * @iter: a #GSequenceIter
1190 * @delta: A positive or negative number indicating how many positions away
1191 * from @iter the returned #GSequenceIter will be.
1193 * Returns the #GSequenceIter which is @delta positions away from @iter.
1194 * If @iter is closer than -@delta positions to the beginning of the sequence,
1195 * the begin iterator is returned. If @iter is closer than @delta positions
1196 * to the end of the sequence, the end iterator is returned.
1198 * Return value: a #GSequenceIter which is @delta positions away from @iter.
1203 g_sequence_iter_move (GSequenceIter *iter,
1208 g_return_val_if_fail (iter != NULL, NULL);
1210 new_pos = node_get_pos (iter) + delta;
1212 new_pos = clamp_position (get_sequence (iter), new_pos);
1214 return node_get_by_pos (iter, new_pos);
1219 * @a: a #GSequenceIter
1220 * @b: a #GSequenceIter
1222 * Swaps the items pointed to by @a and @b
1227 g_sequence_swap (GSequenceIter *a,
1230 GSequenceNode *leftmost, *rightmost, *rightmost_next;
1233 g_return_if_fail (!g_sequence_iter_is_end (a));
1234 g_return_if_fail (!g_sequence_iter_is_end (b));
1239 a_pos = g_sequence_iter_get_position (a);
1240 b_pos = g_sequence_iter_get_position (b);
1253 rightmost_next = node_get_next (rightmost);
1255 /* The situation is now like this:
1257 * ..., leftmost, ......., rightmost, rightmost_next, ...
1260 g_sequence_move (rightmost, leftmost);
1261 g_sequence_move (leftmost, rightmost_next);
1265 * Implementation of the splay tree.
1268 node_update_fields (GSequenceNode *node)
1270 g_assert (node != NULL);
1275 node->n_nodes += node->left->n_nodes;
1278 node->n_nodes += node->right->n_nodes;
1281 #define NODE_LEFT_CHILD(n) (((n)->parent) && ((n)->parent->left) == (n))
1282 #define NODE_RIGHT_CHILD(n) (((n)->parent) && ((n)->parent->right) == (n))
1285 node_rotate (GSequenceNode *node)
1287 GSequenceNode *tmp, *old;
1289 g_assert (node->parent);
1290 g_assert (node->parent != node);
1292 if (NODE_LEFT_CHILD (node))
1297 node->right = node->parent;
1298 node->parent = node->parent->parent;
1301 if (node->parent->left == node->right)
1302 node->parent->left = node;
1304 node->parent->right = node;
1307 g_assert (node->right);
1309 node->right->parent = node;
1310 node->right->left = tmp;
1312 if (node->right->left)
1313 node->right->left->parent = node->right;
1322 node->left = node->parent;
1323 node->parent = node->parent->parent;
1326 if (node->parent->right == node->left)
1327 node->parent->right = node;
1329 node->parent->left = node;
1332 g_assert (node->left);
1334 node->left->parent = node;
1335 node->left->right = tmp;
1337 if (node->left->right)
1338 node->left->right->parent = node->left;
1343 node_update_fields (old);
1344 node_update_fields (node);
1347 static GSequenceNode *
1348 splay (GSequenceNode *node)
1350 while (node->parent)
1352 if (!node->parent->parent)
1357 else if ((NODE_LEFT_CHILD (node) && NODE_LEFT_CHILD (node->parent)) ||
1358 (NODE_RIGHT_CHILD (node) && NODE_RIGHT_CHILD (node->parent)))
1361 node_rotate (node->parent);
1375 static GSequenceNode *
1376 node_new (gpointer data)
1378 GSequenceNode *node = g_slice_new0 (GSequenceNode);
1380 node->parent = NULL;
1381 node->parent = NULL;
1391 static GSequenceNode *
1392 find_min (GSequenceNode *node)
1402 static GSequenceNode *
1403 find_max (GSequenceNode *node)
1413 static GSequenceNode *
1414 node_get_first (GSequenceNode *node)
1416 return splay (find_min (node));
1419 static GSequenceNode *
1420 node_get_last (GSequenceNode *node)
1422 return splay (find_max (node));
1426 get_n_nodes (GSequenceNode *node)
1429 return node->n_nodes;
1434 static GSequenceNode *
1435 node_get_by_pos (GSequenceNode *node,
1440 g_assert (node != NULL);
1444 while ((i = get_n_nodes (node->left)) != pos)
1454 g_assert (node->parent != NULL);
1458 return splay (node);
1461 static GSequenceNode *
1462 node_get_prev (GSequenceNode *node)
1473 return splay (node);
1476 static GSequenceNode *
1477 node_get_next (GSequenceNode *node)
1488 return splay (node);
1492 node_get_pos (GSequenceNode *node)
1496 return get_n_nodes (node->left);
1499 /* Return closest node _strictly_ bigger than @needle. This node
1500 * always exists because the tree has an explicit end node).
1501 * This end node of @haystack must be passed in @end.
1503 static GSequenceNode *
1504 node_find_closest (GSequenceNode *haystack,
1505 GSequenceNode *needle,
1507 GSequenceIterCompareFunc iter_cmp,
1510 GSequenceNode *best;
1513 g_assert (haystack);
1515 haystack = splay (haystack);
1521 /* iter_cmp can't be passed the end node, since the function may
1524 if (haystack == end)
1527 c = iter_cmp (haystack, needle, cmp_data);
1529 /* In the following we don't break even if c == 0. Instaed we go on
1530 * searching along the 'bigger' nodes, so that we find the last one
1531 * that is equal to the needle.
1534 haystack = haystack->left;
1536 haystack = haystack->right;
1538 while (haystack != NULL);
1540 /* If the best node is smaller or equal to the data, then move one step
1541 * to the right to make sure the best one is strictly bigger than the data
1543 if (best != end && c <= 0)
1544 best = node_get_next (best);
1550 node_free (GSequenceNode *node,
1553 GPtrArray *stack = g_ptr_array_new ();
1557 g_ptr_array_add (stack, node);
1559 while (stack->len > 0)
1561 node = g_ptr_array_remove_index (stack, stack->len - 1);
1565 g_ptr_array_add (stack, node->right);
1566 g_ptr_array_add (stack, node->left);
1568 if (seq && seq->data_destroy_notify && node != seq->end_node)
1569 seq->data_destroy_notify (node->data);
1571 g_slice_free (GSequenceNode, node);
1575 g_ptr_array_free (stack, TRUE);
1578 /* Splits into two trees. @node will be part of the right tree
1581 node_cut (GSequenceNode *node)
1585 g_assert (node->parent == NULL);
1588 node->left->parent = NULL;
1591 node_update_fields (node);
1595 node_insert_before (GSequenceNode *node,
1598 g_assert (node != NULL);
1599 g_assert (new != NULL);
1603 new = splay (find_min (new));
1604 g_assert (new->left == NULL);
1607 node->left->parent = new;
1609 new->left = node->left;
1614 node_update_fields (new);
1615 node_update_fields (node);
1619 node_insert_after (GSequenceNode *node,
1622 g_assert (node != NULL);
1623 g_assert (new != NULL);
1627 new = splay (find_max (new));
1628 g_assert (new->right == NULL);
1629 g_assert (node->parent == NULL);
1632 node->right->parent = new;
1634 new->right = node->right;
1639 node_update_fields (new);
1640 node_update_fields (node);
1644 node_get_length (GSequenceNode *node)
1646 g_assert (node != NULL);
1649 return node->n_nodes;
1653 node_unlink (GSequenceNode *node)
1655 GSequenceNode *right, *left;
1660 right = node->right;
1662 node->parent = node->left = node->right = NULL;
1663 node_update_fields (node);
1667 right->parent = NULL;
1669 right = node_get_first (right);
1670 g_assert (right->left == NULL);
1675 left->parent = right;
1676 node_update_fields (right);
1681 left->parent = NULL;
1686 node_insert_sorted (GSequenceNode *node,
1689 GSequenceIterCompareFunc iter_cmp,
1692 GSequenceNode *closest;
1694 closest = node_find_closest (node, new, end, iter_cmp, cmp_data);
1698 node_insert_before (closest, new);
1702 node_calc_height (GSequenceNode *node)
1713 left_height = node_calc_height (node->left);
1716 right_height = node_calc_height (node->right);
1718 return MAX (left_height, right_height) + 1;
1724 /* Self-test function */
1726 check_node (GSequenceNode *node)
1730 g_assert (node->parent != node);
1731 g_assert (node->n_nodes ==
1732 1 + get_n_nodes (node->left) + get_n_nodes (node->right));
1733 check_node (node->left);
1734 check_node (node->right);
1739 g_sequence_self_test_internal_to_glib_dont_use (GSequence *seq)
1741 GSequenceNode *node = splay (seq->end_node);
1746 #define __G_SEQUENCE_C__
1747 #include "galiasdef.c"