Add list module
This commit is contained in:
parent
cf2535c9e5
commit
0d6fca9b26
|
@ -0,0 +1,132 @@
|
|||
/******************************************************************************
|
||||
*
|
||||
* Copyright (c) 2017-2019 by Löwenware Ltd
|
||||
* Please, refer LICENSE file for legal information
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
/**
|
||||
* @file list.c
|
||||
* @author Ilja Kartašov <ik@lowenware.com>
|
||||
* @brief cStuff list module
|
||||
*
|
||||
* @see https://lowenware.com/
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "list.h"
|
||||
|
||||
CStuffRetcode
|
||||
cstuff_list_init(CStuffList self, int size)
|
||||
{
|
||||
if (!(self->items = calloc(size, sizeof (void *))))
|
||||
return CSTUFF_MALLOC_ERROR;
|
||||
|
||||
self->length = 0;
|
||||
self->size = size;
|
||||
|
||||
return CSTUFF_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
cstuff_list_release(CStuffList self, CStuffListFree item_free)
|
||||
{
|
||||
if (self->items) {
|
||||
if (item_free) {
|
||||
int i;
|
||||
for (i = 0; i < self->length; i++) {
|
||||
void *ptr = self->items[i];
|
||||
if (ptr) {
|
||||
item_free(ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
free(self->items);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
cstuff_list_append(CStuffList self, void *item)
|
||||
{
|
||||
void **items;
|
||||
int size = self->length + 1;
|
||||
|
||||
if (size > self->size) {
|
||||
if (!(items = realloc(self->items, size * sizeof (void*))))
|
||||
return -1;
|
||||
|
||||
self->items = items;
|
||||
self->size = size;
|
||||
}
|
||||
|
||||
self->items[self->length] = item;
|
||||
|
||||
return self->length++;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
cstuff_list_insert(CStuffList self, void *item, int index)
|
||||
{
|
||||
int r;
|
||||
|
||||
if ((r = cstuff_list_append(self, item)) == -1)
|
||||
return -1;
|
||||
|
||||
if (index >= r || index < 0)
|
||||
return r;
|
||||
|
||||
memmove(&self->items[index + 1], &self->items[index],
|
||||
(self->length - index - 1) * sizeof (void*));
|
||||
|
||||
self->items[index] = item;
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
|
||||
void *
|
||||
cstuff_list_remove(CStuffList self, int index)
|
||||
{
|
||||
void *result;
|
||||
int i;
|
||||
if (index < self->length) {
|
||||
result = self->items[index];
|
||||
self->length--;
|
||||
for (i = index; i < self->length; i++) {
|
||||
self->items[i] = self->items[i + 1];
|
||||
}
|
||||
} else {
|
||||
result = NULL;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
cstuff_list_remove_item(CStuffList self, void *item)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < self->length; i++) {
|
||||
if (self->items[i] == item) {
|
||||
cstuff_list_remove(self, i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
cstuff_list_set_item(CStuffList self, int index, void *value)
|
||||
{
|
||||
if (self->size > index) {
|
||||
self->items[index] = value;
|
||||
if (index >= self->length)
|
||||
self->length = index + 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
/******************************************************************************
|
||||
*
|
||||
* Copyright (c) 2017-2019 by Löwenware Ltd
|
||||
* Please, refer LICENSE file for legal information
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
/**
|
||||
* @file list.h
|
||||
* @author Ilja Kartašov <ik@lowenware.com>
|
||||
* @brief cStuff List module header file
|
||||
*
|
||||
* @see https://lowenware.com/
|
||||
*/
|
||||
|
||||
#ifndef CSTUFF_LIST_H_372E2D14_8DEE_4A7C_A5AB_8808A87DF436
|
||||
#define CSTUFF_LIST_H_372E2D14_8DEE_4A7C_A5AB_8808A87DF436
|
||||
|
||||
#include "retcode.h"
|
||||
|
||||
#define cstuff_list_index(LIST, IDX) (LIST->items[IDX])
|
||||
|
||||
|
||||
struct cstuff_list {
|
||||
void **items;
|
||||
int length;
|
||||
int size;
|
||||
};
|
||||
|
||||
typedef struct cstuff_list * CStuffList;
|
||||
|
||||
typedef void
|
||||
(*CStuffListFree)(void *item);
|
||||
|
||||
|
||||
CStuffRetcode
|
||||
cstuff_list_init(CStuffList self, int size);
|
||||
|
||||
|
||||
void
|
||||
cstuff_list_release(CStuffList self, CStuffListFree item_free);
|
||||
|
||||
|
||||
int
|
||||
cstuff_list_append(CStuffList self, void *item);
|
||||
|
||||
|
||||
int
|
||||
cstuff_list_insert(CStuffList self, void *item, int index);
|
||||
|
||||
|
||||
void *
|
||||
cstuff_list_remove(CStuffList self, int index);
|
||||
|
||||
|
||||
void
|
||||
cstuff_list_remove_item(CStuffList self, void *item);
|
||||
|
||||
|
||||
void
|
||||
cstuff_list_set_item(CStuffList self, int index, void *value);
|
||||
|
||||
|
||||
#endif /* !LIST_H */
|
Loading…
Reference in New Issue