From c28417c8ff3133d4586969f2276e6c29ef26c10c Mon Sep 17 00:00:00 2001 From: biagio montesano Date: Tue, 26 Aug 2014 18:46:46 +0200 Subject: [PATCH] Array32 class removal. --- .../opencv2/line_descriptor/descriptor.hpp | 52 +---- .../src/binary_descriptor_matcher.cpp | 208 +++++------------- 2 files changed, 62 insertions(+), 198 deletions(-) diff --git a/modules/line_descriptor/include/opencv2/line_descriptor/descriptor.hpp b/modules/line_descriptor/include/opencv2/line_descriptor/descriptor.hpp index 63164478a..45a5747a9 100644 --- a/modules/line_descriptor/include/opencv2/line_descriptor/descriptor.hpp +++ b/modules/line_descriptor/include/opencv2/line_descriptor/descriptor.hpp @@ -888,51 +888,6 @@ protected: AlgorithmInfo* info() const; private: -class Array32 -{ - -public: -/* set ARRAY_RESIZE_FACTOR */ -static void setArrayResizeFactor( double arf ); - -/* constructor */ -Array32(); - -/* destructor */ -~Array32(); - -/* cleaning function used in destructor */ -void cleanup(); - -/* push data */ -void push( UINT32 data ); - -/* insert data at given index */ -void insert( UINT32 index, UINT32 data ); - -/* return data */ -UINT32* data(); - -/* return data size */ -UINT32 size(); - -/* return capacity */ -UINT32 capacity(); - -/* definition of operator = */ -void operator=( const Array32& ); - -/* print data */ -void print(); - -/* initializer */ -void init( int size ); - -/* data */ -UINT32 *arr; - -}; - class BucketGroup { @@ -949,9 +904,14 @@ void insert( int subindex, UINT32 data ); /* perform a query to the bucket */ UINT32* query( int subindex, int *size ); +/* utility functions */ +void insert_value( std::vector& vec, int index, UINT32 data ); +void push_value( std::vector& vec, UINT32 Data ); + /* data fields */ UINT32 empty; -Array32 *group; +std::vector group; + }; diff --git a/modules/line_descriptor/src/binary_descriptor_matcher.cpp b/modules/line_descriptor/src/binary_descriptor_matcher.cpp index 268a4dc4b..0e10a5241 100644 --- a/modules/line_descriptor/src/binary_descriptor_matcher.cpp +++ b/modules/line_descriptor/src/binary_descriptor_matcher.cpp @@ -844,7 +844,7 @@ int BinaryDescriptorMatcher::SparseHashtable::init( int _b ) /* destructor */ BinaryDescriptorMatcher::SparseHashtable::~SparseHashtable() { - free( table ); + free (table); } /* insert data */ @@ -863,205 +863,109 @@ UINT32* BinaryDescriptorMatcher::SparseHashtable::query( UINT64 index, int *Size BinaryDescriptorMatcher::BucketGroup::BucketGroup() { empty = 0; - group = NULL; + group = std::vector < uint32_t > ( 2, 0 ); } /* destructor */ BinaryDescriptorMatcher::BucketGroup::~BucketGroup() { - if( group != NULL ) - delete group; } -/* insert data into the bucket */ -void BinaryDescriptorMatcher::BucketGroup::insert( int subindex, UINT32 data ) + void BinaryDescriptorMatcher::BucketGroup::insert_value( std::vector& vec, int index, UINT32 data ) { - if( group == NULL ) - { - group = new Array32(); - group->push( 0 ); - } - - UINT32 lowerbits = ( (UINT32) 1 << subindex ) - 1; - int end = popcnt( empty & lowerbits ); - - if( ! ( empty & ( (UINT32) 1 << subindex ) ) ) + if( vec.size() > 1 ) { - group->insert( end, group->arr[end + 2] ); - empty |= (UINT32) 1 << subindex; - } + if( vec[0] == vec[1] ) + { + vec[1] = (UINT32) ceil( vec[0] * 1.1 ); + for ( int i = 0; i < (int) ( 2 + vec[1] - vec.size() ); i++ ) + vec.push_back( 0 ); - int totones = popcnt( empty ); - group->insert( totones + 1 + group->arr[2 + end + 1], data ); - for ( int i = end + 1; i < totones + 1; i++ ) - group->arr[2 + i]++; -} + } -/* perform a query to the bucket */ -UINT32* BinaryDescriptorMatcher::BucketGroup::query( int subindex, int *size ) -{ - if( empty & ( (UINT32) 1 << subindex ) ) - { - UINT32 lowerbits = ( (UINT32) 1 << subindex ) - 1; - int end = popcnt( empty & lowerbits ); - int totones = popcnt( empty ); - *size = group->arr[2 + end + 1] - group->arr[2 + end]; - return group->arr + 2 + totones + 1 + group->arr[2 + end]; + vec.insert( vec.begin() + 2 + index, data ); + vec[2 + index] = data; + vec[0]++; } else { - *size = 0; - return NULL; + vec = std::vector < uint32_t > ( 3, 0 ); + vec[0] = 1; + vec[1] = 1; + vec[2] = data; } } -/* set ARRAY_RESIZE_FACTOR */ -void BinaryDescriptorMatcher::Array32::setArrayResizeFactor( double arf ) -{ - ARRAY_RESIZE_FACTOR = arf; -} - -/* constructor */ -BinaryDescriptorMatcher::Array32::Array32() -{ - arr = NULL; - ARRAY_RESIZE_FACTOR = 1.1; // minimum is 1.0 - ARRAY_RESIZE_ADD_FACTOR = 4; // minimum is 1 - -} - -/* definition of operator = - Array32& Array32::operator = (const Array32 &rhs) */ -void BinaryDescriptorMatcher::Array32::operator =( const Array32 &rhs ) +void BinaryDescriptorMatcher::BucketGroup::push_value( std::vector& vec, UINT32 Data ) { - if( &rhs != this ) - this->arr = rhs.arr; -} - -/* destructor */ -BinaryDescriptorMatcher::Array32::~Array32() -{ - cleanup(); -} - -/* cleaning function used in destructor */ -void BinaryDescriptorMatcher::Array32::cleanup() -{ - free( arr ); -} - -/* push data */ -void BinaryDescriptorMatcher::Array32::push( UINT32 Data ) -{ - if( arr ) + if( vec.size() > 0 ) { - if( arr[0] == arr[1] ) + if( vec[0] == vec[1] ) { - arr[1] = (UINT32) std::max( ceil( arr[1] * ARRAY_RESIZE_FACTOR ), arr[1] + ARRAY_RESIZE_ADD_FACTOR ); - UINT32* new_Data = static_cast( realloc( arr, sizeof(UINT32) * ( 2 + arr[1] ) ) ); - if( new_Data == NULL ) - { - /* could not realloc, but orig still valid */ - std::cout << "ALERT!!!! Not enough memory, operation aborted!" << std::endl; - exit( 0 ); - } - else - { - arr = new_Data; - } - + vec[1] = (UINT32) std::max( ceil( vec[1] * ARRAY_RESIZE_FACTOR ), vec[1] + ARRAY_RESIZE_ADD_FACTOR ); + for ( int i = 0; i < (int) ( 2 + vec[1] - vec.size() ); i++ ) + vec.push_back( 0 ); } - arr[2 + arr[0]] = Data; - arr[0]++; + vec[2 + vec[0]] = Data; + vec[0]++; } else { - arr = (UINT32*) malloc( ( size_t )( 2 + ARRAY_RESIZE_ADD_FACTOR ) * sizeof(UINT32) ); - arr[0] = 1; - arr[1] = 1; - arr[2] = Data; + vec = std::vector < uint32_t > ( 2 + ARRAY_RESIZE_ADD_FACTOR, 0 ); + vec[0] = 1; + vec[1] = 1; + vec[2] = Data; } } -/* insert data at given index */ -void BinaryDescriptorMatcher::Array32::insert( UINT32 index, UINT32 Data ) +/* insert data into the bucket */ +void BinaryDescriptorMatcher::BucketGroup::insert( int subindex, UINT32 data ) { - if( arr ) + if( group.size() == 0 ) { - if( arr[0] == arr[1] ) - { - arr[1] = (UINT32) ceil( arr[0] * 1.1 ); - UINT32* new_data = static_cast( realloc( arr, sizeof(UINT32) * ( 2 + arr[1] ) ) ); - if( new_data == NULL ) - { - // could not realloc, but orig still valid - std::cout << "ALERT!!!! Not enough memory, operation aborted!" << std::endl; - exit( 0 ); - } - else - { - arr = new_data; - } - } - - memmove( arr + ( 2 + index ) + 1, arr + ( 2 + index ), ( arr[0] - index ) * sizeof ( *arr ) ); - - arr[2 + index] = Data; - arr[0]++; + push_value( group, 0 ); } - else + UINT32 lowerbits = ( (UINT32) 1 << subindex ) - 1; + int end = popcnt( empty & lowerbits ); + + if( ! ( empty & ( (UINT32) 1 << subindex ) ) ) { - arr = (UINT32*) malloc( 3 * sizeof(UINT32) ); - arr[0] = 1; - arr[1] = 1; - arr[2] = Data; + insert_value( group, end, group[end + 2] ); + empty |= (UINT32) 1 << subindex; } -} -/* return data */ -UINT32* BinaryDescriptorMatcher::Array32::data() -{ - return arr ? arr + 2 : NULL; -} - -/* return data size */ -UINT32 BinaryDescriptorMatcher::Array32::size() -{ - return arr ? arr[0] : 0; -} + int totones = popcnt( empty ); + insert_value( group, totones + 1 + group[2 + end + 1], data ); -/* return capacity */ -UINT32 BinaryDescriptorMatcher::Array32::capacity() -{ - return arr ? arr[1] : 0; + for ( int i = end + 1; i < totones + 1; i++ ) + group[2 + i]++; } -/* print data */ -void BinaryDescriptorMatcher::Array32::print() +/* perform a query to the bucket */ +UINT32* BinaryDescriptorMatcher::BucketGroup::query( int subindex, int *size ) { - for ( int i = 0; i < (int) size(); i++ ) - printf( "%d, ", arr[i + 2] ); + if( empty & ( (UINT32) 1 << subindex ) ) + { + UINT32 lowerbits = ( (UINT32) 1 << subindex ) - 1; + int end = popcnt( empty & lowerbits ); + int totones = popcnt( empty ); - printf( "\n" ); -} + *size = group[2 + end + 1] - group[2 + end]; + return & ( * ( group.begin() + 2 + totones + 1 + (int) group[2 + end] ) ); + } -/* initializer */ -void BinaryDescriptorMatcher::Array32::init( int Size ) -{ - if( arr == NULL ) + else { - arr = (UINT32*) malloc( ( 2 + Size ) * sizeof(UINT32) ); - arr[0] = 0; - arr[1] = Size; + *size = 0; + return NULL; } } - } }