/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.facebook.presto.array;

import io.airlift.slice.SizeOf;
import org.openjdk.jol.info.ClassLayout;

import java.util.Arrays;

import static com.facebook.presto.array.BigArrays.INITIAL_SEGMENTS;
import static com.facebook.presto.array.BigArrays.SEGMENT_SIZE;
import static com.facebook.presto.array.BigArrays.offset;
import static com.facebook.presto.array.BigArrays.segment;
import static io.airlift.slice.SizeOf.sizeOfObjectArray;

// Note: this code was forked from fastutil (http://fastutil.di.unimi.it/)
// Copyright (C) 2010-2013 Sebastiano Vigna
public final class ObjectBigArray<T>
{
    private static final int INSTANCE_SIZE = ClassLayout.parseClass(ObjectBigArray.class).instanceSize();
    private static final long SIZE_OF_SEGMENT = sizeOfObjectArray(SEGMENT_SIZE);

    private final Object initialValue;

    private Object[][] array;
    private long capacity;
    private int segments;

    /**
     * Creates a new big array containing one initial segment
     */
    public ObjectBigArray()
    {
        this(null);
    }

    public ObjectBigArray(Object initialValue)
    {
        this.initialValue = initialValue;
        array = new Object[INITIAL_SEGMENTS][];
        allocateNewSegment();
    }

    /**
     * Returns the size of this big array in bytes.
     */
    public long sizeOf()
    {
        return INSTANCE_SIZE + SizeOf.sizeOf(array) + (segments * SIZE_OF_SEGMENT);
    }

    /**
     * Returns the element of this big array at specified index.
     *
     * @param index a position in this big array.
     * @return the element of this big array at the specified position.
     */
    @SuppressWarnings("unchecked")
    public T get(long index)
    {
        return (T) array[segment(index)][offset(index)];
    }

    /**
     * Sets the element of this big array at specified index.
     *
     * @param index a position in this big array.
     */
    public void set(long index, T value)
    {
        array[segment(index)][offset(index)] = value;
    }

    /**
     * Ensures this big array is at least the specified length.  If the array is smaller, segments
     * are added until the array is larger then the specified length.
     */
    public void ensureCapacity(long length)
    {
        if (capacity > length) {
            return;
        }

        grow(length);
    }

    private void grow(long length)
    {
        // how many segments are required to get to the length?
        int requiredSegments = segment(length) + 1;

        // grow base array if necessary
        if (array.length < requiredSegments) {
            array = Arrays.copyOf(array, requiredSegments);
        }

        // add new segments
        while (segments < requiredSegments) {
            allocateNewSegment();
        }
    }

    private void allocateNewSegment()
    {
        Object[] newSegment = new Object[SEGMENT_SIZE];
        if (initialValue != null) {
            Arrays.fill(newSegment, initialValue);
        }
        array[segments] = newSegment;
        capacity += SEGMENT_SIZE;
        segments++;
    }
}

@@ -1969,32 +1969,170 @@
 EGMENT);%0A    %7D%0A%0A
+    /**%0A     * Returns the current capacity of this big array%0A     */%0A    public long getCapacity()%0A    %7B%0A        return capacity;%0A    %7D%0A%0A
     /**%0A     * R
@@ -2478,226 +2478,1352 @@
   * 
-Sets the element of this big array at specified index.%0A     *%0A     * @param index a position in this big array.%0A     */%0A    public void set(long index, T value)%0A    %7B%0A        array%5Bsegment(index)%5D%5Boffset(index)%5D = valu
+Gets the element of this big array at specified index and then sets the provided%0A     * replacement value to the same index%0A     *%0A     * @param index a position in this big array.%0A     *%0A     * @return the previously stored value for the specified index%0A     */%0A    @SuppressWarnings(%22unchecked%22)%0A    public T getAndSet(long index, T replacement)%0A    %7B%0A        Object%5B%5D segment = array%5Bsegment(index)%5D;%0A        int offset = offset(index);%0A        T result = (T) segment%5Boffset%5D;%0A        segment%5Boffset%5D = replacement;%0A        return result;%0A    %7D%0A%0A    /**%0A     * Sets the element of this big array at specified index.%0A     *%0A     * @param index a position in this big array.%0A     */%0A    public void set(long index, T value)%0A    %7B%0A        array%5Bsegment(index)%5D%5Boffset(index)%5D = value;%0A    %7D%0A%0A    /**%0A     * Sets the element of this big array at specified index if and only if the existing value%0A     * at that index is null%0A     *%0A     * @param index a position in this big array.%0A     *%0A     * @return whether the previous value was null and the new value stored%0A     */%0A    public boolean setIfNull(long index, T value)%0A    %7B%0A        Object%5B%5D segment = array%5Bsegment(index)%5D;%0A        int offset = offset(index);%0A        if (segment%5Boffset%5D == null) %7B%0A            segment%5Boffset%5D = value;%0A            return true;%0A        %7D%0A        return fals
 e;%0A 

Add utility methods to ObjectBigArray