]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
- Split Stack into Array and Stack.
authorrousskov <>
Sat, 21 Mar 1998 01:07:34 +0000 (01:07 +0000)
committerrousskov <>
Sat, 21 Mar 1998 01:07:34 +0000 (01:07 +0000)
include/Array.h [new file with mode: 0644]
include/Stack.h
lib/Array.cc [new file with mode: 0644]
lib/Makefile.in
lib/Stack.c

diff --git a/include/Array.h b/include/Array.h
new file mode 100644 (file)
index 0000000..2b2ab5f
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * $Id: Array.h,v 1.1 1998/03/20 18:07:34 rousskov Exp $
+ *
+ * AUTHOR: Alex Rousskov
+ *
+ * SQUID Internet Object Cache  http://squid.nlanr.net/Squid/
+ * --------------------------------------------------------
+ *
+ *  Squid is the result of efforts by numerous individuals from the
+ *  Internet community.  Development is led by Duane Wessels of the
+ *  National Laboratory for Applied Network Research and funded by
+ *  the National Science Foundation.
+ *
+ *  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., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  
+ */
+
+#ifndef _ARRAY_H_
+#define _ARRAY_H_
+
+/* see Array.c for more documentation */
+
+typedef struct {
+    int capacity;
+    int count;
+    void **items;
+} Array;
+
+
+extern Array *arrayCreate();
+extern void arrayInit(Array * s);
+extern void arrayClean(Array * s);
+extern void arrayDestroy(Array *s);
+extern void arrayAppend(Array *s, void *obj);
+extern void arrayPreAppend(Array * s, int app_count);
+
+
+#endif /* ndef _ARRAY_H_ */
index e313fe6190a0ee8cdbf46ea22a84faf0e18aba6b..30abb4bc9ee4594181b255d61dd19700d14989bc 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: Stack.h,v 1.5 1998/03/11 22:18:42 rousskov Exp $
+ * $Id: Stack.h,v 1.6 1998/03/20 18:07:35 rousskov Exp $
  *
  * AUTHOR: Alex Rousskov
  *
 #ifndef _STACK_H_
 #define _STACK_H_
 
-/* see Stack.c for more documentation */
+#include "Array.h"
 
-struct _Stack {
-    /* public, read only */
-    size_t capacity;
+typedef Array Stack;
 
-    /* protected, do not use these, use interface functions instead */
-    int count;
-    void **items;
-};
-
-typedef struct _Stack Stack;
-
-extern Stack *stackCreate();
-extern void stackInit(Stack * s);
-extern void stackClean(Stack * s);
-extern void stackDestroy(Stack *s);
+#define stackCreate arrayCreate
+#define stackInit arrayInit
+#define stackClean arrayClean
+#define stackDestroy arrayDestroy
 extern void *stackPop(Stack *s);
-extern void stackPush(Stack *s, void *obj);
-extern void stackPrePush(Stack * s, int push_count);
-
+#define stackPush arrayAppend
+#define stackPrePush arrayPreAppend
 
 #endif /* ndef _STACK_H_ */
diff --git a/lib/Array.cc b/lib/Array.cc
new file mode 100644 (file)
index 0000000..55e1d4b
--- /dev/null
@@ -0,0 +1,121 @@
+/*
+ * $Id: Array.cc,v 1.1 1998/03/20 18:07:35 rousskov Exp $
+ *
+ * AUTHOR: Alex Rousskov
+ *
+ * SQUID Internet Object Cache  http://squid.nlanr.net/Squid/
+ * --------------------------------------------------------
+ *
+ *  Squid is the result of efforts by numerous individuals from the
+ *  Internet community.  Development is led by Duane Wessels of the
+ *  National Laboratory for Applied Network Research and funded by
+ *  the National Science Foundation.
+ *
+ *  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., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  
+ */
+
+/*
+ * Array is an array of (void*) items with unlimited capacity
+ *
+ * Array grows when arrayAppend() is called and no space is left
+ * Currently, array does not have an interface for deleting an item because
+ *     we do not need such an interface yet.
+ */
+
+
+#include "config.h"
+#if HAVE_ASSERT_H
+#include <assert.h>
+#endif
+#if HAVE_STRING_H
+#include <string.h>
+#endif
+#include "util.h"
+#include "Array.h"
+
+static void arrayGrow(Array * a, int min_capacity);
+
+Array *
+arrayCreate()
+{
+    Array *a = xmalloc(sizeof(Array));
+    arrayInit(a);
+    return a;
+}
+
+void
+arrayInit(Array * a)
+{
+    assert(a);
+    memset(a, 0, sizeof(Array));
+}
+
+void
+arrayClean(Array * a)
+{
+    assert(a);
+    /* could also warn if some objects are left */
+    xfree(a->items);
+    a->items = NULL;
+}
+
+void
+arrayDestroy(Array * a)
+{
+    assert(a);
+    arrayClean(a);
+    xfree(a);
+}
+
+void
+arrayAppend(Array * a, void *obj)
+{
+    assert(a);
+    if (a->count >= a->capacity)
+       arrayGrow(a, a->count+1);
+    a->items[a->count++] = obj;
+}
+
+/* if you are going to append a known and large number of items, call this first */
+void
+arrayPreAppend(Array * a, int app_count)
+{
+    assert(a);
+    if (a->count + app_count > a->capacity)
+       arrayGrow(a, a->count + app_count);
+}
+
+/* grows internal buffer to satisfy required minimal capacity */
+static void
+arrayGrow(Array * a, int min_capacity)
+{
+    const int min_delta = 16;
+    int delta;
+    assert(a->capacity < min_capacity);
+    delta = min_capacity;
+    /* make delta a multiple of min_delta */
+    delta += min_delta-1;
+    delta /= min_delta;
+    delta *= min_delta;
+    /* actual grow */
+    assert(delta > 0);
+    a->capacity += delta;
+    a->items = a->items ?
+       xrealloc(a->items, a->capacity * sizeof(void*)) :
+       xmalloc(a->capacity * sizeof(void*));
+    /* reset, just in case */
+    memset(a->items+a->count, 0, (a->capacity-a->count) * sizeof(void*));
+}
index 5c4550be538270515928576694228e93e385257f..fd7707836ebca0de57ab0b146e7ceeb8fce02614 100644 (file)
@@ -1,5 +1,5 @@
 #
