project_files/frontlib/util/list.h
author Medo <smaxein@googlemail.com>
Wed, 27 Jun 2012 22:52:19 +0200
changeset 7314 6171f0bad318
parent 7275 15f722e0b96f
child 7316 f7b49b2c5d84
permissions -rw-r--r--
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib files that did not have their license fixed already.

/*
 * Hedgewars, a free turn based strategy game
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

/**
 * Simple dynamic array manipulation functions.
 */

#ifndef LIST_H_
#define LIST_H_

#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include "util.h"
#include "logging.h"

/**
 * Generate a static function that inserts a new value into a heap array of the given type,
 * using realloc and memmove to increase the capacity and shift existing values.
 * The function takes a pointer to the array variable and a pointer to the size variable
 * because both can be changed by this operation (realloc / increment).
 * The function returns 0 on success and leaves the array unchanged on error.
 */
#define GENERATE_STATIC_LIST_INSERT(fname, type) \
	static int fname(type **listptr, int *listSizePtr, type element, int pos) { \
		int result = -1; \
		if(!listptr || !listSizePtr || pos < 0 || pos > *listSizePtr) { \
			flib_log_e("Invalid parameter in "#fname); \
		} else { \
			type *newList = flib_realloc(*listptr, ((*listSizePtr)+1)*sizeof(type)); \
			if(newList) { \
				memmove(newList + (pos+1), newList + pos, ((*listSizePtr)-pos)*sizeof(type)); \
				newList[pos] = element; \
				(*listSizePtr)++; \
				*listptr = newList; \
				result = 0; \
			} \
		} \
		return result; \
	}

/**
 * Generate a static function that deletes a value from a heap array of the given type,
 * using realloc and memmove to decrease the capacity and shift existing values.
 * The function takes a pointer to the array variable and a pointer to the size variable
 * because both can be changed by this operation (realloc / decrement).
 * The function returns 0 on success and leaves the array unchanged on error.
 */
#define GENERATE_STATIC_LIST_DELETE(fname, type) \
	static int fname(type **listPtr, int *listSizePtr, int pos) { \
		int result = -1; \
		if(!listPtr || !listSizePtr || pos < 0 || pos >= *listSizePtr) { \
			flib_log_e("Invalid parameter in "#fname); \
		} else { \
			memmove((*listPtr) + pos, (*listPtr) + (pos+1), ((*listSizePtr)-(pos+1))*sizeof(type)); \
			(*listSizePtr)--; \
			\
			size_t newCharSize = (*listSizePtr)*sizeof(type); \
			type *newList = flib_realloc((*listPtr), newCharSize); \
			if(newList || newCharSize==0) { \
				(*listPtr) = newList; \
			} /* If the realloc fails, just keep using the old buffer...*/ \
			result = 0; \
		} \
		return result; \
	}

#endif /* LIST_H_ */