SHOGUN
v3.2.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2011 Shashwat Lal Das 00008 * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society 00009 */ 00010 #ifndef __STREAMING_FILEFROMDENSE_H__ 00011 #define __STREAMING_FILEFROMDENSE_H__ 00012 00013 #include <shogun/io/streaming/StreamingFileFromFeatures.h> 00014 #include <shogun/features/DenseFeatures.h> 00015 00016 namespace shogun 00017 { 00027 template<class T> class CStreamingFileFromDenseFeatures: 00028 public CStreamingFileFromFeatures 00029 { 00030 public: 00034 CStreamingFileFromDenseFeatures(); 00035 00042 CStreamingFileFromDenseFeatures(CDenseFeatures<T>* feat, 00043 float64_t* lab=NULL); 00044 00048 virtual ~CStreamingFileFromDenseFeatures(); 00049 00058 virtual void get_vector(T* &vec, int32_t &len); 00059 00069 virtual void get_vector_and_label(T* &vec, int32_t &len, float64_t &label); 00070 00076 void reset_stream() 00077 { 00078 vector_num=0; 00079 } 00080 00082 virtual const char* get_name() const 00083 { 00084 return "StreamingFileFromDenseFeatures"; 00085 00086 } 00087 00088 private: 00092 void init(); 00093 00094 protected: 00095 00097 CDenseFeatures<T>* features; 00098 00100 int32_t vector_num; 00101 00102 }; 00103 00104 template<class T> 00105 CStreamingFileFromDenseFeatures<T>::CStreamingFileFromDenseFeatures() : 00106 CStreamingFileFromFeatures() 00107 { 00108 init(); 00109 } 00110 00111 template<class T> 00112 CStreamingFileFromDenseFeatures<T>::CStreamingFileFromDenseFeatures( 00113 CDenseFeatures<T>* feat, float64_t* lab) : 00114 CStreamingFileFromFeatures() 00115 { 00116 init(); 00117 00118 REQUIRE(feat,"%s::CStreamingFileFromDenseFeatures() features required!\n", 00119 get_name()); 00120 features=feat; 00121 SG_REF(feat); 00122 00123 labels=lab; 00124 00125 } 00126 00127 template<class T> 00128 CStreamingFileFromDenseFeatures<T>::~CStreamingFileFromDenseFeatures() 00129 { 00130 SG_UNREF(features); 00131 } 00132 00133 template<class T> 00134 void CStreamingFileFromDenseFeatures<T>::init() 00135 { 00136 vector_num=0; 00137 features=NULL; 00138 00139 set_generic<T>(); 00140 } 00141 00142 /* Functions to return the vector from the DenseFeatures object 00143 * If the class is of type T, specialize this function to work for 00144 * vectors of that type. */ 00145 template<class T> 00146 void CStreamingFileFromDenseFeatures<T>::get_vector(T*& vector, 00147 int32_t& num_feat) 00148 { 00149 if (vector_num>=features->get_num_vectors()) 00150 { 00151 vector=NULL; 00152 num_feat=-1; 00153 return; 00154 } 00155 00156 SGVector<T> sg_vector=features->get_feature_vector(vector_num); 00157 00158 vector=sg_vector.vector; 00159 num_feat=sg_vector.vlen; 00160 vector_num++; 00161 00162 } 00163 00164 /* Functions to return the vector from the DenseFeatures object with label */ 00165 template<class T> 00166 void CStreamingFileFromDenseFeatures<T>::get_vector_and_label(T*& vector, 00167 int32_t& num_feat, float64_t& label) 00168 { 00169 if (vector_num>=features->get_num_vectors()) 00170 { 00171 vector=NULL; 00172 num_feat=-1; 00173 return; 00174 } 00175 00176 SGVector<T> sg_vector=features->get_feature_vector(vector_num); 00177 00178 vector=sg_vector.vector; 00179 num_feat=sg_vector.vlen; 00180 label=labels[vector_num]; 00181 00182 vector_num++; 00183 } 00184 00185 00186 } 00187 #endif //__STREAMING_FILEFROMDENSE_H__