home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_DEV08B.LHA / gerlib / libg++ / gperf / src / bool-array.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  3.0 KB  |  115 lines

  1. /* This may look like C code, but it is really -*- C++ -*- */
  2.  
  3. /* Simple lookup table abstraction implemented as an Iteration Number Array.
  4.  
  5.    Copyright (C) 1989 Free Software Foundation, Inc.
  6.    written by Douglas C. Schmidt (schmidt@ics.uci.edu)
  7.  
  8. This file is part of GNU GPERF.
  9.  
  10. GNU GPERF is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 1, or (at your option)
  13. any later version.
  14.  
  15. GNU GPERF is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. GNU General Public License for more details.
  19.  
  20. You should have received a copy of the GNU General Public License
  21. along with GNU GPERF; see the file COPYING.  If not, write to
  22. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  23.  
  24. /* Define and implement a simple boolean array abstraction,
  25.    uses an Iteration Numbering implementation to save on initialization time. */
  26.  
  27. #ifndef bool_array_h
  28. #define bool_array_h 1
  29.  
  30. #include <std.h>
  31. #include "trace.h"
  32.  
  33. #ifdef LO_CAL
  34. /* If we are on a memory diet then we'll only make these use a limited
  35.    amount of storage space. */
  36. typedef unsigned short STORAGE_TYPE;
  37. #else
  38. typedef int STORAGE_TYPE;
  39. #endif
  40.  
  41. class Bool_Array 
  42. {
  43. private:
  44.   static STORAGE_TYPE *storage_array;    /* Initialization of the index space. */
  45.   static STORAGE_TYPE  iteration_number; /* Keep track of the current iteration. */
  46.   static int           size;             /* Keep track of array size. */
  47.  
  48. public:
  49.        Bool_Array (void);
  50.       ~Bool_Array (void);
  51.   static void init (STORAGE_TYPE *buffer, STORAGE_TYPE s);
  52.   static int  find (int hash_value);
  53.   static void reset (void);
  54. };
  55.  
  56. #ifdef __OPTIMIZE__  /* efficiency hack! */
  57.  
  58. inline 
  59. Bool_Array::Bool_Array (void)
  60. {
  61.   T (Trace t ("Bool_Array::Bool_Array");)
  62.   storage_array = 0;
  63.   iteration_number = size = 0;
  64. }
  65.  
  66. inline void
  67. Bool_Array::init (STORAGE_TYPE *buffer, STORAGE_TYPE s)
  68. {
  69.   T (Trace t ("Bool_Array::init");)
  70.   size             = s;
  71.   iteration_number = 1;
  72.   storage_array    = buffer;
  73.   memset (storage_array, 0, s * sizeof *storage_array);
  74.   if (option[DEBUG])
  75.     fprintf (stderr, "\nbool array size = %d, total bytes = %d\n",
  76.              size, size * sizeof *storage_array);
  77. }
  78.  
  79. inline int  
  80. Bool_Array::find (int index) 
  81. {
  82.   T (Trace t ("Bool_Array::find");)
  83.   if (storage_array[index] == iteration_number)
  84.     return 1;
  85.   else
  86.     {
  87.       storage_array[index] = iteration_number;
  88.       return 0;
  89.     }
  90. }
  91.  
  92. inline void 
  93. Bool_Array::reset (void) 
  94.   T (Trace t ("Bool_Array::reset");)
  95.   if (++iteration_number == 0)
  96.     {
  97.       if (option[DEBUG])
  98.         {
  99.           fprintf (stderr, "(re-initializing bool_array)...");
  100.           fflush (stderr);
  101.         }
  102.       iteration_number = 1;
  103.       memset (storage_array, 0, size * sizeof *storage_array);
  104.       if (option[DEBUG])
  105.         {
  106.           fprintf (stderr, "done\n");
  107.           fflush (stderr);
  108.         }
  109.     }
  110. }
  111. #endif
  112.  
  113. #endif
  114.