public int getAfterPosition()

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


    public int getAfterPosition( K key )
    {
        if ( key == null )
        {
            return -1;
        }

        switch ( size )
        {
            case 0:
                return -1;

            case 1:
                if ( comparator.compare( array[0], key ) > 0 )
                {
                    return 0;
                }
                else
                {
                    return -1;
                }

            case 2:
                if ( comparator.compare( array[0], key ) > 0 )
                {
                    return 0;
                }

                if ( comparator.compare( array[1], key ) > 0 )
                {
                    return 1;
                }
                else
                {
                    return -1;
                }

            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 )
                    {
                        if ( current != size - 1 )
                        {
                            return current + 1;
                        }
                        else
                        {
                            return -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:
                        if ( comparator.compare( array[start], key ) > 0 )
                        {
                            return start;
                        }
                        else
                        {
                            return -1;
                        }

                    case 2:
                        if ( comparator.compare( array[start], key ) > 0 )
                        {
                            return start;
                        }

                        if ( comparator.compare( array[end], key ) > 0 )
                        {
                            return end;
                        }
                        else
                        {
                            return -1;
                        }

                    default:
                        return -1;
                }
        }
    }