--- parser3/src/main/pa_dictionary.C 2001/11/05 11:46:27 1.9 +++ parser3/src/main/pa_dictionary.C 2007/04/23 10:30:31 1.24 @@ -1,78 +1,69 @@ /** @file Parser: dictionary class impl. - Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexander Petrosyan (http://paf.design.ru) - - $Id: pa_dictionary.C,v 1.9 2001/11/05 11:46:27 paf Exp $ + Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexandr Petrosian (http://paf.design.ru) */ +static const char * const IDENT_DICTIONARY_C="$Date: 2007/04/23 10:30:31 $"; + #include "pa_dictionary.h" #include "pa_exception.h" -void Dictionary::add_first(Array::Item *value, void *info) { - Dictionary& self=*static_cast(info); - Array *row=static_cast(value); +// could not make this static: gcc complains "prev decl was extern" +void pa_dictionary_add_first(Table::element_type row, Dictionary* self) { // get a=>b values - const String& a=*row->get_string(0); - const String& b=*row->get_string(1); + const String* a=row->get(0); // empty 'a' check - if(a.size()==0) { - Pool& pool=self.pool(); - throw Exception(0, 0, - &a, + if(a->is_empty()) { + throw Exception(PARSER_RUNTIME, + 0, //&a, "dictionary table 'from' column elements must not be empty"); } - unsigned char c=(unsigned char)a.first_char(); - if(!self.starting_line_of[c]) - self.starting_line_of[c]=self.constructor_line; - - double ratio=((double)b.size())/a.size(); - if(ratio>self.fmax_ratio) - self.fmax_ratio=ratio; + const String* b; + if(row->count()>1) { // are there any b? + b=row->get(1); + if(b->is_empty()) + b=0; + } else + b=0; - self.constructor_line++; -} + // record simplier 'a' for quick comparisons in 'starts' extremely-tight-callback + self->substs+=Dictionary::Subst(a->cstr(), b); -Dictionary::Dictionary(Table& atable, double amin_ratio) : Pooled(atable.pool()), - table(atable), - fmax_ratio(amin_ratio) { + unsigned char c=(unsigned char)a->first_char(); + if(!self->starting_line_of[c]) + self->starting_line_of[c]=self->constructor_line; + self->constructor_line++; +} +Dictionary::Dictionary(Table& atable): substs(atable.count()) { // clear starting_lines memset(starting_line_of, 0, sizeof(starting_line_of)); // grab first letters of first column of a table - constructor_line=1; table.for_each(add_first, this); + constructor_line=1; atable.for_each(pa_dictionary_add_first, this); } #ifndef DOXYGEN -struct First_that_starts_info { +struct First_that_begins_info { int line; - const char *src; size_t src_size; + const char* str; }; #endif -static void *starts(Array::Item *value, void *info) { - First_that_starts_info& i=*static_cast(info); +static bool starts(Dictionary::Subst subst, First_that_begins_info* info) { // skip irrelevant lines - if(i.line>1) { - --i.line; + if(info->line>1) { + --info->line; return 0; } - Array *row=static_cast(value); - int partial; - row->get_string(0)->cmp(partial, i.src, i.src_size); - return ( - partial==0 || // full match - partial==1) // typo left column starts 'src' - ?value:0; + return strncmp(subst.from, info->str, subst.from_length)==0; } - -void* Dictionary::first_that_starts(const char *src, size_t src_size) const { - First_that_starts_info info; - if(info.line=starting_line_of[(unsigned char)*src]) { - info.src=src; - info.src_size=src_size; - return table.first_that(starts, &info); +Dictionary::Subst Dictionary::first_that_begins(const char* str) const { + First_that_begins_info info; + if((info.line=starting_line_of[(unsigned char)*str])) { + info.str=str; + return substs.first_that(starts, &info); } else return 0; }