📄 event.h
字号:
//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, Yusuke Miyao/// You may distribute under the terms of the Artistic License.////// <id>$Id: Event.h,v 1.10 2003/05/16 08:20:14 yusuke Exp $</id>/// <collection>Maximum Entropy Estimator</collection>/// <name>Event.h</name>/// <overview>Event in ME Model</overview>/////////////////////////////////////////////////////////////////////////#ifndef Amis_Event_h_#define Amis_Event_h_#include <amis/configure.h>#include <amis/FeatureList.h>#include <amis/ErrorBase.h>#include <amis/Real.h>#include <amis/ModelBase.h>#include <amis/objstream.h>#include <amis/StringStream.h>#include <vector>#include <algorithm>AMIS_NAMESPACE_BEGINtypedef FeatureListFreq EventFreq;class IllegalEventError : public ErrorBase { public: IllegalEventError( const std::string& s ) : ErrorBase( s ) {} IllegalEventError( char* s ) : ErrorBase( s ) {}};///////////////////////////////////////////////////////////////////////// <classdef>/// <name>Event</name>/// <overview>An event</overview>/// <desc>/// This class describes an event object./// </desc>/// <body>template < class Feature >class Event {public: typedef Feature FeatureType; typedef typename Feature::FeatureFreq FeatureFreq; //typedef FeatureList< Feature >* iterator; //typedef const FeatureList< Feature >* const_iterator; typedef typename std::vector< FeatureList<Feature> >::iterator iterator; typedef typename std::vector< FeatureList<Feature> >::const_iterator const_iterator; typedef typename std::vector< FeatureList<Feature> >::size_type size_type; iterator begin() { return feature_lists.begin(); } const_iterator begin() const { return feature_lists.begin(); } iterator end() { return feature_lists.end(); } const_iterator end() const { return feature_lists.end(); }private:#ifdef AMIS_JOINT_PROB Real prob;#endif // AMIS_JOINT_PROB EventFreq freq; std::vector< FeatureList< Feature > > feature_lists; int active_id; static const int NULL_ACTIVE_ID = -1;public: Event() {#ifdef AMIS_JOINT_PROB prob = 1.0;#endif // AMIS_JOINT_PROB freq = 0; active_id = NULL_ACTIVE_ID; } ~Event() {} void clear() {#ifdef AMIS_JOINT_PROB prob = 1.0;#endif // AMIS_JOINT_PROB freq = 0; active_id = NULL_ACTIVE_ID; feature_lists.resize( 0 ); } void swap( Event< Feature >& event ) {#ifdef AMIS_JOINT_PROB ::swap( prob, event.prob );#endif // AMIS_JOINT_PROB ::swap( freq, event.freq ); feature_lists.swap( event.feature_lists ); ::swap( active_id, event.active_id ); } void addFeatureList( const std::vector< Feature >& vec, FeatureListFreq f ) { if ( f > 0 ) { if ( active_id != NULL_ACTIVE_ID ) throw IllegalEventError( "Ambiguous event is disallowed" ); active_id = feature_lists.size(); feature_lists.push_back( FeatureList< Feature >( vec, 1 ) ); freq = f; } else if ( f == 0 ) { feature_lists.push_back( FeatureList< Feature >( vec, 0 ) ); } else { throw IllegalEventError( "Event frequency must be positive" ); } } bool isValid() const { return active_id != NULL_ACTIVE_ID; } const FeatureList< Feature >& observedEvent() const { return feature_lists[ active_id ]; } int observedEventID() const { return active_id; } size_type size() const { return feature_lists.size(); } size_type numFeatureList() const { return size(); } const EventFreq eventFrequency() const { return freq; } const FeatureList< Feature >& operator[]( int i ) const { return feature_lists[ i ]; } Real eventProbability() const {#ifdef AMIS_JOINT_PROB return prob;#else // AMIS_JOINT_PROB return 1.0;#endif // AMIS_JOINT_PROB } void setEventProbability( Real p ) {#ifdef AMIS_JOINT_PROB prob = p;#else // AMIS_JOINT_PROB // do nothing#endif // AMIS_JOINT_PROB } const FeatureListFreq frequency() const { return freq; } const FeatureFreq maxFeatureCount() const { FeatureFreq freq = Feature::MIN_FEATURE_FREQ; for ( const_iterator it = begin(); it != end(); ++it ) { freq = std::max( freq, it->featureCount() ); } return freq; } bool isObserved( int i ) const { if ( active_id == NULL_ACTIVE_ID ) throw IllegalEventError( "Observed feature list not found" ); return i == active_id; } bool isObserved( const_iterator fl ) const { return isObserved( fl - begin() ); } ////////////////////////////////////////////////////////////public: void readObject( objstream& is ) {#ifdef AMIS_JOINT_PROB is >> prob;#endif // AMIS_JOINT_PROB is >> freq; size_type num_fl; is >> num_fl; is >> active_id; feature_lists.resize( num_fl ); for ( iterator it = begin(); it != end(); ++it ) { it->readObject( is ); } } /// Read an object from a stream void writeObject( objstream& os ) const {#ifdef AMIS_JOINT_PROB os << prob;#endif // AMIS_JOINT_PROB os << freq; os << feature_lists.size(); os << active_id; for ( const_iterator it = begin(); it != end(); ++it ) { it->writeObject( os ); } } /// Write an object into a stream};AMIS_NAMESPACE_END/// </body>/// </classdef>#endif // Event_h_// end of Event.h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -