繁体   English   中英

预先添加scala向量的复杂性

[英]Prepend complexity of scala vector

在此输入图像描述

我指的是官方文件

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

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

在此输入图像描述

以下是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
    }
  }

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

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

它由一个小端序位映射矢量trie支持,分支因子为32。

这意味着所有操作都需要执行O(log32(n))时间。 如果这对您来说不是很明显,只需遵循更熟悉的二叉树中的逻辑,这些树在所有操作中都具有O(log2(n))复杂度。

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

李浩义有一篇关于这个主题的好文章

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM