|
SHOGUN v0.9.3
|
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) 2009 Soeren Sonnenburg 00008 * Copyright (C) 2009 Berlin Institute of Technology 00009 */ 00010 00011 #ifndef _CDECOMPRESS_STRING__H__ 00012 #define _CDECOMPRESS_STRING__H__ 00013 00014 #include "features/Features.h" 00015 #include "features/StringFeatures.h" 00016 #include "lib/common.h" 00017 #include "lib/Compressor.h" 00018 #include "preproc/StringPreProc.h" 00019 00020 namespace shogun 00021 { 00022 template <class ST> class CStringFeatures; 00023 class CCompressor; 00024 enum E_COMPRESSION_TYPE; 00025 00036 template <class ST> class CDecompressString : public CStringPreProc<ST> 00037 { 00038 public: 00041 CDecompressString(E_COMPRESSION_TYPE ct) 00042 : CStringPreProc<ST>("DecompressString", "DECS") 00043 { 00044 compressor=new CCompressor(ct); 00045 } 00046 00048 virtual ~CDecompressString() 00049 { 00050 delete compressor; 00051 } 00052 00054 virtual bool init(CFeatures* f) 00055 { 00056 ASSERT(f->get_feature_class()==C_STRING); 00057 return true; 00058 } 00059 00061 virtual void cleanup() 00062 { 00063 } 00064 00066 bool load(FILE* f) 00067 { 00068 return false; 00069 } 00070 00072 bool save(FILE* f) 00073 { 00074 return false; 00075 } 00076 00080 virtual bool apply_to_string_features(CFeatures* f) 00081 { 00082 int32_t i; 00083 int32_t num_vec=((CStringFeatures<ST>*)f)->get_num_vectors(); 00084 00085 for (i=0; i<num_vec; i++) 00086 { 00087 int32_t len=0; 00088 bool free_vec; 00089 ST* vec=((CStringFeatures<ST>*)f)-> 00090 get_feature_vector(i, len, free_vec); 00091 00092 ST* decompressed=apply_to_string(vec, len); 00093 ((CStringFeatures<ST>*)f)-> 00094 free_feature_vector(vec, i, free_vec); 00095 ((CStringFeatures<ST>*)f)-> 00096 cleanup_feature_vector(i); 00097 ((CStringFeatures<ST>*)f)-> 00098 set_feature_vector(i, decompressed, len); 00099 } 00100 return true; 00101 } 00102 00104 virtual ST* apply_to_string(ST* f, int32_t &len) 00105 { 00106 uint64_t compressed_size=((int32_t*) f)[0]; 00107 uint64_t uncompressed_size=((int32_t*) f)[1]; 00108 00109 int32_t offs=CMath::ceil(2.0*sizeof(int32_t)/sizeof(ST)); 00110 ASSERT(uint64_t(len)==uint64_t(offs)+compressed_size); 00111 00112 len=uncompressed_size; 00113 uncompressed_size*=sizeof(ST); 00114 ST* vec=new ST[len]; 00115 compressor->decompress((uint8_t*) (&f[offs]), compressed_size, 00116 (uint8_t*) vec, uncompressed_size); 00117 00118 ASSERT(uncompressed_size==((uint64_t) len)*sizeof(ST)); 00119 return vec; 00120 } 00121 00122 protected: 00124 CCompressor* compressor; 00125 }; 00126 } 00127 #endif