-#  $Id: Makefile.in,v 1.36 1998/03/08 07:34:41 rousskov Exp $
+#  $Id: Makefile.in,v 1.37 1998/03/20 18:07:36 rousskov Exp $
 #
 prefix         = @prefix@
 top_srcdir     = @top_srcdir@
@@ -35,6 +35,7 @@ UTILOBJS      = rfc1123.o \
                  md5.o \
                  radix.o \
                  stub_memaccount.o \
+                 Array.o \
                  Stack.o \
                  $(LIBOBJS)
 REGEXOBJS      = GNUregex.o
index b828f467f53b65c23d5d3d1f5e0b39c3bad167ad..5deed6acebd404e4b3b671fb5c15d13eb9e7832e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: Stack.c,v 1.4 1998/03/03 00:30:57 rousskov Exp $
+ * $Id: Stack.c,v 1.5 1998/03/20 18:07:36 rousskov Exp $
  *
  * AUTHOR: Alex Rousskov
  *
@@ -28,7 +28,7 @@
  */
 
 /*
- * Stack is a (void*) stack with unlimited capacity and limited accounting.
+ * Stack is a (void*) stack with unlimited capacity; based on Array
  */
 
 
 #include "util.h"
 #include "Stack.h"
 
-static void stackGrow(Stack * s, int min_capacity);
-
-Stack *
-stackCreate()
-{
-    Stack *s = xmalloc(sizeof(Stack));
-    stackInit(s);
-    return s;
-}
-
-void
-stackInit(Stack * s)
-{
-    assert(s);
-    memset(s, 0, sizeof(Stack));
-}
-
-void
-stackClean(Stack * s)
-{
-    assert(s);
-    /* could also warn if some objects are left */
-    xfree(s->items);
-    s->items = NULL;
-}
-
-void
-stackDestroy(Stack * s)
-{
-    assert(s);
-    stackClean(s);
-    xfree(s);
-}
 
 void *
 stackPop(Stack * s)
@@ -83,43 +50,3 @@ stackPop(Stack * s)
     assert(s->count);
     return s->items[--s->count];
 }
-
-void
-stackPush(Stack * s, void *obj)
-{
-    assert(s);
-    if (s->count >= s->capacity)
-       stackGrow(s, s->count+1);
-    s->items[s->count++] = obj;
-}
-
-/* if you are going to push a known and large number of items, call this first */
-void
-stackPrePush(Stack * s, int push_count)
-{
-    assert(s);
-    if (s->count + push_count > s->capacity)
-       stackGrow(s, s->count + push_count);
-}
-
-/* grows internal buffer to satisfy required minimal capacity */
-static void
-stackGrow(Stack * s, int min_capacity)
-{
-    static const int min_delta = 16;
-    int delta;
-    assert(s->capacity < min_capacity);
-    delta = min_capacity;
-    /* make delta a multiple of min_delta */
-    delta += min_delta-1;
-    delta /= min_delta;
-    delta *= min_delta;
-    /* actual grow */
-    assert(delta > 0);
-    s->capacity += delta;
-    s->items = s->items ?
-       xrealloc(s->items, s->capacity * sizeof(void*)) :
-       xmalloc(s->capacity * sizeof(void*));
-    /* reset, just in case */
-    memset(s->items+s->count, 0, (s->capacity-s->count) * sizeof(void*));
-}