This source file includes following definitions.
- Reinit
- AddGram
- Add
- CurrentTopKey
- Sort
- Dump
- Reinit
- Add
- Find
- CurrentTopKey
- Sort
- Dump
#include "encodings/compact_lang_det/tote.h"
#include <string.h>
#include "encodings/compact_lang_det/win/cld_logging.h"
Tote::Tote() {
gram_count_ = 0;
incr_count_ = 0;
byte_count_ = 0;
memset(key_, 0, sizeof(key_));
}
Tote::~Tote() {
}
void Tote::Reinit() {
gram_count_ = 0;
incr_count_ = 0;
byte_count_ = 0;
memset(key_, 0, sizeof(key_));
}
void Tote::AddGram() {
++gram_count_;
}
void Tote::Add(uint8 ikey, int idelta) {
DCHECK(ikey != 0);
++incr_count_;
int sub0 = ikey & 15;
if (key_[sub0] == ikey) {
value_[sub0] += idelta;
return;
}
int sub1 = sub0 ^ 8;
if (key_[sub1] == ikey) {
value_[sub1] += idelta;
return;
}
int sub2 = (ikey & 7) + 16;
if (key_[sub2] == ikey) {
value_[sub2] += idelta;
return;
}
int alloc = -1;
if (key_[sub0] == 0) {
alloc = sub0;
} else if (key_[sub1] == 0) {
alloc = sub1;
} else if (key_[sub2] == 0) {
alloc = sub2;
} else {
alloc = sub0;
if (value_[sub1] < value_[alloc]) {alloc = sub1;}
if (value_[sub2] < value_[alloc]) {alloc = sub2;}
}
key_[alloc] = ikey;
value_[alloc] = idelta;
return;
}
int Tote::CurrentTopKey() {
int top_key = 0;
int top_value = -1;
for (int sub = 0; sub < kMaxSize_; ++sub) {
if (key_[sub] == 0) {continue;}
if (top_value < value_[sub]) {
top_value = value_[sub];
top_key = key_[sub];
}
}
return top_key;
}
void Tote::Sort(int n) {
for (int sub = 0; sub < n; ++sub) {
if (key_[sub] == 0) {value_[sub] = -1;}
for (int sub2 = sub + 1; sub2 < kMaxSize_; ++sub2) {
if (key_[sub2] == 0) {value_[sub2] = -1;}
if (value_[sub] < value_[sub2]) {
uint8 tmpk = key_[sub];
key_[sub] = key_[sub2];
key_[sub2] = tmpk;
int tmpv = value_[sub];
value_[sub] = value_[sub2];
value_[sub2] = tmpv;
}
}
}
}
void Tote::Dump(FILE* f) {
for (int sub = 0; sub < kMaxSize_; ++sub) {
if (key_[sub] > 0) {
fprintf(f, "[%2d] %3d %8d\n", sub, key_[sub], value_[sub]);
}
}
fprintf(f, "%d %d %d\n", gram_count_, incr_count_, byte_count_);
}
ToteWithReliability::ToteWithReliability() {
incr_count_ = 0;
sorted_ = 0;
memset(closepair_, 0, sizeof(closepair_));
memset(key_, 0, sizeof(key_));
}
ToteWithReliability::~ToteWithReliability() {
}
void ToteWithReliability::Reinit() {
incr_count_ = 0;
sorted_ = 0;
memset(closepair_, 0, sizeof(closepair_));
memset(key_, 0, sizeof(key_));
}
void ToteWithReliability::Add(uint8 ikey, int ibytes,
int score, int ireliability) {
DCHECK(ikey != 0);
CHECK(sorted_ == 0);
++incr_count_;
int sub0 = ikey & 15;
if (key_[sub0] == ikey) {
value_[sub0] += ibytes;
score_[sub0] += score;
reliability_[sub0] += ireliability * ibytes;
return;
}
int sub1 = sub0 ^ 8;
if (key_[sub1] == ikey) {
value_[sub1] += ibytes;
score_[sub1] += score;
reliability_[sub1] += ireliability * ibytes;
return;
}
int sub2 = (ikey & 7) + 16;
if (key_[sub2] == ikey) {
value_[sub2] += ibytes;
score_[sub2] += score;
reliability_[sub2] += ireliability * ibytes;
return;
}
int alloc = -1;
if (key_[sub0] == 0) {
alloc = sub0;
} else if (key_[sub1] == 0) {
alloc = sub1;
} else if (key_[sub2] == 0) {
alloc = sub2;
} else {
alloc = sub0;
if (value_[sub1] < value_[alloc]) {alloc = sub1;}
if (value_[sub2] < value_[alloc]) {alloc = sub2;}
}
key_[alloc] = ikey;
value_[alloc] = ibytes;
score_[alloc] = score;
reliability_[alloc] = ireliability * ibytes;
return;
}
int ToteWithReliability::Find(uint8 ikey) {
DCHECK(ikey != 0);
if (sorted_) {
for (int sub = 0; sub < kMaxSize_; ++sub) {
if (key_[sub] == ikey) {return sub;}
}
return -1;
}
int sub0 = ikey & 15;
if (key_[sub0] == ikey) {
return sub0;
}
int sub1 = sub0 ^ 8;
if (key_[sub1] == ikey) {
return sub1;
}
int sub2 = (ikey & 7) + 16;
if (key_[sub2] == ikey) {
return sub2;
}
return -1;
}
int ToteWithReliability::CurrentTopKey() {
int top_key = 0;
int top_value = -1;
for (int sub = 0; sub < kMaxSize_; ++sub) {
if (key_[sub] == 0) {continue;}
if (top_value < value_[sub]) {
top_value = value_[sub];
top_key = key_[sub];
}
}
return top_key;
}
void ToteWithReliability::Sort(int n) {
for (int sub = 0; sub < n; ++sub) {
if (key_[sub] == 0) {value_[sub] = -1;}
for (int sub2 = sub + 1; sub2 < kMaxSize_; ++sub2) {
if (key_[sub2] == 0) {value_[sub2] = -1;}
if (value_[sub] < value_[sub2]) {
uint8 tmpk = key_[sub];
key_[sub] = key_[sub2];
key_[sub2] = tmpk;
int tmpv = value_[sub];
value_[sub] = value_[sub2];
value_[sub2] = tmpv;
int tmps = score_[sub];
score_[sub] = score_[sub2];
score_[sub2] = tmps;
int tmpr = reliability_[sub];
reliability_[sub] = reliability_[sub2];
reliability_[sub2] = tmpr;
}
}
}
sorted_ = 1;
}
void ToteWithReliability::Dump(FILE* f) {
for (int sub = 0; sub < kMaxSize_; ++sub) {
if (key_[sub] > 0) {
fprintf(f, "[%2d] %3d %6d %5d %4d\n",
sub, key_[sub], value_[sub], score_[sub], reliability_[sub]);
}
}
fprintf(f, " %d#\n", incr_count_);
}