预先添加scala向量的复杂性

时间:2018-04-19 08:16:30

标签: scala

enter image description here

我指的是官方documentation

显示将Vector的复杂性作为“有效常量”(eC)。但我的理解是,对于一个向量,前置意味着所有其他索引也需要调整,这将使操作O(n)或L(线性)。任何人都可以解释一下如何在矢量eC中前置(实际上是常数)。

2 个答案:

答案 0 :(得分:5)

找到以下视觉说明的前置操作,其中每个步骤中都有一个字符。图片仅显示每个块2个插槽以便于解释,但是在矢量的情况下,每个块将有32个插槽。 Vector维护一个起始索引(或图片中的偏移量)以跟踪空位。

enter image description here

以下是Vector.scala的源代码。由于它不会移动所有元素,因此不是O(n)。

override def prepended[B >: A](value: B): Vector[B] = {
    if (endIndex != startIndex) {
      val blockIndex = (startIndex - 1) & ~31
      val lo = (startIndex - 1) & 31

      if (startIndex != blockIndex + 32) {
        val s = new Vector(startIndex - 1, endIndex, blockIndex)
        s.initFrom(this)
        s.dirty = dirty
        s.gotoPosWritable(focus, blockIndex, focus ^ blockIndex)
        s.display0(lo) = value.asInstanceOf[AnyRef]
        s
      } else {

        val freeSpace = (1 << (5 * depth)) - endIndex           // free space at the right given the current tree-structure depth
        val shift = freeSpace & ~((1 << (5 * (depth - 1))) - 1) // number of elements by which we'll shift right (only move at top level)
        val shiftBlocks = freeSpace >>> (5 * (depth - 1))       // number of top-level blocks

        if (shift != 0) {
          // case A: we can shift right on the top level
          if (depth > 1) {
            val newBlockIndex = blockIndex + shift
            val newFocus = focus + shift

            val s = new Vector(startIndex - 1 + shift, endIndex + shift, newBlockIndex)
            s.initFrom(this)
            s.dirty = dirty
            s.shiftTopLevel(0, shiftBlocks) // shift right by n blocks
            s.gotoFreshPosWritable(newFocus, newBlockIndex, newFocus ^ newBlockIndex) // maybe create pos; prepare for writing
            s.display0(lo) = value.asInstanceOf[AnyRef]
            s
          } else {
            val newBlockIndex = blockIndex + 32
            val newFocus = focus

            val s = new Vector(startIndex - 1 + shift, endIndex + shift, newBlockIndex)
            s.initFrom(this)
            s.dirty = dirty
            s.shiftTopLevel(0, shiftBlocks) // shift right by n elements
            s.gotoPosWritable(newFocus, newBlockIndex, newFocus ^ newBlockIndex) // prepare for writing
            s.display0(shift - 1) = value.asInstanceOf[AnyRef]
            s
          }
        } else if (blockIndex < 0) {
          // case B: we need to move the whole structure
          val move = (1 << (5 * (depth + 1))) - (1 << (5 * depth))
          val newBlockIndex = blockIndex + move
          val newFocus = focus + move

          val s = new Vector(startIndex - 1 + move, endIndex + move, newBlockIndex)
          s.initFrom(this)
          s.dirty = dirty
          s.gotoFreshPosWritable(newFocus, newBlockIndex, newFocus ^ newBlockIndex) // could optimize: we know it will create a whole branch
          s.display0(lo) = value.asInstanceOf[AnyRef]
          s
        } else {
          val newBlockIndex = blockIndex
          val newFocus = focus

          val s = new Vector(startIndex - 1, endIndex, newBlockIndex)
          s.initFrom(this)
          s.dirty = dirty
          s.gotoFreshPosWritable(newFocus, newBlockIndex, newFocus ^ newBlockIndex)
          s.display0(lo) = value.asInstanceOf[AnyRef]
          s
        }
      }
    } else {
      // empty vector, just insert single element at the back
      val elems = new Array[AnyRef](32)
      elems(31) = value.asInstanceOf[AnyRef]
      val s = new Vector(31, 32, 0)
      s.depth = 1
      s.display0 = elems
      s
    }
  }

答案 1 :(得分:2)

不需要调整索引,因为Scala的Vector是作为树实现的。更具体地说,一个32路树,意味着每个父母有32个孩子。

http://www.scala-lang.org/api/2.12.0/scala/collection/immutable/Vector.html

  

它由一个带有a的小端点位映射矢量trie支持   分支因子为32。

这意味着所有操作都需要O(log32(n))次执行。如果您不能立即明白这一点,请遵循更熟悉的二叉树中的逻辑,这些树在所有操作中都会O(log2(n))复杂。

然而,对于这种复杂性是应该被视为对数还是有效不变,存在一些争议。从理论上讲,它是一个很好的旧O(log n),但是对数的基数为32,结合其他一些实现细节(例如缓存)这一事实使得它在实践中看起来几乎不变(或者,正如他们所说的那样, “有效地不断”)。

李浩义在这个问题上有一个great article