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.
23 #include "gsequence.h"
26 #include "gtestutils.h"
30 * @short_description: scalable lists
32 * The #GSequence data structure has the API of a list, but is
33 * implemented internally with a balanced binary tree. This means that
34 * it is possible to maintain a sorted list of n elements in time O(n
35 * log n). The data contained in each element can be either integer
36 * values, by using of the <link
37 * linkend="glib-Type-Conversion-Macros">Type Conversion Macros</link>,
38 * or simply pointers to any type of data.
40 * A #GSequence is accessed through <firstterm>iterators</firstterm>,
41 * represented by a #GSequenceIter. An iterator represents a position
42 * between two elements of the sequence. For example, the
43 * <firstterm>begin</firstterm> iterator represents the gap immediately
44 * before the first element of the sequence, and the
45 * <firstterm>end</firstterm> iterator represents the gap immediately
46 * after the last element. In an empty sequence, the begin and end
47 * iterators are the same.
49 * Some methods on #GSequence operate on ranges of items. For example
50 * g_sequence_foreach_range() will call a user-specified function on
51 * each element with the given range. The range is delimited by the
52 * gaps represented by the passed-in iterators, so if you pass in the
53 * begin and end iterators, the range in question is the entire
56 * The function g_sequence_get() is used with an iterator to access the
57 * element immediately following the gap that the iterator represents.
58 * The iterator is said to <firstterm>point</firstterm> to that element.
60 * Iterators are stable across most operations on a #GSequence. For
61 * example an iterator pointing to some element of a sequence will
62 * continue to point to that element even after the sequence is sorted.
63 * Even moving an element to another sequence using for example
64 * g_sequence_move_range() will not invalidate the iterators pointing
65 * to it. The only operation that will invalidate an iterator is when
66 * the element it points to is removed from any sequence.
72 * The #GSequenceIter struct is an opaque data type representing an
73 * iterator pointing into a #GSequence.
77 * GSequenceIterCompareFunc:
78 * @a: a #GSequenceIter
79 * @b: a #GSequenceIter
81 * @Returns: zero if the iterators are equal, a negative value if @a
82 * comes before @b, and a positive value if @b comes before
85 * A #GSequenceIterCompareFunc is a function used to compare iterators.
86 * It must return zero if the iterators compare equal, a negative value
87 * if @a comes before @b, and a positive value if @b comes before @a.
90 typedef struct _GSequenceNode GSequenceNode;
95 * The #GSequence struct is an opaque data type representing a
96 * <link linkend="glib-Sequences">Sequence</link> data type.
100 GSequenceNode * end_node;
101 GDestroyNotify data_destroy_notify;
102 gboolean access_prohibited;
104 /* The 'real_sequence' is used when temporary sequences are created
105 * to hold nodes that are being rearranged. The 'real_sequence' of such
106 * a temporary sequence points to the sequence that is actually being
107 * manipulated. The only reason we need this is so that when the
108 * sort/sort_changed/search_iter() functions call out to the application
109 * g_sequence_iter_get_sequence() will return the correct sequence.
111 GSequence * real_sequence;
114 struct _GSequenceNode
117 GSequenceNode * parent;
118 GSequenceNode * left;
119 GSequenceNode * right;
120 gpointer data; /* For the end node, this field points
126 * Declaration of GSequenceNode methods
128 static GSequenceNode *node_new (gpointer data);
129 static GSequenceNode *node_get_first (GSequenceNode *node);
130 static GSequenceNode *node_get_last (GSequenceNode *node);
131 static GSequenceNode *node_get_prev (GSequenceNode *node);
132 static GSequenceNode *node_get_next (GSequenceNode *node);
133 static gint node_get_pos (GSequenceNode *node);
134 static GSequenceNode *node_get_by_pos (GSequenceNode *node,
136 static GSequenceNode *node_find (GSequenceNode *haystack,
137 GSequenceNode *needle,
139 GSequenceIterCompareFunc cmp,
141 static GSequenceNode *node_find_closest (GSequenceNode *haystack,
142 GSequenceNode *needle,
144 GSequenceIterCompareFunc cmp,
146 static gint node_get_length (GSequenceNode *node);
147 static void node_free (GSequenceNode *node,
149 static void node_cut (GSequenceNode *split);
150 static void node_insert_before (GSequenceNode *node,
152 static void node_unlink (GSequenceNode *node);
153 static void node_join (GSequenceNode *left,
154 GSequenceNode *right);
155 static void node_insert_sorted (GSequenceNode *node,
158 GSequenceIterCompareFunc cmp_func,
163 * Various helper functions
166 check_seq_access (GSequence *seq)
168 if (G_UNLIKELY (seq->access_prohibited))
170 g_warning ("Accessing a sequence while it is "
171 "being sorted or searched is not allowed");
176 get_sequence (GSequenceNode *node)
178 return (GSequence *)node_get_last (node)->data;
182 check_iter_access (GSequenceIter *iter)
184 check_seq_access (get_sequence (iter));
188 is_end (GSequenceIter *iter)
198 if (iter->parent->right != iter)
201 seq = get_sequence (iter);
203 return seq->end_node == iter;
208 GCompareDataFunc cmp_func;
210 GSequenceNode *end_node;
213 /* This function compares two iters using a normal compare
214 * function and user_data passed in in a SortInfo struct
217 iter_compare (GSequenceIter *node1,
218 GSequenceIter *node2,
221 const SortInfo *info = data;
224 if (node1 == info->end_node)
227 if (node2 == info->end_node)
230 retval = info->cmp_func (node1->data, node2->data, info->cmp_data);
241 * @data_destroy: a #GDestroyNotify function, or %NULL
243 * Creates a new GSequence. The @data_destroy function, if non-%NULL will
244 * be called on all items when the sequence is destroyed and on items that
245 * are removed from the sequence.
247 * Return value: a new #GSequence
252 g_sequence_new (GDestroyNotify data_destroy)
254 GSequence *seq = g_new (GSequence, 1);
255 seq->data_destroy_notify = data_destroy;
257 seq->end_node = node_new (seq);
259 seq->access_prohibited = FALSE;
261 seq->real_sequence = seq;
270 * Frees the memory allocated for @seq. If @seq has a data destroy
271 * function associated with it, that function is called on all items in
277 g_sequence_free (GSequence *seq)
279 g_return_if_fail (seq != NULL);
281 check_seq_access (seq);
283 node_free (seq->end_node, seq);
289 * g_sequence_foreach_range:
290 * @begin: a #GSequenceIter
291 * @end: a #GSequenceIter
293 * @user_data: user data passed to @func
295 * Calls @func for each item in the range (@begin, @end) passing
296 * @user_data to the function.
301 g_sequence_foreach_range (GSequenceIter *begin,
309 g_return_if_fail (func != NULL);
310 g_return_if_fail (begin != NULL);
311 g_return_if_fail (end != NULL);
313 seq = get_sequence (begin);
315 seq->access_prohibited = TRUE;
320 GSequenceIter *next = node_get_next (iter);
322 func (iter->data, user_data);
327 seq->access_prohibited = FALSE;
331 * g_sequence_foreach:
333 * @func: the function to call for each item in @seq
334 * @user_data: user data passed to @func
336 * Calls @func for each item in the sequence passing @user_data
342 g_sequence_foreach (GSequence *seq,
346 GSequenceIter *begin, *end;
348 check_seq_access (seq);
350 begin = g_sequence_get_begin_iter (seq);
351 end = g_sequence_get_end_iter (seq);
353 g_sequence_foreach_range (begin, end, func, user_data);
357 * g_sequence_range_get_midpoint:
358 * @begin: a #GSequenceIter
359 * @end: a #GSequenceIter
361 * Finds an iterator somewhere in the range (@begin, @end). This
362 * iterator will be close to the middle of the range, but is not
363 * guaranteed to be <emphasis>exactly</emphasis> in the middle.
365 * The @begin and @end iterators must both point to the same sequence and
366 * @begin must come before or be equal to @end in the sequence.
368 * Return value: A #GSequenceIter pointing somewhere in the
369 * (@begin, @end) range.
374 g_sequence_range_get_midpoint (GSequenceIter *begin,
377 int begin_pos, end_pos, mid_pos;
379 g_return_val_if_fail (begin != NULL, NULL);
380 g_return_val_if_fail (end != NULL, NULL);
381 g_return_val_if_fail (get_sequence (begin) == get_sequence (end), NULL);
383 begin_pos = node_get_pos (begin);
384 end_pos = node_get_pos (end);
386 g_return_val_if_fail (end_pos >= begin_pos, NULL);
388 mid_pos = begin_pos + (end_pos - begin_pos) / 2;
390 return node_get_by_pos (begin, mid_pos);
394 * g_sequence_iter_compare:
395 * @a: a #GSequenceIter
396 * @b: a #GSequenceIter
398 * Returns a negative number if @a comes before @b, 0 if they are equal,
399 * and a positive number if @a comes after @b.
401 * The @a and @b iterators must point into the same sequence.
403 * Return value: A negative number if @a comes before @b, 0 if they are
404 * equal, and a positive number if @a comes after @b.
409 g_sequence_iter_compare (GSequenceIter *a,
414 g_return_val_if_fail (a != NULL, 0);
415 g_return_val_if_fail (b != NULL, 0);
416 g_return_val_if_fail (get_sequence (a) == get_sequence (b), 0);
418 check_iter_access (a);
419 check_iter_access (b);
421 a_pos = node_get_pos (a);
422 b_pos = node_get_pos (b);
426 else if (a_pos > b_pos)
434 * @seq: a #GSequencePointer
435 * @data: the data for the new item
437 * Adds a new item to the end of @seq.
439 * Return value: an iterator pointing to the new item
444 g_sequence_append (GSequence *seq,
449 g_return_val_if_fail (seq != NULL, NULL);
451 check_seq_access (seq);
453 node = node_new (data);
454 node_insert_before (seq->end_node, node);
460 * g_sequence_prepend:
462 * @data: the data for the new item
464 * Adds a new item to the front of @seq
466 * Return value: an iterator pointing to the new item
471 g_sequence_prepend (GSequence *seq,
474 GSequenceNode *node, *first;
476 g_return_val_if_fail (seq != NULL, NULL);
478 check_seq_access (seq);
480 node = node_new (data);
481 first = node_get_first (seq->end_node);
483 node_insert_before (first, node);
489 * g_sequence_insert_before:
490 * @iter: a #GSequenceIter
491 * @data: the data for the new item
493 * Inserts a new item just before the item pointed to by @iter.
495 * Return value: an iterator pointing to the new item
500 g_sequence_insert_before (GSequenceIter *iter,
505 g_return_val_if_fail (iter != NULL, NULL);
507 check_iter_access (iter);
509 node = node_new (data);
511 node_insert_before (iter, node);
518 * @iter: a #GSequenceIter
520 * Removes the item pointed to by @iter. It is an error to pass the
521 * end iterator to this function.
523 * If the sequnce has a data destroy function associated with it, this
524 * function is called on the data for the removed item.
529 g_sequence_remove (GSequenceIter *iter)
533 g_return_if_fail (iter != NULL);
534 g_return_if_fail (!is_end (iter));
536 check_iter_access (iter);
538 seq = get_sequence (iter);
541 node_free (iter, seq);
545 * g_sequence_remove_range:
546 * @begin: a #GSequenceIter
547 * @end: a #GSequenceIter
549 * Removes all items in the (@begin, @end) range.
551 * If the sequence has a data destroy function associated with it, this
552 * function is called on the data for the removed items.
557 g_sequence_remove_range (GSequenceIter *begin,
560 g_return_if_fail (get_sequence (begin) == get_sequence (end));
562 check_iter_access (begin);
563 check_iter_access (end);
565 g_sequence_move_range (NULL, begin, end);
569 * g_sequence_move_range:
570 * @dest: a #GSequenceIter
571 * @begin: a #GSequenceIter
572 * @end: a #GSequenceIter
574 * Inserts the (@begin, @end) range at the destination pointed to by ptr.
575 * The @begin and @end iters must point into the same sequence. It is
576 * allowed for @dest to point to a different sequence than the one pointed
577 * into by @begin and @end.
579 * If @dest is NULL, the range indicated by @begin and @end is
580 * removed from the sequence. If @dest iter points to a place within
581 * the (@begin, @end) range, the range does not move.
586 g_sequence_move_range (GSequenceIter *dest,
587 GSequenceIter *begin,
591 GSequenceNode *first;
593 g_return_if_fail (begin != NULL);
594 g_return_if_fail (end != NULL);
596 check_iter_access (begin);
597 check_iter_access (end);
599 check_iter_access (dest);
601 src_seq = get_sequence (begin);
603 g_return_if_fail (src_seq == get_sequence (end));
605 /* Dest points to begin or end? */
606 if (dest == begin || dest == end)
609 /* begin comes after end? */
610 if (g_sequence_iter_compare (begin, end) >= 0)
613 /* dest points somewhere in the (begin, end) range? */
614 if (dest && get_sequence (dest) == src_seq &&
615 g_sequence_iter_compare (dest, begin) > 0 &&
616 g_sequence_iter_compare (dest, end) < 0)
621 src_seq = get_sequence (begin);
623 first = node_get_first (begin);
630 node_join (first, end);
634 first = node_get_first (dest);
638 node_join (begin, dest);
641 node_join (first, begin);
645 node_free (begin, src_seq);
652 * @cmp_func: the #GCompareDataFunc used to sort @seq. This function is
653 * passed two items of @seq and should return 0 if they are equal,
654 * a negative value if the first comes before the second, and a
655 * positive value if the second comes before the first.
656 * @cmp_data: user data passed to @cmp_func
658 * Sorts @seq using @cmp_func.
663 g_sequence_sort (GSequence *seq,
664 GCompareDataFunc cmp_func,
669 info.cmp_func = cmp_func;
670 info.cmp_data = cmp_data;
671 info.end_node = seq->end_node;
673 check_seq_access (seq);
675 g_sequence_sort_iter (seq, iter_compare, &info);
679 * g_sequence_insert_sorted:
681 * @data: the data to insert
682 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
683 * is called with two items of the @seq and @user_data. It should
684 * return 0 if the items are equal, a negative value if the first
685 * item comes before the second, and a positive value if the second
686 * item comes before the first.
687 * @cmp_data: user data passed to @cmp_func.
689 * Inserts @data into @sequence using @func to determine the new position.
690 * The sequence must already be sorted according to @cmp_func; otherwise the
691 * new position of @data is undefined.
693 * Return value: a #GSequenceIter pointing to the new item.
698 g_sequence_insert_sorted (GSequence *seq,
700 GCompareDataFunc cmp_func,
705 g_return_val_if_fail (seq != NULL, NULL);
706 g_return_val_if_fail (cmp_func != NULL, NULL);
708 info.cmp_func = cmp_func;
709 info.cmp_data = cmp_data;
710 info.end_node = seq->end_node;
711 check_seq_access (seq);
713 return g_sequence_insert_sorted_iter (seq, data, iter_compare, &info);
717 * g_sequence_sort_changed:
718 * @iter: A #GSequenceIter
719 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
720 * is called with two items of the @seq and @user_data. It should
721 * return 0 if the items are equal, a negative value if the first
722 * item comes before the second, and a positive value if the second
723 * item comes before the first.
724 * @cmp_data: user data passed to @cmp_func.
726 * Moves the data pointed to a new position as indicated by @cmp_func. This
727 * function should be called for items in a sequence already sorted according
728 * to @cmp_func whenever some aspect of an item changes so that @cmp_func
729 * may return different values for that item.
734 g_sequence_sort_changed (GSequenceIter *iter,
735 GCompareDataFunc cmp_func,
740 g_return_if_fail (!is_end (iter));
742 info.cmp_func = cmp_func;
743 info.cmp_data = cmp_data;
744 info.end_node = get_sequence (iter)->end_node;
745 check_iter_access (iter);
747 g_sequence_sort_changed_iter (iter, iter_compare, &info);
753 * @data: data for the new item
754 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
755 * is called with two items of the @seq and @user_data. It should
756 * return 0 if the items are equal, a negative value if the first
757 * item comes before the second, and a positive value if the second
758 * item comes before the first.
759 * @cmp_data: user data passed to @cmp_func.
761 * Returns an iterator pointing to the position where @data would
762 * be inserted according to @cmp_func and @cmp_data.
764 * Return value: an #GSequenceIter pointing to the position where @data
765 * would have been inserted according to @cmp_func and @cmp_data.
770 g_sequence_search (GSequence *seq,
772 GCompareDataFunc cmp_func,
777 g_return_val_if_fail (seq != NULL, NULL);
779 info.cmp_func = cmp_func;
780 info.cmp_data = cmp_data;
781 info.end_node = seq->end_node;
782 check_seq_access (seq);
784 return g_sequence_search_iter (seq, data, iter_compare, &info);
790 * @data: data to lookup
791 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
792 * is called with two items of the @seq and @user_data. It should
793 * return 0 if the items are equal, a negative value if the first
794 * item comes before the second, and a positive value if the second
795 * item comes before the first.
796 * @cmp_data: user data passed to @cmp_func.
798 * Returns an iterator pointing to the position of the first item found
799 * equal to @data according to @cmp_func and @cmp_data. If more than one item
800 * is equal, it is not guaranteed that it is the first which is returned.
801 * In that case, you can use g_sequence_iter_next() and g_sequence_iter_prev()
804 * Return value: an #GSequenceIter pointing to the position of the first item
805 * found equal to @data according to @cmp_func and @cmp_data.
810 g_sequence_lookup (GSequence *seq,
812 GCompareDataFunc cmp_func,
817 g_return_val_if_fail (seq != NULL, NULL);
819 info.cmp_func = cmp_func;
820 info.cmp_data = cmp_data;
821 info.end_node = seq->end_node;
822 check_seq_access (seq);
824 return g_sequence_lookup_iter (seq, data, iter_compare, &info);
828 * g_sequence_sort_iter:
830 * @cmp_func: the #GSequenceItercompare used to compare iterators in the
831 * sequence. It is called with two iterators pointing into @seq. It should
832 * return 0 if the iterators are equal, a negative value if the first
833 * iterator comes before the second, and a positive value if the second
834 * iterator comes before the first.
835 * @cmp_data: user data passed to @cmp_func
837 * Like g_sequence_sort(), but uses a #GSequenceIterCompareFunc instead
838 * of a GCompareDataFunc as the compare function
843 g_sequence_sort_iter (GSequence *seq,
844 GSequenceIterCompareFunc cmp_func,
848 GSequenceNode *begin, *end;
850 g_return_if_fail (seq != NULL);
851 g_return_if_fail (cmp_func != NULL);
853 check_seq_access (seq);
855 begin = g_sequence_get_begin_iter (seq);
856 end = g_sequence_get_end_iter (seq);
858 tmp = g_sequence_new (NULL);
859 tmp->real_sequence = seq;
861 g_sequence_move_range (g_sequence_get_begin_iter (tmp), begin, end);
863 seq->access_prohibited = TRUE;
864 tmp->access_prohibited = TRUE;
866 while (g_sequence_get_length (tmp) > 0)
868 GSequenceNode *node = g_sequence_get_begin_iter (tmp);
870 node_insert_sorted (seq->end_node, node, seq->end_node,
874 tmp->access_prohibited = FALSE;
875 seq->access_prohibited = FALSE;
877 g_sequence_free (tmp);
881 * g_sequence_sort_changed_iter:
882 * @iter: a #GSequenceIter
883 * @iter_cmp: the #GSequenceItercompare used to compare iterators in the
884 * sequence. It is called with two iterators pointing into @seq. It should
885 * return 0 if the iterators are equal, a negative value if the first
886 * iterator comes before the second, and a positive value if the second
887 * iterator comes before the first.
888 * @cmp_data: user data passed to @cmp_func
890 * Like g_sequence_sort_changed(), but uses
891 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
892 * the compare function.
897 g_sequence_sort_changed_iter (GSequenceIter *iter,
898 GSequenceIterCompareFunc iter_cmp,
901 GSequence *seq, *tmp_seq;
902 GSequenceIter *next, *prev;
904 g_return_if_fail (iter != NULL);
905 g_return_if_fail (!is_end (iter));
906 g_return_if_fail (iter_cmp != NULL);
907 check_iter_access (iter);
909 /* If one of the neighbours is equal to iter, then
910 * don't move it. This ensures that sort_changed() is
911 * a stable operation.
914 next = node_get_next (iter);
915 prev = node_get_prev (iter);
917 if (prev != iter && iter_cmp (prev, iter, cmp_data) == 0)
920 if (!is_end (next) && iter_cmp (next, iter, cmp_data) == 0)
923 seq = get_sequence (iter);
925 seq->access_prohibited = TRUE;
927 tmp_seq = g_sequence_new (NULL);
928 tmp_seq->real_sequence = seq;
931 node_insert_before (tmp_seq->end_node, iter);
933 node_insert_sorted (seq->end_node, iter, seq->end_node,
936 g_sequence_free (tmp_seq);
938 seq->access_prohibited = FALSE;
942 * g_sequence_insert_sorted_iter:
944 * @data: data for the new item
945 * @iter_cmp: the #GSequenceItercompare used to compare iterators in the
946 * sequence. It is called with two iterators pointing into @seq. It should
947 * return 0 if the iterators are equal, a negative value if the first
948 * iterator comes before the second, and a positive value if the second
949 * iterator comes before the first.
950 * @cmp_data: user data passed to @cmp_func
952 * Like g_sequence_insert_sorted(), but uses
953 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
954 * the compare function.
956 * Return value: a #GSequenceIter pointing to the new item
961 g_sequence_insert_sorted_iter (GSequence *seq,
963 GSequenceIterCompareFunc iter_cmp,
966 GSequenceNode *new_node;
969 g_return_val_if_fail (seq != NULL, NULL);
970 g_return_val_if_fail (iter_cmp != NULL, NULL);
972 check_seq_access (seq);
974 seq->access_prohibited = TRUE;
976 /* Create a new temporary sequence and put the new node into
977 * that. The reason for this is that the user compare function
978 * will be called with the new node, and if it dereferences,
979 * "is_end" will be called on it. But that will crash if the
980 * node is not actually in a sequence.
982 * node_insert_sorted() makes sure the node is unlinked before
985 * The reason we need the "iter" versions at all is that that
986 * is the only kind of compare functions GtkTreeView can use.
988 tmp_seq = g_sequence_new (NULL);
989 tmp_seq->real_sequence = seq;
991 new_node = g_sequence_append (tmp_seq, data);
993 node_insert_sorted (seq->end_node, new_node,
994 seq->end_node, iter_cmp, cmp_data);
996 g_sequence_free (tmp_seq);
998 seq->access_prohibited = FALSE;
1004 * g_sequence_search_iter:
1005 * @seq: a #GSequence
1006 * @data: data for the new item
1007 * @iter_cmp: the #GSequenceIterCompare function used to compare iterators
1008 * in the sequence. It is called with two iterators pointing into @seq.
1009 * It should return 0 if the iterators are equal, a negative value if the
1010 * first iterator comes before the second, and a positive value if the
1011 * second iterator comes before the first.
1012 * @cmp_data: user data passed to @iter_cmp
1014 * Like g_sequence_search(), but uses
1015 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
1016 * the compare function.
1018 * Return value: a #GSequenceIter pointing to the position in @seq
1019 * where @data would have been inserted according to @iter_cmp and @cmp_data.
1024 g_sequence_search_iter (GSequence *seq,
1026 GSequenceIterCompareFunc iter_cmp,
1029 GSequenceNode *node;
1030 GSequenceNode *dummy;
1033 g_return_val_if_fail (seq != NULL, NULL);
1035 check_seq_access (seq);
1037 seq->access_prohibited = TRUE;
1039 tmp_seq = g_sequence_new (NULL);
1040 tmp_seq->real_sequence = seq;
1042 dummy = g_sequence_append (tmp_seq, data);
1044 node = node_find_closest (seq->end_node, dummy,
1045 seq->end_node, iter_cmp, cmp_data);
1047 g_sequence_free (tmp_seq);
1049 seq->access_prohibited = FALSE;
1055 * g_sequence_lookup_iter:
1056 * @seq: a #GSequence
1057 * @data: data to lookup
1058 * @iter_cmp: the #GSequenceIterCompare function used to compare iterators
1059 * in the sequence. It is called with two iterators pointing into @seq.
1060 * It should return 0 if the iterators are equal, a negative value if the
1061 * first iterator comes before the second, and a positive value if the
1062 * second iterator comes before the first.
1063 * @cmp_data: user data passed to @iter_cmp
1065 * Like g_sequence_lookup(), but uses
1066 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
1067 * the compare function.
1069 * Return value: an #GSequenceIter pointing to the position of the first item
1070 * found equal to @data according to @cmp_func and @cmp_data.
1075 g_sequence_lookup_iter (GSequence *seq,
1077 GSequenceIterCompareFunc iter_cmp,
1080 GSequenceNode *node;
1081 GSequenceNode *dummy;
1084 g_return_val_if_fail (seq != NULL, NULL);
1086 check_seq_access (seq);
1088 seq->access_prohibited = TRUE;
1090 tmp_seq = g_sequence_new (NULL);
1091 tmp_seq->real_sequence = seq;
1093 dummy = g_sequence_append (tmp_seq, data);
1095 node = node_find (seq->end_node, dummy,
1096 seq->end_node, iter_cmp, cmp_data);
1098 g_sequence_free (tmp_seq);
1100 seq->access_prohibited = FALSE;
1106 * g_sequence_iter_get_sequence:
1107 * @iter: a #GSequenceIter
1109 * Returns the #GSequence that @iter points into.
1111 * Return value: the #GSequence that @iter points into.
1116 g_sequence_iter_get_sequence (GSequenceIter *iter)
1120 g_return_val_if_fail (iter != NULL, NULL);
1122 seq = get_sequence (iter);
1124 /* For temporary sequences, this points to the sequence that
1125 * is actually being manipulated
1127 return seq->real_sequence;
1132 * @iter: a #GSequenceIter
1134 * Returns the data that @iter points to.
1136 * Return value: the data that @iter points to
1141 g_sequence_get (GSequenceIter *iter)
1143 g_return_val_if_fail (iter != NULL, NULL);
1144 g_return_val_if_fail (!is_end (iter), NULL);
1151 * @iter: a #GSequenceIter
1152 * @data: new data for the item
1154 * Changes the data for the item pointed to by @iter to be @data. If
1155 * the sequence has a data destroy function associated with it, that
1156 * function is called on the existing data that @iter pointed to.
1161 g_sequence_set (GSequenceIter *iter,
1166 g_return_if_fail (iter != NULL);
1167 g_return_if_fail (!is_end (iter));
1169 seq = get_sequence (iter);
1171 /* If @data is identical to iter->data, it is destroyed
1172 * here. This will work right in case of ref-counted objects. Also
1173 * it is similar to what ghashtables do.
1175 * For non-refcounted data it's a little less convenient, but
1176 * code relying on self-setting not destroying would be
1177 * pretty dubious anyway ...
1180 if (seq->data_destroy_notify)
1181 seq->data_destroy_notify (iter->data);
1187 * g_sequence_get_length:
1188 * @seq: a #GSequence
1190 * Returns the length of @seq
1192 * Return value: the length of @seq
1197 g_sequence_get_length (GSequence *seq)
1199 return node_get_length (seq->end_node) - 1;
1203 * g_sequence_get_end_iter:
1204 * @seq: a #GSequence
1206 * Returns the end iterator for @seg
1208 * Return value: the end iterator for @seq
1213 g_sequence_get_end_iter (GSequence *seq)
1215 g_return_val_if_fail (seq != NULL, NULL);
1217 return seq->end_node;
1221 * g_sequence_get_begin_iter:
1222 * @seq: a #GSequence
1224 * Returns the begin iterator for @seq.
1226 * Return value: the begin iterator for @seq.
1231 g_sequence_get_begin_iter (GSequence *seq)
1233 g_return_val_if_fail (seq != NULL, NULL);
1235 return node_get_first (seq->end_node);
1239 clamp_position (GSequence *seq,
1242 gint len = g_sequence_get_length (seq);
1244 if (pos > len || pos < 0)
1251 * if pos > number of items or -1, will return end pointer
1254 * g_sequence_get_iter_at_pos:
1255 * @seq: a #GSequence
1256 * @pos: a position in @seq, or -1 for the end.
1258 * Returns the iterator at position @pos. If @pos is negative or larger
1259 * than the number of items in @seq, the end iterator is returned.
1261 * Return value: The #GSequenceIter at position @pos
1266 g_sequence_get_iter_at_pos (GSequence *seq,
1269 g_return_val_if_fail (seq != NULL, NULL);
1271 pos = clamp_position (seq, pos);
1273 return node_get_by_pos (seq->end_node, pos);
1278 * @src: a #GSequenceIter pointing to the item to move
1279 * @dest: a #GSequenceIter pointing to the position to which
1280 * the item is moved.
1282 * Moves the item pointed to by @src to the position indicated by @dest.
1283 * After calling this function @dest will point to the position immediately
1284 * after @src. It is allowed for @src and @dest to point into different
1290 g_sequence_move (GSequenceIter *src,
1291 GSequenceIter *dest)
1293 g_return_if_fail (src != NULL);
1294 g_return_if_fail (dest != NULL);
1295 g_return_if_fail (!is_end (src));
1301 node_insert_before (dest, src);
1307 * g_sequence_iter_is_end:
1308 * @iter: a #GSequenceIter
1310 * Returns whether @iter is the end iterator
1312 * Return value: Whether @iter is the end iterator.
1317 g_sequence_iter_is_end (GSequenceIter *iter)
1319 g_return_val_if_fail (iter != NULL, FALSE);
1321 return is_end (iter);
1325 * g_sequence_iter_is_begin:
1326 * @iter: a #GSequenceIter
1328 * Returns whether @iter is the begin iterator
1330 * Return value: whether @iter is the begin iterator
1335 g_sequence_iter_is_begin (GSequenceIter *iter)
1337 g_return_val_if_fail (iter != NULL, FALSE);
1339 return (node_get_prev (iter) == iter);
1343 * g_sequence_iter_get_position:
1344 * @iter: a #GSequenceIter
1346 * Returns the position of @iter
1348 * Return value: the position of @iter
1353 g_sequence_iter_get_position (GSequenceIter *iter)
1355 g_return_val_if_fail (iter != NULL, -1);
1357 return node_get_pos (iter);
1361 * g_sequence_iter_next:
1362 * @iter: a #GSequenceIter
1364 * Returns an iterator pointing to the next position after @iter. If
1365 * @iter is the end iterator, the end iterator is returned.
1367 * Return value: a #GSequenceIter pointing to the next position after @iter.
1372 g_sequence_iter_next (GSequenceIter *iter)
1374 g_return_val_if_fail (iter != NULL, NULL);
1376 return node_get_next (iter);
1380 * g_sequence_iter_prev:
1381 * @iter: a #GSequenceIter
1383 * Returns an iterator pointing to the previous position before @iter. If
1384 * @iter is the begin iterator, the begin iterator is returned.
1386 * Return value: a #GSequenceIter pointing to the previous position before
1392 g_sequence_iter_prev (GSequenceIter *iter)
1394 g_return_val_if_fail (iter != NULL, NULL);
1396 return node_get_prev (iter);
1400 * g_sequence_iter_move:
1401 * @iter: a #GSequenceIter
1402 * @delta: A positive or negative number indicating how many positions away
1403 * from @iter the returned #GSequenceIter will be.
1405 * Returns the #GSequenceIter which is @delta positions away from @iter.
1406 * If @iter is closer than -@delta positions to the beginning of the sequence,
1407 * the begin iterator is returned. If @iter is closer than @delta positions
1408 * to the end of the sequence, the end iterator is returned.
1410 * Return value: a #GSequenceIter which is @delta positions away from @iter.
1415 g_sequence_iter_move (GSequenceIter *iter,
1420 g_return_val_if_fail (iter != NULL, NULL);
1422 new_pos = node_get_pos (iter) + delta;
1424 new_pos = clamp_position (get_sequence (iter), new_pos);
1426 return node_get_by_pos (iter, new_pos);
1431 * @a: a #GSequenceIter
1432 * @b: a #GSequenceIter
1434 * Swaps the items pointed to by @a and @b. It is allowed for @a and @b
1435 * to point into difference sequences.
1440 g_sequence_swap (GSequenceIter *a,
1443 GSequenceNode *leftmost, *rightmost, *rightmost_next;
1446 g_return_if_fail (!g_sequence_iter_is_end (a));
1447 g_return_if_fail (!g_sequence_iter_is_end (b));
1452 a_pos = g_sequence_iter_get_position (a);
1453 b_pos = g_sequence_iter_get_position (b);
1466 rightmost_next = node_get_next (rightmost);
1468 /* The situation is now like this:
1470 * ..., leftmost, ......., rightmost, rightmost_next, ...
1473 g_sequence_move (rightmost, leftmost);
1474 g_sequence_move (leftmost, rightmost_next);
1478 * Implementation of a treap
1483 get_priority (GSequenceNode *node)
1485 guint key = GPOINTER_TO_UINT (node);
1487 /* This hash function is based on one found on Thomas Wang's
1490 * http://www.concentric.net/~Ttwang/tech/inthash.htm
1493 key = (key << 15) - key - 1;
1494 key = key ^ (key >> 12);
1495 key = key + (key << 2);
1496 key = key ^ (key >> 4);
1497 key = key + (key << 3) + (key << 11);
1498 key = key ^ (key >> 16);
1500 /* We rely on 0 being less than all other priorities */
1501 return key? key : 1;
1504 static GSequenceNode *
1505 find_root (GSequenceNode *node)
1507 while (node->parent)
1508 node = node->parent;
1513 static GSequenceNode *
1514 node_new (gpointer data)
1516 GSequenceNode *node = g_slice_new0 (GSequenceNode);
1522 node->parent = NULL;
1527 static GSequenceNode *
1528 node_get_first (GSequenceNode *node)
1530 node = find_root (node);
1538 static GSequenceNode *
1539 node_get_last (GSequenceNode *node)
1541 node = find_root (node);
1549 #define NODE_LEFT_CHILD(n) (((n)->parent) && ((n)->parent->left) == (n))
1550 #define NODE_RIGHT_CHILD(n) (((n)->parent) && ((n)->parent->right) == (n))
1552 static GSequenceNode *
1553 node_get_next (GSequenceNode *node)
1555 GSequenceNode *n = node;
1565 while (NODE_RIGHT_CHILD (n))
1577 static GSequenceNode *
1578 node_get_prev (GSequenceNode *node)
1580 GSequenceNode *n = node;
1590 while (NODE_LEFT_CHILD (n))
1602 #define N_NODES(n) ((n)? (n)->n_nodes : 0)
1605 node_get_pos (GSequenceNode *node)
1610 n_smaller = node->left->n_nodes;
1614 if (NODE_RIGHT_CHILD (node))
1615 n_smaller += N_NODES (node->parent->left) + 1;
1617 node = node->parent;
1623 static GSequenceNode *
1624 node_get_by_pos (GSequenceNode *node,
1629 node = find_root (node);
1631 while ((i = N_NODES (node->left)) != pos)
1647 static GSequenceNode *
1648 node_find (GSequenceNode *haystack,
1649 GSequenceNode *needle,
1651 GSequenceIterCompareFunc iter_cmp,
1656 haystack = find_root (haystack);
1660 /* iter_cmp can't be passed the end node, since the function may
1663 if (haystack == end)
1666 c = iter_cmp (haystack, needle, cmp_data);
1672 haystack = haystack->left;
1674 haystack = haystack->right;
1676 while (haystack != NULL);
1681 static GSequenceNode *
1682 node_find_closest (GSequenceNode *haystack,
1683 GSequenceNode *needle,
1685 GSequenceIterCompareFunc iter_cmp,
1688 GSequenceNode *best;
1691 haystack = find_root (haystack);
1697 /* iter_cmp can't be passed the end node, since the function may
1700 if (haystack == end)
1703 c = iter_cmp (haystack, needle, cmp_data);
1705 /* In the following we don't break even if c == 0. Instead we go on
1706 * searching along the 'bigger' nodes, so that we find the last one
1707 * that is equal to the needle.
1710 haystack = haystack->left;
1712 haystack = haystack->right;
1714 while (haystack != NULL);
1716 /* If the best node is smaller or equal to the data, then move one step
1717 * to the right to make sure the best one is strictly bigger than the data
1719 if (best != end && c <= 0)
1720 best = node_get_next (best);
1726 node_get_length (GSequenceNode *node)
1728 node = find_root (node);
1730 return node->n_nodes;
1734 real_node_free (GSequenceNode *node,
1739 real_node_free (node->left, seq);
1740 real_node_free (node->right, seq);
1742 if (seq && seq->data_destroy_notify && node != seq->end_node)
1743 seq->data_destroy_notify (node->data);
1745 g_slice_free (GSequenceNode, node);
1750 node_free (GSequenceNode *node,
1753 node = find_root (node);
1755 real_node_free (node, seq);
1759 node_update_fields (GSequenceNode *node)
1763 n_nodes += N_NODES (node->left);
1764 n_nodes += N_NODES (node->right);
1766 node->n_nodes = n_nodes;
1770 node_rotate (GSequenceNode *node)
1772 GSequenceNode *tmp, *old;
1774 g_assert (node->parent);
1775 g_assert (node->parent != node);
1777 if (NODE_LEFT_CHILD (node))
1782 node->right = node->parent;
1783 node->parent = node->parent->parent;
1786 if (node->parent->left == node->right)
1787 node->parent->left = node;
1789 node->parent->right = node;
1792 g_assert (node->right);
1794 node->right->parent = node;
1795 node->right->left = tmp;
1797 if (node->right->left)
1798 node->right->left->parent = node->right;
1807 node->left = node->parent;
1808 node->parent = node->parent->parent;
1811 if (node->parent->right == node->left)
1812 node->parent->right = node;
1814 node->parent->left = node;
1817 g_assert (node->left);
1819 node->left->parent = node;
1820 node->left->right = tmp;
1822 if (node->left->right)
1823 node->left->right->parent = node->left;
1828 node_update_fields (old);
1829 node_update_fields (node);
1833 node_update_fields_deep (GSequenceNode *node)
1837 node_update_fields (node);
1839 node_update_fields_deep (node->parent);
1844 rotate_down (GSequenceNode *node,
1849 left = node->left ? get_priority (node->left) : 0;
1850 right = node->right ? get_priority (node->right) : 0;
1852 while (priority < left || priority < right)
1855 node_rotate (node->left);
1857 node_rotate (node->right);
1859 left = node->left ? get_priority (node->left) : 0;
1860 right = node->right ? get_priority (node->right) : 0;
1865 node_cut (GSequenceNode *node)
1867 while (node->parent)
1871 node->left->parent = NULL;
1874 node_update_fields (node);
1876 rotate_down (node, get_priority (node));
1880 node_join (GSequenceNode *left,
1881 GSequenceNode *right)
1883 GSequenceNode *fake = node_new (NULL);
1885 fake->left = find_root (left);
1886 fake->right = find_root (right);
1887 fake->left->parent = fake;
1888 fake->right->parent = fake;
1890 node_update_fields (fake);
1894 node_free (fake, NULL);
1898 node_insert_before (GSequenceNode *node,
1901 new->left = node->left;
1903 new->left->parent = new;
1908 node_update_fields_deep (new);
1910 while (new->parent && get_priority (new) > get_priority (new->parent))
1913 rotate_down (new, get_priority (new));
1917 node_unlink (GSequenceNode *node)
1919 rotate_down (node, 0);
1921 if (NODE_RIGHT_CHILD (node))
1922 node->parent->right = NULL;
1923 else if (NODE_LEFT_CHILD (node))
1924 node->parent->left = NULL;
1927 node_update_fields_deep (node->parent);
1929 node->parent = NULL;
1933 node_insert_sorted (GSequenceNode *node,
1936 GSequenceIterCompareFunc iter_cmp,
1939 GSequenceNode *closest;
1941 closest = node_find_closest (node, new, end, iter_cmp, cmp_data);
1945 node_insert_before (closest, new);