00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef moses_WordsRange_h
00023 #define moses_WordsRange_h
00024
00025 #include <iostream>
00026 #include <boost/functional/hash.hpp>
00027 #include "TypeDef.h"
00028 #include "Util.h"
00029 #include "util/exception.hh"
00030
00031 #ifdef WIN32
00032 #undef max
00033 #endif
00034
00035 namespace Moses
00036 {
00037
00038
00039
00040
00041 class Range
00042 {
00043 friend std::ostream& operator << (std::ostream& out, const Range& range);
00044
00045
00046 size_t m_startPos, m_endPos;
00047 public:
00048 inline explicit Range() {}
00049 inline Range(size_t startPos, size_t endPos) : m_startPos(startPos), m_endPos(endPos) {}
00050 inline Range(const Range ©)
00051 : m_startPos(copy.GetStartPos())
00052 , m_endPos(copy.GetEndPos()) {
00053 }
00054
00055 inline size_t GetStartPos() const {
00056 return m_startPos;
00057 }
00058 inline size_t GetEndPos() const {
00059 return m_endPos;
00060 }
00061
00063 inline size_t GetNumWordsCovered() const {
00064 return (m_startPos == NOT_FOUND) ? 0 : m_endPos - m_startPos + 1;
00065 }
00066
00068 inline bool operator<(const Range& x) const {
00069 return (m_startPos<x.m_startPos
00070 || (m_startPos==x.m_startPos && m_endPos<x.m_endPos));
00071 }
00072
00073
00074 inline bool operator==(const Range& x) const {
00075 return (m_startPos==x.m_startPos && m_endPos==x.m_endPos);
00076 }
00077
00078 inline bool Overlap(const Range& x) const {
00079
00080 if ( x.m_endPos < m_startPos || x.m_startPos > m_endPos) return false;
00081
00082 return true;
00083 }
00084
00085 inline size_t GetNumWordsBetween(const Range& x) const {
00086 UTIL_THROW_IF2(Overlap(x), "Overlapping ranges");
00087
00088 if (x.m_endPos < m_startPos) {
00089 return m_startPos - x.m_endPos - 1;
00090 }
00091
00092 return x.m_startPos - m_endPos - 1;
00093 }
00094
00095
00096 TO_STRING();
00097 };
00098
00099 inline size_t hash_value(const Range& range)
00100 {
00101 size_t seed = range.GetStartPos();
00102 boost::hash_combine(seed, range.GetEndPos());
00103 return seed;
00104 }
00105
00106 }
00107 #endif