00001 #ifndef SEARCH_APPLIED__
00002 #define SEARCH_APPLIED__
00003
00004 #include "search/edge.hh"
00005 #include "search/header.hh"
00006 #include "util/pool.hh"
00007
00008 #include <cmath>
00009
00010 namespace search {
00011
00012
00013 template <class Below> class GenericApplied : public Header {
00014 public:
00015 GenericApplied() {}
00016
00017 GenericApplied(void *location, PartialEdge partial)
00018 : Header(location) {
00019 memcpy(Base(), partial.Base(), kHeaderSize);
00020 Below *child_out = Children();
00021 const PartialVertex *part = partial.NT();
00022 const PartialVertex *const part_end_loop = part + partial.GetArity();
00023 for (; part != part_end_loop; ++part, ++child_out)
00024 *child_out = Below(part->End());
00025 }
00026
00027 GenericApplied(void *location, Score score, Arity arity, Note note, Moses::Range range) : Header(location, arity) {
00028 SetScore(score);
00029 SetNote(note);
00030 SetRange(range);
00031 }
00032
00033 explicit GenericApplied(History from) : Header(from) {}
00034
00035
00036
00037 Below *Children() {
00038 return reinterpret_cast<Below*>(After());
00039 }
00040 const Below *Children() const {
00041 return reinterpret_cast<const Below*>(After());
00042 }
00043
00044 static std::size_t Size(Arity arity) {
00045 return kHeaderSize + arity * sizeof(const Below);
00046 }
00047 };
00048
00049
00050 class Applied : public GenericApplied<Applied> {
00051 private:
00052 typedef GenericApplied<Applied> P;
00053
00054 public:
00055 Applied() {}
00056 Applied(void *location, PartialEdge partial) : P(location, partial) {}
00057 Applied(History from) : P(from) {}
00058 };
00059
00060
00061 class SingleBest {
00062 public:
00063 typedef PartialEdge Combine;
00064
00065 void Add(PartialEdge &existing, PartialEdge add) const {
00066 if (!existing.Valid() || existing.GetScore() < add.GetScore())
00067 existing = add;
00068 }
00069
00070 NBestComplete Complete(PartialEdge partial) {
00071 if (!partial.Valid())
00072 return NBestComplete(NULL, lm::ngram::ChartState(), -INFINITY);
00073 void *place_final = pool_.Allocate(Applied::Size(partial.GetArity()));
00074 Applied(place_final, partial);
00075 return NBestComplete(
00076 place_final,
00077 partial.CompletedState(),
00078 partial.GetScore());
00079 }
00080
00081 private:
00082 util::Pool pool_;
00083 };
00084
00085 }
00086
00087 #endif // SEARCH_APPLIED__