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) 2013 Thoralf Klein 00008 * Written (W) 2012 Fernando José Iglesias García 00009 * Written (W) 2010,2012 Soeren Sonnenburg 00010 * Copyright (C) 2010 Berlin Institute of Technology 00011 * Copyright (C) 2012 Soeren Sonnenburg 00012 */ 00013 00014 #ifndef __SGSPARSEVECTOR_H__ 00015 #define __SGSPARSEVECTOR_H__ 00016 00017 #include <shogun/lib/config.h> 00018 #include <shogun/lib/DataType.h> 00019 #include <shogun/lib/SGReferencedData.h> 00020 00021 namespace shogun 00022 { 00023 class CFile; 00024 00026 template <class T> struct SGSparseVectorEntry 00027 { 00029 index_t feat_index; 00031 T entry; 00032 }; 00033 00039 template <class T> class SGSparseVector : public SGReferencedData 00040 { 00041 public: 00043 SGSparseVector(); 00044 00051 SGSparseVector(SGSparseVectorEntry<T>* feats, index_t num_entries, 00052 bool ref_counting=true); 00053 00055 SGSparseVector(index_t num_entries, bool ref_counting=true); 00056 00058 SGSparseVector(const SGSparseVector& orig); 00059 00060 virtual ~SGSparseVector(); 00061 00073 T dense_dot(T alpha, T* vec, int32_t dim, T b); 00074 00081 template<typename ST> T dense_dot(SGVector<ST> vec); 00082 00090 T sparse_dot(const SGSparseVector<T>& v); 00091 00099 static T sparse_dot(const SGSparseVector<T>& a, const SGSparseVector<T>& b); 00100 00106 inline SGSparseVector<T> get() 00107 { 00108 return *this; 00109 } 00110 00116 int32_t get_num_dimensions(); 00117 00126 void sort_features(bool stable_pointer = false); 00127 00134 T get_feature(int32_t index); 00135 00142 SGVector<T> get_dense(int32_t dimension); 00143 00149 SGVector<T> get_dense(); 00150 00152 SGSparseVector<T> clone() const; 00153 00158 void load(CFile* loader); 00159 00164 void save(CFile* saver); 00165 00166 00172 void display_vector(const char* name="vector", 00173 const char* prefix=""); 00174 00175 protected: 00176 virtual void copy_data(const SGReferencedData& orig); 00177 00178 virtual void init_data(); 00179 00180 virtual void free_data(); 00181 00194 static int32_t cmp_dot_prod_symmetry_fast(index_t alen, index_t blen); 00195 00205 static T dot_prod_asymmetric(const SGSparseVector<T>& a, const SGSparseVector<T>& b); 00206 00216 static T dot_prod_symmetric(const SGSparseVector<T>& a, const SGSparseVector<T>& b); 00217 00218 public: 00220 index_t num_feat_entries; 00221 00223 SGSparseVectorEntry<T>* features; 00224 00225 }; 00226 00227 } 00228 00229 #endif // __SGSPARSEVECTOR_H__