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)
122 if (iter->parent->right != iter)
125 seq = get_sequence (iter);
127 return seq->end_node == iter;
132 GCompareDataFunc cmp_func;
134 GSequenceNode *end_node;
137 /* This function compares two iters using a normal compare
138 * function and user_data passed in in a SortInfo struct
141 iter_compare (GSequenceIter *node1,
142 GSequenceIter *node2,
145 const SortInfo *info = data;
148 if (node1 == info->end_node)
151 if (node2 == info->end_node)
154 retval = info->cmp_func (node1->data, node2->data, info->cmp_data);
165 * @data_destroy: a #GDestroyNotify function, or %NULL
167 * Creates a new GSequence. The @data_destroy function, if non-%NULL will
168 * be called on all items when the sequence is destroyed and on items that
169 * are removed from the sequence.
171 * Return value: a new #GSequence
176 g_sequence_new (GDestroyNotify data_destroy)
178 GSequence *seq = g_new (GSequence, 1);
179 seq->data_destroy_notify = data_destroy;
181 seq->end_node = node_new (seq);
183 seq->access_prohibited = FALSE;
185 seq->real_sequence = seq;
194 * Frees the memory allocated for @seq. If @seq has a data destroy
195 * function associated with it, that function is called on all items in
201 g_sequence_free (GSequence *seq)
203 g_return_if_fail (seq != NULL);
205 check_seq_access (seq);
207 node_free (seq->end_node, seq);
213 * g_sequence_foreach_range:
214 * @begin: a #GSequenceIter
215 * @end: a #GSequenceIter
217 * @user_data: user data passed to @func
219 * Calls @func for each item in the range (@begin, @end) passing
220 * @user_data to the function.
225 g_sequence_foreach_range (GSequenceIter *begin,
233 g_return_if_fail (func != NULL);
234 g_return_if_fail (begin != NULL);
235 g_return_if_fail (end != NULL);
237 seq = get_sequence (begin);
239 seq->access_prohibited = TRUE;
244 GSequenceIter *next = node_get_next (iter);
246 func (iter->data, user_data);
251 seq->access_prohibited = FALSE;
255 * g_sequence_foreach:
257 * @func: the function to call for each item in @seq
258 * @user_data: user data passed to @func
260 * Calls @func for each item in the sequence passing @user_data
266 g_sequence_foreach (GSequence *seq,
270 GSequenceIter *begin, *end;
272 check_seq_access (seq);
274 begin = g_sequence_get_begin_iter (seq);
275 end = g_sequence_get_end_iter (seq);
277 g_sequence_foreach_range (begin, end, func, user_data);
281 * g_sequence_range_get_midpoint:
282 * @begin: a #GSequenceIter
283 * @end: a #GSequenceIter
285 * Finds an iterator somewhere in the range (@begin, @end). This
286 * iterator will be close to the middle of the range, but is not
287 * guaranteed to be <emphasis>exactly</emphasis> in the middle.
289 * The @begin and @end iterators must both point to the same sequence and
290 * @begin must come before or be equal to @end in the sequence.
292 * Return value: A #GSequenceIter pointing somewhere in the
293 * (@begin, @end) range.
298 g_sequence_range_get_midpoint (GSequenceIter *begin,
301 int begin_pos, end_pos, mid_pos;
303 g_return_val_if_fail (begin != NULL, NULL);
304 g_return_val_if_fail (end != NULL, NULL);
305 g_return_val_if_fail (get_sequence (begin) == get_sequence (end), NULL);
307 begin_pos = node_get_pos (begin);
308 end_pos = node_get_pos (end);
310 g_return_val_if_fail (end_pos >= begin_pos, NULL);
312 mid_pos = begin_pos + (end_pos - begin_pos) / 2;
314 return node_get_by_pos (begin, mid_pos);
318 * g_sequence_iter_compare:
319 * @a: a #GSequenceIter
320 * @b: a #GSequenceIter
322 * Returns a negative number if @a comes before @b, 0 if they are equal,
323 * and a positive number if @a comes after @b.
325 * The @a and @b iterators must point into the same sequence.
327 * Return value: A negative number if @a comes before @b, 0 if they are
328 * equal, and a positive number if @a comes after @b.
333 g_sequence_iter_compare (GSequenceIter *a,
338 g_return_val_if_fail (a != NULL, 0);
339 g_return_val_if_fail (b != NULL, 0);
340 g_return_val_if_fail (get_sequence (a) == get_sequence (b), 0);
342 check_iter_access (a);
343 check_iter_access (b);
345 a_pos = node_get_pos (a);
346 b_pos = node_get_pos (b);
350 else if (a_pos > b_pos)
358 * @seq: a #GSequencePointer
359 * @data: the data for the new item
361 * Adds a new item to the end of @seq.
363 * Return value: an iterator pointing to the new item
368 g_sequence_append (GSequence *seq,
373 g_return_val_if_fail (seq != NULL, NULL);
375 check_seq_access (seq);
377 node = node_new (data);
378 node_insert_before (seq->end_node, node);
384 * g_sequence_prepend:
386 * @data: the data for the new item
388 * Adds a new item to the front of @seq
390 * Return value: an iterator pointing to the new item
395 g_sequence_prepend (GSequence *seq,
398 GSequenceNode *node, *first;
400 g_return_val_if_fail (seq != NULL, NULL);
402 check_seq_access (seq);
404 node = node_new (data);
405 first = node_get_first (seq->end_node);
407 node_insert_before (first, node);
413 * g_sequence_insert_before:
414 * @iter: a #GSequenceIter
415 * @data: the data for the new item
417 * Inserts a new item just before the item pointed to by @iter.
419 * Return value: an iterator pointing to the new item
424 g_sequence_insert_before (GSequenceIter *iter,
429 g_return_val_if_fail (iter != NULL, NULL);
431 check_iter_access (iter);
433 node = node_new (data);
435 node_insert_before (iter, node);
442 * @iter: a #GSequenceIter
444 * Removes the item pointed to by @iter. It is an error to pass the
445 * end iterator to this function.
447 * If the sequnce has a data destroy function associated with it, this
448 * function is called on the data for the removed item.
453 g_sequence_remove (GSequenceIter *iter)
457 g_return_if_fail (iter != NULL);
458 g_return_if_fail (!is_end (iter));
460 check_iter_access (iter);
462 seq = get_sequence (iter);
465 node_free (iter, seq);
469 * g_sequence_remove_range:
470 * @begin: a #GSequenceIter
471 * @end: a #GSequenceIter
473 * Removes all items in the (@begin, @end) range.
475 * If the sequence has a data destroy function associated with it, this
476 * function is called on the data for the removed items.
481 g_sequence_remove_range (GSequenceIter *begin,
484 g_return_if_fail (get_sequence (begin) == get_sequence (end));
486 check_iter_access (begin);
487 check_iter_access (end);
489 g_sequence_move_range (NULL, begin, end);
493 * g_sequence_move_range:
494 * @dest: a #GSequenceIter
495 * @begin: a #GSequenceIter
496 * @end: a #GSequenceIter
498 * Inserts the (@begin, @end) range at the destination pointed to by ptr.
499 * The @begin and @end iters must point into the same sequence. It is
500 * allowed for @dest to point to a different sequence than the one pointed
501 * into by @begin and @end.
503 * If @dest is NULL, the range indicated by @begin and @end is
504 * removed from the sequence. If @dest iter points to a place within
505 * the (@begin, @end) range, the range does not move.
510 g_sequence_move_range (GSequenceIter *dest,
511 GSequenceIter *begin,
515 GSequenceNode *first;
517 g_return_if_fail (begin != NULL);
518 g_return_if_fail (end != NULL);
520 check_iter_access (begin);
521 check_iter_access (end);
523 check_iter_access (dest);
525 src_seq = get_sequence (begin);
527 g_return_if_fail (src_seq == get_sequence (end));
529 /* Dest points to begin or end? */
530 if (dest == begin || dest == end)
533 /* begin comes after end? */
534 if (g_sequence_iter_compare (begin, end) >= 0)
537 /* dest points somewhere in the (begin, end) range? */
538 if (dest && get_sequence (dest) == src_seq &&
539 g_sequence_iter_compare (dest, begin) > 0 &&
540 g_sequence_iter_compare (dest, end) < 0)
545 src_seq = get_sequence (begin);
547 first = node_get_first (begin);
554 node_insert_after (node_get_last (first), end);
557 node_insert_before (dest, begin);
559 node_free (begin, src_seq);
565 * @cmp_func: the #GCompareDataFunc used to sort @seq. This function is
566 * passed two items of @seq and should return 0 if they are equal,
567 * a negative value fi the first comes before the second, and a
568 * positive value if the second comes before the first.
569 * @cmp_data: user data passed to @cmp_func
571 * Sorts @seq using @cmp_func.
576 g_sequence_sort (GSequence *seq,
577 GCompareDataFunc cmp_func,
580 SortInfo info = { cmp_func, cmp_data, seq->end_node };
582 check_seq_access (seq);
584 g_sequence_sort_iter (seq, iter_compare, &info);
588 * g_sequence_insert_sorted:
590 * @data: the data to insert
591 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
592 * is called with two items of the @seq and @user_data. It should
593 * return 0 if the items are equal, a negative value if the first
594 * item comes before the second, and a positive value if the second
595 * item comes before the first.
596 * @cmp_data: user data passed to @cmp_func.
598 * Inserts @data into @sequence using @func to determine the new position.
599 * The sequence must already be sorted according to @cmp_func; otherwise the
600 * new position of @data is undefined.
602 * Return value: a #GSequenceIter pointing to the new item.
607 g_sequence_insert_sorted (GSequence *seq,
609 GCompareDataFunc cmp_func,
612 SortInfo info = { cmp_func, cmp_data, NULL };
614 g_return_val_if_fail (seq != NULL, NULL);
615 g_return_val_if_fail (cmp_func != NULL, NULL);
617 info.end_node = seq->end_node;
618 check_seq_access (seq);
620 return g_sequence_insert_sorted_iter (seq, data, iter_compare, &info);
624 * g_sequence_sort_changed:
625 * @iter: A #GSequenceIter
626 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
627 * is called with two items of the @seq and @user_data. It should
628 * return 0 if the items are equal, a negative value if the first
629 * item comes before the second, and a positive value if the second
630 * item comes before the first.
631 * @cmp_data: user data passed to @cmp_func.
633 * Moves the data pointed to a new position as indicated by @cmp_func. This
634 * function should be called for items in a sequence already sorted according
635 * to @cmp_func whenever some aspect of an item changes so that @cmp_func
636 * may return different values for that item.
641 g_sequence_sort_changed (GSequenceIter *iter,
642 GCompareDataFunc cmp_func,
645 SortInfo info = { cmp_func, cmp_data, NULL };
647 g_return_if_fail (!is_end (iter));
649 info.end_node = get_sequence (iter)->end_node;
650 check_iter_access (iter);
652 g_sequence_sort_changed_iter (iter, iter_compare, &info);
658 * @data: data for the new item
659 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
660 * is called with two items of the @seq and @user_data. It should
661 * return 0 if the items are equal, a negative value if the first
662 * item comes before the second, and a positive value if the second
663 * item comes before the first.
664 * @cmp_data: user data passed to @cmp_func.
666 * Returns an iterator pointing to the position where @data would
667 * be inserted according to @cmp_func and @cmp_data.
669 * Return value: an #GSequenceIter pointing to the position where @data
670 * would have been inserted according to @cmp_func and @cmp_data.
675 g_sequence_search (GSequence *seq,
677 GCompareDataFunc cmp_func,
680 SortInfo info = { cmp_func, cmp_data, NULL };
682 g_return_val_if_fail (seq != NULL, NULL);
684 info.end_node = seq->end_node;
685 check_seq_access (seq);
687 return g_sequence_search_iter (seq, data, iter_compare, &info);
691 * g_sequence_sort_iter:
693 * @cmp_func: the #GSequenceItercompare used to compare iterators in the
694 * sequence. It is called with two iterators pointing into @seq. It should
695 * return 0 if the iterators are equal, a negative value if the first
696 * iterator comes before the second, and a positive value if the second
697 * iterator comes before the first.
698 * @cmp_data: user data passed to @cmp_func
700 * Like g_sequence_sort(), but uses a #GSequenceIterCompareFunc instead
701 * of a GCompareDataFunc as the compare function
706 g_sequence_sort_iter (GSequence *seq,
707 GSequenceIterCompareFunc cmp_func,
711 GSequenceNode *begin, *end;
713 g_return_if_fail (seq != NULL);
714 g_return_if_fail (cmp_func != NULL);
716 check_seq_access (seq);
718 begin = g_sequence_get_begin_iter (seq);
719 end = g_sequence_get_end_iter (seq);
721 tmp = g_sequence_new (NULL);
722 tmp->real_sequence = seq;
724 g_sequence_move_range (g_sequence_get_begin_iter (tmp), begin, end);
726 seq->access_prohibited = TRUE;
727 tmp->access_prohibited = TRUE;
729 while (g_sequence_get_length (tmp) > 0)
731 GSequenceNode *node = g_sequence_get_begin_iter (tmp);
733 node_insert_sorted (seq->end_node, node, seq->end_node,
737 tmp->access_prohibited = FALSE;
738 seq->access_prohibited = FALSE;
740 g_sequence_free (tmp);
744 * g_sequence_sort_changed_iter:
745 * @iter: a #GSequenceIter
746 * @iter_cmp: the #GSequenceItercompare used to compare iterators in the
747 * sequence. It is called with two iterators pointing into @seq. It should
748 * return 0 if the iterators are equal, a negative value if the first
749 * iterator comes before the second, and a positive value if the second
750 * iterator comes before the first.
751 * @cmp_data: user data passed to @cmp_func
753 * Like g_sequence_sort_changed(), but uses
754 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
755 * the compare function.
760 g_sequence_sort_changed_iter (GSequenceIter *iter,
761 GSequenceIterCompareFunc iter_cmp,
764 GSequence *seq, *tmp_seq;
765 GSequenceIter *next, *prev;
767 g_return_if_fail (iter != NULL);
768 g_return_if_fail (!is_end (iter));
769 g_return_if_fail (iter_cmp != NULL);
770 check_iter_access (iter);
772 /* If one of the neighbours is equal to iter, then
773 * don't move it. This ensures that sort_changed() is
774 * a stable operation.
777 next = node_get_next (iter);
778 prev = node_get_prev (iter);
780 if (prev != iter && iter_cmp (prev, iter, cmp_data) == 0)
783 if (!is_end (next) && iter_cmp (next, iter, cmp_data) == 0)
786 seq = get_sequence (iter);
788 seq->access_prohibited = TRUE;
790 tmp_seq = g_sequence_new (NULL);
791 tmp_seq->real_sequence = seq;
794 node_insert_before (tmp_seq->end_node, iter);
796 node_insert_sorted (seq->end_node, iter, seq->end_node,
799 g_sequence_free (tmp_seq);
801 seq->access_prohibited = FALSE;
805 * g_sequence_insert_sorted_iter:
807 * @data: data for the new item
808 * @iter_cmp: the #GSequenceItercompare used to compare iterators in the
809 * sequence. It is called with two iterators pointing into @seq. It should
810 * return 0 if the iterators are equal, a negative value if the first
811 * iterator comes before the second, and a positive value if the second
812 * iterator comes before the first.
813 * @cmp_data: user data passed to @cmp_func
815 * Like g_sequence_insert_sorted(), but uses
816 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
817 * the compare function.
819 * Return value: a #GSequenceIter pointing to the new item
824 g_sequence_insert_sorted_iter (GSequence *seq,
826 GSequenceIterCompareFunc iter_cmp,
829 GSequenceNode *new_node;
832 g_return_val_if_fail (seq != NULL, NULL);
833 g_return_val_if_fail (iter_cmp != NULL, NULL);
835 check_seq_access (seq);
837 seq->access_prohibited = TRUE;
839 /* Create a new temporary sequence and put the new node into
840 * that. The reason for this is that the user compare function
841 * will be called with the new node, and if it dereferences,
842 * "is_end" will be called on it. But that will crash if the
843 * node is not actually in a sequence.
845 * node_insert_sorted() makes sure the node is unlinked before
848 * The reason we need the "iter" versions at all is that that
849 * is the only kind of compare functions GtkTreeView can use.
851 tmp_seq = g_sequence_new (NULL);
852 tmp_seq->real_sequence = seq;
854 new_node = g_sequence_append (tmp_seq, data);
856 node_insert_sorted (seq->end_node, new_node,
857 seq->end_node, iter_cmp, cmp_data);
859 g_sequence_free (tmp_seq);
861 seq->access_prohibited = FALSE;
867 * g_sequence_search_iter:
869 * @data: data for the new item
870 * @iter_cmp: the #GSequenceIterCompare function used to compare iterators
871 * in the sequence. It is called with two iterators pointing into @seq.
872 * It should return 0 if the iterators are equal, a negative value if the
873 * first iterator comes before the second, and a positive value if the
874 * second iterator comes before the first.
875 * @cmp_data: user data passed to @iter_cmp
877 * Like g_sequence_search(), but uses
878 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
879 * the compare function.
881 * Return value: a #GSequenceIter pointing to the position in @seq
882 * where @data would have been inserted according to @iter_cmp and @cmp_data.
887 g_sequence_search_iter (GSequence *seq,
889 GSequenceIterCompareFunc iter_cmp,
893 GSequenceNode *dummy;
896 g_return_val_if_fail (seq != NULL, NULL);
898 check_seq_access (seq);
900 seq->access_prohibited = TRUE;
902 /* Create a new temporary sequence and put the dummy node into
903 * that. The reason for this is that the user compare function
904 * will be called with the new node, and if it dereferences,
905 * "is_end" will be called on it. But that will crash if the
906 * node is not actually in a sequence.
908 * node_insert_sorted() makes sure the node is unlinked before
911 * The reason we need the "iter" versions at all is that that
912 * is the only kind of compare functions GtkTreeView can use.
914 tmp_seq = g_sequence_new (NULL);
915 tmp_seq->real_sequence = seq;
917 dummy = g_sequence_append (tmp_seq, data);
919 node = node_find_closest (seq->end_node, dummy,
920 seq->end_node, iter_cmp, cmp_data);
922 g_sequence_free (tmp_seq);
924 seq->access_prohibited = FALSE;
930 * g_sequence_iter_get_sequence:
931 * @iter: a #GSequenceIter
933 * Returns the #GSequence that @iter points into.
935 * Return value: the #GSequence that @iter points into.
940 g_sequence_iter_get_sequence (GSequenceIter *iter)
944 g_return_val_if_fail (iter != NULL, NULL);
946 seq = get_sequence (iter);
948 /* For temporary sequences, this points to the sequence that
949 * is actually being manipulated
951 return seq->real_sequence;
956 * @iter: a #GSequenceIter
958 * Returns the data that @iter points to.
960 * Return value: the data that @iter points to
965 g_sequence_get (GSequenceIter *iter)
967 g_return_val_if_fail (iter != NULL, NULL);
968 g_return_val_if_fail (!is_end (iter), NULL);
975 * @iter: a #GSequenceIter
976 * @data: new data for the item
978 * Changes the data for the item pointed to by @iter to be @data. If
979 * the sequence has a data destroy function associated with it, that
980 * function is called on the existing data that @iter pointed to.
985 g_sequence_set (GSequenceIter *iter,
990 g_return_if_fail (iter != NULL);
991 g_return_if_fail (!is_end (iter));
993 seq = get_sequence (iter);
995 /* If @data is identical to iter->data, it is destroyed
996 * here. This will work right in case of ref-counted objects. Also
997 * it is similar to what ghashtables do.
999 * For non-refcounted data it's a little less convenient, but
1000 * code relying on self-setting not destroying would be
1001 * pretty dubious anyway ...
1004 if (seq->data_destroy_notify)
1005 seq->data_destroy_notify (iter->data);
1011 * g_sequence_get_length:
1012 * @seq: a #GSequence
1014 * Returns the length of @seq
1016 * Return value: the length of @seq
1021 g_sequence_get_length (GSequence *seq)
1023 return node_get_length (seq->end_node) - 1;
1027 * g_sequence_get_end_iter:
1028 * @seq: a #GSequence
1030 * Returns the end iterator for @seg
1032 * Return value: the end iterator for @seq
1037 g_sequence_get_end_iter (GSequence *seq)
1039 g_return_val_if_fail (seq != NULL, NULL);
1041 return seq->end_node;
1045 * g_sequence_get_begin_iter:
1046 * @seq: a #GSequence
1048 * Returns the begin iterator for @seq.
1050 * Return value: the begin iterator for @seq.
1055 g_sequence_get_begin_iter (GSequence *seq)
1057 g_return_val_if_fail (seq != NULL, NULL);
1058 return node_get_first (seq->end_node);
1062 clamp_position (GSequence *seq,
1065 gint len = g_sequence_get_length (seq);
1067 if (pos > len || pos < 0)
1074 * if pos > number of items or -1, will return end pointer
1077 * g_sequence_get_iter_at_pos:
1078 * @seq: a #GSequence
1079 * @pos: a position in @seq, or -1 for the end.
1081 * Returns the iterator at position @pos. If @pos is negative or larger
1082 * than the number of items in @seq, the end iterator is returned.
1084 * Return value: The #GSequenceIter at position @pos
1089 g_sequence_get_iter_at_pos (GSequence *seq,
1092 g_return_val_if_fail (seq != NULL, NULL);
1094 pos = clamp_position (seq, pos);
1096 return node_get_by_pos (seq->end_node, pos);
1101 * @src: a #GSequenceIter pointing to the item to move
1102 * @dest: a #GSequenceIter pointing to the position to which
1103 * the item is moved.
1105 * Moves the item pointed to by @src to the position indicated by @dest.
1106 * After calling this function @dest will point to the position immediately
1112 g_sequence_move (GSequenceIter *src,
1113 GSequenceIter *dest)
1115 g_return_if_fail (src != NULL);
1116 g_return_if_fail (dest != NULL);
1117 g_return_if_fail (!is_end (src));
1123 node_insert_before (dest, src);
1129 * g_sequence_iter_is_end:
1130 * @iter: a #GSequenceIter
1132 * Returns whether @iter is the end iterator
1134 * Return value: Whether @iter is the end iterator.
1139 g_sequence_iter_is_end (GSequenceIter *iter)
1141 g_return_val_if_fail (iter != NULL, FALSE);
1143 return is_end (iter);
1147 * g_sequence_iter_is_begin:
1148 * @iter: a #GSequenceIter
1150 * Returns whether @iter is the begin iterator
1152 * Return value: whether @iter is the begin iterator
1157 g_sequence_iter_is_begin (GSequenceIter *iter)
1159 g_return_val_if_fail (iter != NULL, FALSE);
1161 return (node_get_prev (iter) == iter);
1165 * g_sequence_iter_get_position:
1166 * @iter: a #GSequenceIter
1168 * Returns the position of @iter
1170 * Return value: the position of @iter
1175 g_sequence_iter_get_position (GSequenceIter *iter)
1177 g_return_val_if_fail (iter != NULL, -1);
1179 return node_get_pos (iter);
1183 * g_sequence_iter_next:
1184 * @iter: a #GSequenceIter
1186 * Returns an iterator pointing to the next position after @iter. If
1187 * @iter is the end iterator, the end iterator is returned.
1189 * Return value: a #GSequenceIter pointing to the next position after @iter.
1194 g_sequence_iter_next (GSequenceIter *iter)
1196 g_return_val_if_fail (iter != NULL, NULL);
1198 return node_get_next (iter);
1202 * g_sequence_iter_prev:
1203 * @iter: a #GSequenceIter
1205 * Returns an iterator pointing to the previous position before @iter. If
1206 * @iter is the begin iterator, the begin iterator is returned.
1208 * Return value: a #GSequenceIter pointing to the previous position before
1214 g_sequence_iter_prev (GSequenceIter *iter)
1216 g_return_val_if_fail (iter != NULL, NULL);
1218 return node_get_prev (iter);
1222 * g_sequence_iter_move:
1223 * @iter: a #GSequenceIter
1224 * @delta: A positive or negative number indicating how many positions away
1225 * from @iter the returned #GSequenceIter will be.
1227 * Returns the #GSequenceIter which is @delta positions away from @iter.
1228 * If @iter is closer than -@delta positions to the beginning of the sequence,
1229 * the begin iterator is returned. If @iter is closer than @delta positions
1230 * to the end of the sequence, the end iterator is returned.
1232 * Return value: a #GSequenceIter which is @delta positions away from @iter.
1237 g_sequence_iter_move (GSequenceIter *iter,
1242 g_return_val_if_fail (iter != NULL, NULL);
1244 new_pos = node_get_pos (iter) + delta;
1246 new_pos = clamp_position (get_sequence (iter), new_pos);
1248 return node_get_by_pos (iter, new_pos);
1253 * @a: a #GSequenceIter
1254 * @b: a #GSequenceIter
1256 * Swaps the items pointed to by @a and @b
1261 g_sequence_swap (GSequenceIter *a,
1264 GSequenceNode *leftmost, *rightmost, *rightmost_next;
1267 g_return_if_fail (!g_sequence_iter_is_end (a));
1268 g_return_if_fail (!g_sequence_iter_is_end (b));
1273 a_pos = g_sequence_iter_get_position (a);
1274 b_pos = g_sequence_iter_get_position (b);
1287 rightmost_next = node_get_next (rightmost);
1289 /* The situation is now like this:
1291 * ..., leftmost, ......., rightmost, rightmost_next, ...
1294 g_sequence_move (rightmost, leftmost);
1295 g_sequence_move (leftmost, rightmost_next);
1299 * Implementation of the splay tree.
1302 /* Splay Tree vs. Other Kinds of Trees
1304 * There are both advantages and disadvantages to using a splay tree vs. some other
1305 * kind of tree such as a red/black tree or a btree.
1307 * Advantages of splay trees
1309 * - They are very simple to implement, especially things like move_range or concatenate
1310 * are easy to do for splay trees. The algorithm to split a red/black tree, while still O(log n),
1311 * is much more complicated
1313 * - If we add aggregates at some point, splay trees make it easy to compute the aggregate
1314 * for an arbitrary range of the tree. In a red/black tree you would have to pick out
1315 * the correct subtrees, then call out to the aggregator function to compute them.
1316 * On the other hand, for a splay tree, aggregates would be invalidated on lookups, so you
1317 * would call the aggregator much more often. The aggregates could be invalidated lazily though.
1318 * In both cases, the aggregator function would be called O(log n) times as a side-effect of
1319 * asking for the aggregate of a range.
1321 * - If you are only using the list API and never the insert_sorted(), the operations on a
1322 * splay tree will actually be O(1) rather than O(log n). But this is most likely just
1323 * not that interesting in practice since the O(log n) of a BTree is actually very fast.
1327 * - Splay trees are only amortized O(log n) which means individual operations could take a long
1328 * time, which is undesirable in GUI applications
1330 * - Red/black trees are more widely known since they are tought in CS101 courses.
1332 * - Red/black trees or btrees are more efficient. Not only is the red/black algorithm faster
1333 * in itself, the splaying writes to nodes on lookup which causes dirty pages that the VM
1334 * system will have to launder.
1336 * - Splay trees are not necessarily balanced at all which means straight-forward recursive
1337 * algorithms can use lots of stack.
1339 * It is likely worth investigating whether a BTree would be a better choice, in particular the
1340 * algorithm to split a BTree may not be all that complicated given that split/join for nodes
1341 * will have to be implemented anyway.
1346 node_update_fields (GSequenceNode *node)
1350 g_assert (node != NULL);
1353 n_nodes += node->left->n_nodes;
1356 n_nodes += node->right->n_nodes;
1358 node->n_nodes = n_nodes;
1361 #define NODE_LEFT_CHILD(n) (((n)->parent) && ((n)->parent->left) == (n))
1362 #define NODE_RIGHT_CHILD(n) (((n)->parent) && ((n)->parent->right) == (n))
1365 node_rotate (GSequenceNode *node)
1367 GSequenceNode *tmp, *old;
1369 g_assert (node->parent);
1370 g_assert (node->parent != node);
1372 if (NODE_LEFT_CHILD (node))
1377 node->right = node->parent;
1378 node->parent = node->parent->parent;
1381 if (node->parent->left == node->right)
1382 node->parent->left = node;
1384 node->parent->right = node;
1387 g_assert (node->right);
1389 node->right->parent = node;
1390 node->right->left = tmp;
1392 if (node->right->left)
1393 node->right->left->parent = node->right;
1402 node->left = node->parent;
1403 node->parent = node->parent->parent;
1406 if (node->parent->right == node->left)
1407 node->parent->right = node;
1409 node->parent->left = node;
1412 g_assert (node->left);
1414 node->left->parent = node;
1415 node->left->right = tmp;
1417 if (node->left->right)
1418 node->left->right->parent = node->left;
1423 node_update_fields (old);
1424 node_update_fields (node);
1427 static GSequenceNode *
1428 splay (GSequenceNode *node)
1430 while (node->parent)
1432 if (!node->parent->parent)
1437 else if ((NODE_LEFT_CHILD (node) && NODE_LEFT_CHILD (node->parent)) ||
1438 (NODE_RIGHT_CHILD (node) && NODE_RIGHT_CHILD (node->parent)))
1441 node_rotate (node->parent);
1455 static GSequenceNode *
1456 node_new (gpointer data)
1458 GSequenceNode *node = g_slice_new0 (GSequenceNode);
1460 node->parent = NULL;
1461 node->parent = NULL;
1471 static GSequenceNode *
1472 find_min (GSequenceNode *node)
1482 static GSequenceNode *
1483 find_max (GSequenceNode *node)
1493 static GSequenceNode *
1494 node_get_first (GSequenceNode *node)
1496 return splay (find_min (node));
1499 static GSequenceNode *
1500 node_get_last (GSequenceNode *node)
1502 return splay (find_max (node));
1506 get_n_nodes (GSequenceNode *node)
1509 return node->n_nodes;
1514 static GSequenceNode *
1515 node_get_by_pos (GSequenceNode *node,
1520 g_assert (node != NULL);
1524 while ((i = get_n_nodes (node->left)) != pos)
1534 g_assert (node->parent != NULL);
1538 return splay (node);
1541 static GSequenceNode *
1542 node_get_prev (GSequenceNode *node)
1553 return splay (node);
1556 static GSequenceNode *
1557 node_get_next (GSequenceNode *node)
1568 return splay (node);
1572 node_get_pos (GSequenceNode *node)
1576 return get_n_nodes (node->left);
1579 /* Return closest node _strictly_ bigger than @needle. This node
1580 * always exists because the tree has an explicit end node).
1581 * This end node of @haystack must be passed in @end.
1583 static GSequenceNode *
1584 node_find_closest (GSequenceNode *haystack,
1585 GSequenceNode *needle,
1587 GSequenceIterCompareFunc iter_cmp,
1590 GSequenceNode *best;
1593 g_assert (haystack);
1595 haystack = splay (haystack);
1601 /* iter_cmp can't be passed the end node, since the function may
1604 if (haystack == end)
1607 c = iter_cmp (haystack, needle, cmp_data);
1609 /* In the following we don't break even if c == 0. Instaed we go on
1610 * searching along the 'bigger' nodes, so that we find the last one
1611 * that is equal to the needle.
1614 haystack = haystack->left;
1616 haystack = haystack->right;
1618 while (haystack != NULL);
1620 /* If the best node is smaller or equal to the data, then move one step
1621 * to the right to make sure the best one is strictly bigger than the data
1623 if (best != end && c <= 0)
1624 best = node_get_next (best);
1630 node_free (GSequenceNode *node,
1633 GPtrArray *stack = g_ptr_array_new ();
1637 g_ptr_array_add (stack, node);
1639 while (stack->len > 0)
1641 node = g_ptr_array_remove_index (stack, stack->len - 1);
1645 g_ptr_array_add (stack, node->right);
1646 g_ptr_array_add (stack, node->left);
1648 if (seq && seq->data_destroy_notify && node != seq->end_node)
1649 seq->data_destroy_notify (node->data);
1651 g_slice_free (GSequenceNode, node);
1655 g_ptr_array_free (stack, TRUE);
1658 /* Splits into two trees. @node will be part of the right tree
1661 node_cut (GSequenceNode *node)
1665 g_assert (node->parent == NULL);
1668 node->left->parent = NULL;
1671 node_update_fields (node);
1675 node_insert_before (GSequenceNode *node,
1678 g_assert (node != NULL);
1679 g_assert (new != NULL);
1683 new = splay (find_min (new));
1684 g_assert (new->left == NULL);
1687 node->left->parent = new;
1689 new->left = node->left;
1694 node_update_fields (new);
1695 node_update_fields (node);
1699 node_insert_after (GSequenceNode *node,
1702 g_assert (node != NULL);
1703 g_assert (new != NULL);
1707 new = splay (find_max (new));
1708 g_assert (new->right == NULL);
1709 g_assert (node->parent == NULL);
1712 node->right->parent = new;
1714 new->right = node->right;
1719 node_update_fields (new);
1720 node_update_fields (node);
1724 node_get_length (GSequenceNode *node)
1726 g_assert (node != NULL);
1729 return node->n_nodes;
1733 node_unlink (GSequenceNode *node)
1735 GSequenceNode *right, *left;
1740 right = node->right;
1742 node->parent = node->left = node->right = NULL;
1743 node_update_fields (node);
1747 right->parent = NULL;
1749 right = node_get_first (right);
1750 g_assert (right->left == NULL);
1755 left->parent = right;
1756 node_update_fields (right);
1761 left->parent = NULL;
1766 node_insert_sorted (GSequenceNode *node,
1769 GSequenceIterCompareFunc iter_cmp,
1772 GSequenceNode *closest;
1774 closest = node_find_closest (node, new, end, iter_cmp, cmp_data);
1778 node_insert_before (closest, new);
1782 node_calc_height (GSequenceNode *node)
1793 left_height = node_calc_height (node->left);
1796 right_height = node_calc_height (node->right);
1798 return MAX (left_height, right_height) + 1;
1804 /* Self-test function */
1806 check_node (GSequenceNode *node)
1810 g_assert (node->parent != node);
1811 g_assert (node->n_nodes ==
1812 1 + get_n_nodes (node->left) + get_n_nodes (node->right));
1813 check_node (node->left);
1814 check_node (node->right);
1819 g_sequence_self_test_internal_to_glib_dont_use (GSequence *seq)
1821 GSequenceNode *node = splay (seq->end_node);
1826 #define __G_SEQUENCE_C__
1827 #include "galiasdef.c"