Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add comment and fix code style.
  • Loading branch information
viirya committed Apr 10, 2018
commit a77128f910eca1e0ced20257fa94ddaef513eae1
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ abstract class ArrayData extends SpecializedGetters with Serializable {
}
}

/**
* Implements an `IndexedSeq` interface for `ArrayData`. Notice that if the original `ArrayData`
* is a primitive array and contains null elements, it is better to ask for `IndexedSeq[Any]`,
* instead of `IndexedSeq[Int]`, in order to keep the null elements.
*/
class ArrayDataIndexedSeq[T](arrayData: ArrayData, dataType: DataType) extends IndexedSeq[T] {

private def getAccessor(dataType: DataType): (Int) => Any = dataType match {
Copy link
Contributor

@hvanhovell hvanhovell Apr 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion for a small follow-up: We could also use the accessor you create here to improve the foreach construct.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I will also want to reuse the accessor getter in #20981 too.

Expand All @@ -191,16 +196,17 @@ class ArrayDataIndexedSeq[T](arrayData: ArrayData, dataType: DataType) extends I

private val accessor: (Int) => Any = getAccessor(dataType)

override def apply(idx: Int): T = if (0 <= idx && idx < arrayData.numElements()) {
if (arrayData.isNullAt(idx)) {
null.asInstanceOf[T]
override def apply(idx: Int): T =
if (0 <= idx && idx < arrayData.numElements()) {
if (arrayData.isNullAt(idx)) {
null.asInstanceOf[T]
} else {
accessor(idx).asInstanceOf[T]
}
} else {
accessor(idx).asInstanceOf[T]
throw new IndexOutOfBoundsException(
s"Index $idx must be between 0 and the length of the ArrayData.")
}
} else {
throw new IndexOutOfBoundsException(
s"Index $idx must be between 0 and the length of the ArrayData.")
}

override def length: Int = arrayData.numElements()
}