From 706bf01d0f923c5c6d1d63d8ae068ef29ffaefe0 Mon Sep 17 00:00:00 2001 Message-Id: <706bf01d0f923c5c6d1d63d8ae068ef29ffaefe0.1714089765.git.mdw@distorted.org.uk> From: Mark Wooding Date: Sat, 3 Mar 2001 12:20:23 +0000 Subject: [PATCH] New macros @DA_FIRST@ and @DA_LAST@ for stack/queue peeking. Organization: Straylight/Edgeware From: mdw --- Makefile.am | 13 ++++++++----- da-gtest | 6 +++++- da-ref | 4 ++++ da-test.c | 4 ++++ darray.h | 27 +++++++++++++++++++++++++-- man/darray.3 | 11 +++++++++++ 6 files changed, 57 insertions(+), 8 deletions(-) diff --git a/Makefile.am b/Makefile.am index ded3be5..0181434 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,6 +1,6 @@ ## -*-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 ## @@ -29,6 +29,9 @@ ##----- 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. ## @@ -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.in: +da-test.in: $(srcdir)/da-gtest 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.ref da-test.test: da-test da-test.in da-test.ref ./da-test 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.in: +sym-test.in: $(srcdir)/sym-gtest 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.ref sym-test.test: sym-test sym-test.in sym-test.ref ./sym-test sym-test.test diff --git a/da-gtest b/da-gtest index 4412e49..0348833 100755 --- 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 +# first, last -- show first or last item # 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) { - $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") { @@ -61,6 +62,9 @@ while ($lines) { } 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; } diff --git a/da-ref b/da-ref index 2c1e592..aed94a4 100755 --- a/da-ref +++ b/da-ref @@ -56,6 +56,10 @@ while (<>) { } 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"; diff --git a/da-test.c b/da-test.c index 4b1966a..8c6a7bb 100644 --- a/da-test.c +++ b/da-test.c @@ -103,6 +103,10 @@ int main(void) 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*"); diff --git a/darray.h b/darray.h index b8f17eb..003c6d7 100644 --- a/darray.h +++ b/darray.h @@ -1,6 +1,6 @@ /* -*-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 * @@ -30,6 +30,9 @@ /*----- 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. * @@ -410,6 +413,26 @@ typedef struct da_base { /*----- 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) @@ -462,7 +485,7 @@ typedef struct da_base { */ #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++, \ diff --git a/man/darray.3 b/man/darray.3 index b0669aa..db50fd1 100644 --- a/man/darray.3 +++ b/man/darray.3 @@ -43,6 +43,8 @@ darray \- dense, dynamically resizing arrays .\" @DA_UNSAFE_SHRINK .\" @DA_UNSLIDE .\" @DA_UNSAFE_UNSLIDE +.\" @DA_FIRST +.\" @DA_LAST .\" @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 ); +.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 ); @@ -329,6 +333,13 @@ and 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 -- [mdw]