X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/3745e24b5b5526174fda7f83999a0ca301c4c146..120800815c7572b685ca8c9e34efd461a571aa68:/darray.h diff --git a/darray.h b/darray.h index 93a806f..f2ebe1f 100644 --- a/darray.h +++ b/darray.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: darray.h,v 1.1 1999/10/22 22:37:26 mdw Exp $ + * $Id: darray.h,v 1.3 1999/11/05 14:32:43 mdw Exp $ * * Dynamically growing dense arrays * @@ -30,6 +30,12 @@ /*----- Revision history --------------------------------------------------* * * $Log: darray.h,v $ + * Revision 1.3 1999/11/05 14:32:43 mdw + * Minor change in argument naming. + * + * Revision 1.2 1999/10/29 22:59:22 mdw + * New array adjustment macros for unsigned arguments. + * * Revision 1.1 1999/10/22 22:37:26 mdw * New dynamic array implementation replaces `dynarray.h'. * @@ -105,8 +111,8 @@ typedef struct da_base { * Use: Declares a structure for decribing a dynamic array. */ -#define DA_DECL(atype, type) \ - typedef struct atype { da_base b; type *v; } atype +#define DA_DECL(type_v, type) \ + typedef struct type_v { da_base b; type *v; } type_v /*----- Initialization, creation and destruction --------------------------*/ @@ -185,6 +191,16 @@ typedef struct da_base { #define DA_TIDY(a) ((a)->v = da_tidy(&(a)->b, (a)->v, sizeof((a)->v[0]))) +/* --- @DA_RESET@ --- * + * + * Arguments: @a@ = pointer to array block + * + * Use: Removes all the items from the named array. This might not + * be a good idea. No storage is freed. + */ + +#define DA_RESET(a) ((a)->b.len = 0) + /*----- Access operations -------------------------------------------------*/ /* --- @DA@ --- * @@ -269,7 +285,7 @@ typedef struct da_base { DA_UNSAFE_EXTEND(a, n); \ } while (0) -/* --- @DA_EXTEND@ --- * +/* --- @DA_UNSAFE_EXTEND@ --- * * * Arguments: @a@ = pointer to array block (multiply evaluated) * @n@ = number of slots to add (multiply evaluated) @@ -322,6 +338,68 @@ typedef struct da_base { (a)->b.off -= (n); \ } while (0) +/* --- @DA_SHRINK@ --- * + * + * Arguments: @a@ = pointer to array block (multiply evaluated) + * @n@ = number of slots to remove (multiply evaluated) + * + * Use: As for @DA_EXTEND@, with the sense of the argument reversed. + */ + +#define DA_SHRINK(a, n) do { \ + if ((n) > 0 && (n) > DA_LEN(a)) \ + THROW(DAEXC_UFLOW); \ + else if ((n) < 0 && -(n) > DA_SPARE(a)) \ + THROW(DAEXC_OFLOW); \ + DA_UNSAFE_SHRINK(a, n); \ +} while (0) + +/* --- @DA_UNSAFE_SHRINK@ --- * + * + * Arguments: @a@ = pointer to array block (multiply evaluated) + * @n@ = number of slots to add (multiply evaluated) + * + * Use: As for @DA_SHRINK@, only it doesn't check for errors. + */ + +#define DA_UNSAFE_SHRINK(a, n) do { \ + (a)->b.len -= (n); \ +} while (0) + +/* --- @DA_UNSLIDE@ --- * + * + * Arguments: @a@ = pointer to array block (multiply evaluated) + * @n@ = number of positions to slide the array (multiply + * evaluated) + * + * + * Use: As for @DA_SLIDE@, only in the other direction. + */ + +#define DA_UNSLIDE(a, n) do { \ + if ((n) > 0 && (n) > DA_LEN(a)) \ + THROW(DAEXC_UFLOW); \ + else if ((n) < 0 && -(n) > DA_OFFSET(a)) \ + THROW(DAEXC_OFLOW); \ + DA_UNSAFE_UNSLIDE((a), (n)); \ +} while (0) + +/* --- @DA_UNSAFE_UNSLIDE@ --- * + * + * Arguments: @a@ = pointer to array block (multiply evaluated) + * @n@ = number of positions to slide the array (multiply + * evaluated) + * + * Use: As for @DA_UNSLIDE@, only it doesn't check for errors. + */ + +#define DA_UNSAFE_UNSLIDE(a, n) do { \ + (a)->v += (n); \ + (a)->b.len -= (n); \ + (a)->b.sz -= (n); \ + (a)->b.off += (n); \ +} while (0) + /*----- Stack-like operations ---------------------------------------------*/ /* --- @DA_PUSH@ --- *