Annotation of parser3/src/main/pa_dictionary.C, revision 1.7
1.1 parser 1: /** @file
2: Parser: dictionary class impl.
3:
4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.6 parser 5: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.1 parser 6:
1.7 ! parser 7: $Id: pa_dictionary.C,v 1.6 2001/09/26 10:32:26 parser Exp $
1.1 parser 8: */
9:
10: #include "pa_dictionary.h"
1.4 parser 11: #include "pa_exception.h"
1.1 parser 12:
1.5 parser 13: void Dictionary::add_first(Array::Item *value, void *info) {
1.1 parser 14: Dictionary& self=*static_cast<Dictionary *>(info);
1.4 parser 15: Array *row=static_cast<Array *>(value);
16: // get a=>b values
17: const String& a=*row->get_string(0);
18: const String& b=*row->get_string(1);
19: // empty 'a' check
20: if(a.size()==0) {
21: Pool& pool=self.pool();
22: PTHROW(0, 0,
23: &a,
24: "dictionary table 'from' column elements must not be empty");
25: }
26: unsigned char c=(unsigned char)a.first_char();
27: if(!self.starting_line_of[c])
28: self.starting_line_of[c]=self.constructor_line;
29:
1.7 ! parser 30: double ratio=((double)b.size())/a.size();
1.4 parser 31: if(ratio>self.fmax_ratio)
32: self.fmax_ratio=ratio;
33:
34: self.constructor_line++;
1.1 parser 35: }
36:
1.4 parser 37: Dictionary::Dictionary(Table& atable, double amin_ratio) : Pooled(atable.pool()),
38: table(atable),
39: fmax_ratio(amin_ratio) {
40:
41: // clear starting_lines
42: memset(starting_line_of, 0, sizeof(starting_line_of));
1.1 parser 43: // grab first letters of first column of a table
1.4 parser 44: constructor_line=1; table.for_each(add_first, this);
1.1 parser 45: }
46:
1.4 parser 47: #ifndef DOXYGEN
48: struct First_that_starts_info {
49: int line;
1.7 ! parser 50: const char *src; size_t src_size;
1.4 parser 51: };
52: #endif
53: static void *starts(Array::Item *value, void *info) {
54: First_that_starts_info& i=*static_cast<First_that_starts_info *>(info);
55: // skip irrelevant lines
56: if(i.line>1) {
57: --i.line;
58: return 0;
59: }
1.1 parser 60: Array *row=static_cast<Array *>(value);
61:
62: int partial;
1.7 ! parser 63: row->get_string(0)->cmp(partial, i.src, i.src_size);
1.3 parser 64: return (
1.1 parser 65: partial==0 || // full match
1.3 parser 66: partial==1) // typo left column starts 'src'
67: ?value:0;
1.1 parser 68: }
69:
1.7 ! parser 70: void* Dictionary::first_that_starts(const char *src, size_t src_size) const {
1.4 parser 71: First_that_starts_info info;
72: if(info.line=starting_line_of[(unsigned char)*src]) {
73: info.src=src;
1.7 ! parser 74: info.src_size=src_size;
1.4 parser 75: return table.first_that(starts, &info);
76: } else
1.1 parser 77: return 0;
78: }
E-mail: