1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007
3 * Soeren Sandmann (sandmann@daimi.au.dk)
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
26 typedef struct _GSequenceNode GSequenceNode;
30 GSequenceNode * end_node;
31 GDestroyNotify data_destroy_notify;
32 gboolean access_prohibited;
34 /* The 'real_sequence' is used when temporary sequences are created
35 * to hold nodes that are being rearranged. The 'real_sequence' of such
36 * a temporary sequence points to the sequence that is actually being
37 * manipulated. The only reason we need this is so that when the
38 * sort/sort_changed/search_iter() functions call out to the application
39 * g_sequence_iter_get_sequence() will return the correct sequence.
41 GSequence * real_sequence;
47 GSequenceNode * parent;
49 GSequenceNode * right;
50 gpointer data; /* For the end node, this field points
56 * Declaration of GSequenceNode methods
58 static GSequenceNode *node_new (gpointer data);
59 static GSequenceNode *node_get_first (GSequenceNode *node);
60 static GSequenceNode *node_get_last (GSequenceNode *node);
61 static GSequenceNode *node_get_prev (GSequenceNode *node);
62 static GSequenceNode *node_get_next (GSequenceNode *node);
63 static gint node_get_pos (GSequenceNode *node);
64 static GSequenceNode *node_get_by_pos (GSequenceNode *node,
66 static GSequenceNode *node_find_closest (GSequenceNode *haystack,
67 GSequenceNode *needle,
69 GSequenceIterCompareFunc cmp,
71 static gint node_get_length (GSequenceNode *node);
72 static void node_free (GSequenceNode *node,
74 static void node_cut (GSequenceNode *split);
75 static void node_insert_before (GSequenceNode *node,
77 static void node_unlink (GSequenceNode *node);
78 static void node_join (GSequenceNode *left,
79 GSequenceNode *right);
80 static void node_insert_sorted (GSequenceNode *node,
83 GSequenceIterCompareFunc cmp_func,
88 * Various helper functions
91 check_seq_access (GSequence *seq)
93 if (G_UNLIKELY (seq->access_prohibited))
95 g_warning ("Accessing a sequence while it is "
96 "being sorted or searched is not allowed");
101 get_sequence (GSequenceNode *node)
103 return (GSequence *)node_get_last (node)->data;
107 check_iter_access (GSequenceIter *iter)
109 check_seq_access (get_sequence (iter));
113 is_end (GSequenceIter *iter)
123 if (iter->parent->right != iter)
126 seq = get_sequence (iter);
128 return seq->end_node == iter;
133 GCompareDataFunc cmp_func;
135 GSequenceNode *end_node;
138 /* This function compares two iters using a normal compare
139 * function and user_data passed in in a SortInfo struct
142 iter_compare (GSequenceIter *node1,
143 GSequenceIter *node2,
146 const SortInfo *info = data;
149 if (node1 == info->end_node)
152 if (node2 == info->end_node)
155 retval = info->cmp_func (node1->data, node2->data, info->cmp_data);
166 * @data_destroy: a #GDestroyNotify function, or %NULL
168 * Creates a new GSequence. The @data_destroy function, if non-%NULL will
169 * be called on all items when the sequence is destroyed and on items that
170 * are removed from the sequence.
172 * Return value: a new #GSequence
177 g_sequence_new (GDestroyNotify data_destroy)
179 GSequence *seq = g_new (GSequence, 1);
180 seq->data_destroy_notify = data_destroy;
182 seq->end_node = node_new (seq);
184 seq->access_prohibited = FALSE;
186 seq->real_sequence = seq;
195 * Frees the memory allocated for @seq. If @seq has a data destroy
196 * function associated with it, that function is called on all items in
202 g_sequence_free (GSequence *seq)
204 g_return_if_fail (seq != NULL);
206 check_seq_access (seq);
208 node_free (seq->end_node, seq);
214 * g_sequence_foreach_range:
215 * @begin: a #GSequenceIter
216 * @end: a #GSequenceIter
218 * @user_data: user data passed to @func
220 * Calls @func for each item in the range (@begin, @end) passing
221 * @user_data to the function.
226 g_sequence_foreach_range (GSequenceIter *begin,
234 g_return_if_fail (func != NULL);
235 g_return_if_fail (begin != NULL);
236 g_return_if_fail (end != NULL);
238 seq = get_sequence (begin);
240 seq->access_prohibited = TRUE;
245 GSequenceIter *next = node_get_next (iter);
247 func (iter->data, user_data);
252 seq->access_prohibited = FALSE;
256 * g_sequence_foreach:
258 * @func: the function to call for each item in @seq
259 * @user_data: user data passed to @func
261 * Calls @func for each item in the sequence passing @user_data
267 g_sequence_foreach (GSequence *seq,
271 GSequenceIter *begin, *end;
273 check_seq_access (seq);
275 begin = g_sequence_get_begin_iter (seq);
276 end = g_sequence_get_end_iter (seq);
278 g_sequence_foreach_range (begin, end, func, user_data);
282 * g_sequence_range_get_midpoint:
283 * @begin: a #GSequenceIter
284 * @end: a #GSequenceIter
286 * Finds an iterator somewhere in the range (@begin, @end). This
287 * iterator will be close to the middle of the range, but is not
288 * guaranteed to be <emphasis>exactly</emphasis> in the middle.
290 * The @begin and @end iterators must both point to the same sequence and
291 * @begin must come before or be equal to @end in the sequence.
293 * Return value: A #GSequenceIter pointing somewhere in the
294 * (@begin, @end) range.
299 g_sequence_range_get_midpoint (GSequenceIter *begin,
302 int begin_pos, end_pos, mid_pos;
304 g_return_val_if_fail (begin != NULL, NULL);
305 g_return_val_if_fail (end != NULL, NULL);
306 g_return_val_if_fail (get_sequence (begin) == get_sequence (end), NULL);
308 begin_pos = node_get_pos (begin);
309 end_pos = node_get_pos (end);
311 g_return_val_if_fail (end_pos >= begin_pos, NULL);
313 mid_pos = begin_pos + (end_pos - begin_pos) / 2;
315 return node_get_by_pos (begin, mid_pos);
319 * g_sequence_iter_compare:
320 * @a: a #GSequenceIter
321 * @b: a #GSequenceIter
323 * Returns a negative number if @a comes before @b, 0 if they are equal,
324 * and a positive number if @a comes after @b.
326 * The @a and @b iterators must point into the same sequence.
328 * Return value: A negative number if @a comes before @b, 0 if they are
329 * equal, and a positive number if @a comes after @b.
334 g_sequence_iter_compare (GSequenceIter *a,
339 g_return_val_if_fail (a != NULL, 0);
340 g_return_val_if_fail (b != NULL, 0);
341 g_return_val_if_fail (get_sequence (a) == get_sequence (b), 0);
343 check_iter_access (a);
344 check_iter_access (b);
346 a_pos = node_get_pos (a);
347 b_pos = node_get_pos (b);
351 else if (a_pos > b_pos)
359 * @seq: a #GSequencePointer
360 * @data: the data for the new item
362 * Adds a new item to the end of @seq.
364 * Return value: an iterator pointing to the new item
369 g_sequence_append (GSequence *seq,
374 g_return_val_if_fail (seq != NULL, NULL);
376 check_seq_access (seq);
378 node = node_new (data);
379 node_insert_before (seq->end_node, node);
385 * g_sequence_prepend:
387 * @data: the data for the new item
389 * Adds a new item to the front of @seq
391 * Return value: an iterator pointing to the new item
396 g_sequence_prepend (GSequence *seq,
399 GSequenceNode *node, *first;
401 g_return_val_if_fail (seq != NULL, NULL);
403 check_seq_access (seq);
405 node = node_new (data);
406 first = node_get_first (seq->end_node);
408 node_insert_before (first, node);
414 * g_sequence_insert_before:
415 * @iter: a #GSequenceIter
416 * @data: the data for the new item
418 * Inserts a new item just before the item pointed to by @iter.
420 * Return value: an iterator pointing to the new item
425 g_sequence_insert_before (GSequenceIter *iter,
430 g_return_val_if_fail (iter != NULL, NULL);
432 check_iter_access (iter);
434 node = node_new (data);
436 node_insert_before (iter, node);
443 * @iter: a #GSequenceIter
445 * Removes the item pointed to by @iter. It is an error to pass the
446 * end iterator to this function.
448 * If the sequnce has a data destroy function associated with it, this
449 * function is called on the data for the removed item.
454 g_sequence_remove (GSequenceIter *iter)
458 g_return_if_fail (iter != NULL);
459 g_return_if_fail (!is_end (iter));
461 check_iter_access (iter);
463 seq = get_sequence (iter);
466 node_free (iter, seq);
470 * g_sequence_remove_range:
471 * @begin: a #GSequenceIter
472 * @end: a #GSequenceIter
474 * Removes all items in the (@begin, @end) range.
476 * If the sequence has a data destroy function associated with it, this
477 * function is called on the data for the removed items.
482 g_sequence_remove_range (GSequenceIter *begin,
485 g_return_if_fail (get_sequence (begin) == get_sequence (end));
487 check_iter_access (begin);
488 check_iter_access (end);
490 g_sequence_move_range (NULL, begin, end);
494 * g_sequence_move_range:
495 * @dest: a #GSequenceIter
496 * @begin: a #GSequenceIter
497 * @end: a #GSequenceIter
499 * Inserts the (@begin, @end) range at the destination pointed to by ptr.
500 * The @begin and @end iters must point into the same sequence. It is
501 * allowed for @dest to point to a different sequence than the one pointed
502 * into by @begin and @end.
504 * If @dest is NULL, the range indicated by @begin and @end is
505 * removed from the sequence. If @dest iter points to a place within
506 * the (@begin, @end) range, the range does not move.
511 g_sequence_move_range (GSequenceIter *dest,
512 GSequenceIter *begin,
516 GSequenceNode *first;
518 g_return_if_fail (begin != NULL);
519 g_return_if_fail (end != NULL);
521 check_iter_access (begin);
522 check_iter_access (end);
524 check_iter_access (dest);
526 src_seq = get_sequence (begin);
528 g_return_if_fail (src_seq == get_sequence (end));
530 /* Dest points to begin or end? */
531 if (dest == begin || dest == end)
534 /* begin comes after end? */
535 if (g_sequence_iter_compare (begin, end) >= 0)
538 /* dest points somewhere in the (begin, end) range? */
539 if (dest && get_sequence (dest) == src_seq &&
540 g_sequence_iter_compare (dest, begin) > 0 &&
541 g_sequence_iter_compare (dest, end) < 0)
546 src_seq = get_sequence (begin);
548 first = node_get_first (begin);
555 node_join (first, end);
559 first = node_get_first (dest);
563 node_join (begin, dest);
566 node_join (first, begin);
570 node_free (begin, src_seq);
577 * @cmp_func: the #GCompareDataFunc used to sort @seq. This function is
578 * passed two items of @seq and should return 0 if they are equal,
579 * a negative value if the first comes before the second, and a
580 * positive value if the second comes before the first.
581 * @cmp_data: user data passed to @cmp_func
583 * Sorts @seq using @cmp_func.
588 g_sequence_sort (GSequence *seq,
589 GCompareDataFunc cmp_func,
594 info.cmp_func = cmp_func;
595 info.cmp_data = cmp_data;
596 info.end_node = seq->end_node;
598 check_seq_access (seq);
600 g_sequence_sort_iter (seq, iter_compare, &info);
604 * g_sequence_insert_sorted:
606 * @data: the data to insert
607 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
608 * is called with two items of the @seq and @user_data. It should
609 * return 0 if the items are equal, a negative value if the first
610 * item comes before the second, and a positive value if the second
611 * item comes before the first.
612 * @cmp_data: user data passed to @cmp_func.
614 * Inserts @data into @sequence using @func to determine the new position.
615 * The sequence must already be sorted according to @cmp_func; otherwise the
616 * new position of @data is undefined.
618 * Return value: a #GSequenceIter pointing to the new item.
623 g_sequence_insert_sorted (GSequence *seq,
625 GCompareDataFunc cmp_func,
630 g_return_val_if_fail (seq != NULL, NULL);
631 g_return_val_if_fail (cmp_func != NULL, NULL);
633 info.cmp_func = cmp_func;
634 info.cmp_data = cmp_data;
635 info.end_node = seq->end_node;
636 check_seq_access (seq);
638 return g_sequence_insert_sorted_iter (seq, data, iter_compare, &info);
642 * g_sequence_sort_changed:
643 * @iter: A #GSequenceIter
644 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
645 * is called with two items of the @seq and @user_data. It should
646 * return 0 if the items are equal, a negative value if the first
647 * item comes before the second, and a positive value if the second
648 * item comes before the first.
649 * @cmp_data: user data passed to @cmp_func.
651 * Moves the data pointed to a new position as indicated by @cmp_func. This
652 * function should be called for items in a sequence already sorted according
653 * to @cmp_func whenever some aspect of an item changes so that @cmp_func
654 * may return different values for that item.
659 g_sequence_sort_changed (GSequenceIter *iter,
660 GCompareDataFunc cmp_func,
665 g_return_if_fail (!is_end (iter));
667 info.cmp_func = cmp_func;
668 info.cmp_data = cmp_data;
669 info.end_node = get_sequence (iter)->end_node;
670 check_iter_access (iter);
672 g_sequence_sort_changed_iter (iter, iter_compare, &info);
678 * @data: data for the new item
679 * @cmp_func: the #GCompareDataFunc used to compare items in the sequence. It
680 * is called with two items of the @seq and @user_data. It should
681 * return 0 if the items are equal, a negative value if the first
682 * item comes before the second, and a positive value if the second
683 * item comes before the first.
684 * @cmp_data: user data passed to @cmp_func.
686 * Returns an iterator pointing to the position where @data would
687 * be inserted according to @cmp_func and @cmp_data.
689 * Return value: an #GSequenceIter pointing to the position where @data
690 * would have been inserted according to @cmp_func and @cmp_data.
695 g_sequence_search (GSequence *seq,
697 GCompareDataFunc cmp_func,
702 g_return_val_if_fail (seq != NULL, NULL);
704 info.cmp_func = cmp_func;
705 info.cmp_data = cmp_data;
706 info.end_node = seq->end_node;
707 check_seq_access (seq);
709 return g_sequence_search_iter (seq, data, iter_compare, &info);
713 * g_sequence_sort_iter:
715 * @cmp_func: the #GSequenceItercompare used to compare iterators in the
716 * sequence. It is called with two iterators pointing into @seq. It should
717 * return 0 if the iterators are equal, a negative value if the first
718 * iterator comes before the second, and a positive value if the second
719 * iterator comes before the first.
720 * @cmp_data: user data passed to @cmp_func
722 * Like g_sequence_sort(), but uses a #GSequenceIterCompareFunc instead
723 * of a GCompareDataFunc as the compare function
728 g_sequence_sort_iter (GSequence *seq,
729 GSequenceIterCompareFunc cmp_func,
733 GSequenceNode *begin, *end;
735 g_return_if_fail (seq != NULL);
736 g_return_if_fail (cmp_func != NULL);
738 check_seq_access (seq);
740 begin = g_sequence_get_begin_iter (seq);
741 end = g_sequence_get_end_iter (seq);
743 tmp = g_sequence_new (NULL);
744 tmp->real_sequence = seq;
746 g_sequence_move_range (g_sequence_get_begin_iter (tmp), begin, end);
748 seq->access_prohibited = TRUE;
749 tmp->access_prohibited = TRUE;
751 while (g_sequence_get_length (tmp) > 0)
753 GSequenceNode *node = g_sequence_get_begin_iter (tmp);
755 node_insert_sorted (seq->end_node, node, seq->end_node,
759 tmp->access_prohibited = FALSE;
760 seq->access_prohibited = FALSE;
762 g_sequence_free (tmp);
766 * g_sequence_sort_changed_iter:
767 * @iter: a #GSequenceIter
768 * @iter_cmp: the #GSequenceItercompare used to compare iterators in the
769 * sequence. It is called with two iterators pointing into @seq. It should
770 * return 0 if the iterators are equal, a negative value if the first
771 * iterator comes before the second, and a positive value if the second
772 * iterator comes before the first.
773 * @cmp_data: user data passed to @cmp_func
775 * Like g_sequence_sort_changed(), but uses
776 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
777 * the compare function.
782 g_sequence_sort_changed_iter (GSequenceIter *iter,
783 GSequenceIterCompareFunc iter_cmp,
786 GSequence *seq, *tmp_seq;
787 GSequenceIter *next, *prev;
789 g_return_if_fail (iter != NULL);
790 g_return_if_fail (!is_end (iter));
791 g_return_if_fail (iter_cmp != NULL);
792 check_iter_access (iter);
794 /* If one of the neighbours is equal to iter, then
795 * don't move it. This ensures that sort_changed() is
796 * a stable operation.
799 next = node_get_next (iter);
800 prev = node_get_prev (iter);
802 if (prev != iter && iter_cmp (prev, iter, cmp_data) == 0)
805 if (!is_end (next) && iter_cmp (next, iter, cmp_data) == 0)
808 seq = get_sequence (iter);
810 seq->access_prohibited = TRUE;
812 tmp_seq = g_sequence_new (NULL);
813 tmp_seq->real_sequence = seq;
816 node_insert_before (tmp_seq->end_node, iter);
818 node_insert_sorted (seq->end_node, iter, seq->end_node,
821 g_sequence_free (tmp_seq);
823 seq->access_prohibited = FALSE;
827 * g_sequence_insert_sorted_iter:
829 * @data: data for the new item
830 * @iter_cmp: the #GSequenceItercompare used to compare iterators in the
831 * sequence. It is called with two iterators pointing into @seq. It should
832 * return 0 if the iterators are equal, a negative value if the first
833 * iterator comes before the second, and a positive value if the second
834 * iterator comes before the first.
835 * @cmp_data: user data passed to @cmp_func
837 * Like g_sequence_insert_sorted(), but uses
838 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
839 * the compare function.
841 * Return value: a #GSequenceIter pointing to the new item
846 g_sequence_insert_sorted_iter (GSequence *seq,
848 GSequenceIterCompareFunc iter_cmp,
851 GSequenceNode *new_node;
854 g_return_val_if_fail (seq != NULL, NULL);
855 g_return_val_if_fail (iter_cmp != NULL, NULL);
857 check_seq_access (seq);
859 seq->access_prohibited = TRUE;
861 /* Create a new temporary sequence and put the new node into
862 * that. The reason for this is that the user compare function
863 * will be called with the new node, and if it dereferences,
864 * "is_end" will be called on it. But that will crash if the
865 * node is not actually in a sequence.
867 * node_insert_sorted() makes sure the node is unlinked before
870 * The reason we need the "iter" versions at all is that that
871 * is the only kind of compare functions GtkTreeView can use.
873 tmp_seq = g_sequence_new (NULL);
874 tmp_seq->real_sequence = seq;
876 new_node = g_sequence_append (tmp_seq, data);
878 node_insert_sorted (seq->end_node, new_node,
879 seq->end_node, iter_cmp, cmp_data);
881 g_sequence_free (tmp_seq);
883 seq->access_prohibited = FALSE;
889 * g_sequence_search_iter:
891 * @data: data for the new item
892 * @iter_cmp: the #GSequenceIterCompare function used to compare iterators
893 * in the sequence. It is called with two iterators pointing into @seq.
894 * It should return 0 if the iterators are equal, a negative value if the
895 * first iterator comes before the second, and a positive value if the
896 * second iterator comes before the first.
897 * @cmp_data: user data passed to @iter_cmp
899 * Like g_sequence_search(), but uses
900 * a #GSequenceIterCompareFunc instead of a #GCompareDataFunc as
901 * the compare function.
903 * Return value: a #GSequenceIter pointing to the position in @seq
904 * where @data would have been inserted according to @iter_cmp and @cmp_data.
909 g_sequence_search_iter (GSequence *seq,
911 GSequenceIterCompareFunc iter_cmp,
915 GSequenceNode *dummy;
918 g_return_val_if_fail (seq != NULL, NULL);
920 check_seq_access (seq);
922 seq->access_prohibited = TRUE;
924 tmp_seq = g_sequence_new (NULL);
925 tmp_seq->real_sequence = seq;
927 dummy = g_sequence_append (tmp_seq, data);
929 node = node_find_closest (seq->end_node, dummy,
930 seq->end_node, iter_cmp, cmp_data);
932 g_sequence_free (tmp_seq);
934 seq->access_prohibited = FALSE;
940 * g_sequence_iter_get_sequence:
941 * @iter: a #GSequenceIter
943 * Returns the #GSequence that @iter points into.
945 * Return value: the #GSequence that @iter points into.
950 g_sequence_iter_get_sequence (GSequenceIter *iter)
954 g_return_val_if_fail (iter != NULL, NULL);
956 seq = get_sequence (iter);
958 /* For temporary sequences, this points to the sequence that
959 * is actually being manipulated
961 return seq->real_sequence;
966 * @iter: a #GSequenceIter
968 * Returns the data that @iter points to.
970 * Return value: the data that @iter points to
975 g_sequence_get (GSequenceIter *iter)
977 g_return_val_if_fail (iter != NULL, NULL);
978 g_return_val_if_fail (!is_end (iter), NULL);
985 * @iter: a #GSequenceIter
986 * @data: new data for the item
988 * Changes the data for the item pointed to by @iter to be @data. If
989 * the sequence has a data destroy function associated with it, that
990 * function is called on the existing data that @iter pointed to.
995 g_sequence_set (GSequenceIter *iter,
1000 g_return_if_fail (iter != NULL);
1001 g_return_if_fail (!is_end (iter));
1003 seq = get_sequence (iter);
1005 /* If @data is identical to iter->data, it is destroyed
1006 * here. This will work right in case of ref-counted objects. Also
1007 * it is similar to what ghashtables do.
1009 * For non-refcounted data it's a little less convenient, but
1010 * code relying on self-setting not destroying would be
1011 * pretty dubious anyway ...
1014 if (seq->data_destroy_notify)
1015 seq->data_destroy_notify (iter->data);
1021 * g_sequence_get_length:
1022 * @seq: a #GSequence
1024 * Returns the length of @seq
1026 * Return value: the length of @seq
1031 g_sequence_get_length (GSequence *seq)
1033 return node_get_length (seq->end_node) - 1;
1037 * g_sequence_get_end_iter:
1038 * @seq: a #GSequence
1040 * Returns the end iterator for @seg
1042 * Return value: the end iterator for @seq
1047 g_sequence_get_end_iter (GSequence *seq)
1049 g_return_val_if_fail (seq != NULL, NULL);
1051 return seq->end_node;
1055 * g_sequence_get_begin_iter:
1056 * @seq: a #GSequence
1058 * Returns the begin iterator for @seq.
1060 * Return value: the begin iterator for @seq.
1065 g_sequence_get_begin_iter (GSequence *seq)
1067 g_return_val_if_fail (seq != NULL, NULL);
1069 return node_get_first (seq->end_node);
1073 clamp_position (GSequence *seq,
1076 gint len = g_sequence_get_length (seq);
1078 if (pos > len || pos < 0)
1085 * if pos > number of items or -1, will return end pointer
1088 * g_sequence_get_iter_at_pos:
1089 * @seq: a #GSequence
1090 * @pos: a position in @seq, or -1 for the end.
1092 * Returns the iterator at position @pos. If @pos is negative or larger
1093 * than the number of items in @seq, the end iterator is returned.
1095 * Return value: The #GSequenceIter at position @pos
1100 g_sequence_get_iter_at_pos (GSequence *seq,
1103 g_return_val_if_fail (seq != NULL, NULL);
1105 pos = clamp_position (seq, pos);
1107 return node_get_by_pos (seq->end_node, pos);
1112 * @src: a #GSequenceIter pointing to the item to move
1113 * @dest: a #GSequenceIter pointing to the position to which
1114 * the item is moved.
1116 * Moves the item pointed to by @src to the position indicated by @dest.
1117 * After calling this function @dest will point to the position immediately
1118 * after @src. It is allowed for @src and @dest to point into different
1124 g_sequence_move (GSequenceIter *src,
1125 GSequenceIter *dest)
1127 g_return_if_fail (src != NULL);
1128 g_return_if_fail (dest != NULL);
1129 g_return_if_fail (!is_end (src));
1135 node_insert_before (dest, src);
1141 * g_sequence_iter_is_end:
1142 * @iter: a #GSequenceIter
1144 * Returns whether @iter is the end iterator
1146 * Return value: Whether @iter is the end iterator.
1151 g_sequence_iter_is_end (GSequenceIter *iter)
1153 g_return_val_if_fail (iter != NULL, FALSE);
1155 return is_end (iter);
1159 * g_sequence_iter_is_begin:
1160 * @iter: a #GSequenceIter
1162 * Returns whether @iter is the begin iterator
1164 * Return value: whether @iter is the begin iterator
1169 g_sequence_iter_is_begin (GSequenceIter *iter)
1171 g_return_val_if_fail (iter != NULL, FALSE);
1173 return (node_get_prev (iter) == iter);
1177 * g_sequence_iter_get_position:
1178 * @iter: a #GSequenceIter
1180 * Returns the position of @iter
1182 * Return value: the position of @iter
1187 g_sequence_iter_get_position (GSequenceIter *iter)
1189 g_return_val_if_fail (iter != NULL, -1);
1191 return node_get_pos (iter);
1195 * g_sequence_iter_next:
1196 * @iter: a #GSequenceIter
1198 * Returns an iterator pointing to the next position after @iter. If
1199 * @iter is the end iterator, the end iterator is returned.
1201 * Return value: a #GSequenceIter pointing to the next position after @iter.
1206 g_sequence_iter_next (GSequenceIter *iter)
1208 g_return_val_if_fail (iter != NULL, NULL);
1210 return node_get_next (iter);
1214 * g_sequence_iter_prev:
1215 * @iter: a #GSequenceIter
1217 * Returns an iterator pointing to the previous position before @iter. If
1218 * @iter is the begin iterator, the begin iterator is returned.
1220 * Return value: a #GSequenceIter pointing to the previous position before
1226 g_sequence_iter_prev (GSequenceIter *iter)
1228 g_return_val_if_fail (iter != NULL, NULL);
1230 return node_get_prev (iter);
1234 * g_sequence_iter_move:
1235 * @iter: a #GSequenceIter
1236 * @delta: A positive or negative number indicating how many positions away
1237 * from @iter the returned #GSequenceIter will be.
1239 * Returns the #GSequenceIter which is @delta positions away from @iter.
1240 * If @iter is closer than -@delta positions to the beginning of the sequence,
1241 * the begin iterator is returned. If @iter is closer than @delta positions
1242 * to the end of the sequence, the end iterator is returned.
1244 * Return value: a #GSequenceIter which is @delta positions away from @iter.
1249 g_sequence_iter_move (GSequenceIter *iter,
1254 g_return_val_if_fail (iter != NULL, NULL);
1256 new_pos = node_get_pos (iter) + delta;
1258 new_pos = clamp_position (get_sequence (iter), new_pos);
1260 return node_get_by_pos (iter, new_pos);
1265 * @a: a #GSequenceIter
1266 * @b: a #GSequenceIter
1268 * Swaps the items pointed to by @a and @b. It is allowed for @a and @b
1269 * to point into difference sequences.
1274 g_sequence_swap (GSequenceIter *a,
1277 GSequenceNode *leftmost, *rightmost, *rightmost_next;
1280 g_return_if_fail (!g_sequence_iter_is_end (a));
1281 g_return_if_fail (!g_sequence_iter_is_end (b));
1286 a_pos = g_sequence_iter_get_position (a);
1287 b_pos = g_sequence_iter_get_position (b);
1300 rightmost_next = node_get_next (rightmost);
1302 /* The situation is now like this:
1304 * ..., leftmost, ......., rightmost, rightmost_next, ...
1307 g_sequence_move (rightmost, leftmost);
1308 g_sequence_move (leftmost, rightmost_next);
1312 * Implementation of a treap
1317 get_priority (GSequenceNode *node)
1319 guint key = GPOINTER_TO_UINT (node);
1321 /* This hash function is based on one found on Thomas Wang's
1324 * http://www.concentric.net/~Ttwang/tech/inthash.htm
1327 key = (key << 15) - key - 1;
1328 key = key ^ (key >> 12);
1329 key = key + (key << 2);
1330 key = key ^ (key >> 4);
1331 key = key + (key << 3) + (key << 11);
1332 key = key ^ (key >> 16);
1334 /* We rely on 0 being less than all other priorities */
1335 return key? key : 1;
1338 static GSequenceNode *
1339 find_root (GSequenceNode *node)
1341 while (node->parent)
1342 node = node->parent;
1347 static GSequenceNode *
1348 node_new (gpointer data)
1350 GSequenceNode *node = g_slice_new0 (GSequenceNode);
1356 node->parent = NULL;
1361 static GSequenceNode *
1362 node_get_first (GSequenceNode *node)
1364 node = find_root (node);
1372 static GSequenceNode *
1373 node_get_last (GSequenceNode *node)
1375 node = find_root (node);
1383 #define NODE_LEFT_CHILD(n) (((n)->parent) && ((n)->parent->left) == (n))
1384 #define NODE_RIGHT_CHILD(n) (((n)->parent) && ((n)->parent->right) == (n))
1386 static GSequenceNode *
1387 node_get_next (GSequenceNode *node)
1389 GSequenceNode *n = node;
1399 while (NODE_RIGHT_CHILD (n))
1411 static GSequenceNode *
1412 node_get_prev (GSequenceNode *node)
1414 GSequenceNode *n = node;
1424 while (NODE_LEFT_CHILD (n))
1436 #define N_NODES(n) ((n)? (n)->n_nodes : 0)
1439 node_get_pos (GSequenceNode *node)
1444 n_smaller = node->left->n_nodes;
1448 if (NODE_RIGHT_CHILD (node))
1449 n_smaller += N_NODES (node->parent->left) + 1;
1451 node = node->parent;
1457 static GSequenceNode *
1458 node_get_by_pos (GSequenceNode *node,
1463 node = find_root (node);
1465 while ((i = N_NODES (node->left)) != pos)
1481 static GSequenceNode *
1482 node_find_closest (GSequenceNode *haystack,
1483 GSequenceNode *needle,
1485 GSequenceIterCompareFunc iter_cmp,
1488 GSequenceNode *best;
1491 haystack = find_root (haystack);
1497 /* iter_cmp can't be passed the end node, since the function may
1500 if (haystack == end)
1503 c = iter_cmp (haystack, needle, cmp_data);
1505 /* In the following we don't break even if c == 0. Instaed we go on
1506 * searching along the 'bigger' nodes, so that we find the last one
1507 * that is equal to the needle.
1510 haystack = haystack->left;
1512 haystack = haystack->right;
1514 while (haystack != NULL);
1516 /* If the best node is smaller or equal to the data, then move one step
1517 * to the right to make sure the best one is strictly bigger than the data
1519 if (best != end && c <= 0)
1520 best = node_get_next (best);
1526 node_get_length (GSequenceNode *node)
1528 node = find_root (node);
1530 return node->n_nodes;
1534 real_node_free (GSequenceNode *node,
1539 real_node_free (node->left, seq);
1540 real_node_free (node->right, seq);
1542 if (seq && seq->data_destroy_notify && node != seq->end_node)
1543 seq->data_destroy_notify (node->data);
1545 g_slice_free (GSequenceNode, node);
1550 node_free (GSequenceNode *node,
1553 node = find_root (node);
1555 real_node_free (node, seq);
1559 node_update_fields (GSequenceNode *node)
1563 n_nodes += N_NODES (node->left);
1564 n_nodes += N_NODES (node->right);
1566 node->n_nodes = n_nodes;
1570 node_rotate (GSequenceNode *node)
1572 GSequenceNode *tmp, *old;
1574 g_assert (node->parent);
1575 g_assert (node->parent != node);
1577 if (NODE_LEFT_CHILD (node))
1582 node->right = node->parent;
1583 node->parent = node->parent->parent;
1586 if (node->parent->left == node->right)
1587 node->parent->left = node;
1589 node->parent->right = node;
1592 g_assert (node->right);
1594 node->right->parent = node;
1595 node->right->left = tmp;
1597 if (node->right->left)
1598 node->right->left->parent = node->right;
1607 node->left = node->parent;
1608 node->parent = node->parent->parent;
1611 if (node->parent->right == node->left)
1612 node->parent->right = node;
1614 node->parent->left = node;
1617 g_assert (node->left);
1619 node->left->parent = node;
1620 node->left->right = tmp;
1622 if (node->left->right)
1623 node->left->right->parent = node->left;
1628 node_update_fields (old);
1629 node_update_fields (node);
1633 node_update_fields_deep (GSequenceNode *node)
1637 node_update_fields (node);
1639 node_update_fields_deep (node->parent);
1644 rotate_down (GSequenceNode *node,
1649 left = node->left ? get_priority (node->left) : 0;
1650 right = node->right ? get_priority (node->right) : 0;
1652 while (priority < left || priority < right)
1655 node_rotate (node->left);
1657 node_rotate (node->right);
1659 left = node->left ? get_priority (node->left) : 0;
1660 right = node->right ? get_priority (node->right) : 0;
1665 node_cut (GSequenceNode *node)
1667 while (node->parent)
1671 node->left->parent = NULL;
1674 node_update_fields (node);
1676 rotate_down (node, get_priority (node));
1680 node_join (GSequenceNode *left,
1681 GSequenceNode *right)
1683 GSequenceNode *fake = node_new (NULL);
1685 fake->left = find_root (left);
1686 fake->right = find_root (right);
1687 fake->left->parent = fake;
1688 fake->right->parent = fake;
1690 node_update_fields (fake);
1694 node_free (fake, NULL);
1698 node_insert_before (GSequenceNode *node,
1701 new->left = node->left;
1703 new->left->parent = new;
1708 node_update_fields_deep (new);
1710 while (new->parent && get_priority (new) > get_priority (new->parent))
1713 rotate_down (new, get_priority (new));
1717 node_unlink (GSequenceNode *node)
1719 rotate_down (node, 0);
1721 if (NODE_RIGHT_CHILD (node))
1722 node->parent->right = NULL;
1723 else if (NODE_LEFT_CHILD (node))
1724 node->parent->left = NULL;
1727 node_update_fields_deep (node->parent);
1729 node->parent = NULL;
1733 node_insert_sorted (GSequenceNode *node,
1736 GSequenceIterCompareFunc iter_cmp,
1739 GSequenceNode *closest;
1741 closest = node_find_closest (node, new, end, iter_cmp, cmp_data);
1745 node_insert_before (closest, new);
1749 #define __G_SEQUENCE_C__
1750 #include "galiasdef.c"