📄 featurelist.h
字号:
//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, Yusuke Miyao/// You may distribute under the terms of the Artistic License.////// <id>$Id: FeatureList.h,v 1.10 2003/05/16 12:10:44 yusuke Exp $</id>/// <collection>Maximum Entropy Estimator</collection>/// <name>FeatureList.h</name>/// <overview>A set of activated features</overview>/////////////////////////////////////////////////////////////////////////#ifndef Amis_FeatureList_h_#define Amis_FeatureList_h_#include <amis/configure.h>#include <amis/Feature.h>#include <amis/ModelBase.h>#include <amis/objstream.h>#include <vector>#include <algorithm>AMIS_NAMESPACE_BEGINtypedef int FeatureListFreq;class IllegalFeatureListError : public ErrorBase { public: IllegalFeatureListError( const std::string& s ) : ErrorBase( s ) {} IllegalFeatureListError( char* s ) : ErrorBase( s ) {}};///////////////////////////////////////////////////////////////////////// <classdef>/// <name>FeatureList</name>/// <overview>A set of activated features</overview>/// <desc>/// This class describes a set of activated features for an event./// </desc>/// <body>template < class Feature >class FeatureList {public: typedef typename Feature::FeatureFreq FeatureFreq; typedef size_t size_type;private: FeatureFreq count; Feature* features, * tail;protected: public: typedef const Feature* const_iterator; const_iterator begin() const { return features; } const_iterator end() const { return tail; }public: size_type size() const { return static_cast< size_type >( tail - features ); } FeatureList() { count = 0; features = tail = NULL; } FeatureList( const std::vector< Feature >& v, FeatureListFreq f ) { // sort history list according to the history id. // may be junt an overhead, but needed for the fast kernel calculation std::vector<Feature>& vec = const_cast< std::vector<Feature>& >(v); std::sort( vec.begin(), vec.end() ); count = 0; if ( f != 1 && f != 0 ) { AMIS_DEBUG_MESSAGE(2, "In FeatureList(...) f = " << f << '\n' << std::flush ); throw IllegalFeatureListError( "FeatureListFreq must be 1 or 0" ); } if ( vec.size() == 0 ) { features = tail = NULL; } else { features = new Feature[ vec.size() ]; tail = features + vec.size(); for ( size_type i = 0; i < vec.size(); ++i ) { features[ i ] = vec[ i ]; count += features[ i ].freq(); } } } FeatureList( const FeatureList& fl ) { count = fl.count; if ( fl.features == fl.tail ) { features = tail = NULL; } else { features = new Feature[ fl.size() ]; Feature* it = features; for ( Feature* it2 = fl.features; it2 != fl.tail; ++it, ++it2 ) { *it = *it2; } tail = it; } } ~FeatureList() { if ( features != NULL ) { delete [] features; } } FeatureList& operator=( const FeatureList& fl ) { if ( features != NULL ) delete [] features; count = fl.count; if ( fl.features == fl.tail ) { features = tail = NULL; } else { features = new Feature[ fl.size() ]; Feature* it = features; for ( Feature* it2 = fl.features; it2 != fl.tail; ++it, ++it2 ) { *it = *it2; } tail = it; } return *this; } FeatureList& copy_set( std::vector<Feature>& v, FeatureListFreq f) { // sort history list according to the history id. // may be junt an overhead, but needed for the fast kernel calculation std::vector<Feature>& vec = const_cast< std::vector<Feature>& >(v); std::sort( vec.begin(), vec.end() ); // storage management if( features != NULL ){ if( vec.size() <= size() ){ tail = (features + vec.size()); // only resizing } else{ delete[] features; if(vec.size() == 0){ features = tail = NULL; }else{ features = new Feature[ vec.size() ]; tail = (features + vec.size()); } } }else{ if(vec.size() > 0){ features = new Feature[ vec.size() ]; tail = (features + vec.size()); } } count = 0; if ( f != 1 && f != 0 ) { AMIS_DEBUG_MESSAGE(2, "IN: " << __PRETTY_FUNCTION__ << " f = " << f << '\n' << flush ); throw IllegalFeatureListError( "FeatureListFreq must be 1 or 0" ); } for ( size_type i = 0; i < vec.size(); ++i ) { features[ i ] = vec[ i ]; count += features[ i ].freq(); } } void swap( FeatureList< Feature >& fl ) { ::swap( count, fl.count ); ::swap( features, fl.features ); ::swap( tail, fl.tail ); } FeatureFreq featureCount() const { return count; } FeatureListFreq frequency() const { return 1; }public: void readObject( objstream& is ) { if ( features != NULL ) delete [] features; is >> count; size_type num_features; is >> num_features; if ( num_features == 0 ) { features = tail = NULL; } else { features = new Feature[ num_features ]; tail = features + num_features; for ( Feature* it = features; it != tail; ++it ) { it->readObject( is ); } } } void writeObject( objstream& os ) const { os << count; os << size(); for( Feature* it = features; it != tail; ++it ) { it->writeObject( os ); } }};AMIS_NAMESPACE_END/// </body>/// </classdef>#endif // FeatureList_h_// end of FeatureList.h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -