Skip to content
Snippets Groups Projects
Commit c30979c7 authored by Reynold Xin's avatar Reynold Xin
Browse files

Slightly enhanced PrimitiveVector:

1. Added trim() method
2. Added size method.
3. Renamed getUnderlyingArray to array.
4. Minor documentation update.
parent 1b5b3583
No related branches found
No related tags found
No related merge requests found
...@@ -17,35 +17,47 @@ ...@@ -17,35 +17,47 @@
package org.apache.spark.util.collection package org.apache.spark.util.collection
/** Provides a simple, non-threadsafe, array-backed vector that can store primitives. */ /**
* An append-only, non-threadsafe, array-backed vector that is optimized for primitive types.
*/
private[spark] private[spark]
class PrimitiveVector[@specialized(Long, Int, Double) V: ClassManifest](initialSize: Int = 64) { class PrimitiveVector[@specialized(Long, Int, Double) V: ClassManifest](initialSize: Int = 64) {
private var numElements = 0 private var _numElements = 0
private var array: Array[V] = _ private var _array: Array[V] = _
// NB: This must be separate from the declaration, otherwise the specialized parent class // NB: This must be separate from the declaration, otherwise the specialized parent class
// will get its own array with the same initial size. TODO: Figure out why... // will get its own array with the same initial size.
array = new Array[V](initialSize) _array = new Array[V](initialSize)
def apply(index: Int): V = { def apply(index: Int): V = {
require(index < numElements) require(index < _numElements)
array(index) _array(index)
} }
def +=(value: V) { def +=(value: V) {
if (numElements == array.length) { resize(array.length * 2) } if (_numElements == _array.length) {
array(numElements) = value resize(_array.length * 2)
numElements += 1 }
_array(_numElements) = value
_numElements += 1
} }
def length = numElements def capacity: Int = _array.length
def length: Int = _numElements
def size: Int = _numElements
/** Get the underlying array backing this vector. */
def array: Array[V] = _array
def getUnderlyingArray = array /** Trims this vector so that the capacity is equal to the size. */
def trim(): Unit = resize(size)
/** Resizes the array, dropping elements if the total length decreases. */ /** Resizes the array, dropping elements if the total length decreases. */
def resize(newLength: Int) { def resize(newLength: Int) {
val newArray = new Array[V](newLength) val newArray = new Array[V](newLength)
array.copyToArray(newArray) _array.copyToArray(newArray)
array = newArray _array = newArray
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment