From: Pierre Habouzit Date: Sat, 18 Nov 2006 17:19:58 +0000 (+0100) Subject: *oops* I forgot to add those. X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=e176d53ddc6abf804824cad054a424f45c0a945e;p=apps%2Fmadmutt.git *oops* I forgot to add those. Signed-off-by: Pierre Habouzit --- diff --git a/lib-lib/array.c b/lib-lib/array.c new file mode 100644 index 0000000..c497462 --- /dev/null +++ b/lib-lib/array.c @@ -0,0 +1,50 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + * Copyright © 2006 Pierre Habouzit + */ + +#include "lib-lib.h" + +void *__array_take(__array *a, ssize_t pos) +{ + void *ptr; + + if (pos >= a->len || pos < 0) { + return NULL; + } + + ptr = a->arr[pos]; + a->len--; + memmove(a->arr + pos, a->arr + pos + 1, (a->len - pos) * sizeof(void*)); + return ptr; +} + +#define SIZE_AUGMENT 32 + +void __array_insert(__array *a, ssize_t pos, void *item) +{ + pos = MAX(0, MIN(a->len, pos)); + + if (a->len >= a->size) { + p_realloc(&a->arr, a->size + SIZE_AUGMENT); + a->size += SIZE_AUGMENT; + } + + memmove(a->arr + pos + 1, a->arr + pos, (a->len - pos) * sizeof(void *)); + a->arr[pos] = item; + a->len++; +} diff --git a/lib-lib/array.h b/lib-lib/array.h new file mode 100644 index 0000000..f0f6c9c --- /dev/null +++ b/lib-lib/array.h @@ -0,0 +1,60 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + * Copyright © 2006 Pierre Habouzit + */ + +#ifndef MUTT_LIB_LIB_ARRAY_H +#define MUTT_LIB_LIB_ARRAY_H + +#define DO_ARRAY_TYPE(type, prefix) \ + typedef struct prefix##_array { \ + type **arr; \ + ssize_t len; \ + ssize_t size; \ + } prefix##_array + +#define DO_ARRAY_FUNCS(type, prefix, dtor) \ + \ + static inline void prefix##_array_init(prefix##_array *array) { \ + p_clear(array, 1); \ + } \ + static inline void \ + prefix##_array_wipe(prefix##_array *array) { \ + while (array->len) { \ + (dtor)(&array->arr[--array->len]); \ + } \ + p_delete(&array->arr); \ + } \ + \ + static inline void \ + prefix##_array_insert(prefix##_array *array, ssize_t pos, type *item) { \ + __array_insert((__array *)array, pos, (void*)item); \ + } \ + static inline void \ + prefix##_array_append(prefix##_array *array, type *item) { \ + __array_insert((__array *)array, array->len, (void*)item); \ + } \ + static inline type * \ + prefix##_array_take(prefix##_array *array, ssize_t pos) { \ + return (type *)__array_take((__array *)array, pos); \ + } + +DO_ARRAY_TYPE(void, _); +void __array_insert(__array *array, ssize_t pos, void *item); +void *__array_take(__array *array, ssize_t pos); + +#endif /* MUTT_LIB_LIB_ARRAY_H */