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.1 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, see <http://www.gnu.org/licenses/>.
21 #include "gsequence.h"
24 #include "gtestutils.h"
29 * @short_description: scalable lists
31 * The #GSequence data structure has the API of a list, but is
32 * implemented internally with a balanced binary tree. This means that
33 * it is possible to maintain a sorted list of n elements in time O(n log n).
34 * The data contained in each element can be either integer values, by using
35 * of the [Type Conversion Macros][glib-Type-Conversion-Macros], or simply
36 * pointers to any type of data.
38 * A #GSequence is accessed through "iterators", represented by a
39 * #GSequenceIter. An iterator represents a position between two
40 * elements of the sequence. For example, the "begin" iterator
41 * represents the gap immediately before the first element of the
42 * sequence, and the "end" iterator represents the gap immediately
43 * after the last element. In an empty sequence, the begin and end
44 * iterators are the same.
46 * Some methods on #GSequence operate on ranges of items. For example
47 * g_sequence_foreach_range() will call a user-specified function on
48 * each element with the given range. The range is delimited by the
49 * gaps represented by the passed-in iterators, so if you pass in the
50 * begin and end iterators, the range in question is the entire
53 * The function g_sequence_get() is used with an iterator to access the
54 * element immediately following the gap that the iterator represents.
55 * The iterator is said to "point" to that element.
57 * Iterators are stable across most operations on a #GSequence. For
58 * example an iterator pointing to some element of a sequence will
59 * continue to point to that element even after the sequence is sorted.
60 * Even moving an element to another sequence using for example
61 * g_sequence_move_range() will not invalidate the iterators pointing
62 * to it. The only operation that will invalidate an iterator is when
63 * the element it points to is removed from any sequence.
65 * To sort the data, either use g_sequence_insert_sorted() or
66 * g_sequence_insert_sorted_iter() to add data to the #GSequence or, if
67 * you want to add a large amount of data, it is more efficient to call
68 * g_sequence_sort() or g_sequence_sort_iter() after doing unsorted
75 * The #GSequenceIter struct is an opaque data type representing an
76 * iterator pointing into a #GSequence.
80 * GSequenceIterCompareFunc:
81 * @a: a #GSequenceIter
82 * @b: a #GSequenceIter
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.
89 * Returns: zero if the iterators are equal, a negative value if @a
90 * comes before @b, and a positive value if @b comes before @a.
93 typedef struct _GSequenceNode GSequenceNode
;
98 * The #GSequence struct is an opaque data type representing a
99 * [sequence][glib-Sequences] data type.
103 GSequenceNode
* end_node
;
104 GDestroyNotify data_destroy_notify
;
105 gboolean access_prohibited
;
107 /* The 'real_sequence' is used when temporary sequences are created
108 * to hold nodes that are being rearranged. The 'real_sequence' of such
109 * a temporary sequence points to the sequence that is actually being
110 * manipulated. The only reason we need this is so that when the
111 * sort/sort_changed/search_iter() functions call out to the application
112 * g_sequence_iter_get_sequence() will return the correct sequence.
114 GSequence
* real_sequence
;
117 struct _GSequenceNode
120 GSequenceNode
* parent
;
121 GSequenceNode
* left
;
122 GSequenceNode
* right
;
123 gpointer data
; /* For the end node, this field points
129 * Declaration of GSequenceNode methods
131 static GSequenceNode
*node_new (gpointer data
);
132 static GSequenceNode
*node_get_first (GSequenceNode
*node
);
133 static GSequenceNode
*node_get_last (GSequenceNode
*node
);
134 static GSequenceNode
*node_get_prev (GSequenceNode
*node
);
135 static GSequenceNode
*node_get_next (GSequenceNode
*node
);
136 static gint
node_get_pos (GSequenceNode
*node
);
137 static GSequenceNode
*node_get_by_pos (GSequenceNode
*node
,
139 static GSequenceNode
*node_find (GSequenceNode
*haystack
,
140 GSequenceNode
*needle
,
142 GSequenceIterCompareFunc cmp
,
144 static GSequenceNode
*node_find_closest (GSequenceNode
*haystack
,
145 GSequenceNode
*needle
,
147 GSequenceIterCompareFunc cmp
,
149 static gint
node_get_length (GSequenceNode
*node
);
150 static void node_free (GSequenceNode
*node
,
152 static void node_cut (GSequenceNode
*split
);
153 static void node_insert_before (GSequenceNode
*node
,
155 static void node_unlink (GSequenceNode
*node
);
156 static void node_join (GSequenceNode
*left
,
157 GSequenceNode
*right
);
158 static void node_insert_sorted (GSequenceNode
*node
,
161 GSequenceIterCompareFunc cmp_func
,
166 * Various helper functions
169 check_seq_access (GSequence
*seq
)
171 if (G_UNLIKELY (seq
->access_prohibited
))
173 g_warning ("Accessing a sequence while it is "
174 "being sorted or searched is not allowed");
179 get_sequence (GSequenceNode
*node
)
181 return (GSequence
*)node_get_last (node
)->data
;
185 seq_is_end (GSequence
*seq
,
188 return seq
->end_node
== iter
;
192 is_end (GSequenceIter
*iter
)
194 GSequenceIter
*parent
= iter
->parent
;
202 while (parent
->right
== iter
)
205 parent
= iter
->parent
;
216 GCompareDataFunc cmp_func
;
218 GSequenceNode
*end_node
;
221 /* This function compares two iters using a normal compare
222 * function and user_data passed in in a SortInfo struct
225 iter_compare (GSequenceIter
*node1
,
226 GSequenceIter
*node2
,
229 const SortInfo
*info
= data
;
232 if (node1
== info
->end_node
)
235 if (node2
== info
->end_node
)
238 retval
= info
->cmp_func (node1
->data
, node2
->data
, info
->cmp_data
);
249 * @data_destroy: (nullable): a #GDestroyNotify function, or %NULL
251 * Creates a new GSequence. The @data_destroy function, if non-%NULL will
252 * be called on all items when the sequence is destroyed and on items that
253 * are removed from the sequence.
255 * Returns: (transfer full): a new #GSequence
260 g_sequence_new (GDestroyNotify data_destroy
)
262 GSequence
*seq
= g_new (GSequence
, 1);
263 seq
->data_destroy_notify
= data_destroy
;
265 seq
->end_node
= node_new (seq
);
267 seq
->access_prohibited
= FALSE
;
269 seq
->real_sequence
= seq
;
278 * Frees the memory allocated for @seq. If @seq has a data destroy
279 * function associated with it, that function is called on all items
285 g_sequence_free (GSequence
*seq
)
287 g_return_if_fail (seq
!= NULL
);
289 check_seq_access (seq
);
291 node_free (seq
->end_node
, seq
);
297 * g_sequence_foreach_range:
298 * @begin: a #GSequenceIter
299 * @end: a #GSequenceIter
301 * @user_data: user data passed to @func
303 * Calls @func for each item in the range (@begin, @end) passing
304 * @user_data to the function. @func must not modify the sequence
310 g_sequence_foreach_range (GSequenceIter
*begin
,
318 g_return_if_fail (func
!= NULL
);
319 g_return_if_fail (begin
!= NULL
);
320 g_return_if_fail (end
!= NULL
);
322 seq
= get_sequence (begin
);
324 seq
->access_prohibited
= TRUE
;
329 GSequenceIter
*next
= node_get_next (iter
);
331 func (iter
->data
, user_data
);
336 seq
->access_prohibited
= FALSE
;
340 * g_sequence_foreach:
342 * @func: the function to call for each item in @seq
343 * @user_data: user data passed to @func
345 * Calls @func for each item in the sequence passing @user_data
346 * to the function. @func must not modify the sequence itself.
351 g_sequence_foreach (GSequence
*seq
,
355 GSequenceIter
*begin
, *end
;
357 check_seq_access (seq
);
359 begin
= g_sequence_get_begin_iter (seq
);
360 end
= g_sequence_get_end_iter (seq
);
362 g_sequence_foreach_range (begin
, end
, func
, user_data
);
366 * g_sequence_range_get_midpoint:
367 * @begin: a #GSequenceIter
368 * @end: a #GSequenceIter
370 * Finds an iterator somewhere in the range (@begin, @end). This
371 * iterator will be close to the middle of the range, but is not
372 * guaranteed to be exactly in the middle.
374 * The @begin and @end iterators must both point to the same sequence
375 * and @begin must come before or be equal to @end in the sequence.
377 * Returns: (transfer none): a #GSequenceIter pointing somewhere in the
378 * (@begin, @end) range
383 g_sequence_range_get_midpoint (GSequenceIter
*begin
,
386 int begin_pos
, end_pos
, mid_pos
;
388 g_return_val_if_fail (begin
!= NULL
, NULL
);
389 g_return_val_if_fail (end
!= NULL
, NULL
);
390 g_return_val_if_fail (get_sequence (begin
) == get_sequence (end
), NULL
);
392 begin_pos
= node_get_pos (begin
);
393 end_pos
= node_get_pos (end
);
395 g_return_val_if_fail (end_pos
>= begin_pos
, NULL
);
397 mid_pos
= begin_pos
+ (end_pos
- begin_pos
) / 2;
399 return node_get_by_pos (begin
, mid_pos
);
403 * g_sequence_iter_compare:
404 * @a: a #GSequenceIter
405 * @b: a #GSequenceIter
407 * Returns a negative number if @a comes before @b, 0 if they are equal,
408 * and a positive number if @a comes after @b.
410 * The @a and @b iterators must point into the same sequence.
412 * Returns: a negative number if @a comes before @b, 0 if they are
413 * equal, and a positive number if @a comes after @b
418 g_sequence_iter_compare (GSequenceIter
*a
,
422 GSequence
*seq_a
, *seq_b
;
424 g_return_val_if_fail (a
!= NULL
, 0);
425 g_return_val_if_fail (b
!= NULL
, 0);
427 seq_a
= get_sequence (a
);
428 seq_b
= get_sequence (b
);
429 g_return_val_if_fail (seq_a
== seq_b
, 0);
431 check_seq_access (seq_a
);
432 check_seq_access (seq_b
);
434 a_pos
= node_get_pos (a
);
435 b_pos
= node_get_pos (b
);
439 else if (a_pos
> b_pos
)
448 * @data: the data for the new item
450 * Adds a new item to the end of @seq.
452 * Returns: (transfer none): an iterator pointing to the new item
457 g_sequence_append (GSequence
*seq
,
462 g_return_val_if_fail (seq
!= NULL
, NULL
);
464 check_seq_access (seq
);
466 node
= node_new (data
);
467 node_insert_before (seq
->end_node
, node
);
473 * g_sequence_prepend:
475 * @data: the data for the new item
477 * Adds a new item to the front of @seq
479 * Returns: (transfer none): an iterator pointing to the new item
484 g_sequence_prepend (GSequence
*seq
,
487 GSequenceNode
*node
, *first
;
489 g_return_val_if_fail (seq
!= NULL
, NULL
);
491 check_seq_access (seq
);
493 node
= node_new (data
);
494 first
= node_get_first (seq
->end_node
);
496 node_insert_before (first
, node
);
502 * g_sequence_insert_before:
503 * @iter: a #GSequenceIter
504 * @data: the data for the new item
506 * Inserts a new item just before the item pointed to by @iter.
508 * Returns: (transfer none): an iterator pointing to the new item
513 g_sequence_insert_before (GSequenceIter
*iter
,
519 g_return_val_if_fail (iter
!= NULL
, NULL
);
521 seq
= get_sequence (iter
);
522 check_seq_access (seq
);
524 node
= node_new (data
);
526 node_insert_before (iter
, node
);
533 * @iter: a #GSequenceIter
535 * Removes the item pointed to by @iter. It is an error to pass the
536 * end iterator to this function.
538 * If the sequence has a data destroy function associated with it, this
539 * function is called on the data for the removed item.
544 g_sequence_remove (GSequenceIter
*iter
)
548 g_return_if_fail (iter
!= NULL
);
550 seq
= get_sequence (iter
);
551 g_return_if_fail (!seq_is_end (seq
, iter
));
553 check_seq_access (seq
);
556 node_free (iter
, seq
);
560 * g_sequence_remove_range:
561 * @begin: a #GSequenceIter
562 * @end: a #GSequenceIter
564 * Removes all items in the (@begin, @end) range.
566 * If the sequence has a data destroy function associated with it, this
567 * function is called on the data for the removed items.
572 g_sequence_remove_range (GSequenceIter
*begin
,
575 GSequence
*seq_begin
, *seq_end
;
577 seq_begin
= get_sequence (begin
);
578 seq_end
= get_sequence (end
);
579 g_return_if_fail (seq_begin
== seq_end
);
580 /* check_seq_access() calls are done by g_sequence_move_range() */
582 g_sequence_move_range (NULL
, begin
, end
);
586 * g_sequence_move_range:
587 * @dest: a #GSequenceIter
588 * @begin: a #GSequenceIter
589 * @end: a #GSequenceIter
591 * Inserts the (@begin, @end) range at the destination pointed to by @dest.
592 * The @begin and @end iters must point into the same sequence. It is
593 * allowed for @dest to point to a different sequence than the one pointed
594 * into by @begin and @end.
596 * If @dest is %NULL, the range indicated by @begin and @end is
597 * removed from the sequence. If @dest points to a place within
598 * the (@begin, @end) range, the range does not move.
603 g_sequence_move_range (GSequenceIter
*dest
,
604 GSequenceIter
*begin
,
607 GSequence
*src_seq
, *end_seq
, *dest_seq
;
608 GSequenceNode
*first
;
610 g_return_if_fail (begin
!= NULL
);
611 g_return_if_fail (end
!= NULL
);
613 src_seq
= get_sequence (begin
);
614 check_seq_access (src_seq
);
616 end_seq
= get_sequence (end
);
617 check_seq_access (end_seq
);
621 dest_seq
= get_sequence (dest
);
622 check_seq_access (dest_seq
);
625 g_return_if_fail (src_seq
== end_seq
);
627 /* Dest points to begin or end? */
628 if (dest
== begin
|| dest
== end
)
631 /* begin comes after end? */
632 if (g_sequence_iter_compare (begin
, end
) >= 0)
635 /* dest points somewhere in the (begin, end) range? */
636 if (dest
&& dest_seq
== src_seq
&&
637 g_sequence_iter_compare (dest
, begin
) > 0 &&
638 g_sequence_iter_compare (dest
, end
) < 0)
643 first
= node_get_first (begin
);
650 node_join (first
, end
);
654 first
= node_get_first (dest
);
658 node_join (begin
, dest
);
661 node_join (first
, begin
);
665 node_free (begin
, src_seq
);
672 * @cmp_func: the function used to sort the sequence
673 * @cmp_data: user data passed to @cmp_func
675 * Sorts @seq using @cmp_func.
677 * @cmp_func is passed two items of @seq and should
678 * return 0 if they are equal, a negative value if the
679 * first comes before the second, and a positive value
680 * if the second comes before the first.
685 g_sequence_sort (GSequence
*seq
,
686 GCompareDataFunc cmp_func
,
691 info
.cmp_func
= cmp_func
;
692 info
.cmp_data
= cmp_data
;
693 info
.end_node
= seq
->end_node
;
695 check_seq_access (seq
);
697 g_sequence_sort_iter (seq
, iter_compare
, &info
);
701 * g_sequence_insert_sorted:
703 * @data: the data to insert
704 * @cmp_func: the function used to compare items in the sequence
705 * @cmp_data: user data passed to @cmp_func.
707 * Inserts @data into @seq using @cmp_func to determine the new
708 * position. The sequence must already be sorted according to @cmp_func;
709 * otherwise the new position of @data is undefined.
711 * @cmp_func is called with two items of the @seq, and @cmp_data.
712 * It should return 0 if the items are equal, a negative value
713 * if the first item comes before the second, and a positive value
714 * if the second item comes before the first.
716 * Note that when adding a large amount of data to a #GSequence,
717 * it is more efficient to do unsorted insertions and then call
718 * g_sequence_sort() or g_sequence_sort_iter().
720 * Returns: (transfer none): a #GSequenceIter pointing to the new item.
725 g_sequence_insert_sorted (GSequence
*seq
,
727 GCompareDataFunc cmp_func
,
732 g_return_val_if_fail (seq
!= NULL
, NULL
);
733 g_return_val_if_fail (cmp_func
!= NULL
, NULL
);
735 info
.cmp_func
= cmp_func
;
736 info
.cmp_data
= cmp_data
;
737 info
.end_node
= seq
->end_node
;
738 check_seq_access (seq
);
740 return g_sequence_insert_sorted_iter (seq
, data
, iter_compare
, &info
);
744 * g_sequence_sort_changed:
745 * @iter: A #GSequenceIter
746 * @cmp_func: the function used to compare items in the sequence
747 * @cmp_data: user data passed to @cmp_func.
749 * Moves the data pointed to by @iter to a new position as indicated by
751 * function should be called for items in a sequence already sorted according
752 * to @cmp_func whenever some aspect of an item changes so that @cmp_func
753 * may return different values for that item.
755 * @cmp_func is called with two items of the @seq, and @cmp_data.
756 * It should return 0 if the items are equal, a negative value if
757 * the first item comes before the second, and a positive value if
758 * the second item comes before the first.
763 g_sequence_sort_changed (GSequenceIter
*iter
,
764 GCompareDataFunc cmp_func
,
770 g_return_if_fail (iter
!= NULL
);
772 seq
= get_sequence (iter
);
773 /* check_seq_access() call is done by g_sequence_sort_changed_iter() */
774 g_return_if_fail (!seq_is_end (seq
, iter
));
776 info
.cmp_func
= cmp_func
;
777 info
.cmp_data
= cmp_data
;
778 info
.end_node
= seq
->end_node
;
780 g_sequence_sort_changed_iter (iter
, iter_compare
, &info
);
786 * @data: data for the new item
787 * @cmp_func: the function used to compare items in the sequence
788 * @cmp_data: user data passed to @cmp_func
790 * Returns an iterator pointing to the position where @data would
791 * be inserted according to @cmp_func and @cmp_data.
793 * @cmp_func is called with two items of the @seq, and @cmp_data.
794 * It should return 0 if the items are equal, a negative value if
795 * the first item comes before the second, and a positive value if
796 * the second item comes before the first.
798 * If you are simply searching for an existing element of the sequence,
799 * consider using g_sequence_lookup().
801 * This function will fail if the data contained in the sequence is
804 * Returns: (transfer none): an #GSequenceIter pointing to the position where @data
805 * would have been inserted according to @cmp_func and @cmp_data
810 g_sequence_search (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_search_iter (seq
, data
, iter_compare
, &info
);
830 * @data: data to lookup
831 * @cmp_func: the function used to compare items in the sequence
832 * @cmp_data: user data passed to @cmp_func
834 * Returns an iterator pointing to the position of the first item found
835 * equal to @data according to @cmp_func and @cmp_data. If more than one
836 * item is equal, it is not guaranteed that it is the first which is
837 * returned. In that case, you can use g_sequence_iter_next() and
838 * g_sequence_iter_prev() to get others.
840 * @cmp_func is called with two items of the @seq, and @cmp_data.
841 * It should return 0 if the items are equal, a negative value if
842 * the first item comes before the second, and a positive value if
843 * the second item comes before the first.
845 * This function will fail if the data contained in the sequence is
848 * Returns: (transfer none) (nullable): an #GSequenceIter pointing to the position of the
849 * first item found equal to @data according to @cmp_func and
850 * @cmp_data, or %NULL if no such item exists
855 g_sequence_lookup (GSequence
*seq
,
857 GCompareDataFunc cmp_func
,
862 g_return_val_if_fail (seq
!= NULL
, NULL
);
864 info
.cmp_func
= cmp_func
;
865 info
.cmp_data
= cmp_data
;
866 info
.end_node
= seq
->end_node
;
867 check_seq_access (seq
);
869 return g_sequence_lookup_iter (seq
, data
, iter_compare
, &info
);
873 * g_sequence_sort_iter:
875 * @cmp_func: the function used to compare iterators in the sequence
876 * @cmp_data: user data passed to @cmp_func
878 * Like g_sequence_sort(), but uses a #GSequenceIterCompareFunc instead
879 * of a #GCompareDataFunc as the compare function
881 * @cmp_func is called with two iterators pointing into @seq. It should
882 * return 0 if the iterators are equal, a negative value if the first
883 * iterator comes before the second, and a positive value if the second
884 * iterator comes before the first.
889 g_sequence_sort_iter (GSequence
*seq
,
890 GSequenceIterCompareFunc cmp_func
,
894 GSequenceNode
*begin
, *end
;
896 g_return_if_fail (seq
!= NULL
);
897 g_return_if_fail (cmp_func
!= NULL
);
899 check_seq_access (seq
);
901 begin
= g_sequence_get_begin_iter (seq
);
902 end
= g_sequence_get_end_iter (seq
);
904 tmp
= g_sequence_new (NULL
);
905 tmp
->real_sequence
= seq
;
907 g_sequence_move_range (g_sequence_get_begin_iter (tmp
), begin
, end
);
909 seq
->access_prohibited
= TRUE
;
910 tmp
->access_prohibited
= TRUE
;
912 while (!g_sequence_is_empty (tmp
))
914 GSequenceNode
*node
= g_sequence_get_begin_iter (tmp
);
916 node_insert_sorted (seq
->end_node
, node
, seq
->end_node
,
920 tmp
->access_prohibited
= FALSE
;
921 seq
->access_prohibited
= FALSE
;
923 g_sequence_free (tmp
);
927 * g_sequence_sort_changed_iter:
928 * @iter: a #GSequenceIter
929 * @iter_cmp: the function used to compare iterators in the sequence
930 * @cmp_data: user data passed to @cmp_func
932 * Like g_sequence_sort_changed(), but uses
933 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
934 * the compare function.
936 * @iter_cmp is called with two iterators pointing into the #GSequence that
937 * @iter points into. It should
938 * return 0 if the iterators are equal, a negative value if the first
939 * iterator comes before the second, and a positive value if the second
940 * iterator comes before the first.
945 g_sequence_sort_changed_iter (GSequenceIter
*iter
,
946 GSequenceIterCompareFunc iter_cmp
,
949 GSequence
*seq
, *tmp_seq
;
950 GSequenceIter
*next
, *prev
;
952 g_return_if_fail (iter
!= NULL
);
953 g_return_if_fail (iter_cmp
!= NULL
);
955 seq
= get_sequence (iter
);
956 g_return_if_fail (!seq_is_end (seq
, iter
));
958 check_seq_access (seq
);
960 /* If one of the neighbours is equal to iter, then
961 * don't move it. This ensures that sort_changed() is
962 * a stable operation.
965 next
= node_get_next (iter
);
966 prev
= node_get_prev (iter
);
968 if (prev
!= iter
&& iter_cmp (prev
, iter
, cmp_data
) == 0)
971 if (!is_end (next
) && iter_cmp (next
, iter
, cmp_data
) == 0)
974 seq
->access_prohibited
= TRUE
;
976 tmp_seq
= g_sequence_new (NULL
);
977 tmp_seq
->real_sequence
= seq
;
980 node_insert_before (tmp_seq
->end_node
, iter
);
982 node_insert_sorted (seq
->end_node
, iter
, seq
->end_node
,
985 g_sequence_free (tmp_seq
);
987 seq
->access_prohibited
= FALSE
;
991 * g_sequence_insert_sorted_iter:
993 * @data: data for the new item
994 * @iter_cmp: the function used to compare iterators in the sequence
995 * @cmp_data: user data passed to @iter_cmp
997 * Like g_sequence_insert_sorted(), but uses
998 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
999 * the compare function.
1001 * @iter_cmp is called with two iterators pointing into @seq.
1002 * It should return 0 if the iterators are equal, a negative
1003 * value if the first iterator comes before the second, and a
1004 * positive value if the second iterator comes before the first.
1006 * Note that when adding a large amount of data to a #GSequence,
1007 * it is more efficient to do unsorted insertions and then call
1008 * g_sequence_sort() or g_sequence_sort_iter().
1010 * Returns: (transfer none): a #GSequenceIter pointing to the new item
1015 g_sequence_insert_sorted_iter (GSequence
*seq
,
1017 GSequenceIterCompareFunc iter_cmp
,
1020 GSequenceNode
*new_node
;
1023 g_return_val_if_fail (seq
!= NULL
, NULL
);
1024 g_return_val_if_fail (iter_cmp
!= NULL
, NULL
);
1026 check_seq_access (seq
);
1028 seq
->access_prohibited
= TRUE
;
1030 /* Create a new temporary sequence and put the new node into
1031 * that. The reason for this is that the user compare function
1032 * will be called with the new node, and if it dereferences,
1033 * "is_end" will be called on it. But that will crash if the
1034 * node is not actually in a sequence.
1036 * node_insert_sorted() makes sure the node is unlinked before
1039 * The reason we need the "iter" versions at all is that that
1040 * is the only kind of compare functions GtkTreeView can use.
1042 tmp_seq
= g_sequence_new (NULL
);
1043 tmp_seq
->real_sequence
= seq
;
1045 new_node
= g_sequence_append (tmp_seq
, data
);
1047 node_insert_sorted (seq
->end_node
, new_node
,
1048 seq
->end_node
, iter_cmp
, cmp_data
);
1050 g_sequence_free (tmp_seq
);
1052 seq
->access_prohibited
= FALSE
;
1058 * g_sequence_search_iter:
1059 * @seq: a #GSequence
1060 * @data: data for the new item
1061 * @iter_cmp: the function used to compare iterators in the sequence
1062 * @cmp_data: user data passed to @iter_cmp
1064 * Like g_sequence_search(), but uses a #GSequenceIterCompareFunc
1065 * instead of a #GCompareDataFunc as the compare function.
1067 * @iter_cmp is called with two iterators pointing into @seq.
1068 * It should return 0 if the iterators are equal, a negative value
1069 * if the first iterator comes before the second, and a positive
1070 * value if the second iterator comes before the first.
1072 * If you are simply searching for an existing element of the sequence,
1073 * consider using g_sequence_lookup_iter().
1075 * This function will fail if the data contained in the sequence is
1078 * Returns: (transfer none): a #GSequenceIter pointing to the position in @seq
1079 * where @data would have been inserted according to @iter_cmp
1085 g_sequence_search_iter (GSequence
*seq
,
1087 GSequenceIterCompareFunc iter_cmp
,
1090 GSequenceNode
*node
;
1091 GSequenceNode
*dummy
;
1094 g_return_val_if_fail (seq
!= NULL
, NULL
);
1096 check_seq_access (seq
);
1098 seq
->access_prohibited
= TRUE
;
1100 tmp_seq
= g_sequence_new (NULL
);
1101 tmp_seq
->real_sequence
= seq
;
1103 dummy
= g_sequence_append (tmp_seq
, data
);
1105 node
= node_find_closest (seq
->end_node
, dummy
,
1106 seq
->end_node
, iter_cmp
, cmp_data
);
1108 g_sequence_free (tmp_seq
);
1110 seq
->access_prohibited
= FALSE
;
1116 * g_sequence_lookup_iter:
1117 * @seq: a #GSequence
1118 * @data: data to lookup
1119 * @iter_cmp: the function used to compare iterators in the sequence
1120 * @cmp_data: user data passed to @iter_cmp
1122 * Like g_sequence_lookup(), but uses a #GSequenceIterCompareFunc
1123 * instead of a #GCompareDataFunc as the compare function.
1125 * @iter_cmp is called with two iterators pointing into @seq.
1126 * It should return 0 if the iterators are equal, a negative value
1127 * if the first iterator comes before the second, and a positive
1128 * value if the second iterator comes before the first.
1130 * This function will fail if the data contained in the sequence is
1133 * Returns: (transfer none) (nullable): an #GSequenceIter pointing to the position of
1134 * the first item found equal to @data according to @iter_cmp
1135 * and @cmp_data, or %NULL if no such item exists
1140 g_sequence_lookup_iter (GSequence
*seq
,
1142 GSequenceIterCompareFunc iter_cmp
,
1145 GSequenceNode
*node
;
1146 GSequenceNode
*dummy
;
1149 g_return_val_if_fail (seq
!= NULL
, NULL
);
1151 check_seq_access (seq
);
1153 seq
->access_prohibited
= TRUE
;
1155 tmp_seq
= g_sequence_new (NULL
);
1156 tmp_seq
->real_sequence
= seq
;
1158 dummy
= g_sequence_append (tmp_seq
, data
);
1160 node
= node_find (seq
->end_node
, dummy
,
1161 seq
->end_node
, iter_cmp
, cmp_data
);
1163 g_sequence_free (tmp_seq
);
1165 seq
->access_prohibited
= FALSE
;
1171 * g_sequence_iter_get_sequence:
1172 * @iter: a #GSequenceIter
1174 * Returns the #GSequence that @iter points into.
1176 * Returns: (transfer none): the #GSequence that @iter points into
1181 g_sequence_iter_get_sequence (GSequenceIter
*iter
)
1185 g_return_val_if_fail (iter
!= NULL
, NULL
);
1187 seq
= get_sequence (iter
);
1189 /* For temporary sequences, this points to the sequence that
1190 * is actually being manipulated
1192 return seq
->real_sequence
;
1197 * @iter: a #GSequenceIter
1199 * Returns the data that @iter points to.
1201 * Returns: (transfer none): the data that @iter points to
1206 g_sequence_get (GSequenceIter
*iter
)
1208 g_return_val_if_fail (iter
!= NULL
, NULL
);
1209 g_return_val_if_fail (!is_end (iter
), NULL
);
1216 * @iter: a #GSequenceIter
1217 * @data: new data for the item
1219 * Changes the data for the item pointed to by @iter to be @data. If
1220 * the sequence has a data destroy function associated with it, that
1221 * function is called on the existing data that @iter pointed to.
1226 g_sequence_set (GSequenceIter
*iter
,
1231 g_return_if_fail (iter
!= NULL
);
1233 seq
= get_sequence (iter
);
1234 g_return_if_fail (!seq_is_end (seq
, iter
));
1236 /* If @data is identical to iter->data, it is destroyed
1237 * here. This will work right in case of ref-counted objects. Also
1238 * it is similar to what ghashtables do.
1240 * For non-refcounted data it's a little less convenient, but
1241 * code relying on self-setting not destroying would be
1242 * pretty dubious anyway ...
1245 if (seq
->data_destroy_notify
)
1246 seq
->data_destroy_notify (iter
->data
);
1252 * g_sequence_get_length:
1253 * @seq: a #GSequence
1255 * Returns the length of @seq. Note that this method is O(h) where `h' is the
1256 * height of the tree. It is thus more efficient to use g_sequence_is_empty()
1257 * when comparing the length to zero.
1259 * Returns: the length of @seq
1264 g_sequence_get_length (GSequence
*seq
)
1266 return node_get_length (seq
->end_node
) - 1;
1270 * g_sequence_is_empty:
1271 * @seq: a #GSequence
1273 * Returns %TRUE if the sequence contains zero items.
1275 * This function is functionally identical to checking the result of
1276 * g_sequence_get_length() being equal to zero. However this function is
1277 * implemented in O(1) running time.
1279 * Returns: %TRUE if the sequence is empty, otherwise %FALSE.
1284 g_sequence_is_empty (GSequence
*seq
)
1286 return (seq
->end_node
->parent
== NULL
) && (seq
->end_node
->left
== NULL
);
1290 * g_sequence_get_end_iter:
1291 * @seq: a #GSequence
1293 * Returns the end iterator for @seg
1295 * Returns: (transfer none): the end iterator for @seq
1300 g_sequence_get_end_iter (GSequence
*seq
)
1302 g_return_val_if_fail (seq
!= NULL
, NULL
);
1304 return seq
->end_node
;
1308 * g_sequence_get_begin_iter:
1309 * @seq: a #GSequence
1311 * Returns the begin iterator for @seq.
1313 * Returns: (transfer none): the begin iterator for @seq.
1318 g_sequence_get_begin_iter (GSequence
*seq
)
1320 g_return_val_if_fail (seq
!= NULL
, NULL
);
1322 return node_get_first (seq
->end_node
);
1326 clamp_position (GSequence
*seq
,
1329 gint len
= g_sequence_get_length (seq
);
1331 if (pos
> len
|| pos
< 0)
1338 * g_sequence_get_iter_at_pos:
1339 * @seq: a #GSequence
1340 * @pos: a position in @seq, or -1 for the end
1342 * Returns the iterator at position @pos. If @pos is negative or larger
1343 * than the number of items in @seq, the end iterator is returned.
1345 * Returns: (transfer none): The #GSequenceIter at position @pos
1350 g_sequence_get_iter_at_pos (GSequence
*seq
,
1353 g_return_val_if_fail (seq
!= NULL
, NULL
);
1355 pos
= clamp_position (seq
, pos
);
1357 return node_get_by_pos (seq
->end_node
, pos
);
1362 * @src: a #GSequenceIter pointing to the item to move
1363 * @dest: a #GSequenceIter pointing to the position to which
1366 * Moves the item pointed to by @src to the position indicated by @dest.
1367 * After calling this function @dest will point to the position immediately
1368 * after @src. It is allowed for @src and @dest to point into different
1374 g_sequence_move (GSequenceIter
*src
,
1375 GSequenceIter
*dest
)
1377 g_return_if_fail (src
!= NULL
);
1378 g_return_if_fail (dest
!= NULL
);
1379 g_return_if_fail (!is_end (src
));
1385 node_insert_before (dest
, src
);
1391 * g_sequence_iter_is_end:
1392 * @iter: a #GSequenceIter
1394 * Returns whether @iter is the end iterator
1396 * Returns: Whether @iter is the end iterator
1401 g_sequence_iter_is_end (GSequenceIter
*iter
)
1403 g_return_val_if_fail (iter
!= NULL
, FALSE
);
1405 return is_end (iter
);
1409 * g_sequence_iter_is_begin:
1410 * @iter: a #GSequenceIter
1412 * Returns whether @iter is the begin iterator
1414 * Returns: whether @iter is the begin iterator
1419 g_sequence_iter_is_begin (GSequenceIter
*iter
)
1421 g_return_val_if_fail (iter
!= NULL
, FALSE
);
1423 return (node_get_prev (iter
) == iter
);
1427 * g_sequence_iter_get_position:
1428 * @iter: a #GSequenceIter
1430 * Returns the position of @iter
1432 * Returns: the position of @iter
1437 g_sequence_iter_get_position (GSequenceIter
*iter
)
1439 g_return_val_if_fail (iter
!= NULL
, -1);
1441 return node_get_pos (iter
);
1445 * g_sequence_iter_next:
1446 * @iter: a #GSequenceIter
1448 * Returns an iterator pointing to the next position after @iter.
1449 * If @iter is the end iterator, the end iterator is returned.
1451 * Returns: (transfer none): a #GSequenceIter pointing to the next position after @iter
1456 g_sequence_iter_next (GSequenceIter
*iter
)
1458 g_return_val_if_fail (iter
!= NULL
, NULL
);
1460 return node_get_next (iter
);
1464 * g_sequence_iter_prev:
1465 * @iter: a #GSequenceIter
1467 * Returns an iterator pointing to the previous position before @iter.
1468 * If @iter is the begin iterator, the begin iterator is returned.
1470 * Returns: (transfer none): a #GSequenceIter pointing to the previous position
1476 g_sequence_iter_prev (GSequenceIter
*iter
)
1478 g_return_val_if_fail (iter
!= NULL
, NULL
);
1480 return node_get_prev (iter
);
1484 * g_sequence_iter_move:
1485 * @iter: a #GSequenceIter
1486 * @delta: A positive or negative number indicating how many positions away
1487 * from @iter the returned #GSequenceIter will be
1489 * Returns the #GSequenceIter which is @delta positions away from @iter.
1490 * If @iter is closer than -@delta positions to the beginning of the sequence,
1491 * the begin iterator is returned. If @iter is closer than @delta positions
1492 * to the end of the sequence, the end iterator is returned.
1494 * Returns: (transfer none): a #GSequenceIter which is @delta positions away from @iter
1499 g_sequence_iter_move (GSequenceIter
*iter
,
1505 g_return_val_if_fail (iter
!= NULL
, NULL
);
1507 len
= g_sequence_get_length (get_sequence (iter
));
1509 new_pos
= node_get_pos (iter
) + delta
;
1513 else if (new_pos
> len
)
1516 return node_get_by_pos (iter
, new_pos
);
1521 * @a: a #GSequenceIter
1522 * @b: a #GSequenceIter
1524 * Swaps the items pointed to by @a and @b. It is allowed for @a and @b
1525 * to point into difference sequences.
1530 g_sequence_swap (GSequenceIter
*a
,
1533 GSequenceNode
*leftmost
, *rightmost
, *rightmost_next
;
1536 g_return_if_fail (!g_sequence_iter_is_end (a
));
1537 g_return_if_fail (!g_sequence_iter_is_end (b
));
1542 a_pos
= g_sequence_iter_get_position (a
);
1543 b_pos
= g_sequence_iter_get_position (b
);
1556 rightmost_next
= node_get_next (rightmost
);
1558 /* The situation is now like this:
1560 * ..., leftmost, ......., rightmost, rightmost_next, ...
1563 g_sequence_move (rightmost
, leftmost
);
1564 g_sequence_move (leftmost
, rightmost_next
);
1568 * Implementation of a treap
1573 get_priority (GSequenceNode
*node
)
1575 guint key
= GPOINTER_TO_UINT (node
);
1577 /* This hash function is based on one found on Thomas Wang's
1580 * http://www.concentric.net/~Ttwang/tech/inthash.htm
1583 key
= (key
<< 15) - key
- 1;
1584 key
= key
^ (key
>> 12);
1585 key
= key
+ (key
<< 2);
1586 key
= key
^ (key
>> 4);
1587 key
= key
+ (key
<< 3) + (key
<< 11);
1588 key
= key
^ (key
>> 16);
1590 /* We rely on 0 being less than all other priorities */
1591 return key
? key
: 1;
1594 static GSequenceNode
*
1595 find_root (GSequenceNode
*node
)
1597 while (node
->parent
)
1598 node
= node
->parent
;
1603 static GSequenceNode
*
1604 node_new (gpointer data
)
1606 GSequenceNode
*node
= g_slice_new0 (GSequenceNode
);
1612 node
->parent
= NULL
;
1617 static GSequenceNode
*
1618 node_get_first (GSequenceNode
*node
)
1620 node
= find_root (node
);
1628 static GSequenceNode
*
1629 node_get_last (GSequenceNode
*node
)
1631 node
= find_root (node
);
1639 #define NODE_LEFT_CHILD(n) (((n)->parent) && ((n)->parent->left) == (n))
1640 #define NODE_RIGHT_CHILD(n) (((n)->parent) && ((n)->parent->right) == (n))
1642 static GSequenceNode
*
1643 node_get_next (GSequenceNode
*node
)
1645 GSequenceNode
*n
= node
;
1655 while (NODE_RIGHT_CHILD (n
))
1667 static GSequenceNode
*
1668 node_get_prev (GSequenceNode
*node
)
1670 GSequenceNode
*n
= node
;
1680 while (NODE_LEFT_CHILD (n
))
1692 #define N_NODES(n) ((n)? (n)->n_nodes : 0)
1695 node_get_pos (GSequenceNode
*node
)
1700 n_smaller
= node
->left
->n_nodes
;
1704 if (NODE_RIGHT_CHILD (node
))
1705 n_smaller
+= N_NODES (node
->parent
->left
) + 1;
1707 node
= node
->parent
;
1713 static GSequenceNode
*
1714 node_get_by_pos (GSequenceNode
*node
,
1719 node
= find_root (node
);
1721 while ((i
= N_NODES (node
->left
)) != pos
)
1737 static GSequenceNode
*
1738 node_find (GSequenceNode
*haystack
,
1739 GSequenceNode
*needle
,
1741 GSequenceIterCompareFunc iter_cmp
,
1746 haystack
= find_root (haystack
);
1750 /* iter_cmp can't be passed the end node, since the function may
1753 if (haystack
== end
)
1756 c
= iter_cmp (haystack
, needle
, cmp_data
);
1762 haystack
= haystack
->left
;
1764 haystack
= haystack
->right
;
1766 while (haystack
!= NULL
);
1771 static GSequenceNode
*
1772 node_find_closest (GSequenceNode
*haystack
,
1773 GSequenceNode
*needle
,
1775 GSequenceIterCompareFunc iter_cmp
,
1778 GSequenceNode
*best
;
1781 haystack
= find_root (haystack
);
1787 /* iter_cmp can't be passed the end node, since the function may
1790 if (haystack
== end
)
1793 c
= iter_cmp (haystack
, needle
, cmp_data
);
1795 /* In the following we don't break even if c == 0. Instead we go on
1796 * searching along the 'bigger' nodes, so that we find the last one
1797 * that is equal to the needle.
1800 haystack
= haystack
->left
;
1802 haystack
= haystack
->right
;
1804 while (haystack
!= NULL
);
1806 /* If the best node is smaller or equal to the data, then move one step
1807 * to the right to make sure the best one is strictly bigger than the data
1809 if (best
!= end
&& c
<= 0)
1810 best
= node_get_next (best
);
1816 node_get_length (GSequenceNode
*node
)
1818 node
= find_root (node
);
1820 return node
->n_nodes
;
1824 real_node_free (GSequenceNode
*node
,
1829 real_node_free (node
->left
, seq
);
1830 real_node_free (node
->right
, seq
);
1832 if (seq
&& seq
->data_destroy_notify
&& node
!= seq
->end_node
)
1833 seq
->data_destroy_notify (node
->data
);
1835 g_slice_free (GSequenceNode
, node
);
1840 node_free (GSequenceNode
*node
,
1843 node
= find_root (node
);
1845 real_node_free (node
, seq
);
1849 node_update_fields (GSequenceNode
*node
)
1853 n_nodes
+= N_NODES (node
->left
);
1854 n_nodes
+= N_NODES (node
->right
);
1856 node
->n_nodes
= n_nodes
;
1860 node_rotate (GSequenceNode
*node
)
1862 GSequenceNode
*tmp
, *old
;
1864 g_assert (node
->parent
);
1865 g_assert (node
->parent
!= node
);
1867 if (NODE_LEFT_CHILD (node
))
1872 node
->right
= node
->parent
;
1873 node
->parent
= node
->parent
->parent
;
1876 if (node
->parent
->left
== node
->right
)
1877 node
->parent
->left
= node
;
1879 node
->parent
->right
= node
;
1882 g_assert (node
->right
);
1884 node
->right
->parent
= node
;
1885 node
->right
->left
= tmp
;
1887 if (node
->right
->left
)
1888 node
->right
->left
->parent
= node
->right
;
1897 node
->left
= node
->parent
;
1898 node
->parent
= node
->parent
->parent
;
1901 if (node
->parent
->right
== node
->left
)
1902 node
->parent
->right
= node
;
1904 node
->parent
->left
= node
;
1907 g_assert (node
->left
);
1909 node
->left
->parent
= node
;
1910 node
->left
->right
= tmp
;
1912 if (node
->left
->right
)
1913 node
->left
->right
->parent
= node
->left
;
1918 node_update_fields (old
);
1919 node_update_fields (node
);
1923 node_update_fields_deep (GSequenceNode
*node
)
1927 node_update_fields (node
);
1929 node_update_fields_deep (node
->parent
);
1934 rotate_down (GSequenceNode
*node
,
1939 left
= node
->left
? get_priority (node
->left
) : 0;
1940 right
= node
->right
? get_priority (node
->right
) : 0;
1942 while (priority
< left
|| priority
< right
)
1945 node_rotate (node
->left
);
1947 node_rotate (node
->right
);
1949 left
= node
->left
? get_priority (node
->left
) : 0;
1950 right
= node
->right
? get_priority (node
->right
) : 0;
1955 node_cut (GSequenceNode
*node
)
1957 while (node
->parent
)
1961 node
->left
->parent
= NULL
;
1964 node_update_fields (node
);
1966 rotate_down (node
, get_priority (node
));
1970 node_join (GSequenceNode
*left
,
1971 GSequenceNode
*right
)
1973 GSequenceNode
*fake
= node_new (NULL
);
1975 fake
->left
= find_root (left
);
1976 fake
->right
= find_root (right
);
1977 fake
->left
->parent
= fake
;
1978 fake
->right
->parent
= fake
;
1980 node_update_fields (fake
);
1984 node_free (fake
, NULL
);
1988 node_insert_before (GSequenceNode
*node
,
1991 new->left
= node
->left
;
1993 new->left
->parent
= new;
1998 node_update_fields_deep (new);
2000 while (new->parent
&& get_priority (new) > get_priority (new->parent
))
2003 rotate_down (new, get_priority (new));
2007 node_unlink (GSequenceNode
*node
)
2009 rotate_down (node
, 0);
2011 if (NODE_RIGHT_CHILD (node
))
2012 node
->parent
->right
= NULL
;
2013 else if (NODE_LEFT_CHILD (node
))
2014 node
->parent
->left
= NULL
;
2017 node_update_fields_deep (node
->parent
);
2019 node
->parent
= NULL
;
2023 node_insert_sorted (GSequenceNode
*node
,
2026 GSequenceIterCompareFunc iter_cmp
,
2029 GSequenceNode
*closest
;
2031 closest
= node_find_closest (node
, new, end
, iter_cmp
, cmp_data
);
2035 node_insert_before (closest
, new);