list-c

Simple library to create and maintain lists in C
Log | Files | Refs

list.h (1311B)


      1 #pragma once
      2 
      3 #include <stddef.h>
      4 #include <stdio.h>
      5 
      6 #define LIST_DEFAULT_CAPACITY 5
      7 #define LIST_GROW             2    // double the capacity if exceed, half the capacity if half empty
      8 
      9 
     10 typedef struct list      list_t;
     11 typedef struct list_iter list_iter_t;
     12 
     13 typedef void* (*list_malloc_t)(size_t);
     14 typedef void* (*list_realloc_t)(void*, size_t);
     15 typedef void (*list_free_t)(void*);
     16 
     17 list_t*      list(size_t element_size, list_malloc_t list_malloc, list_realloc_t list_realloc, list_free_t list_free);
     18 list_t*      list_default_alloc(size_t element_size);
     19 void         list_free(list_t* this);
     20 int          list_grow(list_t* this, size_t new_length);
     21 int          list_add(list_t* this, void* element);
     22 void*        list_get(list_t* this, ssize_t index);
     23 void*        list_get_copy(list_t* this, ssize_t index);
     24 int          list_shrink(list_t* this, ssize_t new_length);
     25 int          list_remove(list_t* this, ssize_t index);
     26 void*        list_remove_copy(list_t* this, ssize_t index);
     27 size_t       list_length(list_t* this);
     28 size_t       list_capacity(list_t* this);
     29 list_iter_t* list_iter(list_t* this);
     30 void         list_iter_free(list_iter_t* iter);
     31 int          list_iter_has_next(list_iter_t* iter);
     32 void*        list_iter_next(list_iter_t* iter);
     33 void*        list_iter_next_copy(list_iter_t* iter);