scala - 将节点子节点存储在可变 R 树中 - Scala
问题描述
什么是用于将节点的子节点存储在可变 R 树中的最佳集合。
由于我的树是可变的,我的猜测可能是 ListBuffer、List、ArrayBuffer。但我不确定该选哪一个。我需要一个在删除和插入方面都具有相当复杂性的集合。
解决方案
它可以通过组合来实现ArrayBuffer, PriorityQueue
一个简单的非详尽示例
object RTree {
/**
* Construct an empty RTree.
*/
def empty[A]: RTree[A] = new RTree(Node.empty[A], 0)
/**
* Construct an RTree from a sequence of entries.
*/
def apply[A](entries: Entry[A]*): RTree[A] =
entries.foldLeft(RTree.empty[A])(_ insert _)
}
case class RTree[A](root: Node[A], size: Int) {
/**
* To be considered equal, two trees must have the same
* number of entries, and each entry found in one should be found in
* the other.
*/
def ===(that: RTree[A]): Boolean =
size == that.size && entries.forall(that.contains)
/**
* Trees can only be equal to other trees. Unlike some other
* containers.
*
* This means comparing an RTree[Int] and an RTree[Long] will
* always return false.
*/
override def equals(that: Any): Boolean =
that match {
case rt: RTree[_] =>
Try(this === rt.asInstanceOf[RTree[A]]).getOrElse(false)
case _ =>
false
}
/**
* Insert a value into the tree at (x, y), returning a new tree.
*/
def insert(x: Float, y: Float, value: A): RTree[A] =
insert(Entry(Point(x, y), value))
/**
* Insert an entry into the tree, returning a new tree.
*/
def insert(entry: Entry[A]): RTree[A] = {
val r = root.insert(entry) match {
case Left(rs) => Branch(rs, rs.foldLeft(Box.empty)(_ expand _.box))
case Right(r) => r
}
RTree(r, size + 1)
}
/**
* Remove an entry from the tree, returning a new tree.
*
* If the entry was not present, the tree is simply returned.
*/
def remove(entry: Entry[A]): RTree[A] =
root.remove(entry) match {
case None =>
this
case Some((es, None)) =>
e.foldLeft(RTree.empty[A])(_ insert _)
case Some((e, Some(node))) =>
e.foldLeft(RTree(node, size - e.size - 1))(_ insert _)
}
/**
* Return a sequence of all entries found in the given search space.
*/
def search(space: Box): Seq[Entry[A]] =
root.search(space, _ => true)
/**
* Return a sequence of all entries found in the given search space.
*/
def search(space: Box, f: Entry[A] => Boolean): Seq[Entry[A]] =
root.search(space, f)
推荐阅读
- html - 模态关闭后文本模糊
- html - 使用 CSS 选择器选择 HTML 中的特定元素
- amazon-web-services - 从 VPC Lambda 访问 RDS
- cocoscreator - 视频播放器在 Cocos Creator 中总是排在首位?
- javascript - 您在没有 `onChange` 处理程序的情况下为表单字段提供了 `checked` 属性。这将呈现一个只读字段
- angular - 定义 formBuilder formArray - TSLint 错误
- javascript - fabric.js 中 itext 的 maxlength 属性
- apache-kafka - 如何检查来自 Confluent 控制中心的主题的“旧”消息,
- javascript - 如何使用ngrx状态管理编辑表单数据?错误我得到(尝试区分'[object Object]'时出错。只允许数组和可迭代“))
- php - 如何在 PHP 中读取大型 CSV 文件?