diff options
author | Tavian Barnes <tavianator@gmail.com> | 2009-05-07 00:38:23 +0000 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2009-05-07 00:38:23 +0000 |
commit | 4cd8d7439110f2e674a6f6e182a13a67620aa9d1 (patch) | |
tree | 75c4ebb2db7daf20e0ac5db701562b6496405924 /libdimension | |
parent | dd569686fa62d2736f9f5f3586af96b2aac06cd4 (diff) | |
download | dimension-4cd8d7439110f2e674a6f6e182a13a67620aa9d1.tar.xz |
Add new general array interface.
Diffstat (limited to 'libdimension')
-rw-r--r-- | libdimension/Makefile.am | 4 | ||||
-rw-r--r-- | libdimension/array.c | 95 | ||||
-rw-r--r-- | libdimension/dimension.h | 2 | ||||
-rw-r--r-- | libdimension/dimension/array.h | 48 | ||||
-rw-r--r-- | libdimension/dimension/color.h | 6 | ||||
-rw-r--r-- | libdimension/dimension/object.h | 34 |
6 files changed, 184 insertions, 5 deletions
diff --git a/libdimension/Makefile.am b/libdimension/Makefile.am index 2c2d9cf..ee3b6dc 100644 --- a/libdimension/Makefile.am +++ b/libdimension/Makefile.am @@ -17,10 +17,10 @@ ## along with this program. If not, see <http://www.gnu.org/licenses/>. ## ########################################################################### -nobase_include_HEADERS = dimension.h dimension/canvas.h dimension/color.h dimension/error.h dimension/geometry.h +nobase_include_HEADERS = dimension.h dimension/array.h dimension/canvas.h dimension/color.h dimension/error.h dimension/geometry.h lib_LTLIBRARIES = libdimension.la -libdimension_la_SOURCES = $(nobase_include_HEADERS) canvas.c color.c error.c geometry.c +libdimension_la_SOURCES = $(nobase_include_HEADERS) array.c canvas.c color.c error.c geometry.c libdimension_la_LDFLAGS = -version-info 0:0:0 libdimension_la_LIBADD = -lm -lpthread diff --git a/libdimension/array.c b/libdimension/array.c new file mode 100644 index 0000000..5e86f64 --- /dev/null +++ b/libdimension/array.c @@ -0,0 +1,95 @@ +/************************************************************************* + * Copyright (C) 2008 Tavian Barnes <tavianator@gmail.com> * + * * + * This file is part of The Dimension Library. * + * * + * The Dimension Library is free software; you can redistribute it and/ * + * or modify it under the terms of the GNU Lesser General Public License * + * as published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Library 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 * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this program. If not, see * + * <http://www.gnu.org/licenses/>. * + *************************************************************************/ + +#include "dimension.h" +#include <string.h> /* For memcpy */ + +dmnsn_array * +dmnsn_new_array(size_t obj_size) +{ + dmnsn_array array = malloc(sizeof(array)); + if (array) { + array.obj_size = obj_size; + array.length = 0; + array.capacity = 4; /* Start with capacity of 4 */ + + array.ptr = malloc(array.capacity*array.obj_size); + if (!array.ptr) { + free(array); + return NULL; + } + } + + return array; +} + +void +dmnsn_array_push(dmnsn_array *array, const void *obj) +{ + dmnsn_array_set(array, array->length, obj); +} + + +void +dmnsn_array_pop(dmnsn_array *array, void *obj) +{ + dmnsn_array_get(array, array->length - 1, obj); + dmnsn_array_resize(array, array->length - 1); +} + +void +dmnsn_array_get(const dmnsn_array *array, size_t i, void *obj) +{ + if (i >= array->length) { + dmnsn_error(DMNSN_SEVERITY_HIGH, "Array index out of bounds."); + } + + memcpy(obj, array->ptr + array->obj_size*i, array->obj_size); +} + +void +dmnsn_array_set(dmnsn_array *array, size_t i, const void *obj) +{ + if (i >= array->length) { + dmnsn_array_resize(array, i + 1); + } + memcpy(array->ptr + array->obj_size*i, obj, array->obj_size); +} + +void +dmnsn_array_resize(dmnsn_array *array, size_t length) +{ + if (length > array->capacity) { + array->capacity = length*2; /* We are greedy */ + array->ptr = realloc(array->ptr, array->obj_size*array->capacity); + if (!array->ptr) { + dmnsn_error(DMNSN_SEVERITY_HIGH, "Resizing array failed."); + } + } + + array->length = length; +} + +void dmnsn_delete_array(dmnsn_array *array) { + if (array) { + free(array.ptr); + free(array); + } +} diff --git a/libdimension/dimension.h b/libdimension/dimension.h index 735e1a0..ac65f4c 100644 --- a/libdimension/dimension.h +++ b/libdimension/dimension.h @@ -27,9 +27,11 @@ extern "C" { #endif #include <dimension/error.h> +#include <dimension/array.h> #include <dimension/geometry.h> #include <dimension/color.h> #include <dimension/canvas.h> +#include <dimension/object.h> #ifdef __cplusplus } diff --git a/libdimension/dimension/array.h b/libdimension/dimension/array.h new file mode 100644 index 0000000..9ee3584 --- /dev/null +++ b/libdimension/dimension/array.h @@ -0,0 +1,48 @@ +/************************************************************************* + * Copyright (C) 2008 Tavian Barnes <tavianator@gmail.com> * + * * + * This file is part of The Dimension Library. * + * * + * The Dimension Library is free software; you can redistribute it and/ * + * or modify it under the terms of the GNU Lesser General Public License * + * as published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Library 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 * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this program. If not, see * + * <http://www.gnu.org/licenses/>. * + *************************************************************************/ + +#ifndef DIMENSION_ARRAY_H +#define DIMENSION_ARRAY_H + +/* + * Simple generalized arrays, for returning variable-length arrays from + * functions, and other fun stuff. + */ + +#include <stdlib.h> /* For size_t */ + +typedef struct { + void *ptr; + size_t obj_size, length, capacity; +} dmnsn_array; + +dmnsn_array *dmnsn_new_array(size_t obj_size); + +void dmnsn_array_push(dmnsn_array *array, const void *obj); +void dmnsn_array_pop(dmnsn_array *array, void *obj); + +void dmnsn_array_get(const dmnsn_array *array, size_t i, void *obj); +void dmnsn_array_set(dmnsn_array *array, size_t i, const void *obj); + +void dmnsn_array_resize(dmnsn_array *array, size_t length); + +void dmnsn_delete_array(dmnsn_array *array); + +#endif /* DIMENSION_ARRAY_H */ diff --git a/libdimension/dimension/color.h b/libdimension/dimension/color.h index f1964c3..ab6efa3 100644 --- a/libdimension/dimension/color.h +++ b/libdimension/dimension/color.h @@ -18,13 +18,13 @@ * <http://www.gnu.org/licenses/>. * *************************************************************************/ +#ifndef DIMENSION_COLOR_H +#define DIMENSION_COLOR_H + /* * Types to represent color. */ -#ifndef DIMENSION_COLOR_H -#define DIMENSION_COLOR_H - /* Internally, we use CIE 1931 XYZ color. */ typedef struct { double X, Y, Z; diff --git a/libdimension/dimension/object.h b/libdimension/dimension/object.h new file mode 100644 index 0000000..737a49d --- /dev/null +++ b/libdimension/dimension/object.h @@ -0,0 +1,34 @@ +/************************************************************************* + * Copyright (C) 2008 Tavian Barnes <tavianator@gmail.com> * + * * + * This file is part of The Dimension Library. * + * * + * The Dimension Library is free software; you can redistribute it and/ * + * or modify it under the terms of the GNU Lesser General Public License * + * as published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Library 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 * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this program. If not, see * + * <http://www.gnu.org/licenses/>. * + *************************************************************************/ + +#ifndef DIMENSION_OBJECT_H +#define DIMENSION_OBJECT_H + +/* + * Objects. + */ + +typedef dmnsn_array *dmnsn_object_intersections_fn(dmnsn_line line); + +typedef struct { + dmnsn_object_intersections_fn *intersections_fn; +} dmnsn_object; + +#endif /* DIMENSION_OBJECT_H */ |