Joshua
open source statistical hierarchical phrase-based machine translation system
|
00001 #ifndef LM_BUILDER_NGRAM_STREAM_H 00002 #define LM_BUILDER_NGRAM_STREAM_H 00003 00004 #include "lm/common/ngram.hh" 00005 #include "util/stream/chain.hh" 00006 #include "util/stream/multi_stream.hh" 00007 #include "util/stream/stream.hh" 00008 00009 #include <cstddef> 00010 00011 namespace lm { 00012 00013 template <class Proxy> class ProxyStream { 00014 public: 00015 // Make an invalid stream. 00016 ProxyStream() {} 00017 00018 explicit ProxyStream(const util::stream::ChainPosition &position, const Proxy &proxy = Proxy()) 00019 : proxy_(proxy), stream_(position) { 00020 proxy_.ReBase(stream_.Get()); 00021 } 00022 00023 Proxy &operator*() { return proxy_; } 00024 const Proxy &operator*() const { return proxy_; } 00025 00026 Proxy *operator->() { return &proxy_; } 00027 const Proxy *operator->() const { return &proxy_; } 00028 00029 void *Get() { return stream_.Get(); } 00030 const void *Get() const { return stream_.Get(); } 00031 00032 operator bool() const { return stream_; } 00033 bool operator!() const { return !stream_; } 00034 void Poison() { stream_.Poison(); } 00035 00036 ProxyStream<Proxy> &operator++() { 00037 ++stream_; 00038 proxy_.ReBase(stream_.Get()); 00039 return *this; 00040 } 00041 00042 private: 00043 Proxy proxy_; 00044 util::stream::Stream stream_; 00045 }; 00046 00047 template <class Payload> class NGramStream : public ProxyStream<NGram<Payload> > { 00048 public: 00049 // Make an invalid stream. 00050 NGramStream() {} 00051 00052 explicit NGramStream(const util::stream::ChainPosition &position) : 00053 ProxyStream<NGram<Payload> >(position, NGram<Payload>(NULL, NGram<Payload>::OrderFromSize(position.GetChain().EntrySize()))) {} 00054 }; 00055 00056 template <class Payload> class NGramStreams : public util::stream::GenericStreams<NGramStream<Payload> > { 00057 private: 00058 typedef util::stream::GenericStreams<NGramStream<Payload> > P; 00059 public: 00060 NGramStreams() : P() {} 00061 NGramStreams(const util::stream::ChainPositions &positions) : P(positions) {} 00062 }; 00063 00064 } // namespace 00065 #endif // LM_BUILDER_NGRAM_STREAM_H