Implemented array_set() (not tested)
This commit is contained in:
parent
e040dfcdc7
commit
cae65dec73
82
src/array.c
82
src/array.c
|
@ -1,5 +1,5 @@
|
|||
/* $Id$ */
|
||||
/* Copyright (c) 2007 Pierre Pronchery <khorben@defora.org> */
|
||||
/* Copyright (c) 2008 Pierre Pronchery <khorben@defora.org> */
|
||||
/* This file is part of DeforaOS System libSystem */
|
||||
/* libSystem is not free software; you can redistribute it and/or modify it
|
||||
* under the terms of the Creative Commons Attribution-NonCommercial-ShareAlike
|
||||
|
@ -18,10 +18,13 @@
|
|||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include "System.h"
|
||||
|
||||
|
||||
/* Array */
|
||||
/* public */
|
||||
/* array_new */
|
||||
Array * array_new(size_t size)
|
||||
{
|
||||
Array * array;
|
||||
|
@ -35,41 +38,22 @@ Array * array_new(size_t size)
|
|||
}
|
||||
|
||||
|
||||
/* array_delete */
|
||||
void array_delete(Array * array)
|
||||
{
|
||||
object_delete(array);
|
||||
}
|
||||
|
||||
|
||||
/* useful */
|
||||
int array_append(Array * array, void * data)
|
||||
{
|
||||
void * p;
|
||||
|
||||
if((p = realloc(array->data, array->size * (array->count + 1))) == NULL)
|
||||
return 1;
|
||||
array->data = p;
|
||||
memcpy(&p[array->size * array->count], data, array->size);
|
||||
array->count++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void array_apply(Array * array, ArrayApplyFunc func, void * userdata)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for(i = 0; i < array->count; i++)
|
||||
func(&array->data + (i * array->size), userdata);
|
||||
}
|
||||
|
||||
|
||||
/* accessors */
|
||||
/* array_count */
|
||||
size_t array_count(Array * array)
|
||||
{
|
||||
return array->count;
|
||||
}
|
||||
|
||||
|
||||
/* array_get */
|
||||
void * array_get(Array * array, size_t pos)
|
||||
{
|
||||
if(pos >= array->count)
|
||||
|
@ -78,6 +62,7 @@ void * array_get(Array * array, size_t pos)
|
|||
}
|
||||
|
||||
|
||||
/* array_get_copy */
|
||||
int array_get_copy(Array * array, size_t pos, void * data)
|
||||
{
|
||||
if(pos >= array->count)
|
||||
|
@ -87,6 +72,45 @@ int array_get_copy(Array * array, size_t pos, void * data)
|
|||
}
|
||||
|
||||
|
||||
/* array_set */
|
||||
int array_set(Array * array, size_t pos, void * data)
|
||||
/* FIXME not tested */
|
||||
{
|
||||
void * p;
|
||||
size_t cursize;
|
||||
size_t newpos;
|
||||
|
||||
newpos = array->count * (pos);
|
||||
if(array->count <= pos)
|
||||
{
|
||||
if((p = realloc(array->data, array->size * (pos + 1))) == NULL)
|
||||
return error_set_code(1, "%s", strerror(errno));
|
||||
array->data = p;
|
||||
cursize = array->count * array->size;
|
||||
memset(&array->data[cursize], 0, newpos - cursize);
|
||||
array->count = pos + 1;
|
||||
}
|
||||
memcpy(&array->data[newpos], data, array->size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* useful */
|
||||
/* array_append */
|
||||
int array_append(Array * array, void * data)
|
||||
{
|
||||
char * p;
|
||||
|
||||
if((p = realloc(array->data, array->size * (array->count + 1))) == NULL)
|
||||
return error_set_code(1, "%s", strerror(errno));
|
||||
array->data = p;
|
||||
memcpy(&p[array->size * array->count], data, array->size);
|
||||
array->count++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* array_remove_pos */
|
||||
int array_remove_pos(Array * array, size_t pos)
|
||||
{
|
||||
if(pos >= array->count)
|
||||
|
@ -97,3 +121,13 @@ int array_remove_pos(Array * array, size_t pos)
|
|||
(array->count - pos) * array->size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* array_apply */
|
||||
void array_apply(Array * array, ArrayApplyFunc func, void * userdata)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for(i = 0; i < array->count; i++)
|
||||
func(&array->data + (i * array->size), userdata);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user