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 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 #include "gsequence.h"
26 #include "gtestutils.h"
31 * @short_description: scalable lists
33 * The #GSequence data structure has the API of a list, but is
34 * implemented internally with a balanced binary tree. This means that
35 * most of the operations (access, search, insertion, deletion, ...) on
36 * #GSequence are O(log(n)) in average and O(n) in worst case for time
37 * complexity. But, note that maintaining a balanced sorted list of n
38 * elements is done in time O(n log(n)).
39 * The data contained in each element can be either integer values, by using
40 * of the [Type Conversion Macros][glib-Type-Conversion-Macros], or simply
41 * pointers to any type of data.
43 * A #GSequence is accessed through "iterators", represented by a
44 * #GSequenceIter. An iterator represents a position between two
45 * elements of the sequence. For example, the "begin" iterator
46 * represents the gap immediately before the first element of the
47 * sequence, and the "end" iterator represents the gap immediately
48 * after the last element. In an empty sequence, the begin and end
49 * iterators are the same.
51 * Some methods on #GSequence operate on ranges of items. For example
52 * g_sequence_foreach_range() will call a user-specified function on
53 * each element with the given range. The range is delimited by the
54 * gaps represented by the passed-in iterators, so if you pass in the
55 * begin and end iterators, the range in question is the entire
58 * The function g_sequence_get() is used with an iterator to access the
59 * element immediately following the gap that the iterator represents.
60 * The iterator is said to "point" to that element.
62 * Iterators are stable across most operations on a #GSequence. For
63 * example an iterator pointing to some element of a sequence will
64 * continue to point to that element even after the sequence is sorted.
65 * Even moving an element to another sequence using for example
66 * g_sequence_move_range() will not invalidate the iterators pointing
67 * to it. The only operation that will invalidate an iterator is when
68 * the element it points to is removed from any sequence.
70 * To sort the data, either use g_sequence_insert_sorted() or
71 * g_sequence_insert_sorted_iter() to add data to the #GSequence or, if
72 * you want to add a large amount of data, it is more efficient to call
73 * g_sequence_sort() or g_sequence_sort_iter() after doing unsorted
80 * The #GSequenceIter struct is an opaque data type representing an
81 * iterator pointing into a #GSequence.
85 * GSequenceIterCompareFunc:
86 * @a: a #GSequenceIter
87 * @b: a #GSequenceIter
88 * @user_data: user data
90 * A #GSequenceIterCompareFunc is a function used to compare iterators.
91 * It must return zero if the iterators compare equal, a negative value
92 * if @a comes before @b, and a positive value if @b comes before @a.
94 * Returns: zero if the iterators are equal, a negative value if @a
95 * comes before @b, and a positive value if @b comes before @a.
98 typedef struct _GSequenceNode GSequenceNode;
103 * The #GSequence struct is an opaque data type representing a
104 * [sequence][glib-Sequences] data type.
108 GSequenceNode * end_node;
109 GDestroyNotify data_destroy_notify;
110 gboolean access_prohibited;
112 /* The 'real_sequence' is used when temporary sequences are created
113 * to hold nodes that are being rearranged. The 'real_sequence' of such
114 * a temporary sequence points to the sequence that is actually being
115 * manipulated. The only reason we need this is so that when the
116 * sort/sort_changed/search_iter() functions call out to the application
117 * g_sequence_iter_get_sequence() will return the correct sequence.
119 GSequence * real_sequence;
122 struct _GSequenceNode
126 GSequenceNode * parent;
127 GSequenceNode * left;
128 GSequenceNode * right;
129 gpointer data; /* For the end node, this field points
135 * Declaration of GSequenceNode methods
137 static GSequenceNode *node_new (gpointer data);
138 static GSequenceNode *node_get_first (GSequenceNode *node);
139 static GSequenceNode *node_get_last (GSequenceNode *node);
140 static GSequenceNode *node_get_prev (GSequenceNode *node);
141 static GSequenceNode *node_get_next (GSequenceNode *node);
142 static gint node_get_pos (GSequenceNode *node);
143 static GSequenceNode *node_get_by_pos (GSequenceNode *node,
145 static GSequenceNode *node_find (GSequenceNode *haystack,
146 GSequenceNode *needle,
148 GSequenceIterCompareFunc cmp,
150 static GSequenceNode *node_find_closest (GSequenceNode *haystack,
151 GSequenceNode *needle,
153 GSequenceIterCompareFunc cmp,
155 static gint node_get_length (GSequenceNode *node);
156 static void node_free (GSequenceNode *node,
158 static void node_cut (GSequenceNode *split);
159 static void node_insert_before (GSequenceNode *node,
161 static void node_unlink (GSequenceNode *node);
162 static void node_join (GSequenceNode *left,
163 GSequenceNode *right);
164 static void node_insert_sorted (GSequenceNode *node,
167 GSequenceIterCompareFunc cmp_func,
172 * Various helper functions
175 check_seq_access (GSequence *seq)
177 if (G_UNLIKELY (seq->access_prohibited))
179 g_warning ("Accessing a sequence while it is "
180 "being sorted or searched is not allowed");
185 get_sequence (GSequenceNode *node)
187 return (GSequence *)node_get_last (node)->data;
191 seq_is_end (GSequence *seq,
194 return seq->end_node == iter;
198 is_end (GSequenceIter *iter)
200 GSequenceIter *parent = iter->parent;
208 while (parent->right == iter)
211 parent = iter->parent;
222 GCompareDataFunc cmp_func;
224 GSequenceNode *end_node;
227 /* This function compares two iters using a normal compare
228 * function and user_data passed in in a SortInfo struct
231 iter_compare (GSequenceIter *node1,
232 GSequenceIter *node2,
235 const SortInfo *info = data;
238 if (node1 == info->end_node)
241 if (node2 == info->end_node)
244 retval = info->cmp_func (node1->data, node2->data, info->cmp_data);
255 * @data_destroy: (nullable): a #GDestroyNotify function, or %NULL
257 * Creates a new GSequence. The @data_destroy function, if non-%NULL will
258 * be called on all items when the sequence is destroyed and on items that
259 * are removed from the sequence.
261 * Returns: (transfer full): a new #GSequence
266 g_sequence_new (GDestroyNotify data_destroy)
268 GSequence *seq = g_new (GSequence, 1);
269 seq->data_destroy_notify = data_destroy;
271 seq->end_node = node_new (seq);
273 seq->access_prohibited = FALSE;
275 seq->real_sequence = seq;
284 * Frees the memory allocated for @seq. If @seq has a data destroy
285 * function associated with it, that function is called on all items
291 g_sequence_free (GSequence *seq)
293 g_return_if_fail (seq != NULL);
295 check_seq_access (seq);
297 node_free (seq->end_node, seq);
303 * g_sequence_foreach_range:
304 * @begin: a #GSequenceIter
305 * @end: a #GSequenceIter
307 * @user_data: user data passed to @func
309 * Calls @func for each item in the range (@begin, @end) passing
310 * @user_data to the function. @func must not modify the sequence
316 g_sequence_foreach_range (GSequenceIter *begin,
324 g_return_if_fail (func != NULL);
325 g_return_if_fail (begin != NULL);
326 g_return_if_fail (end != NULL);
328 seq = get_sequence (begin);
330 seq->access_prohibited = TRUE;
335 GSequenceIter *next = node_get_next (iter);
337 func (iter->data, user_data);
342 seq->access_prohibited = FALSE;
346 * g_sequence_foreach:
348 * @func: the function to call for each item in @seq
349 * @user_data: user data passed to @func
351 * Calls @func for each item in the sequence passing @user_data
352 * to the function. @func must not modify the sequence itself.
357 g_sequence_foreach (GSequence *seq,
361 GSequenceIter *begin, *end;
363 check_seq_access (seq);
365 begin = g_sequence_get_begin_iter (seq);
366 end = g_sequence_get_end_iter (seq);
368 g_sequence_foreach_range (begin, end, func, user_data);
372 * g_sequence_range_get_midpoint:
373 * @begin: a #GSequenceIter
374 * @end: a #GSequenceIter
376 * Finds an iterator somewhere in the range (@begin, @end). This
377 * iterator will be close to the middle of the range, but is not
378 * guaranteed to be exactly in the middle.
380 * The @begin and @end iterators must both point to the same sequence
381 * and @begin must come before or be equal to @end in the sequence.
383 * Returns: (transfer none): a #GSequenceIter pointing somewhere in the
384 * (@begin, @end) range
389 g_sequence_range_get_midpoint (GSequenceIter *begin,
392 int begin_pos, end_pos, mid_pos;
394 g_return_val_if_fail (begin != NULL, NULL);
395 g_return_val_if_fail (end != NULL, NULL);
396 g_return_val_if_fail (get_sequence (begin) == get_sequence (end), NULL);
398 begin_pos = node_get_pos (begin);
399 end_pos = node_get_pos (end);
401 g_return_val_if_fail (end_pos >= begin_pos, NULL);
403 mid_pos = begin_pos + (end_pos - begin_pos) / 2;
405 return node_get_by_pos (begin, mid_pos);
409 * g_sequence_iter_compare:
410 * @a: a #GSequenceIter
411 * @b: a #GSequenceIter
413 * Returns a negative number if @a comes before @b, 0 if they are equal,
414 * and a positive number if @a comes after @b.
416 * The @a and @b iterators must point into the same sequence.
418 * Returns: a negative number if @a comes before @b, 0 if they are
419 * equal, and a positive number if @a comes after @b
424 g_sequence_iter_compare (GSequenceIter *a,
428 GSequence *seq_a, *seq_b;
430 g_return_val_if_fail (a != NULL, 0);
431 g_return_val_if_fail (b != NULL, 0);
433 seq_a = get_sequence (a);
434 seq_b = get_sequence (b);
435 g_return_val_if_fail (seq_a == seq_b, 0);
437 check_seq_access (seq_a);
438 check_seq_access (seq_b);
440 a_pos = node_get_pos (a);
441 b_pos = node_get_pos (b);
445 else if (a_pos > b_pos)
454 * @data: the data for the new item
456 * Adds a new item to the end of @seq.
458 * Returns: (transfer none): an iterator pointing to the new item
463 g_sequence_append (GSequence *seq,
468 g_return_val_if_fail (seq != NULL, NULL);
470 check_seq_access (seq);
472 node = node_new (data);
473 node_insert_before (seq->end_node, node);
479 * g_sequence_prepend:
481 * @data: the data for the new item
483 * Adds a new item to the front of @seq
485 * Returns: (transfer none): an iterator pointing to the new item
490 g_sequence_prepend (GSequence *seq,
493 GSequenceNode *node, *first;
495 g_return_val_if_fail (seq != NULL, NULL);
497 check_seq_access (seq);
499 node = node_new (data);
500 first = node_get_first (seq->end_node);
502 node_insert_before (first, node);
508 * g_sequence_insert_before:
509 * @iter: a #GSequenceIter
510 * @data: the data for the new item
512 * Inserts a new item just before the item pointed to by @iter.
514 * Returns: (transfer none): an iterator pointing to the new item
519 g_sequence_insert_before (GSequenceIter *iter,
525 g_return_val_if_fail (iter != NULL, NULL);
527 seq = get_sequence (iter);
528 check_seq_access (seq);
530 node = node_new (data);
532 node_insert_before (iter, node);
539 * @iter: a #GSequenceIter
541 * Removes the item pointed to by @iter. It is an error to pass the
542 * end iterator to this function.
544 * If the sequence has a data destroy function associated with it, this
545 * function is called on the data for the removed item.
550 g_sequence_remove (GSequenceIter *iter)
554 g_return_if_fail (iter != NULL);
556 seq = get_sequence (iter);
557 g_return_if_fail (!seq_is_end (seq, iter));
559 check_seq_access (seq);
562 node_free (iter, seq);
566 * g_sequence_remove_range:
567 * @begin: a #GSequenceIter
568 * @end: a #GSequenceIter
570 * Removes all items in the (@begin, @end) range.
572 * If the sequence has a data destroy function associated with it, this
573 * function is called on the data for the removed items.
578 g_sequence_remove_range (GSequenceIter *begin,
581 GSequence *seq_begin, *seq_end;
583 seq_begin = get_sequence (begin);
584 seq_end = get_sequence (end);
585 g_return_if_fail (seq_begin == seq_end);
586 /* check_seq_access() calls are done by g_sequence_move_range() */
588 g_sequence_move_range (NULL, begin, end);
592 * g_sequence_move_range:
593 * @dest: a #GSequenceIter
594 * @begin: a #GSequenceIter
595 * @end: a #GSequenceIter
597 * Inserts the (@begin, @end) range at the destination pointed to by @dest.
598 * The @begin and @end iters must point into the same sequence. It is
599 * allowed for @dest to point to a different sequence than the one pointed
600 * into by @begin and @end.
602 * If @dest is %NULL, the range indicated by @begin and @end is
603 * removed from the sequence. If @dest points to a place within
604 * the (@begin, @end) range, the range does not move.
609 g_sequence_move_range (GSequenceIter *dest,
610 GSequenceIter *begin,
613 GSequence *src_seq, *end_seq, *dest_seq = NULL;
614 GSequenceNode *first;
616 g_return_if_fail (begin != NULL);
617 g_return_if_fail (end != NULL);
619 src_seq = get_sequence (begin);
620 check_seq_access (src_seq);
622 end_seq = get_sequence (end);
623 check_seq_access (end_seq);
627 dest_seq = get_sequence (dest);
628 check_seq_access (dest_seq);
631 g_return_if_fail (src_seq == end_seq);
633 /* Dest points to begin or end? */
634 if (dest == begin || dest == end)
637 /* begin comes after end? */
638 if (g_sequence_iter_compare (begin, end) >= 0)
641 /* dest points somewhere in the (begin, end) range? */
642 if (dest && dest_seq == src_seq &&
643 g_sequence_iter_compare (dest, begin) > 0 &&
644 g_sequence_iter_compare (dest, end) < 0)
649 first = node_get_first (begin);
656 node_join (first, end);
660 first = node_get_first (dest);
664 node_join (begin, dest);
667 node_join (first, begin);
671 node_free (begin, src_seq);
678 * @cmp_func: the function used to sort the sequence
679 * @cmp_data: user data passed to @cmp_func
681 * Sorts @seq using @cmp_func.
683 * @cmp_func is passed two items of @seq and should
684 * return 0 if they are equal, a negative value if the
685 * first comes before the second, and a positive value
686 * if the second comes before the first.
691 g_sequence_sort (GSequence *seq,
692 GCompareDataFunc cmp_func,
697 info.cmp_func = cmp_func;
698 info.cmp_data = cmp_data;
699 info.end_node = seq->end_node;
701 check_seq_access (seq);
703 g_sequence_sort_iter (seq, iter_compare, &info);
707 * g_sequence_insert_sorted:
709 * @data: the data to insert
710 * @cmp_func: the function used to compare items in the sequence
711 * @cmp_data: user data passed to @cmp_func.
713 * Inserts @data into @seq using @cmp_func to determine the new
714 * position. The sequence must already be sorted according to @cmp_func;
715 * otherwise the new position of @data is undefined.
717 * @cmp_func is called with two items of the @seq, and @cmp_data.
718 * It should return 0 if the items are equal, a negative value
719 * if the first item comes before the second, and a positive value
720 * if the second item comes before the first.
722 * Note that when adding a large amount of data to a #GSequence,
723 * it is more efficient to do unsorted insertions and then call
724 * g_sequence_sort() or g_sequence_sort_iter().
726 * Returns: (transfer none): a #GSequenceIter pointing to the new item.
731 g_sequence_insert_sorted (GSequence *seq,
733 GCompareDataFunc cmp_func,
738 g_return_val_if_fail (seq != NULL, NULL);
739 g_return_val_if_fail (cmp_func != NULL, NULL);
741 info.cmp_func = cmp_func;
742 info.cmp_data = cmp_data;
743 info.end_node = seq->end_node;
744 check_seq_access (seq);
746 return g_sequence_insert_sorted_iter (seq, data, iter_compare, &info);
750 * g_sequence_sort_changed:
751 * @iter: A #GSequenceIter
752 * @cmp_func: the function used to compare items in the sequence
753 * @cmp_data: user data passed to @cmp_func.
755 * Moves the data pointed to by @iter to a new position as indicated by
757 * function should be called for items in a sequence already sorted according
758 * to @cmp_func whenever some aspect of an item changes so that @cmp_func
759 * may return different values for that item.
761 * @cmp_func is called with two items of the @seq, and @cmp_data.
762 * It should return 0 if the items are equal, a negative value if
763 * the first item comes before the second, and a positive value if
764 * the second item comes before the first.
769 g_sequence_sort_changed (GSequenceIter *iter,
770 GCompareDataFunc cmp_func,
776 g_return_if_fail (iter != NULL);
778 seq = get_sequence (iter);
779 /* check_seq_access() call is done by g_sequence_sort_changed_iter() */
780 g_return_if_fail (!seq_is_end (seq, iter));
782 info.cmp_func = cmp_func;
783 info.cmp_data = cmp_data;
784 info.end_node = seq->end_node;
786 g_sequence_sort_changed_iter (iter, iter_compare, &info);
792 * @data: data for the new item
793 * @cmp_func: the function used to compare items in the sequence
794 * @cmp_data: user data passed to @cmp_func
796 * Returns an iterator pointing to the position where @data would
797 * be inserted according to @cmp_func and @cmp_data.
799 * @cmp_func is called with two items of the @seq, and @cmp_data.
800 * It should return 0 if the items are equal, a negative value if
801 * the first item comes before the second, and a positive value if
802 * the second item comes before the first.
804 * If you are simply searching for an existing element of the sequence,
805 * consider using g_sequence_lookup().
807 * This function will fail if the data contained in the sequence is
810 * Returns: (transfer none): an #GSequenceIter pointing to the position where @data
811 * would have been inserted according to @cmp_func and @cmp_data
816 g_sequence_search (GSequence *seq,
818 GCompareDataFunc cmp_func,
823 g_return_val_if_fail (seq != NULL, NULL);
825 info.cmp_func = cmp_func;
826 info.cmp_data = cmp_data;
827 info.end_node = seq->end_node;
828 check_seq_access (seq);
830 return g_sequence_search_iter (seq, data, iter_compare, &info);
836 * @data: data to look up
837 * @cmp_func: the function used to compare items in the sequence
838 * @cmp_data: user data passed to @cmp_func
840 * Returns an iterator pointing to the position of the first item found
841 * equal to @data according to @cmp_func and @cmp_data. If more than one
842 * item is equal, it is not guaranteed that it is the first which is
843 * returned. In that case, you can use g_sequence_iter_next() and
844 * g_sequence_iter_prev() to get others.
846 * @cmp_func is called with two items of the @seq, and @cmp_data.
847 * It should return 0 if the items are equal, a negative value if
848 * the first item comes before the second, and a positive value if
849 * the second item comes before the first.
851 * This function will fail if the data contained in the sequence is
854 * Returns: (transfer none) (nullable): an #GSequenceIter pointing to the position of the
855 * first item found equal to @data according to @cmp_func and
856 * @cmp_data, or %NULL if no such item exists
861 g_sequence_lookup (GSequence *seq,
863 GCompareDataFunc cmp_func,
868 g_return_val_if_fail (seq != NULL, NULL);
870 info.cmp_func = cmp_func;
871 info.cmp_data = cmp_data;
872 info.end_node = seq->end_node;
873 check_seq_access (seq);
875 return g_sequence_lookup_iter (seq, data, iter_compare, &info);
879 * g_sequence_sort_iter:
881 * @cmp_func: the function used to compare iterators in the sequence
882 * @cmp_data: user data passed to @cmp_func
884 * Like g_sequence_sort(), but uses a #GSequenceIterCompareFunc instead
885 * of a #GCompareDataFunc as the compare function
887 * @cmp_func is called with two iterators pointing into @seq. It should
888 * return 0 if the iterators are equal, a negative value if the first
889 * iterator comes before the second, and a positive value if the second
890 * iterator comes before the first.
895 g_sequence_sort_iter (GSequence *seq,
896 GSequenceIterCompareFunc cmp_func,
900 GSequenceNode *begin, *end;
902 g_return_if_fail (seq != NULL);
903 g_return_if_fail (cmp_func != NULL);
905 check_seq_access (seq);
907 begin = g_sequence_get_begin_iter (seq);
908 end = g_sequence_get_end_iter (seq);
910 tmp = g_sequence_new (NULL);
911 tmp->real_sequence = seq;
913 g_sequence_move_range (g_sequence_get_begin_iter (tmp), begin, end);
915 seq->access_prohibited = TRUE;
916 tmp->access_prohibited = TRUE;
918 while (!g_sequence_is_empty (tmp))
920 GSequenceNode *node = g_sequence_get_begin_iter (tmp);
922 node_insert_sorted (seq->end_node, node, seq->end_node,
926 tmp->access_prohibited = FALSE;
927 seq->access_prohibited = FALSE;
929 g_sequence_free (tmp);
933 * g_sequence_sort_changed_iter:
934 * @iter: a #GSequenceIter
935 * @iter_cmp: the function used to compare iterators in the sequence
936 * @cmp_data: user data passed to @cmp_func
938 * Like g_sequence_sort_changed(), but uses
939 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
940 * the compare function.
942 * @iter_cmp is called with two iterators pointing into the #GSequence that
943 * @iter points into. It should
944 * return 0 if the iterators are equal, a negative value if the first
945 * iterator comes before the second, and a positive value if the second
946 * iterator comes before the first.
951 g_sequence_sort_changed_iter (GSequenceIter *iter,
952 GSequenceIterCompareFunc iter_cmp,
955 GSequence *seq, *tmp_seq;
956 GSequenceIter *next, *prev;
958 g_return_if_fail (iter != NULL);
959 g_return_if_fail (iter_cmp != NULL);
961 seq = get_sequence (iter);
962 g_return_if_fail (!seq_is_end (seq, iter));
964 check_seq_access (seq);
966 /* If one of the neighbours is equal to iter, then
967 * don't move it. This ensures that sort_changed() is
968 * a stable operation.
971 next = node_get_next (iter);
972 prev = node_get_prev (iter);
974 if (prev != iter && iter_cmp (prev, iter, cmp_data) == 0)
977 if (!is_end (next) && iter_cmp (next, iter, cmp_data) == 0)
980 seq->access_prohibited = TRUE;
982 tmp_seq = g_sequence_new (NULL);
983 tmp_seq->real_sequence = seq;
986 node_insert_before (tmp_seq->end_node, iter);
988 node_insert_sorted (seq->end_node, iter, seq->end_node,
991 g_sequence_free (tmp_seq);
993 seq->access_prohibited = FALSE;
997 * g_sequence_insert_sorted_iter:
999 * @data: data for the new item
1000 * @iter_cmp: the function used to compare iterators in the sequence
1001 * @cmp_data: user data passed to @iter_cmp
1003 * Like g_sequence_insert_sorted(), but uses
1004 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
1005 * the compare function.
1007 * @iter_cmp is called with two iterators pointing into @seq.
1008 * It should return 0 if the iterators are equal, a negative
1009 * value if the first iterator comes before the second, and a
1010 * positive value if the second iterator comes before the first.
1012 * Note that when adding a large amount of data to a #GSequence,
1013 * it is more efficient to do unsorted insertions and then call
1014 * g_sequence_sort() or g_sequence_sort_iter().
1016 * Returns: (transfer none): a #GSequenceIter pointing to the new item
1021 g_sequence_insert_sorted_iter (GSequence *seq,
1023 GSequenceIterCompareFunc iter_cmp,
1026 GSequenceNode *new_node;
1029 g_return_val_if_fail (seq != NULL, NULL);
1030 g_return_val_if_fail (iter_cmp != NULL, NULL);
1032 check_seq_access (seq);
1034 seq->access_prohibited = TRUE;
1036 /* Create a new temporary sequence and put the new node into
1037 * that. The reason for this is that the user compare function
1038 * will be called with the new node, and if it dereferences,
1039 * "is_end" will be called on it. But that will crash if the
1040 * node is not actually in a sequence.
1042 * node_insert_sorted() makes sure the node is unlinked before
1045 * The reason we need the "iter" versions at all is that that
1046 * is the only kind of compare functions GtkTreeView can use.
1048 tmp_seq = g_sequence_new (NULL);
1049 tmp_seq->real_sequence = seq;
1051 new_node = g_sequence_append (tmp_seq, data);
1053 node_insert_sorted (seq->end_node, new_node,
1054 seq->end_node, iter_cmp, cmp_data);
1056 g_sequence_free (tmp_seq);
1058 seq->access_prohibited = FALSE;
1064 * g_sequence_search_iter:
1065 * @seq: a #GSequence
1066 * @data: data for the new item
1067 * @iter_cmp: the function used to compare iterators in the sequence
1068 * @cmp_data: user data passed to @iter_cmp
1070 * Like g_sequence_search(), but uses a #GSequenceIterCompareFunc
1071 * instead of a #GCompareDataFunc as the compare function.
1073 * @iter_cmp is called with two iterators pointing into @seq.
1074 * It should return 0 if the iterators are equal, a negative value
1075 * if the first iterator comes before the second, and a positive
1076 * value if the second iterator comes before the first.
1078 * If you are simply searching for an existing element of the sequence,
1079 * consider using g_sequence_lookup_iter().
1081 * This function will fail if the data contained in the sequence is
1084 * Returns: (transfer none): a #GSequenceIter pointing to the position in @seq
1085 * where @data would have been inserted according to @iter_cmp
1091 g_sequence_search_iter (GSequence *seq,
1093 GSequenceIterCompareFunc iter_cmp,
1096 GSequenceNode *node;
1097 GSequenceNode *dummy;
1100 g_return_val_if_fail (seq != NULL, NULL);
1102 check_seq_access (seq);
1104 seq->access_prohibited = TRUE;
1106 tmp_seq = g_sequence_new (NULL);
1107 tmp_seq->real_sequence = seq;
1109 dummy = g_sequence_append (tmp_seq, data);
1111 node = node_find_closest (seq->end_node, dummy,
1112 seq->end_node, iter_cmp, cmp_data);
1114 g_sequence_free (tmp_seq);
1116 seq->access_prohibited = FALSE;
1122 * g_sequence_lookup_iter:
1123 * @seq: a #GSequence
1124 * @data: data to look up
1125 * @iter_cmp: the function used to compare iterators in the sequence
1126 * @cmp_data: user data passed to @iter_cmp
1128 * Like g_sequence_lookup(), but uses a #GSequenceIterCompareFunc
1129 * instead of a #GCompareDataFunc as the compare function.
1131 * @iter_cmp is called with two iterators pointing into @seq.
1132 * It should return 0 if the iterators are equal, a negative value
1133 * if the first iterator comes before the second, and a positive
1134 * value if the second iterator comes before the first.
1136 * This function will fail if the data contained in the sequence is
1139 * Returns: (transfer none) (nullable): an #GSequenceIter pointing to the position of
1140 * the first item found equal to @data according to @iter_cmp
1141 * and @cmp_data, or %NULL if no such item exists
1146 g_sequence_lookup_iter (GSequence *seq,
1148 GSequenceIterCompareFunc iter_cmp,
1151 GSequenceNode *node;
1152 GSequenceNode *dummy;
1155 g_return_val_if_fail (seq != NULL, NULL);
1157 check_seq_access (seq);
1159 seq->access_prohibited = TRUE;
1161 tmp_seq = g_sequence_new (NULL);
1162 tmp_seq->real_sequence = seq;
1164 dummy = g_sequence_append (tmp_seq, data);
1166 node = node_find (seq->end_node, dummy,
1167 seq->end_node, iter_cmp, cmp_data);
1169 g_sequence_free (tmp_seq);
1171 seq->access_prohibited = FALSE;
1177 * g_sequence_iter_get_sequence:
1178 * @iter: a #GSequenceIter
1180 * Returns the #GSequence that @iter points into.
1182 * Returns: (transfer none): the #GSequence that @iter points into
1187 g_sequence_iter_get_sequence (GSequenceIter *iter)
1191 g_return_val_if_fail (iter != NULL, NULL);
1193 seq = get_sequence (iter);
1195 /* For temporary sequences, this points to the sequence that
1196 * is actually being manipulated
1198 return seq->real_sequence;
1203 * @iter: a #GSequenceIter
1205 * Returns the data that @iter points to.
1207 * Returns: (transfer none): the data that @iter points to
1212 g_sequence_get (GSequenceIter *iter)
1214 g_return_val_if_fail (iter != NULL, NULL);
1215 g_return_val_if_fail (!is_end (iter), NULL);
1222 * @iter: a #GSequenceIter
1223 * @data: new data for the item
1225 * Changes the data for the item pointed to by @iter to be @data. If
1226 * the sequence has a data destroy function associated with it, that
1227 * function is called on the existing data that @iter pointed to.
1232 g_sequence_set (GSequenceIter *iter,
1237 g_return_if_fail (iter != NULL);
1239 seq = get_sequence (iter);
1240 g_return_if_fail (!seq_is_end (seq, iter));
1242 /* If @data is identical to iter->data, it is destroyed
1243 * here. This will work right in case of ref-counted objects. Also
1244 * it is similar to what ghashtables do.
1246 * For non-refcounted data it's a little less convenient, but
1247 * code relying on self-setting not destroying would be
1248 * pretty dubious anyway ...
1251 if (seq->data_destroy_notify)
1252 seq->data_destroy_notify (iter->data);
1258 * g_sequence_get_length:
1259 * @seq: a #GSequence
1261 * Returns the positive length (>= 0) of @seq. Note that this method is
1262 * O(h) where `h' is the height of the tree. It is thus more efficient
1263 * to use g_sequence_is_empty() when comparing the length to zero.
1265 * Returns: the length of @seq
1270 g_sequence_get_length (GSequence *seq)
1272 return node_get_length (seq->end_node) - 1;
1276 * g_sequence_is_empty:
1277 * @seq: a #GSequence
1279 * Returns %TRUE if the sequence contains zero items.
1281 * This function is functionally identical to checking the result of
1282 * g_sequence_get_length() being equal to zero. However this function is
1283 * implemented in O(1) running time.
1285 * Returns: %TRUE if the sequence is empty, otherwise %FALSE.
1290 g_sequence_is_empty (GSequence *seq)
1292 return (seq->end_node->parent == NULL) && (seq->end_node->left == NULL);
1296 * g_sequence_get_end_iter:
1297 * @seq: a #GSequence
1299 * Returns the end iterator for @seg
1301 * Returns: (transfer none): the end iterator for @seq
1306 g_sequence_get_end_iter (GSequence *seq)
1308 g_return_val_if_fail (seq != NULL, NULL);
1310 return seq->end_node;
1314 * g_sequence_get_begin_iter:
1315 * @seq: a #GSequence
1317 * Returns the begin iterator for @seq.
1319 * Returns: (transfer none): the begin iterator for @seq.
1324 g_sequence_get_begin_iter (GSequence *seq)
1326 g_return_val_if_fail (seq != NULL, NULL);
1328 return node_get_first (seq->end_node);
1332 clamp_position (GSequence *seq,
1335 gint len = g_sequence_get_length (seq);
1337 if (pos > len || pos < 0)
1344 * g_sequence_get_iter_at_pos:
1345 * @seq: a #GSequence
1346 * @pos: a position in @seq, or -1 for the end
1348 * Returns the iterator at position @pos. If @pos is negative or larger
1349 * than the number of items in @seq, the end iterator is returned.
1351 * Returns: (transfer none): The #GSequenceIter at position @pos
1356 g_sequence_get_iter_at_pos (GSequence *seq,
1359 g_return_val_if_fail (seq != NULL, NULL);
1361 pos = clamp_position (seq, pos);
1363 return node_get_by_pos (seq->end_node, pos);
1368 * @src: a #GSequenceIter pointing to the item to move
1369 * @dest: a #GSequenceIter pointing to the position to which
1372 * Moves the item pointed to by @src to the position indicated by @dest.
1373 * After calling this function @dest will point to the position immediately
1374 * after @src. It is allowed for @src and @dest to point into different
1380 g_sequence_move (GSequenceIter *src,
1381 GSequenceIter *dest)
1383 g_return_if_fail (src != NULL);
1384 g_return_if_fail (dest != NULL);
1385 g_return_if_fail (!is_end (src));
1391 node_insert_before (dest, src);
1397 * g_sequence_iter_is_end:
1398 * @iter: a #GSequenceIter
1400 * Returns whether @iter is the end iterator
1402 * Returns: Whether @iter is the end iterator
1407 g_sequence_iter_is_end (GSequenceIter *iter)
1409 g_return_val_if_fail (iter != NULL, FALSE);
1411 return is_end (iter);
1415 * g_sequence_iter_is_begin:
1416 * @iter: a #GSequenceIter
1418 * Returns whether @iter is the begin iterator
1420 * Returns: whether @iter is the begin iterator
1425 g_sequence_iter_is_begin (GSequenceIter *iter)
1427 g_return_val_if_fail (iter != NULL, FALSE);
1429 return (node_get_prev (iter) == iter);
1433 * g_sequence_iter_get_position:
1434 * @iter: a #GSequenceIter
1436 * Returns the position of @iter
1438 * Returns: the position of @iter
1443 g_sequence_iter_get_position (GSequenceIter *iter)
1445 g_return_val_if_fail (iter != NULL, -1);
1447 return node_get_pos (iter);
1451 * g_sequence_iter_next:
1452 * @iter: a #GSequenceIter
1454 * Returns an iterator pointing to the next position after @iter.
1455 * If @iter is the end iterator, the end iterator is returned.
1457 * Returns: (transfer none): a #GSequenceIter pointing to the next position after @iter
1462 g_sequence_iter_next (GSequenceIter *iter)
1464 g_return_val_if_fail (iter != NULL, NULL);
1466 return node_get_next (iter);
1470 * g_sequence_iter_prev:
1471 * @iter: a #GSequenceIter
1473 * Returns an iterator pointing to the previous position before @iter.
1474 * If @iter is the begin iterator, the begin iterator is returned.
1476 * Returns: (transfer none): a #GSequenceIter pointing to the previous position
1482 g_sequence_iter_prev (GSequenceIter *iter)
1484 g_return_val_if_fail (iter != NULL, NULL);
1486 return node_get_prev (iter);
1490 * g_sequence_iter_move:
1491 * @iter: a #GSequenceIter
1492 * @delta: A positive or negative number indicating how many positions away
1493 * from @iter the returned #GSequenceIter will be
1495 * Returns the #GSequenceIter which is @delta positions away from @iter.
1496 * If @iter is closer than -@delta positions to the beginning of the sequence,
1497 * the begin iterator is returned. If @iter is closer than @delta positions
1498 * to the end of the sequence, the end iterator is returned.
1500 * Returns: (transfer none): a #GSequenceIter which is @delta positions away from @iter
1505 g_sequence_iter_move (GSequenceIter *iter,
1511 g_return_val_if_fail (iter != NULL, NULL);
1513 len = g_sequence_get_length (get_sequence (iter));
1515 new_pos = node_get_pos (iter) + delta;
1519 else if (new_pos > len)
1522 return node_get_by_pos (iter, new_pos);
1527 * @a: a #GSequenceIter
1528 * @b: a #GSequenceIter
1530 * Swaps the items pointed to by @a and @b. It is allowed for @a and @b
1531 * to point into difference sequences.
1536 g_sequence_swap (GSequenceIter *a,
1539 GSequenceNode *leftmost, *rightmost, *rightmost_next;
1542 g_return_if_fail (!g_sequence_iter_is_end (a));
1543 g_return_if_fail (!g_sequence_iter_is_end (b));
1548 a_pos = g_sequence_iter_get_position (a);
1549 b_pos = g_sequence_iter_get_position (b);
1562 rightmost_next = node_get_next (rightmost);
1564 /* The situation is now like this:
1566 * ..., leftmost, ......., rightmost, rightmost_next, ...
1569 g_sequence_move (rightmost, leftmost);
1570 g_sequence_move (leftmost, rightmost_next);
1574 * Implementation of a treap
1579 hash_uint32 (guint32 key)
1581 /* This hash function is based on one found on Thomas Wang's
1584 * http://www.concentric.net/~Ttwang/tech/inthash.htm
1587 key = (key << 15) - key - 1;
1588 key = key ^ (key >> 12);
1589 key = key + (key << 2);
1590 key = key ^ (key >> 4);
1591 key = key + (key << 3) + (key << 11);
1592 key = key ^ (key >> 16);
1598 get_priority (GSequenceNode *node)
1600 return node->priority;
1604 make_priority (guint32 key)
1606 key = hash_uint32 (key);
1608 /* We rely on 0 being less than all other priorities */
1609 return key? key : 1;
1612 static GSequenceNode *
1613 find_root (GSequenceNode *node)
1615 while (node->parent)
1616 node = node->parent;
1621 static GSequenceNode *
1622 node_new (gpointer data)
1624 GSequenceNode *node = g_slice_new0 (GSequenceNode);
1627 * Make a random number quickly. Some binary magic is used to avoid
1628 * the costs of proper RNG, such as locking around global GRand.
1630 * Using just the node pointer alone is not enough, because in this
1631 * case freeing and re-allocating sequence causes node's priorities
1632 * to no longer be random. This happens for two reasons:
1633 * 1) Nodes are freed from the root and the treap's property is that
1634 * node's priority is >= than its children's priorities.
1635 * 2) g_slice_new0() will reuse freed nodes in the order similar to
1636 * the order of freeing.
1637 * As a result, there are severe problems where building the treap is
1638 * much slower (100x and more after a few sequence new/free
1639 * iterations) and treap becomes more like a list (tree height
1640 * approaches tree's number of elements), which increases costs of
1641 * using the built treap.
1643 * Note that for performance reasons, counter completely ignores
1644 * multi-threading issues. This is fine because it's merely a source
1645 * of additional randomness. Even if it fails to ++ sometimes, this
1646 * won't really matter for its goal.
1648 * Note that 64-bit counter is used to avoid undefined behavior on
1651 * See https://gitlab.gnome.org/GNOME/glib/-/issues/2468
1653 static guint64 counter = 0;
1654 guint32 hash_key = (guint32) GPOINTER_TO_UINT (node);
1655 hash_key ^= (guint32) counter;
1659 node->priority = make_priority (hash_key);
1663 node->parent = NULL;
1668 static GSequenceNode *
1669 node_get_first (GSequenceNode *node)
1671 node = find_root (node);
1679 static GSequenceNode *
1680 node_get_last (GSequenceNode *node)
1682 node = find_root (node);
1690 #define NODE_LEFT_CHILD(n) (((n)->parent) && ((n)->parent->left) == (n))
1691 #define NODE_RIGHT_CHILD(n) (((n)->parent) && ((n)->parent->right) == (n))
1693 static GSequenceNode *
1694 node_get_next (GSequenceNode *node)
1696 GSequenceNode *n = node;
1706 while (NODE_RIGHT_CHILD (n))
1718 static GSequenceNode *
1719 node_get_prev (GSequenceNode *node)
1721 GSequenceNode *n = node;
1731 while (NODE_LEFT_CHILD (n))
1743 #define N_NODES(n) ((n)? (n)->n_nodes : 0)
1746 node_get_pos (GSequenceNode *node)
1751 n_smaller = node->left->n_nodes;
1755 if (NODE_RIGHT_CHILD (node))
1756 n_smaller += N_NODES (node->parent->left) + 1;
1758 node = node->parent;
1764 static GSequenceNode *
1765 node_get_by_pos (GSequenceNode *node,
1770 node = find_root (node);
1772 while ((i = N_NODES (node->left)) != pos)
1788 static GSequenceNode *
1789 node_find (GSequenceNode *haystack,
1790 GSequenceNode *needle,
1792 GSequenceIterCompareFunc iter_cmp,
1797 haystack = find_root (haystack);
1801 /* iter_cmp can't be passed the end node, since the function may
1804 if (haystack == end)
1807 c = iter_cmp (haystack, needle, cmp_data);
1813 haystack = haystack->left;
1815 haystack = haystack->right;
1817 while (haystack != NULL);
1822 static GSequenceNode *
1823 node_find_closest (GSequenceNode *haystack,
1824 GSequenceNode *needle,
1826 GSequenceIterCompareFunc iter_cmp,
1829 GSequenceNode *best;
1832 haystack = find_root (haystack);
1838 /* iter_cmp can't be passed the end node, since the function may
1841 if (haystack == end)
1844 c = iter_cmp (haystack, needle, cmp_data);
1846 /* In the following we don't break even if c == 0. Instead we go on
1847 * searching along the 'bigger' nodes, so that we find the last one
1848 * that is equal to the needle.
1851 haystack = haystack->left;
1853 haystack = haystack->right;
1855 while (haystack != NULL);
1857 /* If the best node is smaller or equal to the data, then move one step
1858 * to the right to make sure the best one is strictly bigger than the data
1860 if (best != end && c <= 0)
1861 best = node_get_next (best);
1867 node_get_length (GSequenceNode *node)
1869 node = find_root (node);
1871 return node->n_nodes;
1875 real_node_free (GSequenceNode *node,
1880 real_node_free (node->left, seq);
1881 real_node_free (node->right, seq);
1883 if (seq && seq->data_destroy_notify && node != seq->end_node)
1884 seq->data_destroy_notify (node->data);
1886 g_slice_free (GSequenceNode, node);
1891 node_free (GSequenceNode *node,
1894 node = find_root (node);
1896 real_node_free (node, seq);
1900 node_update_fields (GSequenceNode *node)
1904 n_nodes += N_NODES (node->left);
1905 n_nodes += N_NODES (node->right);
1907 node->n_nodes = n_nodes;
1911 node_rotate (GSequenceNode *node)
1913 GSequenceNode *tmp, *old;
1915 g_assert (node->parent);
1916 g_assert (node->parent != node);
1918 if (NODE_LEFT_CHILD (node))
1923 node->right = node->parent;
1924 node->parent = node->parent->parent;
1927 if (node->parent->left == node->right)
1928 node->parent->left = node;
1930 node->parent->right = node;
1933 g_assert (node->right);
1935 node->right->parent = node;
1936 node->right->left = tmp;
1938 if (node->right->left)
1939 node->right->left->parent = node->right;
1948 node->left = node->parent;
1949 node->parent = node->parent->parent;
1952 if (node->parent->right == node->left)
1953 node->parent->right = node;
1955 node->parent->left = node;
1958 g_assert (node->left);
1960 node->left->parent = node;
1961 node->left->right = tmp;
1963 if (node->left->right)
1964 node->left->right->parent = node->left;
1969 node_update_fields (old);
1970 node_update_fields (node);
1974 node_update_fields_deep (GSequenceNode *node)
1978 node_update_fields (node);
1980 node_update_fields_deep (node->parent);
1985 rotate_down (GSequenceNode *node,
1990 left = node->left ? get_priority (node->left) : 0;
1991 right = node->right ? get_priority (node->right) : 0;
1993 while (priority < left || priority < right)
1996 node_rotate (node->left);
1998 node_rotate (node->right);
2000 left = node->left ? get_priority (node->left) : 0;
2001 right = node->right ? get_priority (node->right) : 0;
2006 node_cut (GSequenceNode *node)
2008 while (node->parent)
2012 node->left->parent = NULL;
2015 node_update_fields (node);
2017 rotate_down (node, get_priority (node));
2021 node_join (GSequenceNode *left,
2022 GSequenceNode *right)
2024 GSequenceNode *fake = node_new (NULL);
2026 fake->left = find_root (left);
2027 fake->right = find_root (right);
2028 fake->left->parent = fake;
2029 fake->right->parent = fake;
2031 node_update_fields (fake);
2035 node_free (fake, NULL);
2039 node_insert_before (GSequenceNode *node,
2042 new->left = node->left;
2044 new->left->parent = new;
2049 node_update_fields_deep (new);
2051 while (new->parent && get_priority (new) > get_priority (new->parent))
2054 rotate_down (new, get_priority (new));
2058 node_unlink (GSequenceNode *node)
2060 rotate_down (node, 0);
2062 if (NODE_RIGHT_CHILD (node))
2063 node->parent->right = NULL;
2064 else if (NODE_LEFT_CHILD (node))
2065 node->parent->left = NULL;
2068 node_update_fields_deep (node->parent);
2070 node->parent = NULL;
2074 node_insert_sorted (GSequenceNode *node,
2077 GSequenceIterCompareFunc iter_cmp,
2080 GSequenceNode *closest;
2082 closest = node_find_closest (node, new, end, iter_cmp, cmp_data);
2086 node_insert_before (closest, new);