1 /* Creates and initializes a new list node.
2 Copyright (C) 1989-1998, 2000 Free Software Foundation, Inc.
3 written by Douglas C. Schmidt (schmidt@ics.uci.edu)
5 This file is part of GNU GPERF.
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)
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.
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 the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
21 #include "list-node.h"
24 #include <stdlib.h> /* declares exit() */
28 /* Sorts the key set alphabetically to speed up subsequent operations.
29 Uses insertion sort since the set is probably quite small. */
32 List_Node::set_sort (char *base, int len)
34 T (Trace t ("List_Node::set_sort");)
37 for (i = 0, j = len - 1; i < j; i++)
41 for (curr = i + 1, tmp = base[curr]; curr > 0 && tmp < base[curr-1]; curr--)
42 base[curr] = base[curr - 1];
49 /* Initializes a List_Node. This requires obtaining memory for the CHAR_SET
50 initializing them using the information stored in the KEY_POSITIONS array in Options,
51 and checking for simple errors. It's important to note that KEY and REST are
52 both pointers to the different offsets into the same block of dynamic memory pointed
53 to by parameter K. The data member REST is used to store any additional fields
54 of the input file (it is set to the "" string if Option[TYPE] is not enabled).
55 This is useful if the user wishes to incorporate a lookup structure,
56 rather than just an array of keys. Finally, KEY_NUMBER contains a count
57 of the total number of keys seen so far. This is used to initialize
58 the INDEX field to some useful value. */
60 List_Node::List_Node (const char *k, int len, const char *r):
61 link (0), next (0), key (k), key_length (len), rest (r), index (0)
63 T (Trace t ("List_Node::List_Node");)
64 char *key_set = new char[(option[ALLCHARS] ? len : option.get_max_keysig_size ())];
68 if (option[ALLCHARS]) /* Use all the character positions in the KEY. */
69 for (i = len; i > 0; k++, ptr++, i--)
70 ++occurrences[(unsigned char)(*ptr = *k)];
71 else /* Only use those character positions specified by the user. */
73 /* Iterate through the list of key_positions, initializing occurrences table
74 and char_set (via char * pointer ptr). */
76 for (option.reset (); (i = option.get ()) != EOS; )
78 if (i == WORD_END) /* Special notation for last KEY position, i.e. '$'. */
80 else if (i <= len) /* Within range of KEY length, so we'll keep it. */
82 else /* Out of range of KEY length, so we'll just skip it. */
84 ++occurrences[(unsigned char)(*ptr++)];
87 /* Didn't get any hits and user doesn't want to consider the
88 keylength, so there are essentially no usable hash positions! */
89 if (ptr == char_set && option[NOLENGTH])
91 fprintf (stderr, "Can't hash keyword %.*s with chosen key positions.\n",
97 /* Sort the KEY_SET items alphabetically. */
98 set_sort (key_set, ptr - key_set);
101 char_set_length = ptr - key_set;