Save This Page
Home » mahout-collections-1.0-src » org.apache.mahout.math.list » [javadoc | source]
    1   /*
    2   Copyright 1999 CERN - European Organization for Nuclear Research.
    3   Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
    4   is hereby granted without fee, provided that the above copyright notice appear in all copies and 
    5   that both that copyright notice and this permission notice appear in supporting documentation. 
    6   CERN makes no representations about the suitability of this software for any purpose. 
    7   It is provided "as is" without expressed or implied warranty.
    8   */
    9   package org.apache.mahout.math.list;
   10   
   11   /**
   12    Resizable list holding <code>long</code> elements; implemented with arrays; not efficient; just to demonstrate which methods you must override to implement a fully functional list.
   13    */
   14   public class SimpleLongArrayList extends AbstractLongList {
   15   
   16     /**
   17      * The array buffer into which the elements of the list are stored. The capacity of the list is the length of this
   18      * array buffer.
   19      */
   20     private long[] elements;
   21   
   22     /** Constructs an empty list. */
   23     public SimpleLongArrayList() {
   24       this(10);
   25     }
   26   
   27     /**
   28      * Constructs a list containing the specified elements. The initial size and capacity of the list is the length of the
   29      * array.
   30      *
   31      * <b>WARNING:</b> For efficiency reasons and to keep memory usage low, <b>the array is not copied</b>. So if
   32      * subsequently you modify the specified array directly via the [] operator, be sure you know what you're doing.
   33      *
   34      * @param elements the array to be backed by the the constructed list
   35      */
   36     public SimpleLongArrayList(long[] elements) {
   37       elements(elements);
   38     }
   39   
   40     /**
   41      * Constructs an empty list with the specified initial capacity.
   42      *
   43      * @param initialCapacity the number of elements the receiver can hold without auto-expanding itself by allocating new
   44      *                        internal memory.
   45      */
   46     private SimpleLongArrayList(int initialCapacity) {
   47       super();
   48       if (initialCapacity < 0) {
   49         throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity);
   50       }
   51   
   52       this.elements(new long[initialCapacity]);
   53       size = 0;
   54     }
   55   
   56     /**
   57      * Ensures that the receiver can hold at least the specified number of elements without needing to allocate new
   58      * internal memory. If necessary, allocates new internal memory and increases the capacity of the receiver.
   59      *
   60      * @param minCapacity the desired minimum capacity.
   61      */
   62     @Override
   63     public void ensureCapacity(int minCapacity) {
   64       elements = org.apache.mahout.math.Arrays.ensureCapacity(elements, minCapacity);
   65     }
   66   
   67     /**
   68      * Returns the element at the specified position in the receiver; <b>WARNING:</b> Does not check preconditions.
   69      * Provided with invalid parameters this method may return invalid elements without throwing any exception! <b>You
   70      * should only use this method when you are absolutely sure that the index is within bounds.</b> Precondition
   71      * (unchecked): <tt>index &gt;= 0 && index &lt; size()</tt>.
   72      *
   73      * @param index index of element to return.
   74      */
   75     @Override
   76     protected long getQuick(int index) {
   77       return elements[index];
   78     }
   79   
   80     /**
   81      * Replaces the element at the specified position in the receiver with the specified element; <b>WARNING:</b> Does not
   82      * check preconditions. Provided with invalid parameters this method may access invalid indexes without throwing any
   83      * exception! <b>You should only use this method when you are absolutely sure that the index is within bounds.</b>
   84      * Precondition (unchecked): <tt>index &gt;= 0 && index &lt; size()</tt>.
   85      *
   86      * @param index   index of element to replace.
   87      * @param element element to be stored at the specified position.
   88      */
   89     @Override
   90     protected void setQuick(int index, long element) {
   91       elements[index] = element;
   92     }
   93   
   94     /**
   95      * Trims the capacity of the receiver to be the receiver's current size. An application can use this operation to
   96      * minimize the storage of the receiver.
   97      */
   98     @Override
   99     public void trimToSize() {
  100       elements = org.apache.mahout.math.Arrays.trimToCapacity(elements, size());
  101     }
  102   }

Save This Page
Home » mahout-collections-1.0-src » org.apache.mahout.math.list » [javadoc | source]