6 n__list_8h
gullradriel edited this page 2026-10-09 00:55:18 +02:00

include/nilorea/n_list.h

List structures and definitions. More...

Structures

Name
struct LIST_NODE
Structure of a generic list node.
struct LIST
Structure of a generic LIST container.

Detailed Description

List structures and definitions.

Author: Castagnier Mickael

Version: 1.0

Date: 24/04/13

Source code

/*
 * Nilorea Library
 * Copyright (C) 2005-2026 Castagnier Mickael
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 */


#ifndef N_GENERIC_LIST
#define N_GENERIC_LIST

#ifdef __cplusplus
extern "C" {
#endif


#include <stdio.h>
#include <stdlib.h>

typedef struct LIST_NODE {
    void* ptr;

    void (*destroy_func)(void* ptr);

    struct LIST_NODE* next;
    struct LIST_NODE* prev;

} LIST_NODE;

typedef struct LIST {
    size_t nb_items;
    size_t nb_max_items;

    LIST_NODE* start;
    LIST_NODE* end;

} LIST;

#define UNLIMITED_LIST_ITEMS 0
#define MAX_LIST_ITEMS SIZE_MAX

#define link_node(__NODE_1, __NODE_2)                                                                                              \
    do {                                                                                                                           \
        if (!(__NODE_1) || !(__NODE_2)) {                                                                                          \
            n_log(LOG_ERR, "link_node: NULL argument (%s=%p, %s=%p)", #__NODE_1, (void*)(__NODE_1), #__NODE_2, (void*)(__NODE_2)); \
        } else {                                                                                                                   \
            (__NODE_2)->prev = (__NODE_1);                                                                                         \
            (__NODE_1)->next = (__NODE_2);                                                                                         \
        }                                                                                                                          \
    } while (0)

#define list_foreach(__ITEM_, __LIST_)                                                                                                                      \
    for (LIST_NODE* __ITEM_ = (__LIST_) ? (__LIST_)->start : NULL, *__next_##__ITEM_ = __ITEM_ ? __ITEM_->next : NULL; __ITEM_; __ITEM_ = __next_##__ITEM_, \
                    __next_##__ITEM_ = __ITEM_ ? __ITEM_->next : NULL)

#define list_pop(__LIST_, __TYPE_) (__TYPE_*)list_pop_f(__LIST_)
#define list_shift(__LIST_, __TYPE_) (__TYPE_*)list_shift_f(__LIST_, __FILE__, __LINE__)
#define remove_list_node(__LIST_, __NODE_, __TYPE_) (__TYPE_*)remove_list_node_f(__LIST_, __NODE_)

LIST* new_generic_list(size_t max_items);
LIST_NODE* new_list_node(void* ptr, void (*destructor)(void* ptr));
void* remove_list_node_f(LIST* list, LIST_NODE* node);
int list_node_push(LIST* list, LIST_NODE* node);
LIST_NODE* list_node_pop(LIST* list);
LIST_NODE* list_node_shift(LIST* list);
int list_node_unshift(LIST* list, LIST_NODE* node);

int list_push(LIST* list, void* ptr, void (*destructor)(void* ptr));
int list_push_sorted(LIST* list, void* ptr, int (*comparator)(const void* a, const void* b), void (*destructor)(void* ptr));
int list_unshift(LIST* list, void* ptr, void (*destructor)(void* ptr));
int list_unshift_sorted(LIST* list, void* ptr, int (*comparator)(const void* a, const void* b), void (*destructor)(void* ptr));

void* list_pop_f(LIST* list);
void* list_shift_f(LIST* list, char* file, size_t line);

LIST_NODE* list_search(LIST* list, const void* ptr);
LIST_NODE* list_search_with_f(LIST* list, int (*checkfunk)(void* ptr));

int list_empty(LIST* list);
int list_empty_with_f(LIST* list, void (*free_fnct)(void* ptr));

int list_destroy(LIST** list);


#ifdef __cplusplus
}
#endif

#endif