project_files/frontlib/util/list.h
author alfadur
Mon, 19 Nov 2018 09:37:53 +0300
changeset 14242 81fba21495a5
parent 10017 de822cd3df3a
permissions -rw-r--r--
was a wrong copy apparently
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
     1
/*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
     2
 * Hedgewars, a free turn based strategy game
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
     4
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
     5
 * This program is free software; you can redistribute it and/or
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
     6
 * modify it under the terms of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
     8
 * of the License, or (at your option) any later version.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
     9
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    13
 * GNU General Public License for more details.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    14
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    15
 * You should have received a copy of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    16
 * along with this program; if not, write to the Free Software
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    18
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    19
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    20
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    21
 * Simple dynamic array manipulation functions.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    22
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    23
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    24
#ifndef LIST_H_
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    25
#define LIST_H_
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    26
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    27
#include <stddef.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    28
#include <string.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    29
#include <stdlib.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    30
#include "util.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    31
#include "logging.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    32
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    33
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    34
 * Generate a static function that inserts a new value into a heap array of the given type,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    35
 * using realloc and memmove to increase the capacity and shift existing values.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    36
 * The function takes a pointer to the array variable and a pointer to the size variable
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    37
 * because both can be changed by this operation (realloc / increment).
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    38
 * The function returns 0 on success and leaves the array unchanged on error.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    39
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    40
#define GENERATE_STATIC_LIST_INSERT(fname, type) \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    41
    static int fname(type **listptr, int *listSizePtr, type element, int pos) { \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    42
        int result = -1; \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    43
        if(!log_badargs_if4(listptr==NULL, listSizePtr==NULL, pos < 0, pos > *listSizePtr)) { \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    44
            type *newList = flib_realloc(*listptr, ((*listSizePtr)+1)*sizeof(type)); \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    45
            if(newList) { \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    46
                memmove(newList + (pos+1), newList + pos, ((*listSizePtr)-pos)*sizeof(type)); \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    47
                newList[pos] = element; \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    48
                (*listSizePtr)++; \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    49
                *listptr = newList; \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    50
                result = 0; \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    51
            } \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    52
        } \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    53
        return result; \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    54
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    55
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    56
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    57
 * Generate a static function that deletes a value from a heap array of the given type,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    58
 * using realloc and memmove to decrease the capacity and shift existing values.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    59
 * The function takes a pointer to the array variable and a pointer to the size variable
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    60
 * because both can be changed by this operation (realloc / decrement).
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    61
 * The function returns 0 on success and leaves the array unchanged on error.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    62
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    63
#define GENERATE_STATIC_LIST_DELETE(fname, type) \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    64
    static int fname(type **listPtr, int *listSizePtr, int pos) { \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    65
        int result = -1; \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    66
        if(!log_badargs_if4(listPtr==NULL, listSizePtr==NULL, pos < 0, pos >= *listSizePtr)) { \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    67
            memmove((*listPtr) + pos, (*listPtr) + (pos+1), ((*listSizePtr)-(pos+1))*sizeof(type)); \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    68
            (*listSizePtr)--; \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    69
            \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    70
            size_t newCharSize = (*listSizePtr)*sizeof(type); \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    71
            type *newList = flib_realloc((*listPtr), newCharSize); \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    72
            if(newList || newCharSize==0) { \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    73
                (*listPtr) = newList; \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    74
            } /* If the realloc fails, just keep using the old buffer...*/ \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    75
            result = 0; \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    76
        } \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    77
        return result; \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    78
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    79
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7316
diff changeset
    80
#endif /* LIST_H_ */