00001 #ifndef SEARCH_HEADER__ 00002 #define SEARCH_HEADER__ 00003 00004 // Header consisting of Score, Arity, Note and Range 00005 00006 #include "search/types.hh" 00007 #include "moses/Range.h" 00008 00009 #include <stdint.h> 00010 00011 namespace search { 00012 00013 // Copying is shallow. 00014 class Header { 00015 public: 00016 bool Valid() const { return base_; } 00017 00018 Score GetScore() const { 00019 return *reinterpret_cast<const float*>(base_); 00020 } 00021 void SetScore(Score to) { 00022 *reinterpret_cast<float*>(base_) = to; 00023 } 00024 bool operator<(const Header &other) const { 00025 return GetScore() < other.GetScore(); 00026 } 00027 bool operator>(const Header &other) const { 00028 return GetScore() > other.GetScore(); 00029 } 00030 00031 Arity GetArity() const { 00032 return *reinterpret_cast<const Arity*>(base_ + sizeof(Score)); 00033 } 00034 00035 Note GetNote() const { 00036 return *reinterpret_cast<const Note*>(base_ + sizeof(Score) + sizeof(Arity)); 00037 } 00038 void SetNote(Note to) { 00039 *reinterpret_cast<Note*>(base_ + sizeof(Score) + sizeof(Arity)) = to; 00040 } 00041 00042 Moses::Range GetRange() const { 00043 return *reinterpret_cast<const Moses::Range*>(base_ + sizeof(Score) + sizeof(Arity) + sizeof(Note)); 00044 } 00045 void SetRange(Moses::Range to) { 00046 *reinterpret_cast<Moses::Range*>(base_ + sizeof(Score) + sizeof(Arity) + sizeof(Note)) = to; 00047 } 00048 00049 uint8_t *Base() { return base_; } 00050 const uint8_t *Base() const { return base_; } 00051 00052 protected: 00053 Header() : base_(NULL) {} 00054 00055 explicit Header(void *base) : base_(static_cast<uint8_t*>(base)) {} 00056 00057 Header(void *base, Arity arity) : base_(static_cast<uint8_t*>(base)) { 00058 *reinterpret_cast<Arity*>(base_ + sizeof(Score)) = arity; 00059 } 00060 00061 static const std::size_t kHeaderSize = sizeof(Score) + sizeof(Arity) + sizeof(Note) + sizeof(Moses::Range); 00062 00063 uint8_t *After() { return base_ + kHeaderSize; } 00064 const uint8_t *After() const { return base_ + kHeaderSize; } 00065 00066 private: 00067 uint8_t *base_; 00068 }; 00069 00070 } // namespace search 00071 00072 #endif // SEARCH_HEADER__