diff options
| author | Paul Buetow <paul@buetow.org> | 2010-05-09 09:30:29 +0000 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2010-05-09 09:30:29 +0000 |
| commit | a90467d4be3bcf91cab299b4521bf5f762abb1d5 (patch) | |
| tree | 5171d406e6be467807a914ce42923ac997d74858 /src/data | |
added the scheme branch
Diffstat (limited to 'src/data')
| -rw-r--r-- | src/data/array.c | 327 | ||||
| -rw-r--r-- | src/data/array.h | 94 | ||||
| -rw-r--r-- | src/data/cons.c | 88 | ||||
| -rw-r--r-- | src/data/cons.h | 55 | ||||
| -rw-r--r-- | src/data/dat.c | 268 | ||||
| -rw-r--r-- | src/data/dat.h | 88 | ||||
| -rw-r--r-- | src/data/hash.c | 306 | ||||
| -rw-r--r-- | src/data/hash.h | 84 | ||||
| -rw-r--r-- | src/data/list.c | 531 | ||||
| -rw-r--r-- | src/data/list.h | 118 | ||||
| -rw-r--r-- | src/data/map.c | 289 | ||||
| -rw-r--r-- | src/data/map.h | 88 | ||||
| -rw-r--r-- | src/data/queue.c | 210 | ||||
| -rw-r--r-- | src/data/queue.h | 81 | ||||
| -rw-r--r-- | src/data/stack.c | 271 | ||||
| -rw-r--r-- | src/data/stack.h | 79 | ||||
| -rw-r--r-- | src/data/tree.c | 215 | ||||
| -rw-r--r-- | src/data/tree.h | 106 | ||||
| -rw-r--r-- | src/data/tupel.c | 53 | ||||
| -rw-r--r-- | src/data/tupel.h | 47 | ||||
| -rw-r--r-- | src/data/types.h | 64 |
21 files changed, 3462 insertions, 0 deletions
diff --git a/src/data/array.c b/src/data/array.c new file mode 100644 index 0000000..f50e998 --- /dev/null +++ b/src/data/array.c @@ -0,0 +1,327 @@ +/*:* + *: File: ./src/data/array.c + *: A simple Fype interpreter + *: + *: WWW: http://fype.buetow.org + *: AUTHOR: http://paul.buetow.org + *: E-Mail: fype at dev.buetow.org + *: + *: The Fype Language; (c) 2005 - 2010 - Dipl.-Inform. (FH) Paul C. Buetow + *: + *: Redistribution and use in source and binary forms, with or without modi- + *: fication, are permitted provided that the following conditions are met: + *: * Redistributions of source code must retain the above copyright + *: notice, this list of conditions and the following disclaimer. + *: * Redistributions in binary form must reproduce the above copyright + *: notice, this list of conditions and the following disclaimer in the + *: documentation and/or other materials provided with the distribution. + *: * Neither the name of buetow.org nor the names of its contributors may + *: be used to endorse or promote products derived from this software + *: without specific prior written permission. + *: + *: THIS SOFTWARE IS PROVIDED BY PAUL C. BUETOW AS IS'' AND ANY EXPRESS OR + *: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + *: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + *: DISCLAIMED. IN NO EVENT SHALL PAUL C. BUETOW BE LIABLE FOR ANY DIRECT, + *: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + *: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + *: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + *: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + *: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + *: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + *: POSSIBILITY OF SUCH DAMAGE. + *:*/ + +#include "array.h" + +Array* +array_new() { + Array *p_array = malloc(sizeof(Array)); + + p_array->i_size = 0; + p_array->pp_ae = NULL; + array_set_used(p_array, 0); + + return (p_array); +} + +Array* +array_new_size(int i_size) { + Array *p_array = array_new(); + + array_resize(p_array, i_size); + + return (p_array); +} + +void +_unshift_cb(void *p_array, void *p_void) { + array_unshift(p_array, p_void); +} + +Array* +array_new_copy(Array *p_array) { + Array *p_array_cpy = array_new_size(array_get_size(p_array)); + array_iterate2(p_array, _unshift_cb, p_array_cpy); + + return (p_array_cpy); +} + +void +array_delete(Array *p_array) { + if (!p_array) + return; + + if (p_array->i_size) + for (int i = p_array->i_size - 1; i >= 0; --i) + arrayelement_delete(p_array->pp_ae[i]); + + if (p_array->pp_ae) + free(p_array->pp_ae); + + free(p_array); +} + +void +array_delete_iterate(Array *p_array, void (*func)(void *)) { + if (!p_array) + return; + + array_iterate(p_array, func); + + if (p_array->i_size) + for (int i = p_array->i_size - 1; i >= 0; --i) + arrayelement_delete(p_array->pp_ae[i]); + + if (p_array->pp_ae) + free(p_array->pp_ae); + + free(p_array); +} + +void +array_set(Array *p_array, int i_index, void *p_val) { + if (p_array->i_size > i_index) { + p_array->pp_ae[i_index]->p_val = p_val; + + } else { + array_resize(p_array, i_index + 1); + p_array->pp_ae[i_index]->p_val = p_val; + } + + if (p_array->i_used < i_index) + array_set_used(p_array, i_index); +} + +void +array_set_used(Array *p_array, int i_used) { + p_array->i_used = i_used; +} + + +void +array_insert(Array *p_array, int i_index, void *p_val) { + if (p_array->i_size <= i_index) { + array_set(p_array, i_index, p_val); + + } else { + array_resize(p_array, p_array->i_size + 1); + + ArrayElement *p_ae = p_array->pp_ae[p_array->i_size-1]; + int i; + for (i = p_array->i_size - 1; i > i_index; --i) + p_array->pp_ae[i] = p_array->pp_ae[i-1]; + + p_array->pp_ae[i] = p_ae; + p_ae->p_val = p_val; + } + + if (p_array->i_used < i_index) + array_set_used(p_array, i_index); +} + +void* +array_remove(Array *p_array, int i_index) { + if (p_array->i_size <= i_index) + return (NULL); + + ArrayElement *p_ae = p_array->pp_ae[i_index]; + void *p_ret = p_ae->p_val; + int i; + + for (i = i_index+1; i < p_array->i_size; ++i) + p_array->pp_ae[i-1] = p_array->pp_ae[i]; + + p_array->pp_ae[i-1] = p_ae; + + array_resize(p_array, p_array->i_size - 1); + return (p_ret); +} + +void +array_print_int(Array *p_array) { + printf("Array:"); + for (int i = 0; i < p_array->i_size; ++i) + printf(" (%d,%d)", i, (int) array_get(p_array, i)); + printf("\n"); +} + +void +array_resize(Array *p_array, int i_size) { + if (i_size == p_array->i_size) + return; + + if (i_size < p_array->i_size) + for (int i = p_array->i_size - 1; i >= i_size; --i) + arrayelement_delete(p_array->pp_ae[i]); + + if (i_size == 0) { + free(p_array->pp_ae); + p_array->pp_ae = NULL; + + } else if (p_array->pp_ae != NULL) { + p_array->pp_ae = realloc(p_array->pp_ae, + sizeof(ArrayElement) * i_size); + + } else { + p_array->pp_ae = malloc(sizeof(ArrayElement) * i_size); + } + + if (i_size > p_array->i_size) + for (int i = p_array->i_size; i < i_size; ++i) + p_array->pp_ae[i] = arrayelement_new(NULL); + + p_array->i_size = i_size; + if (p_array->i_used > i_size) + array_set_used(p_array, i_size); +} + +void* +array_get(Array *p_array, int i_index) { + if (p_array->i_size > i_index) + return (p_array->pp_ae[i_index]->p_val); + + return (NULL); +} + +_Bool +array_defined(Array *p_array, int i_index) { + if (i_index >= p_array->i_size) + return (false); + + return (p_array->pp_ae[i_index]->p_val != NULL); +} + +void +array_splice(Array *p_array, int i_index, Array *p_array2) { + if (i_index >= array_get_size(p_array)) + return; + + array_remove(p_array, i_index); + + int i_size1= array_get_size(p_array); + int i_size2 = array_get_size(p_array2); + int i_size = i_size1 + i_size2; + + array_resize(p_array, i_size); + + for (int i = i_size1 - 1; i >= i_index; --i) + p_array->pp_ae[i+i_size2]->p_val = p_array->pp_ae[i]->p_val; + + for (int i = 0; i < i_size2; ++i) + p_array->pp_ae[i+i_index]->p_val = p_array2->pp_ae[i]->p_val; + +} + +void +array_unshift(Array *p_array, void *p_void) { + int i_used = array_get_used(p_array); + array_set(p_array, i_used, p_void); + array_set_used(p_array, 1+i_used); +} + +void +array_push(Array *p_array, void *p_void) { + int i_size = array_get_size(p_array); + array_resize(p_array, ++i_size); + + for (int i = i_size - 1; i > 0; --i) + p_array->pp_ae[i]->p_val = p_array->pp_ae[i-1]->p_val; + + array_set(p_array, 0, p_void); +} + +void +array_append(Array *p_array, Array *p_array_append) { + int i_size = array_get_size(p_array) + array_get_size(p_array_append); + array_resize(p_array, i_size); + array_iterate2(p_array_append, _unshift_cb, p_array); +} + +void +array_iterate(Array *p_array, void (*func)(void *)) { + if (!p_array) + return; + + for (int i = 0; i < array_get_used(p_array); ++i) + (*func) (array_get(p_array, i)); +} + +void +array_iterate2(Array *p_array, void (*func)(void *, void *), void *p_void) { + if (!p_array) + return; + + for (int i = 0; i < array_get_used(p_array); ++i) + (*func) (array_get(p_array, i), p_void); +} + +ArrayElement* +arrayelement_new(void *p_val) { + ArrayElement *p_ae = malloc(sizeof(ArrayElement)); + + p_ae->p_val = p_val; + + return (p_ae); +} + +void +arrayelement_delete(ArrayElement *p_ae) { + if (!p_ae) + return; + + free(p_ae); +} + +ArrayIterator* +arrayiterator_new(Array *p_array) { + if (!p_array) + return (NULL); + + ArrayIterator *p_arrayiterator = malloc(sizeof(ArrayIterator)); + p_arrayiterator->p_array = p_array; + p_arrayiterator->i_cur_pos = 0; + + return (p_arrayiterator); +} + +void +arrayiterator_delete(ArrayIterator *p_arrayiterator) { + if (p_arrayiterator) + free(p_arrayiterator); +} + +_Bool +arrayiterator_has_next(ArrayIterator *p_arrayiterator) { + //printf("[%d]", p_arrayiterator->p_array->i_used); + return (p_arrayiterator->i_cur_pos < + array_get_used(p_arrayiterator->p_array)); +} + +void* +arrayiterator_next(ArrayIterator *p_arrayiterator) { + if (!arrayiterator_has_next(p_arrayiterator)) + return (NULL); + + return (array_get(p_arrayiterator->p_array, p_arrayiterator->i_cur_pos++)); +} diff --git a/src/data/array.h b/src/data/array.h new file mode 100644 index 0000000..6800061 --- /dev/null +++ b/src/data/array.h @@ -0,0 +1,94 @@ +/*:* + *: File: ./src/data/array.h + *: A simple Fype interpreter + *: + *: WWW: http://fype.buetow.org + *: AUTHOR: http://paul.buetow.org + *: E-Mail: fype at dev.buetow.org + *: + *: The Fype Language; (c) 2005 - 2010 - Dipl.-Inform. (FH) Paul C. Buetow + *: + *: Redistribution and use in source and binary forms, with or without modi- + *: fication, are permitted provided that the following conditions are met: + *: * Redistributions of source code must retain the above copyright + *: notice, this list of conditions and the following disclaimer. + *: * Redistributions in binary form must reproduce the above copyright + *: notice, this list of conditions and the following disclaimer in the + *: documentation and/or other materials provided with the distribution. + *: * Neither the name of buetow.org nor the names of its contributors may + *: be used to endorse or promote products derived from this software + *: without specific prior written permission. + *: + *: THIS SOFTWARE IS PROVIDED BY PAUL C. BUETOW AS IS'' AND ANY EXPRESS OR + *: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + *: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + *: DISCLAIMED. IN NO EVENT SHALL PAUL C. BUETOW BE LIABLE FOR ANY DIRECT, + *: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + *: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + *: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + *: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + *: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + *: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + *: POSSIBILITY OF SUCH DAMAGE. + *:*/ + +#ifndef ARRAY_H +#define ARRAY_H + +#include <stdlib.h> +#include <string.h> + +#include "../defines.h" + +#define array_get_size(a) a->i_size +#define array_get_used(a) a->i_used +#define array_get_ind(a) (a->i_used - 1) +#define array_empty(a) a->i_size == 0 +#define array_clear(a) array_resize(a, 0) +#define array_get_first(a) array_get(a, 0) +#define array_get_last(a) array_get(a, array_get_size(a)-1) + +typedef struct { + void *p_val; +} ArrayElement; + +typedef struct { + ArrayElement **pp_ae; + int i_used; + int i_size; +} Array; + +typedef struct { + Array *p_array; + int i_cur_pos; +} ArrayIterator; + +Array *array_new(); +Array *array_new_size(int i_size); +Array *array_new_copy(Array *p_array); +void array_delete(Array *p_array); +void array_delete_iterate(Array *p_array, void (*func)(void*)); +void array_set(Array *p_array, int i_index, void *p_val); +void array_insert(Array *p_array, int i_index, void *p_val); +void *array_remove(Array *p_array, int i_index); +void *array_get(Array *p_array, int i_index); +void array_resize(Array *p_array, int i_size); +_Bool array_defined(Array *p_array, int i_index); +void array_print_int(Array *p_array); +void array_splice(Array *p_array, int i_index, Array *p_array2); +void array_push(Array *p_array, void *p_void); +void array_append(Array *p_array, Array *p_array_append); +void array_unshift(Array *p_array, void *p_void); +void array_iterate(Array *p_array, void (*func)(void *)); +void array_iterate2(Array *p_array, void (*func)(void *, void *), + void *p_void); +void array_set_used(Array *p_array, int i_used); + +ArrayElement *arrayelement_new(void *p_val); +void arrayelement_delete(ArrayElement *p_ae); + +ArrayIterator *arrayiterator_new(Array *p_array); +void arrayiterator_delete(ArrayIterator *p_arrayiterator); +_Bool arrayiterator_has_next(ArrayIterator *p_arrayiterator); +void *arrayiterator_next(ArrayIterator *p_arrayiterator); +#endif diff --git a/src/data/cons.c b/src/data/cons.c new file mode 100644 index 0000000..7331167 --- /dev/null +++ b/src/data/cons.c @@ -0,0 +1,88 @@ +/*:* + *: File: ./src/data/cons.c + *: A simple Fype interpreter + *: + *: WWW: http://fype.buetow.org + *: AUTHOR: http://paul.buetow.org + *: E-Mail: fype at dev.buetow.org + *: + *: The Fype Language; (c) 2005 - 2010 - Dipl.-Inform. (FH) Paul C. Buetow + *: + *: Redistribution and use in source and binary forms, with or without modi- + *: fication, are permitted provided that the following conditions are met: + *: * Redistributions of source code must retain the above copyright + *: notice, this list of conditions and the following disclaimer. + *: * Redistributions in binary form must reproduce the above copyright + *: notice, this list of conditions and the following disclaimer in the + *: documentation and/or other materials provided with the distribution. + *: * Neither the name of buetow.org nor the names of its contributors may + *: be used to endorse or promote products derived from this software + *: without specific prior written permission. + *: + *: THIS SOFTWARE IS PROVIDED BY PAUL C. BUETOW AS IS'' AND ANY EXPRESS OR + *: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + *: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + *: DISCLAIMED. IN NO EVENT SHALL PAUL C. BUETOW BE LIABLE FOR ANY DIRECT, + *: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + *: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + *: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + *: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + *: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + *: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + *: POSSIBILITY OF SUCH DAMAGE. + *:*/ + +#include "cons.h" + +Cons* +cons_new(void *p_val) { + Cons *p_cons = malloc(sizeof(Cons)); + + p_cons->p_val = p_val; + p_cons->p_succ = NULL; + + return (p_cons); +} + +void +cons_delete(Cons *p_cons) { + free(p_cons); +} + +void +cons_delete_cb(void *p_cons) { + cons_delete(p_cons); +} + +void +cons_iterate(Cons *p_cons, void (*func)(void *)) { + if (p_cons != NULL && p_cons->p_val != NULL) { + (*func) (p_cons->p_val); + cons_iterate(p_cons->p_succ, func); + } +} + +void* +cons_car(Cons *p_cons) { + return (p_cons->p_val); +} + +Cons* +cons_cdr(Cons *p_cons) { + return (p_cons->p_succ); +} + +Cons* +cons_cons(Cons *p_cons, void *p_val) { + if (p_cons->p_val == NULL) { + p_cons->p_val = p_val; + return (p_cons); + + } else if (p_val == NULL) { + return (p_cons); + } + + Cons *p_new = cons_new(p_val); + p_new->p_succ = p_cons; + return (p_new); +} diff --git a/src/data/cons.h b/src/data/cons.h new file mode 100644 index 0000000..130415b --- /dev/null +++ b/src/data/cons.h @@ -0,0 +1,55 @@ +/*:* + *: File: ./src/data/cons.h + *: A simple Fype interpreter + *: + *: WWW: http://fype.buetow.org + *: AUTHOR: http://paul.buetow.org + *: E-Mail: fype at dev.buetow.org + *: + *: The Fype Language; (c) 2005 - 2010 - Dipl.-Inform. (FH) Paul C. Buetow + *: + *: Redistribution and use in source and binary forms, with or without modi- + *: fication, are permitted provided that the following conditions are met: + *: * Redistributions of source code must retain the above copyright + *: notice, this list of conditions and the following disclaimer. + *: * Redistributions in binary form must reproduce the above copyright + *: notice, this list of conditions and the following disclaimer in the + *: documentation and/or other materials provided with the distribution. + *: * Neither the name of buetow.org nor the names of its contributors may + *: be used to endorse or promote products derived from this software + *: without specific prior written permission. + *: + *: THIS SOFTWARE IS PROVIDED BY PAUL C. BUETOW AS IS'' AND ANY EXPRESS OR + *: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + *: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + *: DISCLAIMED. IN NO EVENT SHALL PAUL C. BUETOW BE LIABLE FOR ANY DIRECT, + *: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + *: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + *: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + *: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + *: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + *: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + *: POSSIBILITY OF SUCH DAMAGE. + *:*/ + +#ifndef CONS_H +#define CONS_H + +#include <stdlib.h> + +#include "../defines.h" + +typedef struct Cons_ { + void *p_val; + struct Cons_ *p_succ; +} Cons; + +Cons *cons_new(void *p_val); +void cons_delete(Cons *p_cons); +void cons_delete_cb(void *p_cons); +void cons_iterate(Cons *p_cons, void (*func)(void *)); +void *cons_car(Cons *p_cons); +Cons *cons_cdr(Cons *p_cons); +Cons *cons_cons(Cons *p_cons, void *p_val); + +#endif diff --git a/src/data/dat.c b/src/data/dat.c new file mode 100644 index 0000000..b448cdc --- /dev/null +++ b/src/data/dat.c @@ -0,0 +1,268 @@ +/*:* + *: File: ./src/data/dat.c + *: A simple Fype interpreter + *: + *: WWW: http://fype.buetow.org + *: AUTHOR: http://paul.buetow.org + *: E-Mail: fype at dev.buetow.org + *: + *: The Fype Language; (c) 2005 - 2010 - Dipl.-Inform. (FH) Paul C. Buetow + *: + *: Redistribution and use in source and binary forms, with or without modi- + *: fication, are permitted provided that the following conditions are met: + *: * Redistributions of source code must retain the above copyright + *: notice, this list of conditions and the following disclaimer. + *: * Redistributions in binary form must reproduce the above copyright + *: notice, this list of conditions and the following disclaimer in the + *: documentation and/or other materials provided with the distribution. + *: * Neither the name of buetow.org nor the names of its contributors may + *: be used to endorse or promote products derived from this software + *: without specific prior written permission. + *: + *: THIS SOFTWARE IS PROVIDED BY PAUL C. BUETOW AS IS'' AND ANY EXPRESS OR + *: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + *: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + *: DISCLAIMED. IN NO EVENT SHALL PAUL C. BUETOW BE LIABLE FOR ANY DIRECT, + *: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + *: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + *: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + *: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + *: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + *: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + *: POSSIBILITY OF SUCH DAMAGE. + *:*/ + +#include "dat.h" + +#include <stdlib.h> +#include "../defines.h" + +Dat* +dat_new() { + Dat *p_dat = (Dat *) malloc(sizeof(Dat)); + + p_dat->p_first = 0; + p_dat->p_last = 0; + p_dat->i_size = 0; + + return (p_dat); +} + +DatElem* +datelem_new() { + return (datelem_new_t(TYPE_UNKNOWN)); +} + +DatElem* +datelem_new_t(TYPE type) { + DatElem *p_elem = (DatElem *) malloc(sizeof(DatElem)); + + p_elem->p_next = 0; + p_elem->p_val = 0; + p_elem->type = type; + + return (p_elem); +} + +_Bool +dat_empty(Dat *p_dat) { + if (p_dat == NULL) + return (false); + + return (p_dat->i_size == 0); +} + +void +dat_push(Dat *p_dat, void *p_val) { + dat_push_t(p_dat, p_val, TYPE_UNKNOWN); +} + +void +dat_push_t(Dat *p_dat, void *p_val, TYPE type) { + DatElem *p_elem = datelem_new_t(type); + p_elem->p_val = p_val; + + if (0 == p_dat->i_size++) + p_dat->p_first = p_elem; + else + p_dat->p_last->p_next = p_elem; + + p_dat->p_last = p_elem; +} + +void* +dat_pop(Dat *p_dat) { + TYPE type; + return (dat_pop_t(p_dat, &type)); +} + +void* +dat_pop_t(Dat *p_dat, TYPE *p_type) { + if (dat_empty(p_dat)) + return (NULL); + + DatElem *p_elem = p_dat->p_first; + p_dat->p_first = p_elem->p_next; + + --p_dat->i_size; + + void *p_ret = p_elem->p_val; + *p_type = p_elem->type; + free(p_elem); + return (p_ret); +} + +void +dat_clear(Dat *p_dat) { + for (;!dat_empty(p_dat); dat_pop(p_dat)); +} + +void +dat_delete(Dat *p_dat) { + dat_clear(p_dat); + free(p_dat); +} + +unsigned +dat_size(Dat *p_dat) { + return (p_dat->i_size); +} + +void +dat_iterate(Dat *p_dat, void (*func)(void *)) { + DatElem *p_elem = p_dat->p_first; + while (p_elem) { + if (p_elem->p_val) + (*func) (p_elem->p_val); + + p_elem = p_elem->p_next; + } +} + +void +dat_iterate_t(Dat *p_dat, void (*func)(void *, TYPE)) { + DatElem *p_elem = p_dat->p_first; + while (p_elem) { + if (p_elem->p_val) + (*func) (p_elem->p_val, p_elem->type); + + p_elem = p_elem->p_next; + } +} + +void +dat_iterate_tl(Dat *p_dat, void (*func)(void *, TYPE, _Bool)) { + DatElem *p_elem = p_dat->p_first; + while (p_elem) { + if (p_elem->p_val) + (*func) (p_elem->p_val, p_elem->type, p_elem->p_next == NULL); + + p_elem = p_elem->p_next; + } +} + +void* +dat_first(Dat *p_dat) { + if (dat_empty(p_dat)) + return (NULL); + + return (p_dat->p_first->p_val); +} + +void* +dat_second(Dat *p_dat) { + if (2 > dat_size(p_dat)) + return (NULL); + + return (p_dat->p_first->p_next->p_val); +} + +void* +dat_last(Dat *p_dat) { + if (dat_empty(p_dat)) + return (NULL); + + return (p_dat->p_last->p_val); +} + +void* +dat_first_t(Dat *p_dat, TYPE *p_type) { + if (dat_empty(p_dat)) + return (NULL); + + *p_type = p_dat->p_first->type; + return (p_dat->p_first->p_val); +} + +void* +dat_second_t(Dat *p_dat, TYPE *p_type) { + if (2 > dat_size(p_dat)) + return (NULL); + + *p_type = p_dat->p_first->p_next->type; + return (p_dat->p_first->p_next->p_val); +} + +void* +dat_last_t(Dat *p_dat, TYPE *p_type) { + if (dat_empty(p_dat)) + return (NULL); + + *p_type = p_dat->p_last->type; + return (p_dat->p_last->p_val); +} + +DatIter* +datiter_new(Dat *p_dat) { + DatIter *p_iter = + (DatIter *) malloc(sizeof(DatIter)); + + p_iter->p_current = NULL; + p_iter->p_next = p_dat->p_first; + p_iter->i_left = dat_size(p_dat); + p_iter->p_dat = p_dat; + + return (p_iter); +} + +void +datiter_delete(DatIter *p_iter) { + free(p_iter); +} + +void +datiter_skip(DatIter *p_iter, unsigned i_num) { + for (int i = 0; i < i_num; ++i) + datiter_next(p_iter); +} + +void* +datiter_next(DatIter *p_iter) { + TYPE type; + return (datiter_next_t(p_iter, &type)); +} + +void* +datiter_next_t(DatIter *p_iter, TYPE *p_type) { + if (p_iter->p_next == NULL) + return (NULL); + + void *p_ret = p_iter->p_next->p_val; + *p_type = p_iter->p_next->type; + p_iter->p_current = p_iter->p_next; + p_iter->p_next = p_iter->p_next->p_next; + --p_iter->i_left; + + return (p_ret); +} + +unsigned +datiter_left(DatIter *p_iter) { + return (p_iter->i_left); +} + +Dat* +datiter_dat(DatIter *p_iter) { + return (p_iter->p_dat); +} + diff --git a/src/data/dat.h b/src/data/dat.h new file mode 100644 index 0000000..dcc7928 --- /dev/null +++ b/src/data/dat.h @@ -0,0 +1,88 @@ +/*:* + *: File: ./src/data/dat.h + *: A simple Fype interpreter + *: + *: WWW: http://fype.buetow.org + *: AUTHOR: http://paul.buetow.org + *: E-Mail: fype at dev.buetow.org + *: + *: The Fype Language; (c) 2005 - 2010 - Dipl.-Inform. (FH) Paul C. Buetow + *: + *: Redistribution and use in source and binary forms, with or without modi- + *: fication, are permitted provided that the following conditions are met: + *: * Redistributions of source code must retain the above copyright + *: notice, this list of conditions and the following disclaimer. + *: * Redistributions in binary form must reproduce the above copyright + *: notice, this list of conditions and the following disclaimer in the + *: documentation and/or other materials provided with the distribution. + *: * Neither the name of buetow.org nor the names of its contributors may + *: be used to endorse or promote products derived from this software + *: without specific prior written permission. + *: + *: THIS SOFTWARE IS PROVIDED BY PAUL C. BUETOW AS IS'' AND ANY EXPRESS OR + *: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + *: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + *: DISCLAIMED. IN NO EVENT SHALL PAUL C. BUETOW BE LIABLE FOR ANY DIRECT, + *: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + *: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + *: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + *: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + *: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + *: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + *: POSSIBILITY OF SUCH DAMAGE. + *:*/ + +#ifndef DAT_H +#define DAT_H + +#include "types.h" + +typedef struct DatElem_ { + struct DatElem_ *p_next; + void *p_val; + TYPE type; +} DatElem; + +typedef struct { + DatElem *p_first; + DatElem *p_last; + unsigned i_size; +} Dat; + +typedef struct { + unsigned i_left; + Dat *p_dat; + DatElem *p_current; + DatElem *p_next; +} DatIter; + +Dat *dat_new(); +DatElem *datelem_new(); +DatElem *datelem_new_t(TYPE type); +_Bool dat_empty(Dat *p_dat); +void dat_push(Dat *p_dat, void *p_val); +void dat_push_t(Dat *p_dat, void *p_val, TYPE type); +void *dat_pop(Dat *p_dat); +void *dat_pop_t(Dat *p_dat, TYPE *p_type); +void dat_clear(Dat *p_dat); +void dat_delete(Dat *p_dat); +unsigned dat_size(Dat *p_dat); +void dat_iterate(Dat *p_dat, void (*func)(void *)); +void dat_iterate_t(Dat *p_dat, void (*func)(void *, TYPE)); +void dat_iterate_tl(Dat *p_dat, void (*func)(void *, TYPE, _Bool)); +void *dat_first(Dat *p_dat); +void *dat_second(Dat *p_dat); +void *dat_last(Dat *p_dat); +void *dat_first_t(Dat *p_dat, TYPE *p_type); +void *dat_second_t(Dat *p_dat, TYPE *p_type); +void *dat_last_t(Dat *p_dat, TYPE *p_type); + +DatIter *datiter_new(Dat *p_dat); +void datiter_delete(DatIter *p_iter); +void datiter_skip(DatIter *p_iter, unsigned i_num); +void *datiter_next(DatIter *p_iter); +void *datiter_next_t(DatIter *p_iter, TYPE *p_type); +unsigned datiter_left(DatIter *p_iter); +Dat *datiter_dat(DatIter *p_iter); + +#endif diff --git a/src/data/hash.c b/src/data/hash.c new file mode 100644 index 0000000..e196840 --- /dev/null +++ b/src/data/hash.c @@ -0,0 +1,306 @@ +/*:* + *: File: ./src/data/hash.c + *: A simple Fype interpreter + *: + *: WWW: http://fype.buetow.org + *: AUTHOR: http://paul.buetow.org + *: E-Mail: fype at dev.buetow.org + *: + *: The Fype Language; (c) 2005 - 2010 - Dipl.-Inform. (FH) Paul C. Buetow + *: + *: Redistribution and use in source and binary forms, with or without modi- + *: fication, are permitted provided that the following conditions are met: + *: * Redistributions of source code must retain the above copyright + *: notice, this list of conditions and the following disclaimer. + *: * Redistributions in binary form must reproduce the above copyright + *: notice, this list of conditions and the following disclaimer in the + *: documentation and/or other materials provided with the distribution. + *: * Neither the name of buetow.org nor the names of its contributors may + *: be used to endorse or promote products derived from this software + *: without specific prior written permission. + *: + *: THIS SOFTWARE IS PROVIDED BY PAUL |
