Merge from vendor branch LIBSTDC++:
[dragonfly.git] / contrib / gperf / src / list-node.cc
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)
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 the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111, USA.  */
20
21 #include "list-node.h"
22
23 #include <stdio.h>
24 #include <stdlib.h> /* declares exit() */
25 #include "options.h"
26 #include "trace.h"
27
28 /* Sorts the key set alphabetically to speed up subsequent operations.
29    Uses insertion sort since the set is probably quite small. */
30
31 inline void
32 List_Node::set_sort (char *base, int len)
33 {
34   T (Trace t ("List_Node::set_sort");)
35   int i, j;
36
37   for (i = 0, j = len - 1; i < j; i++)
38     {
39       char curr, tmp;
40
41       for (curr = i + 1, tmp = base[curr]; curr > 0 && tmp < base[curr-1]; curr--)
42         base[curr] = base[curr - 1];
43
44       base[curr] = tmp;
45
46     }
47 }
48
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. */
59
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)
62 {
63   T (Trace t ("List_Node::List_Node");)
64   char *key_set = new char[(option[ALLCHARS] ? len : option.get_max_keysig_size ())];
65   char *ptr = key_set;
66   int i;
67
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. */
72     {
73       /* Iterate through the list of key_positions, initializing occurrences table
74          and char_set (via char * pointer ptr). */
75
76       for (option.reset (); (i = option.get ()) != EOS; )
77         {
78           if (i == WORD_END)            /* Special notation for last KEY position, i.e. '$'. */
79             *ptr = key[len - 1];
80           else if (i <= len)    /* Within range of KEY length, so we'll keep it. */
81             *ptr = key[i - 1];
82           else                  /* Out of range of KEY length, so we'll just skip it. */
83             continue;
84           ++occurrences[(unsigned char)(*ptr++)];
85         }
86
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])
90         {
91           fprintf (stderr, "Can't hash keyword %.*s with chosen key positions.\n",
92                    key_length, key);
93           exit (1);
94         }
95     }
96
97   /* Sort the KEY_SET items alphabetically. */
98   set_sort (key_set, ptr - key_set);
99
100   char_set = key_set;
101   char_set_length = ptr - key_set;
102 }