Commit 523d8156 authored by Chris Müller's avatar Chris Müller

lib: arrays are only based on byte processing.

lib: implement array structure for crystal.
parent 3a1bd388
......@@ -16,35 +16,50 @@
*
*/
#include "array.h"
#include <assert.h>
#include <string.h>
struct CryArray
{
unsigned char* data;
uint8_t* data;
size_t alloc;
size_t length;
size_t extend;
size_t elementsize;
};
static void resize_memory(struct CryArray* array, size_t size)
{
assert(array != 0);
while(array->alloc < size)
array->alloc += array->extend;
array->data = cry_realloc(uint8_t, array->data, array->extend);
}
struct CryArray*
cry_array_new(size_t elementsize, size_t init, size_t extend)
cry_array_new(size_t init, size_t extend)
{
struct CryArray* array = cry_malloc(struct CryArray);
array->alloc = init * elementsize;
array->alloc = 0;
array->length = 0;
array->extend = extend * elementsize;
array->elementsize = elementsize;
array->extend = extend;
while(array->alloc < init)
array->alloc += extend;
array->data = cry_calloc(unsigned char, init * elementsize);
array->data = cry_calloc(uint8_t, array->alloc);
return array;
}
void
cry_array_free(struct CryArray* array)
{
......@@ -55,6 +70,7 @@ cry_array_free(struct CryArray* array)
}
size_t
cry_array_size(struct CryArray* array)
{
......@@ -64,87 +80,84 @@ cry_array_size(struct CryArray* array)
}
size_t
cry_array_alloc(struct CryArray* array)
{
assert(array != 0);
return array->alloc / array->elementsize;
return array->alloc;
}
size_t
cry_array_elementsize(struct CryArray* array)
void
cry_array_set_alloc(struct CryArray* array, size_t space)
{
assert(array != 0);
return array->elementsize;
if(space > array->alloc) {
array->data = cry_realloc(uint8_t, array->data, space);
array->alloc = space;
}
}
void
cry_array_set_alloc(struct CryArray* array, size_t range)
{
}
void
cry_array_append(struct CryArray* array, pointer data)
cry_array_append(struct CryArray* array, pointer data, size_t size)
{
assert(array != 0);
}
if(array->alloc < array->length + size)
resize_memory(array, array->length + size);
memcpy(array->data + array->length, data, size);
void
cry_array_append_range(struct CryArray* array, pointer data, size_t range)
{
array->length += size;
}
void
cry_array_prepend(struct CryArray* array, pointer data)
cry_array_insert(struct CryArray* array, size_t index, pointer data, size_t size)
{
}
assert(array != 0);
void
cry_array_prepend_range(struct CryArray* array, pointer data, size_t range)
{
}
if(array->alloc < array->length + size)
resize_memory(array, array->length + size);
memmove(array->data + index, array->data + index + size, size);
memcpy(array->data + index, data, size);
void
cry_array_insert(struct CryArray* array, pointer data)
{
array->length += size;
}
void
cry_array_insert_range(struct CryArray* array, pointer data, size_t range)
return_code
cry_array_remove(struct CryArray* array, size_t index, size_t size)
{
}
assert(array != 0);
if(index > array->length)
return CRY_OUT_OF_INDEX_RANGE;
int
cry_array_remove(struct CryArray* array, size_t index)
{
return 0;
}
memmove(array->data + index + size, array->data + index, array->length - index - size);
array->length -= size;
int
cry_array_remove_range(struct CryArray* array, size_t index, size_t range)
{
return 0;
return CRY_OKAY;
}
pointer
cry_array_get(struct CryArray* array, size_t index)
{
return 0;
}
assert(array != 0);
if(index < array->length)
return array->data + index;
return 0;
}
......@@ -19,31 +19,23 @@
#ifndef CRYSTAL_STRUCTURES_ARRAY_H
#define CRYSTAL_STRUCTURES_ARRAY_H
#include "standard.h"
#include "structures.h"
struct CryArray;
struct CryArray* cry_array_new(size_t elementsize, size_t init, size_t extend);
struct CryArray* cry_array_new(size_t initialsize, size_t extend);
void cry_array_free(struct CryArray* array);
size_t cry_array_size(struct CryArray* array);
size_t cry_array_alloc(struct CryArray* array);
size_t cry_array_elementsize(struct CryArray* array);
void cry_array_set_alloc(struct CryArray* array, size_t range);
void cry_array_append(struct CryArray* array, pointer data);
void cry_array_prepend(struct CryArray* array, pointer data);
void cry_array_insert(struct CryArray* array, pointer data);
int cry_array_remove(struct CryArray* array, size_t index);
void cry_array_append_range(struct CryArray* array, pointer data, size_t range);
void cry_array_prepend_range(struct CryArray* array, pointer data, size_t range);
void cry_array_insert_range(struct CryArray* array, pointer data, size_t range);
int cry_array_remove_range(struct CryArray* array, size_t index, size_t range);
void cry_array_set_alloc(struct CryArray* array, size_t space);
void cry_array_append(struct CryArray* array, pointer data, size_t size);
void cry_array_insert(struct CryArray* array, size_t index, pointer data, size_t size);
return_code cry_array_remove(struct CryArray* array, size_t index, size_t size);
pointer cry_array_get(struct CryArray* array, size_t index);
......
......@@ -21,6 +21,8 @@
#include "../standard.h"
#define CRY_OUT_OF_INDEX_RANGE -1
typedef void (*cry_free_funptr)(pointer data);
typedef int (*cry_predicate_funptr)(pointer element, pointer context);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment