Annotation of parser3/src/main/pa_dictionary.C, revision 1.4
1.1 parser 1: /** @file
2: Parser: dictionary class impl.
3:
4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
5:
6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
7: */
1.4 ! parser 8: static const char *RCSId="$Id: pa_dictionary.C,v 1.3 2001/08/06 16:18:26 parser Exp $";
1.1 parser 9:
10: #include "pa_dictionary.h"
1.4 ! parser 11: #include "pa_exception.h"
1.1 parser 12:
13: static void add_first(Array::Item *value, void *info) {
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:
! 30: double ratio=b.size()/a.size();
! 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;
! 50: const char *src;
! 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.4 ! parser 63: row->get_string(0)->cmp(partial, i.src);
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:
70: void* Dictionary::first_that_starts(const char *src) const {
1.4 ! parser 71: First_that_starts_info info;
! 72: if(info.line=starting_line_of[(unsigned char)*src]) {
! 73: info.src=src;
! 74: return table.first_that(starts, &info);
! 75: } else
1.1 parser 76: return 0;
77: }
E-mail: