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_closest (GSequenceNode *haystack,
137 GSequenceNode *needle,
139 GSequenceIterCompareFunc cmp,
141 static gint node_get_length (GSequenceNode *node);
142 static void node_free (GSequenceNode *node,
144 static void node_cut (GSequenceNode *split);
145 static void node_insert_before (GSequenceNode *node,
147 static void node_unlink (GSequenceNode *node);
148 static void node_join (GSequenceNode *left,
149 GSequenceNode *right);
150 static void node_insert_sorted (GSequenceNode *node,
153 GSequenceIterCompareFunc cmp_func,
158 * Various helper functions
161 check_seq_access (GSequence *seq)
163 if (G_UNLIKELY (seq->access_prohibited))
165 g_warning ("Accessing a sequence while it is "
166 "being sorted or searched is not allowed");
171 get_sequence (GSequenceNode *node)
173 return (GSequence *)node_get_last (node)->data;
177 check_iter_access (GSequenceIter *iter)
179 check_seq_access (get_sequence (iter));
183 is_end (GSequenceIter *iter)
193 if (iter->parent->right != iter)
196 seq = get_sequence (iter);
198 return seq->end_node == iter;
203 GCompareDataFunc cmp_func;
205 GSequenceNode *end_node;
208 /* This function compares two iters using a normal compare
209 * function and user_data passed in in a SortInfo struct
212 iter_compare (GSequenceIter *node1,
213 GSequenceIter *node2,
216 const SortInfo *info = data;
219 if (node1 == info->end_node)
222 if (node2 == info->end_node)
225 retval = info->cmp_func (node1->data, node2->data, info->cmp_data);
236 * @data_destroy: a #GDestroyNotify function, or %NULL
238 * Creates a new GSequence. The @data_destroy function, if non-%NULL will
239 * be called on all items when the sequence is destroyed and on items that
240 * are removed from the sequence.
242 * Return value: a new #GSequence
247 g_sequence_new (GDestroyNotify data_destroy)
249 GSequence *seq = g_new (GSequence, 1);
250 seq->data_destroy_notify = data_destroy;
252 seq->end_node = node_new (seq);
254 seq->access_prohibited = FALSE;
256 seq->real_sequence = seq;
265 * Frees the memory allocated for @seq. If @seq has a data destroy
266 * function associated with it, that function is called on all items in
272 g_sequence_free (GSequence *seq)
274 g_return_if_fail (seq != NULL);
276 check_seq_access (seq);
278 node_free (seq->end_node, seq);
284 * g_sequence_foreach_range:
285 * @begin: a #GSequenceIter
286 * @end: a #GSequenceIter
288 * @user_data: user data passed to @func
290 * Calls @func for each item in the range (@begin, @end) passing
291 * @user_data to the function.
296 g_sequence_foreach_range (GSequenceIter *begin,
304 g_return_if_fail (func != NULL);
305 g_return_if_fail (begin != NULL);
306 g_return_if_fail (end != NULL);
308 seq = get_sequence (begin);
310 seq->access_prohibited = TRUE;
315 GSequenceIter *next = node_get_next (iter);
317 func (iter->data, user_data);
322 seq->access_prohibited = FALSE;
326 * g_sequence_foreach:
328 * @func: the function to call for each item in @seq
329 * @user_data: user data passed to @func
331 * Calls @func for each item in the sequence passing @user_data
337 g_sequence_foreach (GSequence *seq,
341 GSequenceIter *begin, *end;
343 check_seq_access (seq);
345 begin = g_sequence_get_begin_iter (seq);
346 end = g_sequence_get_end_iter (seq);
348 g_sequence_foreach_range (begin, end, func, user_data);
352 * g_sequence_range_get_midpoint:
353 * @begin: a #GSequenceIter
354 * @end: a #GSequenceIter
356 * Finds an iterator somewhere in the range (@begin, @end). This
357 * iterator will be close to the middle of the range, but is not
358 * guaranteed to be <emphasis>exactly</emphasis> in the middle.
360 * The @begin and @end iterators must both point to the same sequence and
361 * @begin must come before or be equal to @end in the sequence.
363 * Return value: A #GSequenceIter pointing somewhere in the
364 * (@begin, @end) range.
369 g_sequence_range_get_midpoint (GSequenceIter *begin,
372 int begin_pos, end_pos, mid_pos;
374 g_return_val_if_fail (begin != NULL, NULL);
375 g_return_val_if_fail (end != NULL, NULL);
376 g_return_val_if_fail (get_sequence (begin) == get_sequence (end), NULL);
378 begin_pos = node_get_pos (begin);
379 end_pos = node_get_pos (end);
381 g_return_val_if_fail (end_pos >= begin_pos, NULL);
383 mid_pos = begin_pos + (end_pos - begin_pos) / 2;
385 return node_get_by_pos (begin, mid_pos);
389 * g_sequence_iter_compare:
390 * @a: a #GSequenceIter
391 * @b: a #GSequenceIter
393 * Returns a negative number if @a comes before @b, 0 if they are equal,
394 * and a positive number if @a comes after @b.
396 * The @a and @b iterators must point into the same sequence.
398 * Return value: A negative number if @a comes before @b, 0 if they are
399 * equal, and a positive number if @a comes after @b.
404 g_sequence_iter_compare (GSequenceIter *a,
409 g_return_val_if_fail (a != NULL, 0);
410 g_return_val_if_fail (b != NULL, 0);
411 g_return_val_if_fail (get_sequence (a) == get_sequence (b), 0);
413 check_iter_access (a);
414 check_iter_access (b);
416 a_pos = node_get_pos (a);
417 b_pos = node_get_pos (b);
421 else if (a_pos > b_pos)
429 * @seq: a #GSequencePointer
430 * @data: the data for the new item
432 * Adds a new item to the end of @seq.
434 * Return value: an iterator pointing to the new item
439 g_sequence_append (GSequence *seq,
444 g_return_val_if_fail (seq != NULL, NULL);
446 check_seq_access (seq);
448 node = node_new (data);
449 node_insert_before (seq->end_node, node);
455 * g_sequence_prepend:
457 * @data: the data for the new item
459 * Adds a new item to the front of @seq
461 * Return value: an iterator pointing to the new item
466 g_sequence_prepend (GSequence *seq,
469 GSequenceNode *node, *first;
471 g_return_val_if_fail (seq != NULL, NULL);
473 check_seq_access (seq);
475 node = node_new (data);
476 first = node_get_first (seq->end_node);
478 node_insert_before (first, node);
484 * g_sequence_insert_before:
485 * @iter: a #GSequenceIter
486 * @data: the data for the new item
488 * Inserts a new item just before the item pointed to by @iter.
490 * Return value: an iterator pointing to the new item
495 g_sequence_insert_before (GSequenceIter *iter,
500 g_return_val_if_fail (iter != NULL, NULL);
502 check_iter_access (iter);
504 node = node_new (data);
506 node_insert_before (iter, node);
513 * @iter: a #GSequenceIter
515 * Removes the item pointed to by @iter. It is an error to pass the
516 * end iterator to this function.
518 * If the sequnce has a data destroy function associated with it, this
519 * function is called on the data for the removed item.
524 g_sequence_remove (GSequenceIter *iter)
528 g_return_if_fail (iter != NULL);
529 g_return_if_fail (!is_end (iter));
531 check_iter_access (iter);
533 seq = get_sequence (iter);
536 node_free (iter, seq);
540 * g_sequence_remove_range:
541 * @begin: a #GSequenceIter
542 * @end: a #GSequenceIter
544 * Removes all items in the (@begin, @end) range.
546 * If the sequence has a data destroy function associated with it, this
547 * function is called on the data for the removed items.
552 g_sequence_remove_range (GSequenceIter *begin,
555 g_return_if_fail (get_sequence (begin) == get_sequence (end));
557 check_iter_access (begin);
558 check_iter_access (end);
560 g_sequence_move_range (NULL, begin, end);
564 * g_sequence_move_range:
565 * @dest: a #GSequenceIter
566 * @begin: a #GSequenceIter
567 * @end: a #GSequenceIter
569 * Inserts the (@begin, @end) range at the destination pointed to by ptr.
570 * The @begin and @end iters must point into the same sequence. It is
571 * allowed for @dest to point to a different sequence than the one pointed
572 * into by @begin and @end.
574 * If @dest is NULL, the range indicated by @begin and @end is
575 * removed from the sequence. If @dest iter points to a place within
576 * the (@begin, @end) range, the range does not move.
581 g_sequence_move_range (GSequenceIter *dest,
582 GSequenceIter *begin,
586 GSequenceNode *first;
588 g_return_if_fail (begin != NULL);
589 g_return_if_fail (end != NULL);
591 check_iter_access (begin);
592 check_iter_access (end);
594 check_iter_access (dest);
596 src_seq = get_sequence (begin);
598 g_return_if_fail (src_seq == get_sequence (end));
600 /* Dest points to begin or end? */
601 if (dest == begin || dest == end)
604 /* begin comes after end? */
605 if (g_sequence_iter_compare (begin, end) >= 0)
608 /* dest points somewhere in the (begin, end) range? */
609 if (dest && get_sequence (dest) == src_seq &&
610 g_sequence_iter_compare (dest, begin) > 0 &&
611 g_sequence_iter_compare (dest, end) < 0)
616 src_seq = get_sequence (begin);
618 first = node_get_first (begin);
625 node_join (first, end);
629 first = node_get_first (dest);
633 node_join (begin, dest);
636 node_join (first, begin);
640 node_free (begin, src_seq);
647 * @cmp_func: the #GCompareDataFunc used to sort @seq. This function is
648 * passed two items of @seq and should return 0 if they are equal,
649 * a negative value if the first comes before the second, and a
650 * positive value if the second comes before the first.
651 * @cmp_data: user data passed to @cmp_func
653 * Sorts @seq using @cmp_func.
658 g_sequence_sort (GSequence *seq,
659 GCompareDataFunc cmp_func,
664 info.cmp_func = cmp_func;
665 info.cmp_data = cmp_data;
666 info.end_node = seq->end_node;
668 check_seq_access (seq);
670 g_sequence_sort_iter (seq, iter_compare, &info);
674 * g_sequence_insert_sorted:
676 * @data: the data to insert
677 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
678 * is called with two items of the @seq and @user_data. It should
679 * return 0 if the items are equal, a negative value if the first
680 * item comes before the second, and a positive value if the second
681 * item comes before the first.
682 * @cmp_data: user data passed to @cmp_func.
684 * Inserts @data into @sequence using @func to determine the new position.
685 * The sequence must already be sorted according to @cmp_func; otherwise the
686 * new position of @data is undefined.
688 * Return value: a #GSequenceIter pointing to the new item.
693 g_sequence_insert_sorted (GSequence *seq,
695 GCompareDataFunc cmp_func,
700 g_return_val_if_fail (seq != NULL, NULL);
701 g_return_val_if_fail (cmp_func != NULL, NULL);
703 info.cmp_func = cmp_func;
704 info.cmp_data = cmp_data;
705 info.end_node = seq->end_node;
706 check_seq_access (seq);
708 return g_sequence_insert_sorted_iter (seq, data, iter_compare, &info);
712 * g_sequence_sort_changed:
713 * @iter: A #GSequenceIter
714 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
715 * is called with two items of the @seq and @user_data. It should
716 * return 0 if the items are equal, a negative value if the first
717 * item comes before the second, and a positive value if the second
718 * item comes before the first.
719 * @cmp_data: user data passed to @cmp_func.
721 * Moves the data pointed to a new position as indicated by @cmp_func. This
722 * function should be called for items in a sequence already sorted according
723 * to @cmp_func whenever some aspect of an item changes so that @cmp_func
724 * may return different values for that item.
729 g_sequence_sort_changed (GSequenceIter *iter,
730 GCompareDataFunc cmp_func,
735 g_return_if_fail (!is_end (iter));
737 info.cmp_func = cmp_func;
738 info.cmp_data = cmp_data;
739 info.end_node = get_sequence (iter)->end_node;
740 check_iter_access (iter);
742 g_sequence_sort_changed_iter (iter, iter_compare, &info);
748 * @data: data for the new item
749 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
750 * is called with two items of the @seq and @user_data. It should
751 * return 0 if the items are equal, a negative value if the first
752 * item comes before the second, and a positive value if the second
753 * item comes before the first.
754 * @cmp_data: user data passed to @cmp_func.
756 * Returns an iterator pointing to the position where @data would
757 * be inserted according to @cmp_func and @cmp_data.
759 * Return value: an #GSequenceIter pointing to the position where @data
760 * would have been inserted according to @cmp_func and @cmp_data.
765 g_sequence_search (GSequence *seq,
767 GCompareDataFunc cmp_func,
772 g_return_val_if_fail (seq != NULL, NULL);
774 info.cmp_func = cmp_func;
775 info.cmp_data = cmp_data;
776 info.end_node = seq->end_node;
777 check_seq_access (seq);
779 return g_sequence_search_iter (seq, data, iter_compare, &info);
783 * g_sequence_sort_iter:
785 * @cmp_func: the #GSequenceItercompare used to compare iterators in the
786 * sequence. It is called with two iterators pointing into @seq. It should
787 * return 0 if the iterators are equal, a negative value if the first
788 * iterator comes before the second, and a positive value if the second
789 * iterator comes before the first.
790 * @cmp_data: user data passed to @cmp_func
792 * Like g_sequence_sort(), but uses a #GSequenceIterCompareFunc instead
793 * of a GCompareDataFunc as the compare function
798 g_sequence_sort_iter (GSequence *seq,
799 GSequenceIterCompareFunc cmp_func,
803 GSequenceNode *begin, *end;
805 g_return_if_fail (seq != NULL);
806 g_return_if_fail (cmp_func != NULL);
808 check_seq_access (seq);
810 begin = g_sequence_get_begin_iter (seq);
811 end = g_sequence_get_end_iter (seq);
813 tmp = g_sequence_new (NULL);
814 tmp->real_sequence = seq;
816 g_sequence_move_range (g_sequence_get_begin_iter (tmp), begin, end);
818 seq->access_prohibited = TRUE;
819 tmp->access_prohibited = TRUE;
821 while (g_sequence_get_length (tmp) > 0)
823 GSequenceNode *node = g_sequence_get_begin_iter (tmp);
825 node_insert_sorted (seq->end_node, node, seq->end_node,
829 tmp->access_prohibited = FALSE;
830 seq->access_prohibited = FALSE;
832 g_sequence_free (tmp);
836 * g_sequence_sort_changed_iter:
837 * @iter: a #GSequenceIter
838 * @iter_cmp: the #GSequenceItercompare used to compare iterators in the
839 * sequence. It is called with two iterators pointing into @seq. It should
840 * return 0 if the iterators are equal, a negative value if the first
841 * iterator comes before the second, and a positive value if the second
842 * iterator comes before the first.
843 * @cmp_data: user data passed to @cmp_func
845 * Like g_sequence_sort_changed(), but uses
846 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
847 * the compare function.
852 g_sequence_sort_changed_iter (GSequenceIter *iter,
853 GSequenceIterCompareFunc iter_cmp,
856 GSequence *seq, *tmp_seq;
857 GSequenceIter *next, *prev;
859 g_return_if_fail (iter != NULL);
860 g_return_if_fail (!is_end (iter));
861 g_return_if_fail (iter_cmp != NULL);
862 check_iter_access (iter);
864 /* If one of the neighbours is equal to iter, then
865 * don't move it. This ensures that sort_changed() is
866 * a stable operation.
869 next = node_get_next (iter);
870 prev = node_get_prev (iter);
872 if (prev != iter && iter_cmp (prev, iter, cmp_data) == 0)
875 if (!is_end (next) && iter_cmp (next, iter, cmp_data) == 0)
878 seq = get_sequence (iter);
880 seq->access_prohibited = TRUE;
882 tmp_seq = g_sequence_new (NULL);
883 tmp_seq->real_sequence = seq;
886 node_insert_before (tmp_seq->end_node, iter);
888 node_insert_sorted (seq->end_node, iter, seq->end_node,
891 g_sequence_free (tmp_seq);
893 seq->access_prohibited = FALSE;
897 * g_sequence_insert_sorted_iter:
899 * @data: data for the new item
900 * @iter_cmp: the #GSequenceItercompare used to compare iterators in the
901 * sequence. It is called with two iterators pointing into @seq. It should
902 * return 0 if the iterators are equal, a negative value if the first
903 * iterator comes before the second, and a positive value if the second
904 * iterator comes before the first.
905 * @cmp_data: user data passed to @cmp_func
907 * Like g_sequence_insert_sorted(), but uses
908 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
909 * the compare function.
911 * Return value: a #GSequenceIter pointing to the new item
916 g_sequence_insert_sorted_iter (GSequence *seq,
918 GSequenceIterCompareFunc iter_cmp,
921 GSequenceNode *new_node;
924 g_return_val_if_fail (seq != NULL, NULL);
925 g_return_val_if_fail (iter_cmp != NULL, NULL);
927 check_seq_access (seq);
929 seq->access_prohibited = TRUE;
931 /* Create a new temporary sequence and put the new node into
932 * that. The reason for this is that the user compare function
933 * will be called with the new node, and if it dereferences,
934 * "is_end" will be called on it. But that will crash if the
935 * node is not actually in a sequence.
937 * node_insert_sorted() makes sure the node is unlinked before
940 * The reason we need the "iter" versions at all is that that
941 * is the only kind of compare functions GtkTreeView can use.
943 tmp_seq = g_sequence_new (NULL);
944 tmp_seq->real_sequence = seq;
946 new_node = g_sequence_append (tmp_seq, data);
948 node_insert_sorted (seq->end_node, new_node,
949 seq->end_node, iter_cmp, cmp_data);
951 g_sequence_free (tmp_seq);
953 seq->access_prohibited = FALSE;
959 * g_sequence_search_iter:
961 * @data: data for the new item
962 * @iter_cmp: the #GSequenceIterCompare function used to compare iterators
963 * in the sequence. It is called with two iterators pointing into @seq.
964 * It should return 0 if the iterators are equal, a negative value if the
965 * first iterator comes before the second, and a positive value if the
966 * second iterator comes before the first.
967 * @cmp_data: user data passed to @iter_cmp
969 * Like g_sequence_search(), but uses
970 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
971 * the compare function.
973 * Return value: a #GSequenceIter pointing to the position in @seq
974 * where @data would have been inserted according to @iter_cmp and @cmp_data.
979 g_sequence_search_iter (GSequence *seq,
981 GSequenceIterCompareFunc iter_cmp,
985 GSequenceNode *dummy;
988 g_return_val_if_fail (seq != NULL, NULL);
990 check_seq_access (seq);
992 seq->access_prohibited = TRUE;
994 tmp_seq = g_sequence_new (NULL);
995 tmp_seq->real_sequence = seq;
997 dummy = g_sequence_append (tmp_seq, data);
999 node = node_find_closest (seq->end_node, dummy,
1000 seq->end_node, iter_cmp, cmp_data);
1002 g_sequence_free (tmp_seq);
1004 seq->access_prohibited = FALSE;
1010 * g_sequence_iter_get_sequence:
1011 * @iter: a #GSequenceIter
1013 * Returns the #GSequence that @iter points into.
1015 * Return value: the #GSequence that @iter points into.
1020 g_sequence_iter_get_sequence (GSequenceIter *iter)
1024 g_return_val_if_fail (iter != NULL, NULL);
1026 seq = get_sequence (iter);
1028 /* For temporary sequences, this points to the sequence that
1029 * is actually being manipulated
1031 return seq->real_sequence;
1036 * @iter: a #GSequenceIter
1038 * Returns the data that @iter points to.
1040 * Return value: the data that @iter points to
1045 g_sequence_get (GSequenceIter *iter)
1047 g_return_val_if_fail (iter != NULL, NULL);
1048 g_return_val_if_fail (!is_end (iter), NULL);
1055 * @iter: a #GSequenceIter
1056 * @data: new data for the item
1058 * Changes the data for the item pointed to by @iter to be @data. If
1059 * the sequence has a data destroy function associated with it, that
1060 * function is called on the existing data that @iter pointed to.
1065 g_sequence_set (GSequenceIter *iter,
1070 g_return_if_fail (iter != NULL);
1071 g_return_if_fail (!is_end (iter));
1073 seq = get_sequence (iter);
1075 /* If @data is identical to iter->data, it is destroyed
1076 * here. This will work right in case of ref-counted objects. Also
1077 * it is similar to what ghashtables do.
1079 * For non-refcounted data it's a little less convenient, but
1080 * code relying on self-setting not destroying would be
1081 * pretty dubious anyway ...
1084 if (seq->data_destroy_notify)
1085 seq->data_destroy_notify (iter->data);
1091 * g_sequence_get_length:
1092 * @seq: a #GSequence
1094 * Returns the length of @seq
1096 * Return value: the length of @seq
1101 g_sequence_get_length (GSequence *seq)
1103 return node_get_length (seq->end_node) - 1;
1107 * g_sequence_get_end_iter:
1108 * @seq: a #GSequence
1110 * Returns the end iterator for @seg
1112 * Return value: the end iterator for @seq
1117 g_sequence_get_end_iter (GSequence *seq)
1119 g_return_val_if_fail (seq != NULL, NULL);
1121 return seq->end_node;
1125 * g_sequence_get_begin_iter:
1126 * @seq: a #GSequence
1128 * Returns the begin iterator for @seq.
1130 * Return value: the begin iterator for @seq.
1135 g_sequence_get_begin_iter (GSequence *seq)
1137 g_return_val_if_fail (seq != NULL, NULL);
1139 return node_get_first (seq->end_node);
1143 clamp_position (GSequence *seq,
1146 gint len = g_sequence_get_length (seq);
1148 if (pos > len || pos < 0)
1155 * if pos > number of items or -1, will return end pointer
1158 * g_sequence_get_iter_at_pos:
1159 * @seq: a #GSequence
1160 * @pos: a position in @seq, or -1 for the end.
1162 * Returns the iterator at position @pos. If @pos is negative or larger
1163 * than the number of items in @seq, the end iterator is returned.
1165 * Return value: The #GSequenceIter at position @pos
1170 g_sequence_get_iter_at_pos (GSequence *seq,
1173 g_return_val_if_fail (seq != NULL, NULL);
1175 pos = clamp_position (seq, pos);
1177 return node_get_by_pos (seq->end_node, pos);
1182 * @src: a #GSequenceIter pointing to the item to move
1183 * @dest: a #GSequenceIter pointing to the position to which
1184 * the item is moved.
1186 * Moves the item pointed to by @src to the position indicated by @dest.
1187 * After calling this function @dest will point to the position immediately
1188 * after @src. It is allowed for @src and @dest to point into different
1194 g_sequence_move (GSequenceIter *src,
1195 GSequenceIter *dest)
1197 g_return_if_fail (src != NULL);
1198 g_return_if_fail (dest != NULL);
1199 g_return_if_fail (!is_end (src));
1205 node_insert_before (dest, src);
1211 * g_sequence_iter_is_end:
1212 * @iter: a #GSequenceIter
1214 * Returns whether @iter is the end iterator
1216 * Return value: Whether @iter is the end iterator.
1221 g_sequence_iter_is_end (GSequenceIter *iter)
1223 g_return_val_if_fail (iter != NULL, FALSE);
1225 return is_end (iter);
1229 * g_sequence_iter_is_begin:
1230 * @iter: a #GSequenceIter
1232 * Returns whether @iter is the begin iterator
1234 * Return value: whether @iter is the begin iterator
1239 g_sequence_iter_is_begin (GSequenceIter *iter)
1241 g_return_val_if_fail (iter != NULL, FALSE);
1243 return (node_get_prev (iter) == iter);
1247 * g_sequence_iter_get_position:
1248 * @iter: a #GSequenceIter
1250 * Returns the position of @iter
1252 * Return value: the position of @iter
1257 g_sequence_iter_get_position (GSequenceIter *iter)
1259 g_return_val_if_fail (iter != NULL, -1);
1261 return node_get_pos (iter);
1265 * g_sequence_iter_next:
1266 * @iter: a #GSequenceIter
1268 * Returns an iterator pointing to the next position after @iter. If
1269 * @iter is the end iterator, the end iterator is returned.
1271 * Return value: a #GSequenceIter pointing to the next position after @iter.
1276 g_sequence_iter_next (GSequenceIter *iter)
1278 g_return_val_if_fail (iter != NULL, NULL);
1280 return node_get_next (iter);
1284 * g_sequence_iter_prev:
1285 * @iter: a #GSequenceIter
1287 * Returns an iterator pointing to the previous position before @iter. If
1288 * @iter is the begin iterator, the begin iterator is returned.
1290 * Return value: a #GSequenceIter pointing to the previous position before
1296 g_sequence_iter_prev (GSequenceIter *iter)
1298 g_return_val_if_fail (iter != NULL, NULL);
1300 return node_get_prev (iter);
1304 * g_sequence_iter_move:
1305 * @iter: a #GSequenceIter
1306 * @delta: A positive or negative number indicating how many positions away
1307 * from @iter the returned #GSequenceIter will be.
1309 * Returns the #GSequenceIter which is @delta positions away from @iter.
1310 * If @iter is closer than -@delta positions to the beginning of the sequence,
1311 * the begin iterator is returned. If @iter is closer than @delta positions
1312 * to the end of the sequence, the end iterator is returned.
1314 * Return value: a #GSequenceIter which is @delta positions away from @iter.
1319 g_sequence_iter_move (GSequenceIter *iter,
1324 g_return_val_if_fail (iter != NULL, NULL);
1326 new_pos = node_get_pos (iter) + delta;
1328 new_pos = clamp_position (get_sequence (iter), new_pos);
1330 return node_get_by_pos (iter, new_pos);
1335 * @a: a #GSequenceIter
1336 * @b: a #GSequenceIter
1338 * Swaps the items pointed to by @a and @b. It is allowed for @a and @b
1339 * to point into difference sequences.
1344 g_sequence_swap (GSequenceIter *a,
1347 GSequenceNode *leftmost, *rightmost, *rightmost_next;
1350 g_return_if_fail (!g_sequence_iter_is_end (a));
1351 g_return_if_fail (!g_sequence_iter_is_end (b));
1356 a_pos = g_sequence_iter_get_position (a);
1357 b_pos = g_sequence_iter_get_position (b);
1370 rightmost_next = node_get_next (rightmost);
1372 /* The situation is now like this:
1374 * ..., leftmost, ......., rightmost, rightmost_next, ...
1377 g_sequence_move (rightmost, leftmost);
1378 g_sequence_move (leftmost, rightmost_next);
1382 * Implementation of a treap
1387 get_priority (GSequenceNode *node)
1389 guint key = GPOINTER_TO_UINT (node);
1391 /* This hash function is based on one found on Thomas Wang's
1394 * http://www.concentric.net/~Ttwang/tech/inthash.htm
1397 key = (key << 15) - key - 1;
1398 key = key ^ (key >> 12);
1399 key = key + (key << 2);
1400 key = key ^ (key >> 4);
1401 key = key + (key << 3) + (key << 11);
1402 key = key ^ (key >> 16);
1404 /* We rely on 0 being less than all other priorities */
1405 return key? key : 1;
1408 static GSequenceNode *
1409 find_root (GSequenceNode *node)
1411 while (node->parent)
1412 node = node->parent;
1417 static GSequenceNode *
1418 node_new (gpointer data)
1420 GSequenceNode *node = g_slice_new0 (GSequenceNode);
1426 node->parent = NULL;
1431 static GSequenceNode *
1432 node_get_first (GSequenceNode *node)
1434 node = find_root (node);
1442 static GSequenceNode *
1443 node_get_last (GSequenceNode *node)
1445 node = find_root (node);
1453 #define NODE_LEFT_CHILD(n) (((n)->parent) && ((n)->parent->left) == (n))
1454 #define NODE_RIGHT_CHILD(n) (((n)->parent) && ((n)->parent->right) == (n))
1456 static GSequenceNode *
1457 node_get_next (GSequenceNode *node)
1459 GSequenceNode *n = node;
1469 while (NODE_RIGHT_CHILD (n))
1481 static GSequenceNode *
1482 node_get_prev (GSequenceNode *node)
1484 GSequenceNode *n = node;
1494 while (NODE_LEFT_CHILD (n))
1506 #define N_NODES(n) ((n)? (n)->n_nodes : 0)
1509 node_get_pos (GSequenceNode *node)
1514 n_smaller = node->left->n_nodes;
1518 if (NODE_RIGHT_CHILD (node))
1519 n_smaller += N_NODES (node->parent->left) + 1;
1521 node = node->parent;
1527 static GSequenceNode *
1528 node_get_by_pos (GSequenceNode *node,
1533 node = find_root (node);
1535 while ((i = N_NODES (node->left)) != pos)
1551 static GSequenceNode *
1552 node_find_closest (GSequenceNode *haystack,
1553 GSequenceNode *needle,
1555 GSequenceIterCompareFunc iter_cmp,
1558 GSequenceNode *best;
1561 haystack = find_root (haystack);
1567 /* iter_cmp can't be passed the end node, since the function may
1570 if (haystack == end)
1573 c = iter_cmp (haystack, needle, cmp_data);
1575 /* In the following we don't break even if c == 0. Instaed we go on
1576 * searching along the 'bigger' nodes, so that we find the last one
1577 * that is equal to the needle.
1580 haystack = haystack->left;
1582 haystack = haystack->right;
1584 while (haystack != NULL);
1586 /* If the best node is smaller or equal to the data, then move one step
1587 * to the right to make sure the best one is strictly bigger than the data
1589 if (best != end && c <= 0)
1590 best = node_get_next (best);
1596 node_get_length (GSequenceNode *node)
1598 node = find_root (node);
1600 return node->n_nodes;
1604 real_node_free (GSequenceNode *node,
1609 real_node_free (node->left, seq);
1610 real_node_free (node->right, seq);
1612 if (seq && seq->data_destroy_notify && node != seq->end_node)
1613 seq->data_destroy_notify (node->data);
1615 g_slice_free (GSequenceNode, node);
1620 node_free (GSequenceNode *node,
1623 node = find_root (node);
1625 real_node_free (node, seq);
1629 node_update_fields (GSequenceNode *node)
1633 n_nodes += N_NODES (node->left);
1634 n_nodes += N_NODES (node->right);
1636 node->n_nodes = n_nodes;
1640 node_rotate (GSequenceNode *node)
1642 GSequenceNode *tmp, *old;
1644 g_assert (node->parent);
1645 g_assert (node->parent != node);
1647 if (NODE_LEFT_CHILD (node))
1652 node->right = node->parent;
1653 node->parent = node->parent->parent;
1656 if (node->parent->left == node->right)
1657 node->parent->left = node;
1659 node->parent->right = node;
1662 g_assert (node->right);
1664 node->right->parent = node;
1665 node->right->left = tmp;
1667 if (node->right->left)
1668 node->right->left->parent = node->right;
1677 node->left = node->parent;
1678 node->parent = node->parent->parent;
1681 if (node->parent->right == node->left)
1682 node->parent->right = node;
1684 node->parent->left = node;
1687 g_assert (node->left);
1689 node->left->parent = node;
1690 node->left->right = tmp;
1692 if (node->left->right)
1693 node->left->right->parent = node->left;
1698 node_update_fields (old);
1699 node_update_fields (node);
1703 node_update_fields_deep (GSequenceNode *node)
1707 node_update_fields (node);
1709 node_update_fields_deep (node->parent);
1714 rotate_down (GSequenceNode *node,
1719 left = node->left ? get_priority (node->left) : 0;
1720 right = node->right ? get_priority (node->right) : 0;
1722 while (priority < left || priority < right)
1725 node_rotate (node->left);
1727 node_rotate (node->right);
1729 left = node->left ? get_priority (node->left) : 0;
1730 right = node->right ? get_priority (node->right) : 0;
1735 node_cut (GSequenceNode *node)
1737 while (node->parent)
1741 node->left->parent = NULL;
1744 node_update_fields (node);
1746 rotate_down (node, get_priority (node));
1750 node_join (GSequenceNode *left,
1751 GSequenceNode *right)
1753 GSequenceNode *fake = node_new (NULL);
1755 fake->left = find_root (left);
1756 fake->right = find_root (right);
1757 fake->left->parent = fake;
1758 fake->right->parent = fake;
1760 node_update_fields (fake);
1764 node_free (fake, NULL);
1768 node_insert_before (GSequenceNode *node,
1771 new->left = node->left;
1773 new->left->parent = new;
1778 node_update_fields_deep (new);
1780 while (new->parent && get_priority (new) > get_priority (new->parent))
1783 rotate_down (new, get_priority (new));
1787 node_unlink (GSequenceNode *node)
1789 rotate_down (node, 0);
1791 if (NODE_RIGHT_CHILD (node))
1792 node->parent->right = NULL;
1793 else if (NODE_LEFT_CHILD (node))
1794 node->parent->left = NULL;
1797 node_update_fields_deep (node->parent);
1799 node->parent = NULL;
1803 node_insert_sorted (GSequenceNode *node,
1806 GSequenceIterCompareFunc iter_cmp,
1809 GSequenceNode *closest;
1811 closest = node_find_closest (node, new, end, iter_cmp, cmp_data);
1815 node_insert_before (closest, new);