home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / id-utils-3.2-src.tgz / tar.out / fsf / id-utils / libidu / dynvec.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  2KB  |  60 lines

  1. /* dynvec.c -- dynamically growable vectors
  2.    Copyright (C) 1995 Free Software Foundation, Inc.
  3.    Written by Greg McGary <gkm@gnu.ai.mit.edu>
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19.  
  20. #include <config.h>
  21. #include "dynvec.h"
  22. #include "xmalloc.h"
  23.  
  24. struct dynvec *
  25. make_dynvec (int n)
  26. {
  27.   struct dynvec *dv = MALLOC (struct dynvec, 1);
  28.   dv->dv_vec = MALLOC (void *, n);
  29.   dv->dv_capacity = n;
  30.   dv->dv_fill = 0;
  31.   return dv;
  32. }
  33.  
  34. void
  35. dynvec_free (struct dynvec *dv)
  36. {
  37.   free (dv->dv_vec);
  38.   free (dv);
  39. }
  40.  
  41. void
  42. dynvec_freeze (struct dynvec *dv)
  43. {
  44.   if (dv->dv_fill == dv->dv_capacity)
  45.     return;
  46.   dv->dv_capacity = dv->dv_fill;
  47.   dv->dv_vec = REALLOC (dv->dv_vec, void *, dv->dv_capacity);
  48. }
  49.  
  50. void
  51. dynvec_append (struct dynvec *dv, void *element)
  52. {
  53.   if (dv->dv_fill == dv->dv_capacity)
  54.     {
  55.       dv->dv_capacity *= 2;
  56.       dv->dv_vec = REALLOC (dv->dv_vec, void *, dv->dv_capacity);
  57.     }
  58.   dv->dv_vec[dv->dv_fill++] = element;
  59. }
  60.