/** * g_slist_copy: * @list: a #GSList * * Copies a #GSList. * * <note><para> * Note that this is a "shallow" copy. If the list elements * consist of pointers to data, the pointers are copied but * the actual data isn't. * </para></note> * * Returns: a copy of @list */ GSList* g_slist_copy (GSList *list) { GSList *new_list = NULL; if (list) { GSList *last; new_list = _g_slist_alloc (); new_list->data = list->data; last = new_list; list = list->next; while (list) { last->next = _g_slist_alloc (); last = last->next; last->data = list->data; list = list->next; } last->next = NULL; } return new_list; }
/** * g_slist_copy_deep: * @list: a #GSList * @func: a copy function used to copy every element in the list * @user_data: user data passed to the copy function @func, or #NULL * * Makes a full (deep) copy of a #GSList. * * In contrast with g_slist_copy(), this function uses @func to make a copy of * each list element, in addition to copying the list container itself. * * @func, as a #GCopyFunc, takes two arguments, the data to be copied and a user * pointer. It's safe to pass #NULL as user_data, if the copy function takes only * one argument. * * For instance, if @list holds a list of GObjects, you can do: * |[ * another_list = g_slist_copy_deep (list, (GCopyFunc) g_object_ref, NULL); * ]| * * And, to entirely free the new list, you could do: * |[ * g_slist_free_full (another_list, g_object_unref); * ]| * * Returns: a full copy of @list, use #g_slist_free_full to free it * * Since: 2.34 */ GSList* g_slist_copy_deep (GSList *list, GCopyFunc func, gpointer user_data) { GSList *new_list = NULL; if (list) { GSList *last; new_list = _g_slist_alloc (); if (func) new_list->data = func (list->data, user_data); else new_list->data = list->data; last = new_list; list = list->next; while (list) { last->next = _g_slist_alloc (); last = last->next; if (func) last->data = func (list->data, user_data); else last->data = list->data; list = list->next; } last->next = NULL; } return new_list; }
static GSList* g_slist_insert_sorted_real (GSList *list, gpointer data, GFunc func, gpointer user_data) { GSList *tmp_list = list; GSList *prev_list = NULL; GSList *new_list; gint cmp; g_return_val_if_fail (func != NULL, list); if (!list) { new_list = _g_slist_alloc (); new_list->data = data; new_list->next = NULL; return new_list; } cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data); while ((tmp_list->next) && (cmp > 0)) { prev_list = tmp_list; tmp_list = tmp_list->next; cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data); } new_list = _g_slist_alloc (); new_list->data = data; if ((!tmp_list->next) && (cmp > 0)) { tmp_list->next = new_list; new_list->next = NULL; return list; } if (prev_list) { prev_list->next = new_list; new_list->next = tmp_list; return list; } else { new_list->next = list; return new_list; } }
/** * g_slist_insert: * @list: a #GSList * @data: the data for the new element * @position: the position to insert the element. * If this is negative, or is larger than the number * of elements in the list, the new element is added on * to the end of the list. * * Inserts a new element into the list at the given position. * * Returns: the new start of the #GSList */ GSList* g_slist_insert (GSList *list, gpointer data, gint position) { GSList *tmp_list; GSList *new_list; if (position < 0) return g_slist_append (list, data); else if (position == 0 || !list) return g_slist_prepend (list, data); new_list = _g_slist_alloc (); new_list->data = data; tmp_list = list; while ((--position > 0) && tmp_list->next) { tmp_list = tmp_list->next; } new_list->next = tmp_list->next; tmp_list->next = new_list; return list; }
/** * g_slist_prepend: * @list: a #GSList * @data: the data for the new element * * Adds a new element on to the start of the list. * * <note><para> * The return value is the new start of the list, which * may have changed, so make sure you store the new value. * </para></note> * * |[ * /* Notice that it is initialized to the empty list. */ * GSList *list = NULL; * list = g_slist_prepend (list, "last"); * list = g_slist_prepend (list, "first"); * ]| * * Returns: the new start of the #GSList */ GSList* g_slist_prepend (GSList *list, gpointer data) { GSList *new_list; new_list = _g_slist_alloc (); new_list->data = data; new_list->next = list; return new_list; }
/** * g_slist_insert_before: * @slist: a #GSList * @sibling: node to insert @data before * @data: data to put in the newly-inserted node * * Inserts a node before @sibling containing @data. * * Returns: the new head of the list. */ GSList* g_slist_insert_before (GSList *slist, GSList *sibling, gpointer data) { if (!slist) { slist = _g_slist_alloc (); slist->data = data; slist->next = NULL; g_return_val_if_fail (sibling == NULL, slist); return slist; } else { GSList *node, *last = NULL; for (node = slist; node; last = node, node = last->next) if (node == sibling) break; if (!last) { node = _g_slist_alloc (); node->data = data; node->next = slist; return node; } else { node = _g_slist_alloc (); node->data = data; node->next = last->next; last->next = node; return slist; } } }
/** * g_slist_insert: * @list: a #GSList * @data: the data for the new element * @position: the position to insert the element. * If this is negative, or is larger than the number * of elements in the list, the new element is added on * to the end of the list. * * Inserts a new element into the list at the given position. * * Returns: the new start of the #GSList */ GSList* g_slist_insert (GSList *list, gpointer data, gint position) { GSList *prev_list; GSList *tmp_list; GSList *new_list; if (position < 0) return g_slist_append (list, data); else if (position == 0) return g_slist_prepend (list, data); new_list = _g_slist_alloc (); new_list->data = data; if (!list) { new_list->next = NULL; return new_list; } prev_list = NULL; tmp_list = list; while ((position-- > 0) && tmp_list) { prev_list = tmp_list; tmp_list = tmp_list->next; } if (prev_list) { new_list->next = prev_list->next; prev_list->next = new_list; } else { new_list->next = list; list = new_list; } return list; }
GSList* g_slist_append (GSList *list, gpointer data) { GSList *new_list; GSList *last; new_list = _g_slist_alloc (); new_list->data = data; if (list) { last = g_slist_last (list); /* g_assert (last != NULL); */ last->next = new_list; return list; } else return new_list; }
GSList* g_slist_alloc (void) { return _g_slist_alloc (); }