chiark / gitweb /
New macros @DA_FIRST@ and @DA_LAST@ for stack/queue peeking. 2.0.0pre4
authormdw <mdw>
Sat, 3 Mar 2001 12:20:23 +0000 (12:20 +0000)
committermdw <mdw>
Sat, 3 Mar 2001 12:20:23 +0000 (12:20 +0000)
Makefile.am
da-gtest
da-ref
da-test.c
darray.h
man/darray.3

index ded3be54fa28cd95b07f265e2e1861da55894904..0181434c500b54fe5ca987bb5bd9ea636349290a 100644 (file)
@@ -1,6 +1,6 @@
 ## -*-Makefile-*-
 ##
 ## -*-Makefile-*-
 ##
-## $Id: Makefile.am,v 1.31 2001/02/03 18:43:56 mdw Exp $
+## $Id: Makefile.am,v 1.32 2001/03/03 12:20:23 mdw Exp $
 ##
 ## Building the distribution
 ##
 ##
 ## Building the distribution
 ##
@@ -29,6 +29,9 @@
 ##----- Revision history ----------------------------------------------------
 ##
 ## $Log: Makefile.am,v $
 ##----- Revision history ----------------------------------------------------
 ##
 ## $Log: Makefile.am,v $
+## Revision 1.32  2001/03/03 12:20:23  mdw
+## New macros @DA_FIRST@ and @DA_LAST@ for stack/queue peeking.
+##
 ## Revision 1.31  2001/02/03 18:43:56  mdw
 ## New feature: watch a file for changes.
 ##
 ## Revision 1.31  2001/02/03 18:43:56  mdw
 ## New feature: watch a file for changes.
 ##
@@ -160,9 +163,9 @@ check: da-test.test sym-test.test assoc-test.test bits-test
 
 da_test_LDADD = libmLib.la
 da_test_LDFLAGS = -static
 
 da_test_LDADD = libmLib.la
 da_test_LDFLAGS = -static
-da-test.in:
+da-test.in: $(srcdir)/da-gtest
        perl $(srcdir)/da-gtest 10000 >da-test.in
        perl $(srcdir)/da-gtest 10000 >da-test.in
-da-test.ref: da-test.in
+da-test.ref: da-test.in $(srcdir)/da-ref
        perl $(srcdir)/da-ref <da-test.in >da-test.ref
 da-test.test: da-test da-test.in da-test.ref
        ./da-test <da-test.in >da-test.test
        perl $(srcdir)/da-ref <da-test.in >da-test.ref
 da-test.test: da-test da-test.in da-test.ref
        ./da-test <da-test.in >da-test.test
@@ -171,9 +174,9 @@ da-test.test: da-test da-test.in da-test.ref
 
 sym_test_LDADD = libmLib.la
 sym_test_LDFLAGS = -static
 
 sym_test_LDADD = libmLib.la
 sym_test_LDFLAGS = -static
-sym-test.in:
+sym-test.in: $(srcdir)/sym-gtest
        perl $(srcdir)/sym-gtest 10000 >sym-test.in
        perl $(srcdir)/sym-gtest 10000 >sym-test.in
-sym-test.ref: sym-test.in
+sym-test.ref: sym-test.in $(srcdir)/sym-ref
        perl $(srcdir)/sym-ref <sym-test.in >sym-test.ref
 sym-test.test: sym-test sym-test.in sym-test.ref
        ./sym-test <sym-test.in >sym-test.test
        perl $(srcdir)/sym-ref <sym-test.in >sym-test.ref
 sym-test.test: sym-test sym-test.in sym-test.ref
        ./sym-test <sym-test.in >sym-test.test
index 4412e498df6ded36a2dd6b633775c7df9645dca4..0348833597e8d96ad1f83b0029e5132d087814ee 100755 (executable)
--- a/da-gtest
+++ b/da-gtest
@@ -11,6 +11,7 @@
 #   reduce n -- remove n items from end
 #   set i n -- assign item at index i to be n
 #   get i -- display item at index i
 #   reduce n -- remove n items from end
 #   set i n -- assign item at index i to be n
 #   get i -- display item at index i
