home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / gperf.lzh / GPERF / LIST-NOD.CC < prev    next >
C/C++ Source or Header  |  1993-07-30  |  4KB  |  97 lines

  1. /* Creates and initializes a new list node.
  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 <std.h>
  23. #include "options.h"
  24. #include "list-node.h"
  25. #include "trace.h"
  26.  
  27. /* Sorts the key set alphabetically to speed up subsequent operations.
  28.    Uses insertion sort since the set is probably quite small. */
  29.  
  30. static inline void 
  31. List_Node::set_sort (char *base, int len)
  32. {
  33.   T (Trace t ("List_Node::set_sort");)
  34.   int i, j;
  35.  
  36.   for (i = 0, j = len - 1; i < j; i++)
  37.     {
  38.       char curr, tmp;
  39.       
  40.       for (curr = i + 1, tmp = base[curr]; curr > 0 && tmp < base[curr-1]; curr--)
  41.         base[curr] = base[curr - 1];
  42.  
  43.       base[curr] = tmp;
  44.  
  45.     }
  46. }
  47.  
  48. /* Initializes a List_Node.  This requires obtaining memory for the CHAR_SET
  49.    initializing them using the information stored in the KEY_POSITIONS array in Options,
  50.    and checking for simple errors.  It's important to note that KEY and REST are
  51.    both pointers to the different offsets into the same block of dynamic memory pointed 
  52.    to by parameter K. The data member REST is used to store any additional fields 
  53.    of the input file (it is set to the "" string if Option[TYPE] is not enabled).
  54.    This is useful if the user wishes to incorporate a lookup structure,
  55.    rather than just an array of keys.  Finally, KEY_NUMBER contains a count
  56.    of the total number of keys seen so far.  This is used to initialize
  57.    the INDEX field to some useful value. */
  58.  
  59. List_Node::List_Node (char *k, int len): key (k), next (0), index (0),
  60.      length (len), link (0), rest (option[TYPE] ? k + len + 1 : "")
  61. {
  62.   T (Trace t ("List_Node::List_Node");)
  63.   char *ptr = new char[(option[ALLCHARS] ? len : option.get_max_keysig_size ()) + 1];
  64.   char_set  = ptr;
  65.   k[len]    = '\0';             /* Null terminate KEY to separate it from REST. */
  66.   
  67.   if (option[ALLCHARS])         /* Use all the character position in the KEY. */
  68.     for (; *k; k++, ptr++)
  69.       ++occurrences[*ptr = *k];
  70.   else                          /* Only use those character positions specified by the user. */
  71.     {                           
  72.       int i;
  73.       
  74.       /* Iterate thru the list of key_positions, initializing occurrences table
  75.         and char_set (via char * pointer ptr). */
  76.       
  77.       for (option.reset (); (i = option.get ()) != EOS; )
  78.         {
  79.           if (i == WORD_END)            /* Special notation for last KEY position, i.e. '$'. */
  80.             *ptr = key[len - 1];
  81.           else if (i <= len)    /* Within range of KEY length, so we'll keep it. */
  82.             *ptr = key[i - 1];
  83.           else                  /* Out of range of KEY length, so we'll just skip it. */
  84.             continue;
  85.           ++occurrences[*ptr++];
  86.         }
  87.       
  88.       /* Didn't get any hits and user doesn't want to consider the
  89.         keylength, so there are essentially no usable hash positions! */
  90.       if (ptr == char_set && option[NOLENGTH])
  91.         report_error ("Can't hash keyword %s with chosen key positions.\n%a", key, 1);
  92.     }
  93.   *ptr = '\0';                  /* Terminate this bastard.... */
  94.   /* Sort the KEY_SET items alphabetically. */
  95.   set_sort (char_set, ptr - char_set);
  96. }
  97.