src/array_list.c

Sun, 13 Nov 2022 13:21:48 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 13 Nov 2022 13:21:48 +0100
changeset 607
2d99e978dc34
parent 606
314e9288af2f
child 610
de5d3ee6435f
permissions
-rw-r--r--

implement array list ctor and dtor

also lays out the "glue level" functions

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    29 #include "cx/array_list.h"
    31 /* LOW LEVEL ARRAY LIST FUNCTIONS */
    35 /* HIGH LEVEL ARRAY LIST FUNCTIONS */
    37 typedef struct {
    38     struct cx_list_s base;
    39     void *data;
    40 } cx_array_list;
    42 static void cx_arl_destructor(struct cx_list_s *list) {
    43     cx_array_list *arl = (cx_array_list*) list;
    44     cxFree(list->allocator, arl->data);
    45 }
    47 static int cx_arl_add(
    48         struct cx_list_s *list,
    49         void const *elem
    50 ) {
    51     return 1;
    52 }
    54 static int cx_arl_insert(
    55         struct cx_list_s *list,
    56         size_t index,
    57         void const *elem
    58 ) {
    59     return 1;
    60 }
    62 static int cx_arl_insert_iter(
    63         struct cx_iterator_s *iter,
    64         void const *elem,
    65         int prepend
    66 ) {
    67     return 1;
    68 }
    70 static int cx_arl_remove(
    71         struct cx_list_s *list,
    72         size_t index
    73 ) {
    74     return 1;
    75 }
    77 static void * cx_arl_at(
    78         struct cx_list_s const *list,
    79         size_t index
    80 ) {
    81     return NULL;
    82 }
    84 static size_t cx_arl_find(
    85         struct cx_list_s const *list,
    86         void const *elem
    87 ) {
    88     return 0;
    89 }
    91 static void cx_arl_sort(struct cx_list_s *list) {
    93 }
    95 static int cx_arl_compare(
    96         struct cx_list_s const *list,
    97         struct cx_list_s const *other
    98 ) {
   100 }
   102 static void cx_arl_reverse(struct cx_list_s *list) {
   104 }
   106 static struct cx_iterator_s cx_arl_iterator(
   107         struct cx_list_s *list,
   108         size_t index
   109 ) {
   110     struct cx_iterator_s iter;
   112     return iter;
   113 }
   115 static cx_list_class cx_array_list_class = {
   116         cx_arl_destructor,
   117         cx_arl_add,
   118         cx_arl_insert,
   119         cx_arl_insert_iter,
   120         cx_arl_remove,
   121         cx_arl_at,
   122         cx_arl_find,
   123         cx_arl_sort,
   124         cx_arl_compare,
   125         cx_arl_reverse,
   126         cx_arl_iterator,
   127 };
   129 CxList *cxArrayListCreate(
   130         CxAllocator const *allocator,
   131         CxListComparator comparator,
   132         size_t item_size,
   133         size_t initial_capacity
   134 ) {
   135     cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
   136     if (list == NULL) return NULL;
   138     list->data = cxCalloc(allocator, initial_capacity, item_size);
   139     if (list->data == NULL) {
   140         cxFree(allocator, list);
   141         return NULL;
   142     }
   144     list->base.cl = &cx_array_list_class;
   145     list->base.allocator = allocator;
   146     list->base.cmpfunc = comparator;
   147     list->base.itemsize = item_size;
   148     list->base.capacity = initial_capacity;
   150     return (CxList *) list;
   151 }

mercurial