chiark / gitweb /
New array adjustment macros for unsigned arguments.
[mLib] / darray.h
index 93a806f7454f23eeb3bc5c7e6eb1ee999aa21c21..c04ca8bd24c479a6650c45359ba43a64376d37f7 100644 (file)
--- 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.2 1999/10/29 22:59:22 mdw Exp $
  *
  * Dynamically growing dense arrays
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: darray.h,v $
+ * 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'.
  *
@@ -185,6 +188,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 +282,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 +335,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@ --- *