2 Copyright (C) 1989-1998, 2000, 2002 Free Software Foundation, Inc.
3 Written by Douglas C. Schmidt <schmidt@ics.uci.edu>
4 and Bruno Haible <bruno@clisp.org>.
6 This file is part of GNU GPERF.
8 GNU GPERF is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU GPERF is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; see the file COPYING.
20 If not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
29 #include "positions.h"
32 /* --------------------------- KeywordExt class --------------------------- */
34 /* Sort a small set of 'unsigned int', base[0..len-1], in place. */
35 static inline void sort_char_set (unsigned int *base, int len)
37 /* Bubble sort is sufficient here. */
38 for (int i = 1; i < len; i++)
43 for (j = i, tmp = base[j]; j > 0 && tmp < base[j - 1]; j--)
44 base[j] = base[j - 1];
50 /* Initializes selchars and selchars_length.
53 The hash function will be computed as
54 asso_values[allchars[key_pos[0]]] +
55 asso_values[allchars[key_pos[1]]] + ...
56 We compute selchars as the multiset
57 { allchars[key_pos[0]], allchars[key_pos[1]], ... }
58 so that the hash function becomes
59 asso_values[selchars[0]] + asso_values[selchars[1]] + ...
60 Furthermore we sort the selchars array, to ease detection of duplicates
63 More in detail: The arguments alpha_unify (used for case-insensitive
64 hash functions) and alpha_inc (used to disambiguate permutations)
65 apply slight modifications. The hash function will be computed as
66 sum (j=0,1,...: k = key_pos[j]:
67 asso_values[alpha_unify[allchars[k]+alpha_inc[k]]])
68 + (allchars_length if !option[NOLENGTH], 0 otherwise).
69 We compute selchars as the multiset
70 { alpha_unify[allchars[k]+alpha_inc[k]] : j=0,1,..., k = key_pos[j] }
71 so that the hash function becomes
72 asso_values[selchars[0]] + asso_values[selchars[1]] + ...
73 + (allchars_length if !option[NOLENGTH], 0 otherwise).
77 KeywordExt::init_selchars_low (const Positions& positions, const unsigned int *alpha_unify, const unsigned int *alpha_inc)
79 /* Iterate through the list of positions, initializing selchars
81 PositionIterator iter = positions.iterator(_allchars_length);
83 unsigned int *key_set = new unsigned int[iter.remaining()];
84 unsigned int *ptr = key_set;
86 for (int i; (i = iter.next ()) != PositionIterator::EOS; )
89 if (i == Positions::LASTCHAR)
90 /* Special notation for last KEY position, i.e. '$'. */
91 c = static_cast<unsigned char>(_allchars[_allchars_length - 1]);
92 else if (i < _allchars_length)
94 /* Within range of KEY length, so we'll keep it. */
95 c = static_cast<unsigned char>(_allchars[i]);
100 /* Out of range of KEY length, the iterator should not have
110 _selchars_length = ptr - key_set;
116 KeywordExt::init_selchars_tuple (const Positions& positions, const unsigned int *alpha_unify)
118 init_selchars_low (positions, alpha_unify, NULL);
122 KeywordExt::init_selchars_multiset (const Positions& positions, const unsigned int *alpha_unify, const unsigned int *alpha_inc)
124 unsigned int *selchars =
125 init_selchars_low (positions, alpha_unify, alpha_inc);
127 /* Sort the selchars elements alphabetically. */
128 sort_char_set (selchars, _selchars_length);
131 /* Deletes selchars. */
133 KeywordExt::delete_selchars ()
135 delete[] const_cast<unsigned int *>(_selchars);
139 /* ------------------------- Keyword_Factory class ------------------------- */
141 Keyword_Factory::Keyword_Factory ()
145 Keyword_Factory::~Keyword_Factory ()
150 /* ------------------------------------------------------------------------- */
152 char empty_string[1] = "";
157 #define INLINE /* not inline */
158 #include "keyword.icc"
161 #endif /* not defined __OPTIMIZE__ */