+#   first, last -- show first or last item
 #   show -- write entire array to stdout, space separated on one line
 
 sub random ($) {
 #   show -- write entire array to stdout, space separated on one line
 
 sub random ($) {
@@ -22,7 +23,7 @@ $lines = shift || 100;
 $max = 0; # Estimate of size of array
 $serial = 1;
 while ($lines) {
 $max = 0; # Estimate of size of array
 $serial = 1;
 while ($lines) {
-  $what = random(17);
+  $what = random(21);
   if ($what < 8) {
     my $op = (qw(push pop shift unshift))[$what % 4];
     if ($op eq "push" || $op eq "unshift") {
   if ($what < 8) {
     my $op = (qw(push pop shift unshift))[$what % 4];
     if ($op eq "push" || $op eq "unshift") {
@@ -61,6 +62,9 @@ while ($lines) {
     } else {
       print "get $i\n";
     }
     } else {
       print "get $i\n";
     }
+  } elsif ($what < 20) {
+    my $op = (qw(first last))[$what % 2];
+    print "$op\n" if $max;
   } elsif (random(10) == 0) {
     print "show\n";
   } else { next; }
   } elsif (random(10) == 0) {
     print "show\n";
   } else { next; }
diff --git a/da-ref b/da-ref
index 2c1e592742aae2166575d348721944dee077b306..aed94a45f98e2d426f30a30c1af376007eda208d 100755 (executable)
--- a/da-ref
+++ b/da-ref
@@ -56,6 +56,10 @@ while (<>) {
     } else {
       print int($a[$F[1]]), "\n";
     }
     } else {
       print int($a[$F[1]]), "\n";
     }
+  } elsif ($F[0] eq "first") {
+    print int($a[0]), "\n";
+  } elsif ($F[0] eq "last") {
+    print int($a[$#a]), "\n";
   } elsif ($F[0] eq "show") {
     if (@a) {
       print join(" ", map int, @a), "\n";
   } elsif ($F[0] eq "show") {
     if (@a) {
       print join(" ", map int, @a), "\n";
index 4b1966ae7c9c9bbf4919c6b6690e54925429aefa..8c6a7bba98183dbe456a1d4e82c87b65bedae4c5 100644 (file)
--- a/da-test.c
+++ b/da-test.c
@@ -103,6 +103,10 @@ int main(void)
          puts("*RANGE*");
        else
          printf("%i\n", DA(&v)[i]);
          puts("*RANGE*");
        else
          printf("%i\n", DA(&v)[i]);
+      } else if (strcmp(p, "first") == 0) {
+       printf("%i\n", DA_FIRST(&v));
+      } else if (strcmp(p, "last") == 0) {
+       printf("%i\n", DA_LAST(&v));
       } else if (strcmp(p, "show") == 0) {
        if (DA_LEN(&v) == 0)
          puts("*EMPTY*");
       } else if (strcmp(p, "show") == 0) {
        if (DA_LEN(&v) == 0)
          puts("*EMPTY*");
index b8f17eb0499bf5418b23346b46c828abf00242d2..003c6d7aa5d4aeaeaa2b12126f8bcef088ac5510 100644 (file)
--- a/darray.h
+++ b/darray.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
 /* -*-c-*-
  *
- * $Id: darray.h,v 1.5 2000/06/17 10:37:39 mdw Exp $
+ * $Id: darray.h,v 1.6 2001/03/03 12:20:23 mdw Exp $
  *
  * Dynamically growing dense arrays
  *
  *
  * Dynamically growing dense arrays
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: darray.h,v $
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: darray.h,v $
+ * Revision 1.6  2001/03/03 12:20:23  mdw
+ * New macros @DA_FIRST@ and @DA_LAST@ for stack/queue peeking.
+ *
  * Revision 1.5  2000/06/17 10:37:39  mdw
  * Add support for arena management.
  *
  * Revision 1.5  2000/06/17 10:37:39  mdw
  * Add support for arena management.
  *
@@ -410,6 +413,26 @@ typedef struct da_base {
 
 /*----- Stack-like operations ---------------------------------------------*/
 
 
 /*----- Stack-like operations ---------------------------------------------*/
 
+/* --- @DA_FIRST@ --- *
+ *
+ * Arguments:  @a@ = pointer to an array block (multiply evaluated)
+ *
+ * Use:                Evaluates to the initial element in array @a@.  It is unsafe
+ *             to do this if the array is empty.  The array is not changed. 
+ */
+
+#define DA_FIRST(a) (DA(a)[0])
+
+/* --- @DA_LAST@ --- *
+ *
+ * Arguments:  @a@ = pointer to an array block (multiply evaluated)
+ *
+ * Use:                Evaluates to the final element in array @a@.  It is unsafe
+ *             to do this if the array is empty.  The array is not changed.
+ */
+
+#define DA_LAST(a) (DA(a)[(a)->b.len - 1])
+
 /* --- @DA_PUSH@ --- *
  *
  * Arguments:  @a@ = pointer to an array block (multiply evaluated)
 /* --- @DA_PUSH@ --- *
  *
  * Arguments:  @a@ = pointer to an array block (multiply evaluated)
@@ -462,7 +485,7 @@ typedef struct da_base {
  */
 
 #define DA_SHIFT(a)                                                    \
  */
 
 #define DA_SHIFT(a)                                                    \
-  ((a)->b.len ? ((void)0) : THROW(DAEXC_UFLOW),                        \
+  ((a)->b.len ? ((void)0) : THROW(DAEXC_UFLOW),                                \
    (a)->b.len--,                                                       \
    (a)->b.sz--,                                                                \
    (a)->b.off++,                                                       \
    (a)->b.len--,                                                       \
    (a)->b.sz--,                                                                \
    (a)->b.off++,                                                       \
index b0669aa8faf6374b776595558e3baaf5fdaca7e3..db50fd12c1be29c3076bc7fe43bef88e860564a9 100644 (file)
@@ -43,6 +43,8 @@ darray \- dense, dynamically resizing arrays
 .\" @DA_UNSAFE_SHRINK
 .\" @DA_UNSLIDE
 .\" @DA_UNSAFE_UNSLIDE
 .\" @DA_UNSAFE_SHRINK
 .\" @DA_UNSLIDE
 .\" @DA_UNSAFE_UNSLIDE
+.\" @DA_FIRST
+.\" @DA_LAST
 .\" @DA_PUSH
 .\" @DA_POP
 .\" @DA_UNSHIFT
 .\" @DA_PUSH
 .\" @DA_POP
 .\" @DA_UNSHIFT
@@ -82,6 +84,8 @@ darray \- dense, dynamically resizing arrays
 .BI "void DA_UNSAFE_SLIDE(" type_v " *" a ", long " n );
 .BI "void DA_UNSAFE_UNSLIDE(" type_v " *" a ", long " n );
 
 .BI "void DA_UNSAFE_SLIDE(" type_v " *" a ", long " n );
 .BI "void DA_UNSAFE_UNSLIDE(" type_v " *" a ", long " n );
 
+.IB type " DA_FIRST(" type_v " *" a );
+.IB type " DA_LAST(" type_v " *" a );
 .BI "void DA_PUSH(" type_v " *" a ", " type " " x );
 .IB type " DA_POP(" type_v " *" a );
 .BI "void DA_UNSHIFT(" type_v " *" a ", " type " " x );
 .BI "void DA_PUSH(" type_v " *" a ", " type " " x );
 .IB type " DA_POP(" type_v " *" a );
 .BI "void DA_UNSHIFT(" type_v " *" a ", " type " " x );
@@ -329,6 +333,13 @@ and
 can fail because the array is empty, in which case
 .B DAEXC_UFLOW
 is thrown.
 can fail because the array is empty, in which case
 .B DAEXC_UFLOW
 is thrown.
+.PP
+The operations
+.B DA_FIRST
+and
+.B DA_LAST
+are lvalues referring to the first and last elements in the array
+respectively.  They are unsafe if the array is empty.
 .SS "Low-level details"
 This section describes low-level details of the dynamic array
 implementation.  You should try to avoid making use of this information
 .SS "Low-level details"
 This section describes low-level details of the dynamic array
 implementation.  You should try to avoid making use of this information