SHOGUN
v2.0.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) 2012 Fernando José Iglesias García 00008 * Written (W) 2010,2012 Soeren Sonnenburg 00009 * Copyright (C) 2010 Berlin Institute of Technology 00010 * Copyright (C) 2012 Soeren Sonnenburg 00011 */ 00012 00013 #ifndef __SGSPARSEVECTOR_H__ 00014 #define __SGSPARSEVECTOR_H__ 00015 00016 #include <shogun/lib/config.h> 00017 #include <shogun/lib/DataType.h> 00018 #include <shogun/lib/SGReferencedData.h> 00019 #include <map> 00020 00021 namespace shogun 00022 { 00023 00024 00026 template <class T> struct SGSparseVectorEntry 00027 { 00029 index_t feat_index; 00031 T entry; 00032 }; 00033 00035 template <class T> class SGSparseVector : public SGReferencedData 00036 { 00037 public: 00039 SGSparseVector() : SGReferencedData() 00040 { 00041 init_data(); 00042 } 00043 00045 SGSparseVector(SGSparseVectorEntry<T>* feats, index_t num_entries, 00046 bool ref_counting=true) : 00047 SGReferencedData(ref_counting), 00048 num_feat_entries(num_entries), features(feats) 00049 { 00050 } 00051 00053 SGSparseVector(index_t num_entries, bool ref_counting=true) : 00054 SGReferencedData(ref_counting), 00055 num_feat_entries(num_entries) 00056 { 00057 features = SG_MALLOC(SGSparseVectorEntry<T>, num_feat_entries); 00058 } 00059 00061 SGSparseVector(const SGSparseVector& orig) : 00062 SGReferencedData(orig) 00063 { 00064 copy_data(orig); 00065 } 00066 00067 virtual ~SGSparseVector() 00068 { 00069 unref(); 00070 } 00071 00072 protected: 00073 00074 virtual void copy_data(const SGReferencedData& orig) 00075 { 00076 num_feat_entries = ((SGSparseVector*)(&orig))->num_feat_entries; 00077 features = ((SGSparseVector*)(&orig))->features; 00078 } 00079 00080 virtual void init_data() 00081 { 00082 num_feat_entries = 0; 00083 features = NULL; 00084 } 00085 00086 virtual void free_data() 00087 { 00088 num_feat_entries = 0; 00089 SG_FREE(features); 00090 } 00091 00092 public: 00094 index_t num_feat_entries; 00095 00097 SGSparseVectorEntry<T>* features; 00098 00099 }; 00100 00101 } 00102 00103 #endif // __SGSPARSEVECTOR_H__