home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / cperf-2.1 / src / boolarray.c next >
Encoding:
C/C++ Source or Header  |  1989-11-11  |  2.6 KB  |  91 lines

  1. /* Fast lookup table abstraction implemented as a Guilmette Array
  2.    Copyright (C) 1989 Free Software Foundation, Inc.
  3.    written by Douglas C. Schmidt (schmidt@ics.uci.edu)
  4.  
  5. This file is part of GNU GPERF.
  6.  
  7. GNU GPERF is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 1, or (at your option)
  10. any later version.
  11.  
  12. GNU GPERF is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU GPERF; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include <stdio.h>
  22. #include "boolarray.h"
  23. #include "options.h"
  24.  
  25. /* Locally visible BOOL_ARRAY object. */
  26.  
  27. static BOOL_ARRAY bool_array;
  28.  
  29. /* Prints out debugging diagnostics. */
  30.  
  31. void
  32. bool_array_destroy ()
  33. {
  34.   if (OPTION_ENABLED (option, DEBUG))
  35.     fprintf (stderr, "\ndumping boolean array information\niteration number = %d\nend of array dump\n",
  36.              bool_array.iteration_number);
  37.   free ((char *) bool_array.storage_array);
  38. }
  39.  
  40. void
  41. bool_array_init (size)
  42.      int size;
  43. {
  44.     STORAGE_TYPE *xmalloc ();
  45.   bool_array.iteration_number = 1;
  46.   bool_array.size = size;
  47.   bool_array.storage_array = xmalloc (size * sizeof *bool_array.storage_array);
  48.   bzero (bool_array.storage_array, size * sizeof *bool_array.storage_array);
  49.   if (OPTION_ENABLED (option, DEBUG))
  50.     fprintf (stderr, "\nbool array size = %d, total bytes = %d\n",
  51.              bool_array.size, bool_array.size * sizeof *bool_array.storage_array);
  52. }
  53.  
  54. bool 
  55. lookup (index)
  56.      int index;
  57. {
  58.   if (bool_array.storage_array[index] == bool_array.iteration_number)
  59.     return 1;
  60.   else
  61.     {
  62.       bool_array.storage_array[index] = bool_array.iteration_number;
  63.       return 0;
  64.     }
  65. }
  66.  
  67. /* Simple enough to reset, eh?! */
  68.  
  69. void 
  70. bool_array_reset ()  
  71. {
  72.   /* If we wrap around it's time to zero things out again! */
  73.             
  74.   
  75.   if (++bool_array.iteration_number == 0)
  76.     {
  77.       if (OPTION_ENABLED (option, DEBUG))
  78.         {
  79.           fprintf (stderr, "(re-initializing bool_array)...");
  80.           fflush (stderr);
  81.         }
  82.       bool_array.iteration_number = 1;
  83.       bzero (bool_array.storage_array, bool_array.size * sizeof *bool_array.storage_array);
  84.       if (OPTION_ENABLED (option, DEBUG))
  85.         {
  86.           fprintf (stderr, "done\n");
  87.           fflush (stderr);
  88.         }
  89.     }
  90. }
  91.