in core-avl/src/main/java/org/apache/directory/server/core/avltree/ArrayTree.java [318:426]
public K findGreater( K key )
{
if ( key == null )
{
return null;
}
switch ( size )
{
case 0:
return null;
case 1:
if ( comparator.compare( array[0], key ) > 0 )
{
return array[0];
}
else
{
return null;
}
case 2:
if ( comparator.compare( array[0], key ) > 0 )
{
return array[0];
}
else if ( comparator.compare( array[1], key ) > 0 )
{
return array[1];
}
else
{
return null;
}
default:
// Split the array in two parts, the left part an the right part
int current = size >> 1;
int start = 0;
int end = size - 1;
while ( end - start + 1 > 2 )
{
int res = comparator.compare( array[current], key );
if ( res == 0 )
{
// Current can't be equal to zero at this point
return array[current + 1];
}
else if ( res < 0 )
{
start = current;
current = ( current + end + 1 ) >> 1;
}
else
{
end = current;
current = ( current + start + 1 ) >> 1;
}
}
switch ( end - start + 1 )
{
case 1:
int res = comparator.compare( array[start], key );
if ( res <= 0 )
{
if ( start == size )
{
return null;
}
else
{
return array[start + 1];
}
}
return array[start];
case 2:
res = comparator.compare( array[start], key );
if ( res <= 0 )
{
res = comparator.compare( array[start + 1], key );
if ( res <= 0 )
{
if ( start == size - 2 )
{
return null;
}
return array[start + 2];
}
return array[start + 1];
}
return array[start];
default:
return null;
}
}
}