77
88namespace fbow {
99
10-
11- Vocabulary::~Vocabulary (){
12- if (_data!=nullptr ) AlignedFree ( _data);
13- }
14-
15-
1610void Vocabulary::setParams (int aligment, int k, int desc_type, int desc_size, int nblocks, std::string desc_name) {
1711 auto ns= desc_name.size ()<static_cast <size_t >(49 )?desc_name.size ():128 ;
1812 desc_name.resize (ns);
@@ -48,8 +42,8 @@ void Vocabulary::setParams(int aligment, int k, int desc_type, int desc_size, in
4842
4943 // give memory
5044 _params._total_size =_params._block_size_bytes_wp *_params._nblocks ;
51- _data=( char *)AlignedAlloc (_params._aligment ,_params._total_size );
52- memset ( _data, 0 , _params._total_size );
45+ _data = Data_ptr (( char *)AlignedAlloc (_params._aligment , _params._total_size ), &AlignedFree );
46+ memset (_data. get (), 0 , _params._total_size );
5347
5448}
5549
@@ -152,8 +146,7 @@ fBow Vocabulary::transform(const cv::Mat &features)
152146
153147void Vocabulary::clear ()
154148{
155- if (_data!=0 ) AlignedFree (_data);
156- _data=0 ;
149+ _data.reset ();
157150 memset (&_params,0 ,sizeof (_params));
158151 _params._desc_name_ [0 ]=' \0 ' ;
159152}
@@ -180,20 +173,19 @@ void Vocabulary::toStream(std::ostream &str)const{
180173 str.write ((char *)&sig,sizeof (sig));
181174 // save string
182175 str.write ((char *)&_params,sizeof (params));
183- str.write (_data, _params._total_size );
176+ str.write (_data. get (), _params._total_size );
184177}
185178
186179void Vocabulary::fromStream (std::istream &str)
187180{
188- if (_data!=0 ) AlignedFree (_data);
189181 uint64_t sig;
190182 str.read ((char *)&sig,sizeof (sig));
191183 if (sig!=55824124 ) throw std::runtime_error (" Vocabulary::fromStream invalid signature" );
192184 // read string
193185 str.read ((char *)&_params,sizeof (params));
194- _data=( char *)AlignedAlloc (_params._aligment ,_params._total_size );
186+ _data = Data_ptr (( char *)AlignedAlloc (_params._aligment , _params._total_size ), &AlignedFree );
195187 if (_data==0 ) throw std::runtime_error (" Vocabulary::fromStream Could not allocate data" );
196- str.read (_data, _params._total_size );
188+ str.read (_data. get (), _params._total_size );
197189}
198190
199191double fBow::score (const fBow &v1,const fBow &v2){
@@ -262,7 +254,7 @@ uint64_t Vocabulary::hash()const{
262254
263255 uint64_t seed = 0 ;
264256 for (uint64_t i=0 ;i<_params._total_size ;i++)
265- seed^= _data[i] + 0x9e3779b9 + (seed << 6 ) + (seed >> 2 );
257+ seed^= _data. get () [i] + 0x9e3779b9 + (seed << 6 ) + (seed >> 2 );
266258 return seed;
267259}
268260void fBow::toStream (std::ostream &str) const {
0 commit comments