📄 key-list.cc
字号:
/* Routines for building, ordering, and printing the keyword list. Copyright (C) 1989 Free Software Foundation, Inc. written by Douglas C. Schmidt (schmidt@ics.uci.edu)This file is part of GNU GPERF.GNU GPERF is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 1, or (at your option)any later version.GNU GPERF is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU GPERF; see the file COPYING. If not, write tothe Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. *//* AIX requires the alloca decl to be the first thing in the file. */#ifdef __GNUC__#define alloca __builtin_alloca#elif defined(sparc)#include <alloca.h>#elif defined(_AIX)#pragma alloca#elsechar *alloca ();#endif#include <stdio.h>#include <builtin.h>#include <assert.h>#include <limits.h>#include "options.h"#include "read-line.h"#include "hash-table.h"#include "key-list.h"#include "trace.h"/* Make the hash table 8 times larger than the number of keyword entries. */static const int TABLE_MULTIPLE = 10;/* Default type for generated code. */static char *const default_array_type = "char *";/* in_word_set return type, by default. */static char *const default_return_type = "char *";/* Efficiently returns the least power of two greater than or equal to X! */#define POW(X) ((!X)?1:(X-=1,X|=X>>1,X|=X>>2,X|=X>>4,X|=X>>8,X|=X>>16,(++X)))/* How wide the printed field width must be to contain the maximum hash value. */static int field_width = 0;int Key_List::determined[ALPHA_SIZE];/* Destructor dumps diagnostics during debugging. */Key_List::~Key_List (void) { T (Trace t ("Key_List::~Key_List");) if (option[DEBUG]) { fprintf (stderr, "\nDumping key list information:\ntotal non-static linked keywords = %d" "\ntotal keywords = %d\ntotal duplicates = %d\nmaximum key length = %d\n", list_len, total_keys, total_duplicates, max_key_len); dump (); report_error ("End dumping list.\n\n"); }}/* Gathers the input stream into a buffer until one of two things occur: 1. We read a '%' followed by a '%' 2. We read a '%' followed by a '}' The first symbolizes the beginning of the keyword list proper, The second symbolizes the end of the C source code to be generated verbatim in the output file. I assume that the keys are separated from the optional preceding struct declaration by a consecutive % followed by either % or } starting in the first column. The code below uses an expandible buffer to scan off and return a pointer to all the code (if any) appearing before the delimiter. */char *Key_List::get_special_input (char delimiter){ T (Trace t ("Key_List::get_special_input");) int size = 80; char *buf = new char[size]; int c, i; for (i = 0; (c = getchar ()) != EOF; i++) { if (c == '%') { if ((c = getchar ()) == delimiter) { while ((c = getchar ()) != '\n') ; /* discard newline */ if (i == 0) return ""; else { buf[delimiter == '%' && buf[i - 2] == ';' ? i - 2 : i - 1] = '\0'; return buf; } } else buf[i++] = '%'; } else if (i >= size) /* Yikes, time to grow the buffer! */ { char *temp = new char[size *= 2]; int j; for (j = 0; j < i; j++) temp[j] = buf[j]; buf = temp; } buf[i] = c; } return 0; /* Problem here. */}/* Stores any C text that must be included verbatim into the generated code output. */char *Key_List::save_include_src (void){ T (Trace t ("Key_List::save_include_src");) int c; if ((c = getchar ()) != '%') ungetc (c, stdin); else if ((c = getchar ()) != '{') report_error ("internal error, %c != '{' on line %d in file %s%a", c, __LINE__, __FILE__, 1); else return get_special_input ('}'); return "";}/* Determines from the input file whether the user wants to build a table from a user-defined struct, or whether the user is content to simply use the default array of keys. */char *Key_List::get_array_type (void){ T (Trace t ("Key_List::get_array_type");) return get_special_input ('%');} /* strcspn - find length of initial segment of S consisting entirely of characters not from REJECT (borrowed from Henry Spencer's ANSI string package, when GNU libc comes out I'll replace this...). */inline intKey_List::strcspn (const char *s, const char *reject){ T (Trace t ("Key_List::strcspn");) const char *scan; const char *rej_scan; int count = 0; for (scan = s; *scan; scan++) { for (rej_scan = reject; *rej_scan; rej_scan++) if (*scan == *rej_scan) return count; count++; } return count;}/* Sets up the Return_Type, the Struct_Tag type and the Array_Type based upon various user Options. */void Key_List::set_output_types (void){ T (Trace t ("Key_List::set_output_types");) if (option[TYPE] && !(array_type = get_array_type ())) return; /* Something's wrong, bug we'll catch it later on.... */ else if (option[TYPE]) /* Yow, we've got a user-defined type... */ { int struct_tag_length = strcspn (array_type, "{\n\0"); if (option[POINTER]) /* And it must return a pointer... */ { return_type = new char[struct_tag_length + 2]; strncpy (return_type, array_type, struct_tag_length); return_type[struct_tag_length] = '*'; return_type[struct_tag_length + 1] = '\0'; } struct_tag = new char[struct_tag_length + 1]; strncpy (struct_tag, array_type, struct_tag_length); struct_tag[struct_tag_length] = '\0'; } else if (option[POINTER]) /* Return a char *. */ return_type = default_array_type;}/* Reads in all keys from standard input and creates a linked list pointed to by Head. This list is then quickly checked for ``links,'' i.e., unhashable elements possessing identical key sets and lengths. */void Key_List::read_keys (void){ T (Trace t ("Key_List::read_keys");) char *ptr; include_src = save_include_src (); set_output_types (); /* Oops, problem with the input file. */ if (! (ptr = Read_Line::get_line ())) report_error ("No words in input file, did you forget to prepend %s" " or use -t accidentally?\n%a", "%%", 1); /* Read in all the keywords from the input file. */ else { const char *delimiter = option.get_delimiter (); List_Node *temp, *trail = 0; head = new List_Node (ptr, strcspn (ptr, delimiter)); for (temp = head; (ptr = Read_Line::get_line ()) && strcmp (ptr, "%%"); temp = temp->next) { temp->next = new List_Node (ptr, strcspn (ptr, delimiter)); total_keys++; } /* See if any additional C code is included at end of this file. */ if (ptr) additional_code = 1; /* Hash table this number of times larger than keyword number. */ int table_size = (list_len = total_keys) * TABLE_MULTIPLE; /* By allocating the memory here we save on dynamic allocation overhead. Table must be a power of 2 for the hash function scheme to work. */ List_Node *table[POW (table_size)]; /* Make large hash table for efficiency. */ Hash_Table found_link (table, table_size); /* Test whether there are any links and also set the maximum length of an identifier in the keyword list. */ for (temp = head; temp; temp = temp->next) { List_Node *ptr = found_link (temp, option[NOLENGTH]); /* Check for links. We deal with these by building an equivalence class of all duplicate values (i.e., links) so that only 1 keyword is representative of the entire collection. This *greatly* simplifies processing during later stages of the program. */ if (ptr) { total_duplicates++; list_len--; trail->next = temp->next; temp->link = ptr->link; ptr->link = temp; /* Complain if user hasn't enabled the duplicate option. */ if (!option[DUP] || option[DEBUG]) report_error ("Key link: \"%s\" = \"%s\", with key set \"%s\".\n", temp->key, ptr->key, temp->char_set); } else trail = temp; /* Update minimum and maximum keyword length, if needed. */ max_key_len >?= temp->length; min_key_len <?= temp->length; } /* Exit program if links exists and option[DUP] not set, since we can't continue */ if (total_duplicates) report_error (option[DUP] ? "%d input keys have identical hash values, examine output carefully...\n" : "%d input keys have identical hash values,\ntry different key positions or use option -D.\n%a", total_duplicates, 1); if (option[ALLCHARS]) option.set_keysig_size (max_key_len); }}/* Recursively merges two sorted lists together to form one sorted list. The ordering criteria is by frequency of occurrence of elements in the key set or by the hash value. This is a kludge, but permits nice sharing of almost identical code without incurring the overhead of a function call comparison. */ List_Node *Key_List::merge (List_Node *list1, List_Node *list2){ T (Trace t ("Key_List::merge");) if (!list1) return list2; else if (!list2) return list1; else if (occurrence_sort && list1->occurrence < list2->occurrence || hash_sort && list1->hash_value > list2->hash_value) { list2->next = merge (list2->next, list1); return list2; } else { list1->next = merge (list1->next, list2); return list1; }}/* Applies the merge sort algorithm to recursively sort the key list by frequency of occurrence of elements in the key set. */ List_Node *Key_List::merge_sort (List_Node *head){ T (Trace t ("Key_List::merge_sort");) if (!head || !head->next) return head; else { List_Node *middle = head; List_Node *temp = head->next->next; while (temp) { temp = temp->next; middle = middle->next; if (temp) temp = temp->next; } temp = middle->next; middle->next = 0; return merge (merge_sort (head), merge_sort (temp)); } }/* Returns the frequency of occurrence of elements in the key set. */inline int Key_List::get_occurrence (List_Node *ptr){ T (Trace t ("Key_List::get_occurrence");) int value = 0; for (char *temp = ptr->char_set; *temp; temp++) value += occurrences[*temp]; return value;}/* Enables the index location of all key set elements that are now determined. */ inline void Key_List::set_determined (List_Node *ptr){ T (Trace t ("Key_List::set_determined");) for (char *temp = ptr->char_set; *temp; temp++) determined[*temp] = 1;}/* Returns TRUE if PTR's key set is already completely determined. */inline int Key_List::already_determined (List_Node *ptr){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -