Blame shared/nm-std-aux/c-list-util.c

Packit Service 87a54e
/* SPDX-License-Identifier: LGPL-2.1-or-later */
Packit 5756e2
/*
Packit 5756e2
 * Copyright (C) 2017 Red Hat, Inc.
Packit 5756e2
 */
Packit 5756e2
Packit 5756e2
#include "c-list-util.h"
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * c_list_relink:
Packit 5756e2
 * @lst: the head list entry
Packit 5756e2
 *
Packit 5756e2
 * Takes an invalid list, that has undefined prev pointers.
Packit 5756e2
 * Only the next pointers are valid, and the tail's next
Packit 5756e2
 * pointer points to %NULL instead of the head.
Packit 5756e2
 *
Packit 5756e2
 * c_list_relink() fixes the list by updating all prev pointers
Packit 5756e2
 * and close the circular linking by pointing the tails' next
Packit 5756e2
 * pointer to @lst.
Packit 5756e2
 *
Packit 5756e2
 * The use of this function is to do a bulk update, that lets the
Packit Service a1bd4f
 * list degenerate by not updating the prev pointers. At the end,
Packit 5756e2
 * the list can be fixed by c_list_relink().
Packit 5756e2
 */
Packit 5756e2
void
Packit Service a1bd4f
c_list_relink(CList *lst)
Packit 5756e2
{
Packit Service a1bd4f
    CList *ls, *ls_prev;
Packit Service a1bd4f
Packit Service a1bd4f
    ls_prev = lst;
Packit Service a1bd4f
    ls      = lst->next;
Packit Service a1bd4f
    do {
Packit Service a1bd4f
        ls->prev = ls_prev;
Packit Service a1bd4f
        ls_prev  = ls;
Packit Service a1bd4f
        ls       = ls->next;
Packit Service a1bd4f
    } while (ls);
Packit Service a1bd4f
    ls_prev->next = lst;
Packit Service a1bd4f
    lst->prev     = ls_prev;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
static CList *
Packit Service a1bd4f
_c_list_srt_split(CList *ls)
Packit 5756e2
{
Packit Service a1bd4f
    CList *ls2;
Packit Service a1bd4f
Packit Service a1bd4f
    ls2 = ls;
Packit Service a1bd4f
    ls  = ls->next;
Packit Service a1bd4f
    if (!ls)
Packit Service a1bd4f
        return NULL;
Packit Service a1bd4f
    do {
Packit Service a1bd4f
        ls = ls->next;
Packit Service a1bd4f
        if (!ls)
Packit Service a1bd4f
            break;
Packit Service a1bd4f
        ls  = ls->next;
Packit Service a1bd4f
        ls2 = ls2->next;
Packit Service a1bd4f
    } while (ls);
Packit Service a1bd4f
    ls        = ls2->next;
Packit Service a1bd4f
    ls2->next = NULL;
Packit Service a1bd4f
    return ls;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static CList *
Packit Service a1bd4f
_c_list_srt_merge(CList *ls1, CList *ls2, CListSortCmp cmp, const void *user_data)
Packit 5756e2
{
Packit Service a1bd4f
    CList *ls;
Packit Service a1bd4f
    CList  head;
Packit Service a1bd4f
Packit Service a1bd4f
    ls = &head;
Packit Service a1bd4f
    for (;;) {
Packit Service a1bd4f
        /* while invoking the @cmp function, the list
Packit Service a1bd4f
         * elements are not properly linked. Don't try to access
Packit Service a1bd4f
         * their next/prev pointers. */
Packit Service a1bd4f
        if (cmp(ls1, ls2, user_data) <= 0) {
Packit Service a1bd4f
            ls->next = ls1;
Packit Service a1bd4f
            ls       = ls1;
Packit Service a1bd4f
            ls1      = ls1->next;
Packit Service a1bd4f
            if (!ls1)
Packit Service a1bd4f
                break;
Packit Service a1bd4f
        } else {
Packit Service a1bd4f
            ls->next = ls2;
Packit Service a1bd4f
            ls       = ls2;
Packit Service a1bd4f
            ls2      = ls2->next;
Packit Service a1bd4f
            if (!ls2)
Packit Service a1bd4f
                break;
Packit Service a1bd4f
        }
Packit Service a1bd4f
    }
Packit Service a1bd4f
    ls->next = ls1 ?: ls2;
Packit Service a1bd4f
Packit Service a1bd4f
    return head.next;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
typedef struct {
Packit Service a1bd4f
    CList *ls1;
Packit Service a1bd4f
    CList *ls2;
Packit Service a1bd4f
    char   ls1_sorted;
Packit 5756e2
} SortStack;
Packit 5756e2
Packit 5756e2
static CList *
Packit Service a1bd4f
_c_list_sort(CList *ls, CListSortCmp cmp, const void *user_data)
Packit 5756e2
{
Packit Service a1bd4f
    /* reserve a huge stack-size. We need roughly log2(n) entries, hence this
Packit Service a1bd4f
     * is much more we will ever need. We don't guard for stack-overflow either. */
Packit Service a1bd4f
    SortStack  stack_arr[70];
Packit Service a1bd4f
    SortStack *stack_head = stack_arr;
Packit Service a1bd4f
Packit Service a1bd4f
    stack_arr[0].ls1 = ls;
Packit Service a1bd4f
Packit Service a1bd4f
    /* A simple top-down, non-recursive, stable merge-sort.
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * Maybe natural merge-sort would be better, to do better for
Packit Service a1bd4f
     * partially sorted lists. Doing that would be much more complicated,
Packit Service a1bd4f
     * so it's not done. */
Packit 5756e2
_split:
Packit Service a1bd4f
    stack_head[0].ls2 = _c_list_srt_split(stack_head[0].ls1);
Packit Service a1bd4f
    if (stack_head[0].ls2) {
Packit Service a1bd4f
        stack_head[0].ls1_sorted = 0;
Packit Service a1bd4f
        stack_head[1].ls1        = stack_head[0].ls1;
Packit Service a1bd4f
        stack_head++;
Packit Service a1bd4f
        goto _split;
Packit Service a1bd4f
    }
Packit 5756e2
Packit 5756e2
_backtrack:
Packit Service a1bd4f
    if (stack_head == stack_arr)
Packit Service a1bd4f
        return stack_arr[0].ls1;
Packit Service a1bd4f
Packit Service a1bd4f
    stack_head--;
Packit Service a1bd4f
    if (!stack_head[0].ls1_sorted) {
Packit Service a1bd4f
        stack_head[0].ls1        = stack_head[1].ls1;
Packit Service a1bd4f
        stack_head[0].ls1_sorted = 1;
Packit Service a1bd4f
        stack_head[1].ls1        = stack_head[0].ls2;
Packit Service a1bd4f
        stack_head++;
Packit Service a1bd4f
        goto _split;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    stack_head[0].ls1 = _c_list_srt_merge(stack_head[0].ls1, stack_head[1].ls1, cmp, user_data);
Packit Service a1bd4f
    goto _backtrack;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * c_list_sort_headless:
Packit 5756e2
 * @lst: the list.
Packit 5756e2
 * @cmp: compare function for sorting. While comparing two
Packit 5756e2
 *   CList elements, their next/prev pointers are in undefined
Packit 5756e2
 *   state.
Packit 5756e2
 * @user_data: user data for @cmp.
Packit 5756e2
 *
Packit 5756e2
 * Sorts the list @lst according to @cmp. Contrary to
Packit 5756e2
 * c_list_sort(), @lst is not the list head but a
Packit 5756e2
 * valid entry as well. This function returns the new
Packit 5756e2
 * list head.
Packit 5756e2
 */
Packit 5756e2
CList *
Packit Service a1bd4f
c_list_sort_headless(CList *lst, CListSortCmp cmp, const void *user_data)
Packit 5756e2
{
Packit Service a1bd4f
    if (!c_list_is_empty(lst)) {
Packit Service a1bd4f
        lst->prev->next = NULL;
Packit Service a1bd4f
        lst             = _c_list_sort(lst, cmp, user_data);
Packit Service a1bd4f
        c_list_relink(lst);
Packit Service a1bd4f
    }
Packit Service a1bd4f
    return lst;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * c_list_sort:
Packit 5756e2
 * @head: the list head.
Packit 5756e2
 * @cmp: compare function for sorting. While comparing two
Packit 5756e2
 *   CList elements, their next/prev pointers are in undefined
Packit 5756e2
 *   state.
Packit 5756e2
 * @user_data: user data for @cmp.
Packit 5756e2
 *
Packit 5756e2
 * Sorts the list @head according to @cmp.
Packit 5756e2
 */
Packit 5756e2
void
Packit Service a1bd4f
c_list_sort(CList *head, CListSortCmp cmp, const void *user_data)
Packit 5756e2
{
Packit Service a1bd4f
    if (!c_list_is_empty(head) && head->next->next != head) {
Packit Service a1bd4f
        head->prev->next = NULL;
Packit Service a1bd4f
        head->next       = _c_list_sort(head->next, cmp, user_data);
Packit Service a1bd4f
        c_list_relink(head);
Packit Service a1bd4f
    }
Packit 5756e2
}