Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.util.array;
 
 
 
 import static com.facebook.presto.util.array.BigArrays.INITIAL_SEGMENTS;
 import static com.facebook.presto.util.array.BigArrays.SEGMENT_SIZE;
 import static com.facebook.presto.util.array.BigArrays.offset;
 import static com.facebook.presto.util.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 long SIZE_OF_SEGMENT = sizeOfObjectArray();
 
     private final Object initialValue;
 
     private Object[][] array;
     private int capacity;
     private int segments;

    
Creates a new big array containing one initial segment
 
     public ObjectBigArray()
     {
         this(null);
     }
 
     public ObjectBigArray(Object initialValue)
     {
         this. = initialValue;
          = new Object[][];
         allocateNewSegment();
     }

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

    
Returns the element of this big array at specified index.

Parameters:
index a position in this big array.
Returns:
the element of this big array at the specified position.
 
     public T get(long index)
     {
         return (T) [segment(index)][offset(index)];
     }

    
Sets the element of this big array at specified index.

Parameters:
index a position in this big array.
 
     public void set(long index, T value)
     {
         [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 ( > 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 (. < requiredSegments) {
             = Arrays.copyOf(requiredSegments);
        }
        // add new segments
        while ( < requiredSegments) {
            allocateNewSegment();
        }
    }
    private void allocateNewSegment()
    {
        Object[] newSegment = new Object[];
        if ( != null) {
            Arrays.fill(newSegment);
        }
        [] = newSegment;
         += ;
        ++;
    }
New to GrepCode? Check out our FAQ X