60 template <
typename Domain,
typename Value,
typename HashKey>
62 ImageContainerByHashTree<Domain, Value, HashKey>
63 ::ImageContainerByHashTree (
const unsigned int hashKeySize,
64 const unsigned int depth,
65 const Value defaultValue )
66 : myDomain(
Domain() ), myKeySize ( hashKeySize )
70 ASSERT ( hashKeySize <=
sizeof (
HashKey ) *8 );
75 unsigned int acceptedDepth = ( (
sizeof (
HashKey ) * 8 - 1 ) /
dim );
76 if ( depth > acceptedDepth )
78 trace.
error() <<
"ImageContainerByHashTree::Constructor: error !" << endl;
79 trace.
error() <<
"requested depth too high for the key type" << endl;
80 trace.
error() <<
"accepted: " << acceptedDepth <<
" Requested: " << depth << endl;
98 template <
typename Domain,
typename Value,
typename HashKey>
102 const unsigned int hashKeySize,
103 const Value defaultValue ):
104 myDomain(aDomain), myKeySize ( hashKeySize )
106 myOrigin = aDomain.lowerBound() ;
108 ASSERT ( hashKeySize <=
sizeof (
HashKey ) *8 );
110 Point p1 = myDomain.lowerBound();
111 Point p2 = myDomain.upperBound();
113 myPreComputedIntermediateMask = ~ (
static_cast<HashKey> ( ~0 ) << myKeySize );
116 for (
unsigned int i = 0; i < dim; ++i )
117 if ( maxSize < p1[i] - p2[i] )
118 maxSize = p1[i] - p2[i];
119 unsigned int depth = (
unsigned int)(ceil ( log ( (
double) maxSize ) / log((
double) 2.0) ));
121 unsigned int acceptedDepth = ( (
sizeof (
HashKey ) * 8 - 1 ) / dim );
122 if ( depth > acceptedDepth )
124 trace.
error() <<
"ImageContainerByHashTree::Constructor: error !"
125 <<
" requested depth too high for the key type" << endl;
126 trace.
error() <<
"accepted: " << acceptedDepth
127 <<
" Requested: " << depth << endl;
128 setDepth ( acceptedDepth );
134 myArraySize = 1 << hashKeySize;
135 myData =
new Node*[myArraySize];
136 for (
unsigned int i = 0; i < myArraySize; ++i )
139 addNode ( defaultValue, ROOT_KEY );
144 template <
typename Domain,
typename Value,
typename HashKey>
150 const Value defaultValue )
151 : myDomain( p1, p2 ), myKeySize ( hashKeySize ), myOrigin ( p1 )
154 ASSERT ( hashKeySize <=
sizeof (
HashKey ) *8 );
156 myPreComputedIntermediateMask = ~ (
static_cast<HashKey> ( ~0 ) << myKeySize );
159 for (
unsigned int i = 0; i < dim; ++i )
160 if ( maxSize < p1[i] - p2[i] )
161 maxSize = p1[i] - p2[i];
162 unsigned int depth = (
unsigned int)(ceil ( log ( (
double) maxSize ) / log((
double) 2.0) ));
164 unsigned int acceptedDepth = ( (
sizeof (
HashKey ) * 8 - 1 ) / dim );
165 if ( depth > acceptedDepth )
167 trace.
error() <<
"ImageContainerByHashTree::Constructor: error !"
168 <<
" requested depth too high for the key type" << endl;
169 trace.
error() <<
"accepted: " << acceptedDepth
170 <<
" Requested: " << depth << endl;
171 setDepth ( acceptedDepth );
177 myArraySize = 1 << hashKeySize;
178 myData =
new Node*[myArraySize];
179 for (
unsigned int i = 0; i < myArraySize; ++i )
182 addNode ( defaultValue, ROOT_KEY );
191 template <
typename Domain,
typename Value,
typename HashKey>
200 template <
typename Domain,
typename Value,
typename HashKey>
205 return ConstRange( myDomain.begin(), myDomain.end(), *this );
209 template <
typename Domain,
typename Value,
typename HashKey>
218 template <
typename Domain,
typename Value,
typename HashKey>
223 setValue ( getKey ( aPoint ), value );
227 template <
typename Domain,
typename Value,
typename HashKey>
234 bool broValue = ( key !=
static_cast<HashKey> ( 1 ) );
235 myMorton.brotherKeys ( key, brothers );
236 for (
unsigned int i = 0; i < myN - 1; ++ i )
238 Node* n = getNode ( brothers[i] );
239 if ( ! ( n && ( n->
getObject() == value ) ) )
248 setValue ( myMorton.parentKey ( key ), value );
253 Node* n = getNode ( key );
262 std::list< HashKey > nodeList;
263 while ( iterKey != 0 )
266 n = getNode ( iterKey );
270 if ( tempVal == value )
272 removeNode ( iterKey );
273 for (
typename std::list< HashKey >::iterator it = nodeList.begin();
274 it != nodeList.end();
278 addNode ( tempVal, *it );
280 addNode ( value, key );
288 myMorton.brotherKeys ( iterKey, brothersH );
289 for (
unsigned int i = 0; i < myN - 1; ++i )
291 nodeList.push_front ( brothersH[i] );
296 addNode ( value, key );
297 unsigned int nbRecur = myTreeDepth - getKeyDepth ( key ) + 1;
299 myMorton.childrenKeys ( key, children );
300 for (
unsigned int i = 0; i < myN; ++i )
301 recursiveRemoveNode ( children[i], nbRecur );
307 template <
typename Domain,
typename Value,
typename HashKey >
313 template <
typename Domain,
typename Value,
typename HashKey >
317 return get ( aPoint );
320 template <
typename Domain,
typename Value,
typename HashKey >
327 while ( iterKey != 0 )
329 Node* n = getNode ( iterKey );
335 return blendChildren ( key );
339 template <
typename Domain,
typename Value,
typename HashKey >
346 HashKey limit = myDepthMask << 1;
347 while ( iterKey < limit )
349 Node* n = getNode ( iterKey );
351 return blendChildren ( key );
355 while ( iterKey != 0 )
357 Node* n = getNode ( iterKey );
366 template <
typename Domain,
typename Value,
typename HashKey>
371 return get ( getKey ( aPoint ) );
375 template <
typename Domain,
typename Value,
typename HashKey >
385 HashKey key2 = getIntermediateKey ( aKey );
386 Node* iter = myData[key2];
390 if ( iter->
getKey() == aKey )
398 template <
typename Domain,
typename Value,
typename HashKey >
404 Point currentPos = aPoint - myOrigin;
406 myMorton.interleaveBits ( currentPos, result );
410 result |= myDepthMask;
415 template <
typename Domain,
typename Value,
typename HashKey >
420 return ( key & myPreComputedIntermediateMask );
424 template <
typename Domain,
typename Value,
typename HashKey >
455 template <
typename Domain,
typename Value,
typename HashKey >
463 if ( iter && ( iter->
getKey() == key ) )
474 if ( next->
getKey() == key )
486 template <
typename Domain,
typename Value,
typename HashKey >
493 if ( --nbRecursions > 0 )
497 for (
unsigned int i = 0; i <
myN; ++i )
510 template <
typename Domain,
typename Value,
typename HashKey >
522 template <
typename Domain,
typename Value,
typename HashKey >
527 for (
int i = (
sizeof (
HashKey ) << 3 ) - 1; i >= 0; --i )
528 if ( key & ( static_cast<HashKey> ( 1 ) << i ) )
536 template <
typename Domain,
typename Value,
typename HashKey >
542 for (
int i = (
sizeof (
HashKey ) << 3 ) - 1; i >= 0; --i )
544 if ( key & Bits::mask<HashKey> ( i ) )
546 key &= ~
Bits::mask<HashKey> ( i );
550 int* coordinates =
new int[
dim];
552 for (
unsigned int i = 0; i <
dim; ++i )
555 for (
unsigned int bitPos = 0; bitPos < (
sizeof (
HashKey ) << 3 ) /
dim; ++bitPos )
559 coordinates[i] |= Bits::mask<HashKey> ( bitPos );
567 template <
typename Domain,
typename Value,
typename HashKey >
577 int i = (
sizeof (
HashKey ) << 3 ) - 1;
578 for ( ; i >= 0; --i )
579 if ( key & ( static_cast<HashKey> ( 1 ) << i ) )
594 template <
typename Domain,
typename Value ,
typename HashKey >
599 out <<
"ImageContainerByHashTree::printState" << endl;
601 out <<
"dim: " <<
dim <<
"myN: " <<
myN << endl;
605 template <
typename Domain,
typename Value,
typename HashKey >
611 for (
unsigned int i = 0; i < level; ++i )
633 for (
unsigned int i = 0; i <
myN; ++i )
634 printTree ( children[i], out, displayKeys );
638 template <
typename Domain,
typename Value,
typename HashKey >
643 out <<
"ImageContainerByHashTree::printInternalState ----------------------------------" << endl;
644 out <<
"| <template> dim = " <<
dim <<
" myN = " <<
myN << endl;
647 for (
unsigned int i = 0; i < ( 1 <<
myKeySize ); ++i )
669 cout <<
"x]" << endl;
676 out <<
"----------------------------------------------------------------" << endl;
680 template <
typename Domain,
typename Value,
typename HashKey >
686 unsigned int totalSize =
sizeof ( *this ) + ( 1 <<
myKeySize ) *
sizeof (
Node* ) + nbNodes *
sizeof (
Node );
688 out <<
"[ImageContainerByHashTree]: Dimension=" << ( int )
dim <<
", HashKey size="
691 <<
" bytes)" <<
", " << nbNodes <<
" nodes" <<
", Empty lists=" <<
getNbEmptyLists()
694 <<
", total memory usage=" << totalSize <<
" bytes" << endl;
699 template <
typename Domain,
typename Value,
typename HashKey >
704 if ( !
myData[intermediateKey] )
710 unsigned int count = 1;
723 template <
typename Domain,
typename Value,
typename HashKey >
728 unsigned int count = 0;
730 for (
unsigned int i = 0; i < arraySize; ++i )
738 template <
typename Domain,
typename Value,
typename HashKey >
743 unsigned int count = 0;
745 for (
unsigned int i = 0; i < arraySize; ++i )
754 template<
typename Domain,
typename Value,
typename HashKey >
762 for (
unsigned int i = 0; i < arraySize; ++i )
772 trace.
error() <<
"ImageContainerByHashTree::getAverageCollision() - error" << endl
773 <<
"the container is empty !" << endl;
776 return count / nbLists;
781 template <
typename Domain,
typename Value,
typename HashKey >
786 unsigned int count = 0;
788 for (
unsigned int i = 0; i < arraySize; ++i )
792 unsigned int collision =
getNbNodes ( i ) - 1;
793 if ( collision > count )
803 template <
typename Domain,
typename Value,
typename HashKey >
808 return "ImageContainerByHashTree";
811 template <
typename Domain,
typename Value,
typename HashKey >
825 for (
unsigned int i = 0; i <
myN; ++i )
829 return static_cast<Value> ( result /
myN );
834 template <
typename Domain,
typename Value,
typename HashKey >
838 trace.
info() <<
"Checking key=" << key << endl;
848 if ( ( n != 0 ) && ( leafAbove ) )
850 trace.
error() <<
"ImageContainerByHashTree::checkIntegrity - error:" << endl
852 <<
"several leafs found" << endl;
858 if ( leafAbove || n )
864 trace.
error() <<
"ImageContainerByHashTree::checkIntegrity - error:" << endl
866 <<
"no leaf found" << endl;
873 bool returnValue =
true;
874 for (
unsigned int i = 0; i <
myN; ++i )
875 returnValue &=
checkIntegrity ( children[i], ( n || leafAbove ) );
889 template <
typename Domain,
typename Value,
typename HashKey>