home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume20 / gperf / part01 / cperf / src / boolarray.c next >
Encoding:
C/C++ Source or Header  |  1989-10-18  |  2.1 KB  |  75 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\nsize = %d\nend of array dump\n", 
  36.              bool_array.size);
  37. }
  38.  
  39. void
  40. bool_array_init (size)
  41.      int size;
  42. {
  43.     int *xmalloc ();
  44.   bool_array.iteration_number = 1;
  45.   bool_array.size = size;
  46.   bool_array.storage_array = xmalloc (size * sizeof *bool_array.storage_array);
  47.   bzero (bool_array.storage_array, size * sizeof *bool_array.storage_array);
  48. }
  49.  
  50. bool 
  51. lookup (index)
  52.      int index;
  53. {
  54.   if (bool_array.storage_array[index] == bool_array.iteration_number)
  55.     return 1;
  56.   else
  57.     {
  58.       bool_array.storage_array[index] = bool_array.iteration_number;
  59.       return 0;
  60.     }
  61. }
  62.  
  63. /* Simple enough to reset, eh?! */
  64.  
  65. void 
  66. bool_array_reset ()  
  67. {
  68.   /* If we wrap around it's time to zero things out again!
  69.      However, this only occurs once about every 2^31 iterations,
  70.      so it should probably never happen! */
  71.             
  72.   if (bool_array.iteration_number++ == 0)
  73.     bzero (bool_array.storage_array, bool_array.size * sizeof *bool_array.storage_array);
  74. }
  75.