diff options
author | John Langford <jl@hunch.net> | 2009-09-14 22:49:49 +0400 |
---|---|---|
committer | John Langford <jl@hunch.net> | 2009-09-14 22:49:49 +0400 |
commit | 560935f31bd0d187e1e3efdfaec8e6d2043f04a4 (patch) | |
tree | 2f243c28b405e35cbc57fc5cf6f399045fc7e8b6 /simple_label.cc | |
parent | 1d9a3a0dd7484797eb40d0bb2ced7918495af275 (diff) |
First compiling version of multisource support.
Diffstat (limited to 'simple_label.cc')
-rw-r--r-- | simple_label.cc | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/simple_label.cc b/simple_label.cc new file mode 100644 index 00000000..17d04213 --- /dev/null +++ b/simple_label.cc @@ -0,0 +1,97 @@ +#include <float.h> + +#include "simple_label.h" +#include "cache.h" + +size_t read_cached_simple_label(void* v, io_buf& cache) +{ + label_data* ld = (label_data*) v; + char *c; + size_t total = sizeof(ld->label)+sizeof(ld->weight)+int_size; + size_t tag_size = 0; + if (buf_read(cache, c, total) < total) + return 0; + + ld->label = *(double *)c; + c += sizeof(ld->label); + ld->weight = *(float *)c; + c += sizeof(ld->weight); + c = run_len_decode(c, tag_size); + + cache.set(c); + if (buf_read(cache, c, tag_size) < tag_size) + return 0; + + ld->tag.erase(); + push_many(ld->tag, c, tag_size); + return total+tag_size; +} + +void cache_simple_label(void* v, io_buf& cache) +{ + char *c; + label_data* ld = (label_data*) v; + buf_write(cache, c, sizeof(ld->label)+sizeof(ld->weight)+int_size+ld->tag.index()); + *(double *)c = ld->label; + c += sizeof(ld->label); + *(float *)c = ld->weight; + c += sizeof(ld->weight); + + c = run_len_encode(c, ld->tag.index()); + memcpy(c,ld->tag.begin,ld->tag.index()); + c += ld->tag.index(); + cache.set(c); +} + +void default_simple_label(void* v) +{ + label_data* ld = (label_data*) v; + ld->label = FLT_MAX; + ld->weight = 1.; + ld->undo = false; + ld->tag.erase(); +} + +void delete_simple_label(void* v) +{ + label_data* ld = (label_data*) v; + if (ld->tag.end_array != ld->tag.begin) + { + free(ld->tag.begin); + ld->tag.end_array = ld->tag.begin; + } +} + +void parse_simple_label(void* v, substring label_space, v_array<substring>& words) +{ + label_data* ld = (label_data*)v; + char* tab_location = safe_index(label_space.start,'\t',label_space.end); + if (tab_location != label_space.end) + label_space.start = tab_location+1; + + tokenize(' ',label_space, words); + switch(words.index()) { + case 0: + break; + case 1: + ld->label = double_of_substring(words[0]); + break; + case 2: + ld->label = double_of_substring(words[0]); + ld->weight = float_of_substring(words[1]); + break; + case 3: + ld->label = double_of_substring(words[0]); + + ld->weight = float_of_substring(words[1]); + push_many(ld->tag, words[2].start, + words[2].end - words[2].start); + if (ld->tag.index() == 4 && ld->tag[0] == 'u' && ld->tag[1] == 'n' && ld->tag[2] == 'd' && ld->tag[3] == 'o') + ld->undo = true; + break; + default: + cerr << "malformed example!\n"; + cerr << "words.index() = " << words.index() << endl; + } +} + |