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 are 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)
119 if (iter->parent && iter->parent->right != iter)
122 seq = get_sequence (iter);
124 return seq->end_node == iter;
129 GCompareDataFunc cmp_func;
131 GSequenceNode *end_node;
134 /* This function compares two iters using a normal compare
135 * function and user_data passed in in a SortInfo struct
138 iter_compare (GSequenceIter *node1,
139 GSequenceIter *node2,
142 const SortInfo *info = data;
145 if (node1 == info->end_node)
148 if (node2 == info->end_node)
151 retval = info->cmp_func (node1->data, node2->data, info->cmp_data);
162 * @data_destroy: a #GDestroyNotify function, or %NULL
164 * Creates a new GSequence. The @data_destroy function, if non-%NULL will
165 * be called on all items when the sequence is destroyed and on items that
166 * are removed from the sequence.
168 * Return value: a new #GSequence
173 g_sequence_new (GDestroyNotify data_destroy)
175 GSequence *seq = g_new (GSequence, 1);
176 seq->data_destroy_notify = data_destroy;
178 seq->end_node = node_new (seq);
180 seq->access_prohibited = FALSE;
182 seq->real_sequence = seq;
191 * Frees the memory allocated for @seq. If @seq has a data destroy
192 * function associated with it, that function is called on all items in
198 g_sequence_free (GSequence *seq)
200 g_return_if_fail (seq != NULL);
202 check_seq_access (seq);
204 node_free (seq->end_node, seq);
210 * g_sequence_foreach_range:
211 * @begin: a #GSequenceIter
212 * @end: a #GSequenceIter
214 * @user_data: user data passed to @func
216 * Calls @func for each item in the range (@begin, @end) passing
217 * @user_data to the function.
222 g_sequence_foreach_range (GSequenceIter *begin,
230 g_return_if_fail (func != NULL);
231 g_return_if_fail (begin != NULL);
232 g_return_if_fail (end != NULL);
234 seq = get_sequence (begin);
236 seq->access_prohibited = TRUE;
241 GSequenceIter *next = node_get_next (iter);
243 func (iter->data, user_data);
248 seq->access_prohibited = FALSE;
252 * g_sequence_foreach:
254 * @func: the function to call for each item in @seq
255 * @user_data: user data passed to @func
257 * Calls @func for each item in the sequence passing @user_data
263 g_sequence_foreach (GSequence *seq,
267 GSequenceIter *begin, *end;
269 check_seq_access (seq);
271 begin = g_sequence_get_begin_iter (seq);
272 end = g_sequence_get_end_iter (seq);
274 g_sequence_foreach_range (begin, end, func, user_data);
278 * g_sequence_range_get_midpoint:
279 * @begin: a #GSequenceIter
280 * @end: a #GSequenceIter
282 * Finds an iterator somewhere in the range (@begin, @end). This
283 * iterator will be close to the middle of the range, but is not
284 * guaranteed to be <emphasis>exactly</emphasis> in the middle.
286 * The @begin and @end iterators must both point to the same sequence and
287 * @begin must come before or be equal to @end in the sequence.
289 * Return value: A #GSequenceIter pointing somewhere in the
290 * (@begin, @end) range.
295 g_sequence_range_get_midpoint (GSequenceIter *begin,
298 int begin_pos, end_pos, mid_pos;
300 g_return_val_if_fail (begin != NULL, NULL);
301 g_return_val_if_fail (end != NULL, NULL);
302 g_return_val_if_fail (get_sequence (begin) == get_sequence (end), NULL);
304 begin_pos = node_get_pos (begin);
305 end_pos = node_get_pos (end);
307 g_return_val_if_fail (end_pos >= begin_pos, NULL);
309 mid_pos = begin_pos + (end_pos - begin_pos) / 2;
311 return node_get_by_pos (begin, mid_pos);
315 * g_sequence_iter_compare:
316 * @a: a #GSequenceIter
317 * @b: a #GSequenceIter
319 * Returns a negative number if @a comes before @b, 0 if they are equal,
320 * and a positive number if @a comes after @b.
322 * The @a and @b iterators must point into the same sequence.
324 * Return value: A negative number if @a comes before @b, 0 if they are
325 * equal, and a positive number if @a comes after @b.
330 g_sequence_iter_compare (GSequenceIter *a,
335 g_return_val_if_fail (a != NULL, 0);
336 g_return_val_if_fail (b != NULL, 0);
337 g_return_val_if_fail (get_sequence (a) == get_sequence (b), 0);
339 check_iter_access (a);
340 check_iter_access (b);
342 a_pos = node_get_pos (a);
343 b_pos = node_get_pos (b);
347 else if (a_pos > b_pos)
355 * @seq: a #GSequencePointer
356 * @data: the data for the new item
358 * Adds a new item to the end of @seq.
360 * Return value: an iterator pointing to the new item
365 g_sequence_append (GSequence *seq,
370 g_return_val_if_fail (seq != NULL, NULL);
372 check_seq_access (seq);
374 node = node_new (data);
375 node_insert_before (seq->end_node, node);
381 * g_sequence_prepend:
383 * @data: the data for the new item
385 * Adds a new item to the front of @seq
387 * Return value: an iterator pointing to the new item
392 g_sequence_prepend (GSequence *seq,
395 GSequenceNode *node, *first;
397 g_return_val_if_fail (seq != NULL, NULL);
399 check_seq_access (seq);
401 node = node_new (data);
402 first = node_get_first (seq->end_node);
404 node_insert_before (first, node);
410 * g_sequence_insert_before:
411 * @iter: a #GSequenceIter
412 * @data: the data for the new item
414 * Inserts a new item just before the item pointed to by @iter.
416 * Return value: an iterator pointing to the new item
421 g_sequence_insert_before (GSequenceIter *iter,
426 g_return_val_if_fail (iter != NULL, NULL);
428 check_iter_access (iter);
430 node = node_new (data);
432 node_insert_before (iter, node);
439 * @iter: a #GSequenceIter
441 * Removes the item pointed to by @iter. It is an error to pass the
442 * end iterator to this function.
444 * If the sequnce has a data destroy function associated with it, this
445 * function is called on the data for the removed item.
450 g_sequence_remove (GSequenceIter *iter)
454 g_return_if_fail (iter != NULL);
455 g_return_if_fail (!is_end (iter));
457 check_iter_access (iter);
459 seq = get_sequence (iter);
462 node_free (iter, seq);
466 * g_sequence_remove_range:
467 * @begin: a #GSequenceIter
468 * @end: a #GSequenceIter
470 * Removes all items in the (@begin, @end) range.
472 * If the sequence has a data destroy function associated with it, this
473 * function is called on the data for the removed items.
478 g_sequence_remove_range (GSequenceIter *begin,
481 g_return_if_fail (get_sequence (begin) == get_sequence (end));
483 check_iter_access (begin);
484 check_iter_access (end);
486 g_sequence_move_range (NULL, begin, end);
490 * g_sequence_move_range:
491 * @dest: a #GSequenceIter
492 * @begin: a #GSequenceIter
493 * @end: a #GSequenceIter
495 * Inserts the (@begin, @end) range at the destination pointed to by ptr.
496 * The @begin and @end iters must point into the same sequence. It is
497 * allowed for @dest to point to a different sequence than the one pointed
498 * into by @begin and @end.
500 * If @dest is NULL, the range indicated by @begin and @end is
501 * removed from the sequence. If @dest iter points to a place within
502 * the (@begin, @end) range, the range does not move.
507 g_sequence_move_range (GSequenceIter *dest,
508 GSequenceIter *begin,
512 GSequenceNode *first;
514 g_return_if_fail (begin != NULL);
515 g_return_if_fail (end != NULL);
517 check_iter_access (begin);
518 check_iter_access (end);
520 check_iter_access (dest);
522 src_seq = get_sequence (begin);
524 g_return_if_fail (src_seq == get_sequence (end));
526 /* Dest points to begin or end? */
527 if (dest == begin || dest == end)
530 /* begin comes after end? */
531 if (g_sequence_iter_compare (begin, end) >= 0)
534 /* dest points somewhere in the (begin, end) range? */
535 if (dest && get_sequence (dest) == src_seq &&
536 g_sequence_iter_compare (dest, begin) > 0 &&
537 g_sequence_iter_compare (dest, end) < 0)
542 src_seq = get_sequence (begin);
544 first = node_get_first (begin);
551 node_insert_after (node_get_last (first), end);
554 node_insert_before (dest, begin);
556 node_free (begin, src_seq);
562 * @cmp_func: the #GCompareDataFunc used to sort @seq. This function is
563 * passed two items of @seq and should return 0 if they are equal,
564 * a negative value fi the first comes before the second, and a
565 * positive value if the second comes before the first.
566 * @cmp_data: user data passed to @cmp_func
568 * Sorts @seq using @cmp_func.
573 g_sequence_sort (GSequence *seq,
574 GCompareDataFunc cmp_func,
577 SortInfo info = { cmp_func, cmp_data, seq->end_node };
579 check_seq_access (seq);
581 g_sequence_sort_iter (seq, iter_compare, &info);
585 * g_sequence_insert_sorted:
587 * @data: the data to insert
588 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
589 * is called with two items of the @seq and @user_data. It should
590 * return 0 if the items are equal, a negative value if the first
591 * item comes before the second, and a positive value if the second
592 * item comes before the first.
593 * @cmp_data: user data passed to @cmp_func.
595 * Inserts @data into @sequence using @func to determine the new position.
596 * The sequence must already be sorted according to @cmp_func; otherwise the
597 * new position of @data is undefined.
599 * Return value: a #GSequenceIter pointing to the new item.
604 g_sequence_insert_sorted (GSequence *seq,
606 GCompareDataFunc cmp_func,
609 SortInfo info = { cmp_func, cmp_data, NULL };
611 g_return_val_if_fail (seq != NULL, NULL);
612 g_return_val_if_fail (cmp_func != NULL, NULL);
614 info.end_node = seq->end_node;
615 check_seq_access (seq);
617 return g_sequence_insert_sorted_iter (seq, data, iter_compare, &info);
621 * g_sequence_sort_changed:
622 * @iter: A #GSequenceIter
623 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
624 * is called with two items of the @seq and @user_data. It should
625 * return 0 if the items are equal, a negative value if the first
626 * item comes before the second, and a positive value if the second
627 * item comes before the first.
628 * @cmp_data: user data passed to @cmp_func.
630 * Moves the data pointed to a new position as indicated by @cmp_func. This
631 * function should be called for items in a sequence already sorted according
632 * to @cmp_func whenever some aspect of an item changes so that @cmp_func
633 * may return different values for that item.
638 g_sequence_sort_changed (GSequenceIter *iter,
639 GCompareDataFunc cmp_func,
642 SortInfo info = { cmp_func, cmp_data, NULL };
644 g_return_if_fail (!is_end (iter));
646 info.end_node = get_sequence (iter)->end_node;
647 check_iter_access (iter);
649 g_sequence_sort_changed_iter (iter, iter_compare, &info);
655 * @data: data for the new item
656 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
657 * is called with two items of the @seq and @user_data. It should
658 * return 0 if the items are equal, a negative value if the first
659 * item comes before the second, and a positive value if the second
660 * item comes before the first.
661 * @cmp_data: user data passed to @cmp_func.
663 * Returns an iterator pointing to the position where @data would
664 * be inserted according to @cmp_func and @cmp_data.
666 * Return value: an #GSequenceIter pointing to the position where @data
667 * would have been inserted according to @cmp_func and @cmp_data.
672 g_sequence_search (GSequence *seq,
674 GCompareDataFunc cmp_func,
677 SortInfo info = { cmp_func, cmp_data, NULL };
679 g_return_val_if_fail (seq != NULL, NULL);
681 info.end_node = seq->end_node;
682 check_seq_access (seq);
684 return g_sequence_search_iter (seq, data, iter_compare, &info);
688 * g_sequence_sort_iter:
690 * @cmp_func: the #GSequenceItercompare used to compare iterators in the
691 * sequence. It is called with two iterators pointing into @seq. It should
692 * return 0 if the iterators are equal, a negative value if the first
693 * iterator comes before the second, and a positive value if the second
694 * iterator comes before the first.
695 * @cmp_data: user data passed to @cmp_func
697 * Like g_sequence_sort(), but uses a #GSequenceIterCompareFunc instead
698 * of a GCompareDataFunc as the compare function
703 g_sequence_sort_iter (GSequence *seq,
704 GSequenceIterCompareFunc cmp_func,
708 GSequenceNode *begin, *end;
710 g_return_if_fail (seq != NULL);
711 g_return_if_fail (cmp_func != NULL);
713 check_seq_access (seq);
715 begin = g_sequence_get_begin_iter (seq);
716 end = g_sequence_get_end_iter (seq);
718 tmp = g_sequence_new (NULL);
719 tmp->real_sequence = seq;
721 g_sequence_move_range (g_sequence_get_begin_iter (tmp), begin, end);
723 seq->access_prohibited = TRUE;
724 tmp->access_prohibited = TRUE;
726 while (g_sequence_get_length (tmp) > 0)
728 GSequenceNode *node = g_sequence_get_begin_iter (tmp);
730 node_insert_sorted (seq->end_node, node, seq->end_node,
734 tmp->access_prohibited = FALSE;
735 seq->access_prohibited = FALSE;
737 g_sequence_free (tmp);
741 * g_sequence_sort_changed_iter:
742 * @iter: a #GSequenceIter
743 * @iter_cmp: the #GSequenceItercompare used to compare iterators in the
744 * sequence. It is called with two iterators pointing into @seq. It should
745 * return 0 if the iterators are equal, a negative value if the first
746 * iterator comes before the second, and a positive value if the second
747 * iterator comes before the first.
748 * @cmp_data: user data passed to @cmp_func
750 * Like g_sequence_sort_changed(), but uses
751 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
752 * the compare function.
757 g_sequence_sort_changed_iter (GSequenceIter *iter,
758 GSequenceIterCompareFunc iter_cmp,
761 GSequence *seq, *tmp_seq;
762 GSequenceIter *next, *prev;
764 g_return_if_fail (iter != NULL);
765 g_return_if_fail (!is_end (iter));
766 g_return_if_fail (iter_cmp != NULL);
767 check_iter_access (iter);
769 /* If one of the neighbours is equal to iter, then
770 * don't move it. This ensures that sort_changed() is
771 * a stable operation.
774 next = node_get_next (iter);
775 prev = node_get_prev (iter);
777 if (prev != iter && iter_cmp (prev, iter, cmp_data) == 0)
780 if (!is_end (next) && iter_cmp (next, iter, cmp_data) == 0)
783 seq = get_sequence (iter);
785 seq->access_prohibited = TRUE;
787 tmp_seq = g_sequence_new (NULL);
788 tmp_seq->real_sequence = seq;
791 node_insert_before (tmp_seq->end_node, iter);
793 node_insert_sorted (seq->end_node, iter, seq->end_node,
796 g_sequence_free (tmp_seq);
798 seq->access_prohibited = FALSE;
802 * g_sequence_insert_sorted_iter:
804 * @data: data for the new item
805 * @iter_cmp: the #GSequenceItercompare used to compare iterators in the
806 * sequence. It is called with two iterators pointing into @seq. It should
807 * return 0 if the iterators are equal, a negative value if the first
808 * iterator comes before the second, and a positive value if the second
809 * iterator comes before the first.
810 * @cmp_data: user data passed to @cmp_func
812 * Like g_sequence_insert_sorted(), but uses
813 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
814 * the compare function.
816 * Return value: a #GSequenceIter pointing to the new item
821 g_sequence_insert_sorted_iter (GSequence *seq,
823 GSequenceIterCompareFunc iter_cmp,
826 GSequenceNode *new_node;
829 g_return_val_if_fail (seq != NULL, NULL);
830 g_return_val_if_fail (iter_cmp != NULL, NULL);
832 check_seq_access (seq);
834 seq->access_prohibited = TRUE;
836 /* Create a new temporary sequence and put the new node into
837 * that. The reason for this is that the user compare function
838 * will be called with the new node, and if it dereferences,
839 * "is_end" will be called on it. But that will crash if the
840 * node is not actually in a sequence.
842 * node_insert_sorted() makes sure the node is unlinked before
845 * The reason we need the "iter" versions at all is that that
846 * is the only kind of compare functions GtkTreeView can use.
848 tmp_seq = g_sequence_new (NULL);
849 tmp_seq->real_sequence = seq;
851 new_node = g_sequence_append (tmp_seq, data);
853 node_insert_sorted (seq->end_node, new_node,
854 seq->end_node, iter_cmp, cmp_data);
856 g_sequence_free (tmp_seq);
858 seq->access_prohibited = FALSE;
864 * g_sequence_search_iter:
866 * @data: data for the new item
867 * @iter_cmp: the #GSequenceIterCompare function used to compare iterators
868 * in the sequence. It is called with two iterators pointing into @seq.
869 * It should return 0 if the iterators are equal, a negative value if the
870 * first iterator comes before the second, and a positive value if the
871 * second iterator comes before the first.
872 * @cmp_data: user data passed to @iter_cmp
874 * Like g_sequence_search(), but uses
875 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
876 * the compare function.
878 * Return value: a #GSequenceIter pointing to the position in @seq
879 * where @data would have been inserted according to @iter_cmp and @cmp_data.
884 g_sequence_search_iter (GSequence *seq,
886 GSequenceIterCompareFunc iter_cmp,
890 GSequenceNode *dummy;
893 g_return_val_if_fail (seq != NULL, NULL);
895 check_seq_access (seq);
897 seq->access_prohibited = TRUE;
899 /* Create a new temporary sequence and put the dummy node into
900 * that. The reason for this is that the user compare function
901 * will be called with the new node, and if it dereferences,
902 * "is_end" will be called on it. But that will crash if the
903 * node is not actually in a sequence.
905 * node_insert_sorted() makes sure the node is unlinked before
908 * The reason we need the "iter" versions at all is that that
909 * is the only kind of compare functions GtkTreeView can use.
911 tmp_seq = g_sequence_new (NULL);
912 tmp_seq->real_sequence = seq;
914 dummy = g_sequence_append (tmp_seq, data);
916 node = node_find_closest (seq->end_node, dummy,
917 seq->end_node, iter_cmp, cmp_data);
919 g_sequence_free (tmp_seq);
921 seq->access_prohibited = FALSE;
927 * g_sequence_iter_get_sequence:
928 * @iter: a #GSequenceIter
930 * Returns the #GSequence that @iter points into.
932 * Return value: the #GSequence that @iter points into.
937 g_sequence_iter_get_sequence (GSequenceIter *iter)
941 g_return_val_if_fail (iter != NULL, NULL);
943 seq = get_sequence (iter);
945 /* For temporary sequences, this points to the sequence that
946 * is actually being manipulated
948 return seq->real_sequence;
953 * @iter: a #GSequenceIter
955 * Returns the data that @iter points to.
957 * Return value: the data that @iter points to
962 g_sequence_get (GSequenceIter *iter)
964 g_return_val_if_fail (iter != NULL, NULL);
965 g_return_val_if_fail (!is_end (iter), NULL);
972 * @iter: a #GSequenceIter
973 * @data: new data for the item
975 * Changes the data for the item pointed to by @iter to be @data. If
976 * the sequence has a data destroy function associated with it, that
977 * function is called on the existing data that @iter pointed to.
982 g_sequence_set (GSequenceIter *iter,
987 g_return_if_fail (iter != NULL);
988 g_return_if_fail (!is_end (iter));
990 seq = get_sequence (iter);
992 /* If @data is identical to iter->data, it is destroyed
993 * here. This will work right in case of ref-counted objects. Also
994 * it is similar to what ghashtables do.
996 * For non-refcounted data it's a little less convenient, but
997 * code relying on self-setting not destroying would be
998 * pretty dubious anyway ...
1001 if (seq->data_destroy_notify)
1002 seq->data_destroy_notify (iter->data);
1008 * g_sequence_get_length:
1009 * @seq: a #GSequence
1011 * Returns the length of @seq
1013 * Return value: the length of @seq
1018 g_sequence_get_length (GSequence *seq)
1020 return node_get_length (seq->end_node) - 1;
1024 * g_sequence_get_end_iter:
1025 * @seq: a #GSequence
1027 * Returns the end iterator for @seg
1029 * Return value: the end iterator for @seq
1034 g_sequence_get_end_iter (GSequence *seq)
1036 g_return_val_if_fail (seq != NULL, NULL);
1038 g_assert (is_end (seq->end_node));
1040 return seq->end_node;
1044 * g_sequence_get_begin_iter:
1045 * @seq: a #GSequence
1047 * Returns the begin iterator for @seq.
1049 * Return value: the begin iterator for @seq.
1054 g_sequence_get_begin_iter (GSequence *seq)
1056 g_return_val_if_fail (seq != NULL, NULL);
1057 return node_get_first (seq->end_node);
1061 clamp_position (GSequence *seq,
1064 gint len = g_sequence_get_length (seq);
1066 if (pos > len || pos < 0)
1073 * if pos > number of items or -1, will return end pointer
1076 * g_sequence_get_iter_at_pos:
1077 * @seq: a #GSequence
1078 * @pos: a position in @seq, or -1 for the end.
1080 * Returns the iterator at position @pos. If @pos is negative or larger
1081 * than the number of items in @seq, the end iterator is returned.
1083 * Return value: The #GSequenceIter at position @pos
1088 g_sequence_get_iter_at_pos (GSequence *seq,
1091 g_return_val_if_fail (seq != NULL, NULL);
1093 pos = clamp_position (seq, pos);
1095 return node_get_by_pos (seq->end_node, pos);
1100 * @src: a #GSequenceIter pointing to the item to move
1101 * @dest: a #GSequenceIter pointing to the position to which
1102 * the item is moved.
1104 * Moves the item pointed to by @src to the position indicated by @dest.
1105 * After calling this function @dest will point to the position immediately
1111 g_sequence_move (GSequenceIter *src,
1112 GSequenceIter *dest)
1114 g_return_if_fail (src != NULL);
1115 g_return_if_fail (dest != NULL);
1116 g_return_if_fail (!is_end (src));
1122 node_insert_before (dest, src);
1128 * g_sequence_iter_is_end:
1129 * @iter: a #GSequenceIter
1131 * Returns whether @iter is the end iterator
1133 * Return value: Whether @iter is the end iterator.
1138 g_sequence_iter_is_end (GSequenceIter *iter)
1140 g_return_val_if_fail (iter != NULL, FALSE);
1142 return is_end (iter);
1146 * g_sequence_iter_is_begin:
1147 * @iter: a #GSequenceIter
1149 * Returns whether @iter is the begin iterator
1151 * Return value: whether @iter is the begin iterator
1156 g_sequence_iter_is_begin (GSequenceIter *iter)
1158 g_return_val_if_fail (iter != NULL, FALSE);
1160 return (node_get_prev (iter) == iter);
1164 * g_sequence_iter_get_position:
1165 * @iter: a #GSequenceIter
1167 * Returns the position of @iter
1169 * Return value: the position of @iter
1174 g_sequence_iter_get_position (GSequenceIter *iter)
1176 g_return_val_if_fail (iter != NULL, -1);
1178 return node_get_pos (iter);
1182 * g_sequence_iter_next:
1183 * @iter: a #GSequenceIter
1185 * Returns an iterator pointing to the next position after @iter. If
1186 * @iter is the end iterator, the end iterator is returned.
1188 * Return value: a #GSequenceIter pointing to the next position after @iter.
1193 g_sequence_iter_next (GSequenceIter *iter)
1195 g_return_val_if_fail (iter != NULL, NULL);
1197 return node_get_next (iter);
1201 * g_sequence_iter_prev:
1202 * @iter: a #GSequenceIter
1204 * Returns an iterator pointing to the previous position before @iter. If
1205 * @iter is the begin iterator, the begin iterator is returned.
1207 * Return value: a #GSequenceIter pointing to the previous position before
1213 g_sequence_iter_prev (GSequenceIter *iter)
1215 g_return_val_if_fail (iter != NULL, NULL);
1217 return node_get_prev (iter);
1221 * g_sequence_iter_move:
1222 * @iter: a #GSequenceIter
1223 * @delta: A positive or negative number indicating how many positions away
1224 * from @iter the returned #GSequenceIter will be.
1226 * Returns the #GSequenceIter which is @delta positions away from @iter.
1227 * If @iter is closer than -@delta positions to the beginning of the sequence,
1228 * the begin iterator is returned. If @iter is closer than @delta positions
1229 * to the end of the sequence, the end iterator is returned.
1231 * Return value: a #GSequenceIter which is @delta positions away from @iter.
1236 g_sequence_iter_move (GSequenceIter *iter,
1241 g_return_val_if_fail (iter != NULL, NULL);
1243 new_pos = node_get_pos (iter) + delta;
1245 new_pos = clamp_position (get_sequence (iter), new_pos);
1247 return node_get_by_pos (iter, new_pos);
1252 * @a: a #GSequenceIter
1253 * @b: a #GSequenceIter
1255 * Swaps the items pointed to by @a and @b
1260 g_sequence_swap (GSequenceIter *a,
1263 GSequenceNode *leftmost, *rightmost, *rightmost_next;
1266 g_return_if_fail (!g_sequence_iter_is_end (a));
1267 g_return_if_fail (!g_sequence_iter_is_end (b));
1272 a_pos = g_sequence_iter_get_position (a);
1273 b_pos = g_sequence_iter_get_position (b);
1286 rightmost_next = node_get_next (rightmost);
1288 /* The situation is now like this:
1290 * ..., leftmost, ......., rightmost, rightmost_next, ...
1293 g_sequence_move (rightmost, leftmost);
1294 g_sequence_move (leftmost, rightmost_next);
1298 * Implementation of the splay tree.
1301 /* Splay Tree vs. Other Kinds of Trees
1303 * There are both advantages and disadvantages to using a splay tree vs. some other
1304 * kind of tree such as a red/black tree or a btree.
1306 * Advantages of splay trees
1308 * - They are very simple to implement, especially things like move_range() or concatenate()
1309 * are very easy to do for splay trees. The algorithm to split a red/black tree, while still,
1310 * O(log n) is much more involved.
1312 * - If we add aggregates at one point, splay trees make it really easy to compute the aggregate
1313 * for an arbitrary range of the tree. In a red/black tree you would have to pick out the correct
1314 * subtrees, then call out to the aggregator function to compute them.
1315 * On the other hand, for a splay tree, aggregates would be invalidated on lookups, so you
1316 * would call the aggregator much more often. In both cases, the aggregator function would be
1317 * called O(log n) times as a side-effect of asking for the aggregate of a range.
1319 * - If you are only using the list API and never the insert_sorted(), the operations on a
1320 * splay tree will actually be O(1) rather than O(log n). But this is most likely one
1321 * for the "who cares" department, since the O(log n) of a red/black tree really is quite
1322 * fast and if what you need is a queue you can just use GQueue.
1326 * - Splay trees are only amortized O(log n) which means individual operations could take a long
1327 * time, which is undesirable in GUI applications
1329 * - Red/black trees are mode widely known since they are tought in CS101 courses.
1331 * - Red/black trees or btrees are more efficient. In particular, splay trees write to the
1332 * nodes on lookup, which causes dirty pages that the VM system will have to launder.
1334 * - Splay trees are not necessarily balanced at all which means straight-forward recursive
1335 * algorithms can use lots of stack.
1337 * It is likely worth investigating whether a BTree would be a better choice, in particular the
1338 * algorithm to split a BTree may not be all that complicated given that split/join for nodes
1339 * will have to be implemented anyway.
1344 node_update_fields (GSequenceNode *node)
1346 g_assert (node != NULL);
1351 node->n_nodes += node->left->n_nodes;
1354 node->n_nodes += node->right->n_nodes;
1357 #define NODE_LEFT_CHILD(n) (((n)->parent) && ((n)->parent->left) == (n))
1358 #define NODE_RIGHT_CHILD(n) (((n)->parent) && ((n)->parent->right) == (n))
1361 node_rotate (GSequenceNode *node)
1363 GSequenceNode *tmp, *old;
1365 g_assert (node->parent);
1366 g_assert (node->parent != node);
1368 if (NODE_LEFT_CHILD (node))
1373 node->right = node->parent;
1374 node->parent = node->parent->parent;
1377 if (node->parent->left == node->right)
1378 node->parent->left = node;
1380 node->parent->right = node;
1383 g_assert (node->right);
1385 node->right->parent = node;
1386 node->right->left = tmp;
1388 if (node->right->left)
1389 node->right->left->parent = node->right;
1398 node->left = node->parent;
1399 node->parent = node->parent->parent;
1402 if (node->parent->right == node->left)
1403 node->parent->right = node;
1405 node->parent->left = node;
1408 g_assert (node->left);
1410 node->left->parent = node;
1411 node->left->right = tmp;
1413 if (node->left->right)
1414 node->left->right->parent = node->left;
1419 node_update_fields (old);
1420 node_update_fields (node);
1423 static GSequenceNode *
1424 splay (GSequenceNode *node)
1426 while (node->parent)
1428 if (!node->parent->parent)
1433 else if ((NODE_LEFT_CHILD (node) && NODE_LEFT_CHILD (node->parent)) ||
1434 (NODE_RIGHT_CHILD (node) && NODE_RIGHT_CHILD (node->parent)))
1437 node_rotate (node->parent);
1451 static GSequenceNode *
1452 node_new (gpointer data)
1454 GSequenceNode *node = g_slice_new0 (GSequenceNode);
1456 node->parent = NULL;
1457 node->parent = NULL;
1467 static GSequenceNode *
1468 find_min (GSequenceNode *node)
1478 static GSequenceNode *
1479 find_max (GSequenceNode *node)
1489 static GSequenceNode *
1490 node_get_first (GSequenceNode *node)
1492 return splay (find_min (node));
1495 static GSequenceNode *
1496 node_get_last (GSequenceNode *node)
1498 return splay (find_max (node));
1502 get_n_nodes (GSequenceNode *node)
1505 return node->n_nodes;
1510 static GSequenceNode *
1511 node_get_by_pos (GSequenceNode *node,
1516 g_assert (node != NULL);
1520 while ((i = get_n_nodes (node->left)) != pos)
1530 g_assert (node->parent != NULL);
1534 return splay (node);
1537 static GSequenceNode *
1538 node_get_prev (GSequenceNode *node)
1549 return splay (node);
1552 static GSequenceNode *
1553 node_get_next (GSequenceNode *node)
1564 return splay (node);
1568 node_get_pos (GSequenceNode *node)
1572 return get_n_nodes (node->left);
1575 /* Return closest node _strictly_ bigger than @needle. This node
1576 * always exists because the tree has an explicit end node).
1577 * This end node of @haystack must be passed in @end.
1579 static GSequenceNode *
1580 node_find_closest (GSequenceNode *haystack,
1581 GSequenceNode *needle,
1583 GSequenceIterCompareFunc iter_cmp,
1586 GSequenceNode *best;
1589 g_assert (haystack);
1591 haystack = splay (haystack);
1597 /* iter_cmp can't be passed the end node, since the function may
1600 if (haystack == end)
1603 c = iter_cmp (haystack, needle, cmp_data);
1605 /* In the following we don't break even if c == 0. Instaed we go on
1606 * searching along the 'bigger' nodes, so that we find the last one
1607 * that is equal to the needle.
1610 haystack = haystack->left;
1612 haystack = haystack->right;
1614 while (haystack != NULL);
1616 /* If the best node is smaller or equal to the data, then move one step
1617 * to the right to make sure the best one is strictly bigger than the data
1619 if (best != end && c <= 0)
1620 best = node_get_next (best);
1626 node_free (GSequenceNode *node,
1629 GPtrArray *stack = g_ptr_array_new ();
1633 g_ptr_array_add (stack, node);
1635 while (stack->len > 0)
1637 node = g_ptr_array_remove_index (stack, stack->len - 1);
1641 g_ptr_array_add (stack, node->right);
1642 g_ptr_array_add (stack, node->left);
1644 if (seq && seq->data_destroy_notify && node != seq->end_node)
1645 seq->data_destroy_notify (node->data);
1647 g_slice_free (GSequenceNode, node);
1651 g_ptr_array_free (stack, TRUE);
1654 /* Splits into two trees. @node will be part of the right tree
1657 node_cut (GSequenceNode *node)
1661 g_assert (node->parent == NULL);
1664 node->left->parent = NULL;
1667 node_update_fields (node);
1671 node_insert_before (GSequenceNode *node,
1674 g_assert (node != NULL);
1675 g_assert (new != NULL);
1679 new = splay (find_min (new));
1680 g_assert (new->left == NULL);
1683 node->left->parent = new;
1685 new->left = node->left;
1690 node_update_fields (new);
1691 node_update_fields (node);
1695 node_insert_after (GSequenceNode *node,
1698 g_assert (node != NULL);
1699 g_assert (new != NULL);
1703 new = splay (find_max (new));
1704 g_assert (new->right == NULL);
1705 g_assert (node->parent == NULL);
1708 node->right->parent = new;
1710 new->right = node->right;
1715 node_update_fields (new);
1716 node_update_fields (node);
1720 node_get_length (GSequenceNode *node)
1722 g_assert (node != NULL);
1725 return node->n_nodes;
1729 node_unlink (GSequenceNode *node)
1731 GSequenceNode *right, *left;
1736 right = node->right;
1738 node->parent = node->left = node->right = NULL;
1739 node_update_fields (node);
1743 right->parent = NULL;
1745 right = node_get_first (right);
1746 g_assert (right->left == NULL);
1751 left->parent = right;
1752 node_update_fields (right);
1757 left->parent = NULL;
1762 node_insert_sorted (GSequenceNode *node,
1765 GSequenceIterCompareFunc iter_cmp,
1768 GSequenceNode *closest;
1770 closest = node_find_closest (node, new, end, iter_cmp, cmp_data);
1774 node_insert_before (closest, new);
1778 node_calc_height (GSequenceNode *node)
1789 left_height = node_calc_height (node->left);
1792 right_height = node_calc_height (node->right);
1794 return MAX (left_height, right_height) + 1;
1800 /* Self-test function */
1802 check_node (GSequenceNode *node)
1806 g_assert (node->parent != node);
1807 g_assert (node->n_nodes ==
1808 1 + get_n_nodes (node->left) + get_n_nodes (node->right));
1809 check_node (node->left);
1810 check_node (node->right);
1815 g_sequence_self_test_internal_to_glib_dont_use (GSequence *seq)
1817 GSequenceNode *node = splay (seq->end_node);
1822 #define __G_SEQUENCE_C__
1823 #include "galiasdef.c"