diff options
author | Reynold Xin <rxin@apache.org> | 2013-11-17 17:09:40 -0800 |
---|---|---|
committer | Reynold Xin <rxin@apache.org> | 2013-11-17 17:09:40 -0800 |
commit | c30979c7d6009936853e731bfde38ec9d04ea347 (patch) | |
tree | 3605ed0bb1182b0d37295ddf02c8fa55771398ea | |
parent | 1b5b358309a5adfc12b75b0ebb4254ad8e69f5a0 (diff) | |
download | spark-c30979c7d6009936853e731bfde38ec9d04ea347.tar.gz spark-c30979c7d6009936853e731bfde38ec9d04ea347.tar.bz2 spark-c30979c7d6009936853e731bfde38ec9d04ea347.zip |
Slightly enhanced PrimitiveVector:
1. Added trim() method
2. Added size method.
3. Renamed getUnderlyingArray to array.
4. Minor documentation update.
-rw-r--r-- | core/src/main/scala/org/apache/spark/util/collection/PrimitiveVector.scala | 40 |
1 files changed, 26 insertions, 14 deletions
diff --git a/core/src/main/scala/org/apache/spark/util/collection/PrimitiveVector.scala b/core/src/main/scala/org/apache/spark/util/collection/PrimitiveVector.scala index 369519c559..54a5569b3d 100644 --- a/core/src/main/scala/org/apache/spark/util/collection/PrimitiveVector.scala +++ b/core/src/main/scala/org/apache/spark/util/collection/PrimitiveVector.scala @@ -17,35 +17,47 @@ 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] class PrimitiveVector[@specialized(Long, Int, Double) V: ClassManifest](initialSize: Int = 64) { - private var numElements = 0 - private var array: Array[V] = _ + private var _numElements = 0 + private var _array: Array[V] = _ // 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... - array = new Array[V](initialSize) + // will get its own array with the same initial size. + _array = new Array[V](initialSize) def apply(index: Int): V = { - require(index < numElements) - array(index) + require(index < _numElements) + _array(index) } def +=(value: V) { - if (numElements == array.length) { resize(array.length * 2) } - array(numElements) = value - numElements += 1 + if (_numElements == _array.length) { + resize(_array.length * 2) + } + _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. */ def resize(newLength: Int) { val newArray = new Array[V](newLength) - array.copyToArray(newArray) - array = newArray + _array.copyToArray(newArray) + _array = newArray } } |