2012-10-15 16:58:33 +04:00
|
|
|
#ifndef SEARCH_EDGE__
|
|
|
|
#define SEARCH_EDGE__
|
|
|
|
|
|
|
|
#include "lm/state.hh"
|
|
|
|
#include "search/types.hh"
|
|
|
|
#include "search/vertex.hh"
|
2012-10-16 20:35:27 +04:00
|
|
|
#include "util/pool.hh"
|
2012-10-15 16:58:33 +04:00
|
|
|
|
2012-10-16 15:57:18 +04:00
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2012-10-15 16:58:33 +04:00
|
|
|
|
|
|
|
namespace search {
|
|
|
|
|
2012-10-16 15:57:18 +04:00
|
|
|
// Copyable, but the copy will be shallow.
|
|
|
|
class PartialEdge {
|
|
|
|
public:
|
|
|
|
// Allow default construction for STL.
|
|
|
|
PartialEdge() : base_(NULL) {}
|
|
|
|
bool Valid() const { return base_; }
|
|
|
|
|
|
|
|
Score GetScore() const {
|
|
|
|
return *reinterpret_cast<const float*>(base_);
|
|
|
|
}
|
|
|
|
void SetScore(Score to) {
|
|
|
|
*reinterpret_cast<float*>(base_) = to;
|
|
|
|
}
|
|
|
|
bool operator<(const PartialEdge &other) const {
|
|
|
|
return GetScore() < other.GetScore();
|
|
|
|
}
|
|
|
|
|
|
|
|
Arity GetArity() const {
|
|
|
|
return *reinterpret_cast<const Arity*>(base_ + sizeof(Score));
|
|
|
|
}
|
|
|
|
|
2012-10-17 19:49:41 +04:00
|
|
|
Note GetNote() const {
|
|
|
|
return *reinterpret_cast<const Note*>(base_ + sizeof(Score) + sizeof(Arity));
|
|
|
|
}
|
|
|
|
void SetNote(Note to) {
|
|
|
|
*reinterpret_cast<Note*>(base_ + sizeof(Score) + sizeof(Arity)) = to;
|
|
|
|
}
|
|
|
|
|
2012-10-16 15:57:18 +04:00
|
|
|
// Non-terminals
|
|
|
|
const PartialVertex *NT() const {
|
2012-10-17 19:49:41 +04:00
|
|
|
return reinterpret_cast<const PartialVertex*>(base_ + kHeaderSize);
|
2012-10-16 15:57:18 +04:00
|
|
|
}
|
|
|
|
PartialVertex *NT() {
|
2012-10-17 19:49:41 +04:00
|
|
|
return reinterpret_cast<PartialVertex*>(base_ + kHeaderSize);
|
2012-10-16 15:57:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const lm::ngram::ChartState &CompletedState() const {
|
|
|
|
return *Between();
|
|
|
|
}
|
|
|
|
const lm::ngram::ChartState *Between() const {
|
2012-10-17 19:49:41 +04:00
|
|
|
return reinterpret_cast<const lm::ngram::ChartState*>(base_ + kHeaderSize + GetArity() * sizeof(PartialVertex));
|
2012-10-16 15:57:18 +04:00
|
|
|
}
|
|
|
|
lm::ngram::ChartState *Between() {
|
2012-10-17 19:49:41 +04:00
|
|
|
return reinterpret_cast<lm::ngram::ChartState*>(base_ + kHeaderSize + GetArity() * sizeof(PartialVertex));
|
2012-10-16 15:57:18 +04:00
|
|
|
}
|
2012-10-15 16:58:33 +04:00
|
|
|
|
2012-10-16 15:57:18 +04:00
|
|
|
private:
|
2012-10-17 19:49:41 +04:00
|
|
|
static const std::size_t kHeaderSize = sizeof(Score) + sizeof(Arity) + sizeof(Note);
|
|
|
|
|
2012-10-16 15:57:18 +04:00
|
|
|
friend class PartialEdgePool;
|
|
|
|
PartialEdge(void *base, Arity arity) : base_(static_cast<uint8_t*>(base)) {
|
|
|
|
*reinterpret_cast<Arity*>(base_ + sizeof(Score)) = arity;
|
|
|
|
}
|
2012-10-15 16:58:33 +04:00
|
|
|
|
2012-10-16 15:57:18 +04:00
|
|
|
uint8_t *base_;
|
2012-10-15 16:58:33 +04:00
|
|
|
};
|
|
|
|
|
2012-10-16 15:57:18 +04:00
|
|
|
class PartialEdgePool {
|
|
|
|
public:
|
2012-10-17 16:50:08 +04:00
|
|
|
PartialEdge Allocate(Arity arity, Arity chart_states) {
|
2012-10-16 15:57:18 +04:00
|
|
|
return PartialEdge(
|
2012-10-17 19:49:41 +04:00
|
|
|
pool_.Allocate(PartialEdge::kHeaderSize + arity * sizeof(PartialVertex) + chart_states * sizeof(lm::ngram::ChartState)),
|
2012-10-16 15:57:18 +04:00
|
|
|
arity);
|
|
|
|
}
|
|
|
|
|
2012-10-17 16:50:08 +04:00
|
|
|
PartialEdge Allocate(Arity arity) {
|
|
|
|
return Allocate(arity, arity + 1);
|
|
|
|
}
|
|
|
|
|
2012-10-16 15:57:18 +04:00
|
|
|
private:
|
2012-10-16 20:35:27 +04:00
|
|
|
util::Pool pool_;
|
2012-10-16 15:57:18 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-10-15 16:58:33 +04:00
|
|
|
} // namespace search
|
|
|
|
#endif // SEARCH_EDGE__
|