public K findGreaterOrEqual()

in core-avl/src/main/java/org/apache/directory/server/core/avltree/ArrayTree.java [436:545]


    public K findGreaterOrEqual( 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 )
                    {
                        return array[current];
                    }
                    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 )
                        {
                            return array[start];
                        }
                        else
                        {
                            if ( start == size - 1 )
                            {
                                return null;
                            }
                            else
                            {
                                return array[start + 1];
                            }
                        }

                    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;
                }
        }
    }