mirror of
https://github.com/moses-smt/mosesdecoder.git
synced 2024-12-29 06:52:34 +03:00
3ced55e33d
Before with pop 25: real 9m49.480s user 9m38.389s sys 0m10.354s After with pop 25: real 8m46.553s user 8m35.138s sys 0m10.654s
94 lines
3.2 KiB
C++
94 lines
3.2 KiB
C++
#include "search/vertex_generator.hh"
|
|
|
|
#include "lm/left.hh"
|
|
#include "search/context.hh"
|
|
#include "search/edge.hh"
|
|
|
|
#include <stdint.h>
|
|
|
|
namespace search {
|
|
|
|
VertexGenerator::VertexGenerator(ContextBase &context, Vertex &gen) : context_(context), gen_(gen) {
|
|
gen.root_.InitRoot();
|
|
root_.under = &gen.root_;
|
|
}
|
|
|
|
namespace {
|
|
|
|
const uint64_t kCompleteAdd = static_cast<uint64_t>(-1);
|
|
|
|
void FillFinal(PartialEdge partial, Final &out) {
|
|
const Final **final_out = out.Reset(partial.GetScore(), partial.GetNote());
|
|
const PartialVertex *part = partial.NT();
|
|
const PartialVertex *const part_end_loop = part + partial.GetArity();
|
|
for (; part != part_end_loop; ++part, ++final_out) {
|
|
*final_out = &part->End();
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void VertexGenerator::NewHypothesis(PartialEdge partial) {
|
|
const lm::ngram::ChartState &state = partial.CompletedState();
|
|
std::pair<Existing::iterator, bool> got(existing_.insert(std::pair<uint64_t, Final*>(hash_value(state), NULL)));
|
|
if (!got.second) {
|
|
// Found it already.
|
|
Final &exists = *got.first->second;
|
|
if (exists.Bound() < partial.GetScore())
|
|
FillFinal(partial, exists);
|
|
return;
|
|
}
|
|
unsigned char left = 0, right = 0;
|
|
Trie *node = &root_;
|
|
while (true) {
|
|
if (left == state.left.length) {
|
|
node = &FindOrInsert(*node, kCompleteAdd - state.left.full, state, left, true, right, false);
|
|
for (; right < state.right.length; ++right) {
|
|
node = &FindOrInsert(*node, state.right.words[right], state, left, true, right + 1, false);
|
|
}
|
|
break;
|
|
}
|
|
node = &FindOrInsert(*node, state.left.pointers[left], state, left + 1, false, right, false);
|
|
left++;
|
|
if (right == state.right.length) {
|
|
node = &FindOrInsert(*node, kCompleteAdd - state.left.full, state, left, false, right, true);
|
|
for (; left < state.left.length; ++left) {
|
|
node = &FindOrInsert(*node, state.left.pointers[left], state, left + 1, false, right, true);
|
|
}
|
|
break;
|
|
}
|
|
node = &FindOrInsert(*node, state.right.words[right], state, left, false, right + 1, false);
|
|
right++;
|
|
}
|
|
|
|
node = &FindOrInsert(*node, kCompleteAdd - state.left.full, state, state.left.length, true, state.right.length, true);
|
|
got.first->second = CompleteTransition(*node, state, partial);
|
|
}
|
|
|
|
VertexGenerator::Trie &VertexGenerator::FindOrInsert(VertexGenerator::Trie &node, uint64_t added, const lm::ngram::ChartState &state, unsigned char left, bool left_full, unsigned char right, bool right_full) {
|
|
VertexGenerator::Trie &next = node.extend[added];
|
|
if (!next.under) {
|
|
next.under = context_.NewVertexNode();
|
|
lm::ngram::ChartState &writing = next.under->MutableState();
|
|
writing = state;
|
|
writing.left.full &= left_full && state.left.full;
|
|
next.under->MutableRightFull() = right_full && state.left.full;
|
|
writing.left.length = left;
|
|
writing.right.length = right;
|
|
node.under->AddExtend(next.under);
|
|
}
|
|
return next;
|
|
}
|
|
|
|
Final *VertexGenerator::CompleteTransition(VertexGenerator::Trie &starter, const lm::ngram::ChartState &state, PartialEdge partial) {
|
|
VertexNode &node = *starter.under;
|
|
assert(node.State().left.full == state.left.full);
|
|
assert(!node.End());
|
|
Final *final = context_.NewFinal();
|
|
FillFinal(partial, *final);
|
|
node.SetEnd(final);
|
|
return final;
|
|
}
|
|
|
|
} // namespace search
|