Github user concretevitamin commented on a diff in the pull request:

    https://github.com/apache/spark/pull/1297#discussion_r14578164
  
    --- Diff: 
core/src/main/scala/org/apache/spark/util/collection/ImmutableVector.scala ---
    @@ -0,0 +1,219 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You 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 org.apache.spark.util.collection
    +
    +import scala.reflect.ClassTag
    +
    +/**
    + * An immutable vector that supports efficient point updates. Similarly to
    + * scala.collection.immutable.Vector, it is implemented using a 32-ary 
tree with 32-element arrays
    + * at the leaves. Unlike Scala's Vector, it is specialized on the value 
type, making it much more
    + * memory-efficient for primitive values.
    + */
    +private[spark] class ImmutableVector[@specialized(Long, Int) A](val size: 
Int, root: VectorNode[A])
    +  extends Serializable {
    +
    +  def iterator: Iterator[A] = new VectorIterator[A](root)
    +  def apply(index: Int): A = root(index)
    +  def updated(index: Int, elem: A): ImmutableVector[A] =
    +    new ImmutableVector(size, root.updated(index, elem))
    +}
    +
    +private[spark] object ImmutableVector {
    +  def empty[A: ClassTag]: ImmutableVector[A] = new ImmutableVector(0, 
emptyNode)
    +
    +  def fromArray[A: ClassTag](array: Array[A]): ImmutableVector[A] = {
    +    fromArray(array, 0, array.length)
    +  }
    +
    +  def fromArray[A: ClassTag](array: Array[A], start: Int, end: Int): 
ImmutableVector[A] = {
    +    new ImmutableVector(end - start, nodeFromArray(array, start, end))
    +  }
    +
    +  def fill[A: ClassTag](n: Int)(a: A): ImmutableVector[A] = {
    +    // TODO: Implement this without allocating an extra array
    +    fromArray(Array.fill(n)(a), 0, n)
    +  }
    +
    +  /** Returns the root of a 32-ary tree representing the specified 
interval into the array. */
    +  private def nodeFromArray[A: ClassTag](array: Array[A], start: Int, end: 
Int): VectorNode[A] = {
    +    val length = end - start
    +    if (length == 0) {
    +      emptyNode
    +    } else {
    +      val depth = depthOf(length)
    +      if (depth == 0) {
    +        new LeafNode(array.slice(start, end))
    +      } else {
    +        val shift = 5 * depth
    +        val numChildren = ((length - 1) >> shift) + 1
    +        val children = new Array[VectorNode[A]](numChildren)
    +        var i = 0
    +        while (i < numChildren) {
    +          val childStart = start + (i << shift)
    +          var childEnd = start + ((i + 1) << shift)
    +          if (end < childEnd) {
    +            childEnd = end
    +          }
    +          children(i) = nodeFromArray(array, childStart, childEnd)
    +          i += 1
    +        }
    +        new InternalNode(children, depth)
    +      }
    +    }
    +  }
    +
    +  private def emptyNode[A: ClassTag] = new LeafNode(Array.empty)
    +
    +  /** Returns the required tree depth for an ImmutableVector of the given 
size. */
    +  private def depthOf(size: Int): Int = {
    +    var depth = 0
    +    var sizeLeft = (size - 1) >> 5
    +    while (sizeLeft > 0) {
    +      sizeLeft >>= 5
    +      depth += 1
    +    }
    +    depth
    +  }
    +}
    +
    +/** Trait representing nodes in the vector tree. */
    +private sealed trait VectorNode[@specialized(Long, Int) A] extends 
Serializable {
    +  def apply(index: Int): A
    +  def updated(index: Int, elem: A): VectorNode[A]
    +  def numChildren: Int
    +}
    +
    +/** An internal node in the vector tree (one containing other nodes rather 
than vector elements). */
    +private class InternalNode[@specialized(Long, Int) A: ClassTag](
    +    children: Array[VectorNode[A]],
    +    val depth: Int)
    +  extends VectorNode[A] {
    +
    +  require(children.length > 0, "InternalNode must have children")
    +  require(children.length <= 32, "nodes cannot have more than 32 children 
(got ${children.length})")
    +  require(depth >= 1, "InternalNode must have depth >= 1 (got $depth)")
    --- End diff --
    
    minor nit: add "s", same for the line above


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to