|
|
| version 1.6, 2001/09/26 10:32:26 | version 1.17.2.1, 2003/01/24 08:19:09 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: dictionary class impl. | Parser: dictionary class impl. |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| $Id$ | |
| */ | */ |
| static const char* IDENT_DICTIONARY_C="$Date$"; | |
| #include "pa_dictionary.h" | #include "pa_dictionary.h" |
| #include "pa_exception.h" | #include "pa_exception.h" |
| void Dictionary::add_first(Array::Item *value, void *info) { | static void add_first(Table::element_type row, Dictionary* self) { |
| Dictionary& self=*static_cast<Dictionary *>(info); | |
| Array *row=static_cast<Array *>(value); | |
| // get a=>b values | // get a=>b values |
| const String& a=*row->get_string(0); | ConstStringPtr a=row->get(0); |
| const String& b=*row->get_string(1); | ConstStringPtr b=row->get(1); |
| // empty 'a' check | // empty 'a' check |
| if(a.size()==0) { | if(a->is_empty()) { |
| Pool& pool=self.pool(); | throw Exception("parser.runtime", |
| PTHROW(0, 0, | a, |
| &a, | |
| "dictionary table 'from' column elements must not be empty"); | "dictionary table 'from' column elements must not be empty"); |
| } | } |
| unsigned char c=(unsigned char)a.first_char(); | unsigned char c=(unsigned char)a->first_char(); |
| if(!self.starting_line_of[c]) | if(!self->starting_line_of[c]) |
| self.starting_line_of[c]=self.constructor_line; | self->starting_line_of[c]=self->constructor_line; |
| double ratio=b.size()/a.size(); | double ratio=((double)b->size())/a->size(); |
| if(ratio>self.fmax_ratio) | if(ratio>self->fmax_ratio) |
| self.fmax_ratio=ratio; | self->fmax_ratio=ratio; |
| self.constructor_line++; | self->constructor_line++; |
| } | } |
| Dictionary::Dictionary(TablePtr atable, double amin_ratio): | |
| Dictionary::Dictionary(Table& atable, double amin_ratio) : Pooled(atable.pool()), | |
| table(atable), | table(atable), |
| fmax_ratio(amin_ratio) { | fmax_ratio(amin_ratio) { |
| // clear starting_lines | // clear starting_lines |
| memset(starting_line_of, 0, sizeof(starting_line_of)); | memset(starting_line_of, 0, sizeof(starting_line_of)); |
| // grab first letters of first column of a table | // grab first letters of first column of a table |
| constructor_line=1; table.for_each(add_first, this); | constructor_line=1; table->for_each(add_first, this); |
| } | } |
| #ifndef DOXYGEN | #ifndef DOXYGEN |
| struct First_that_starts_info { | struct First_that_starts_info { |
| int line; | int line; |
| const char *src; | const char *src; size_t src_size; |
| }; | }; |
| #endif | #endif |
| static void *starts(Array::Item *value, void *info) { | static bool starts(Table::element_type row, First_that_starts_info* info) { |
| First_that_starts_info& i=*static_cast<First_that_starts_info *>(info); | |
| // skip irrelevant lines | // skip irrelevant lines |
| if(i.line>1) { | if(info->line>1) { |
| --i.line; | --info->line; |
| return 0; | return 0; |
| } | } |
| Array *row=static_cast<Array *>(value); | |
| int partial; | int partial; |
| row->get_string(0)->cmp(partial, i.src); | row->get(0)->cmp(partial, info->src, info->src_size); |
| return ( | return ( |
| partial==0 || // full match | partial==0 || // full match |
| partial==1) // typo left column starts 'src' | partial==1); // typo left column starts 'src' |
| ?value:0; | |
| } | } |
| void* Dictionary::first_that_starts(const char *src) const { | Table::element_type Dictionary::first_that_starts(const char *src, size_t src_size) const { |
| First_that_starts_info info; | First_that_starts_info info; |
| if(info.line=starting_line_of[(unsigned char)*src]) { | if(info.line=starting_line_of[(unsigned char)*src]) { |
| info.src=src; | info.src=src; |
| return table.first_that(starts, &info); | info.src_size=src_size; |
| return table->first_that(starts, &info); | |
| } else | } else |
| return 0; | return 0;//Table::element_type(0); |
| } | } |