私がやろうとしているのは、TreeMapのアイテムのキーの順序を調整できるようにすることです。
- 変化しない「静的」データでオブジェクトを見つけることができるかもしれません
- オブジェクトの位置(マップが平坦化されている場合)は、オブジェクトの「優先度」を尊重する必要があります
次のテストケースは、numEntries = 6の場合はうまく機能しますが、7より大きい値の場合は機能しません。そこで何が問題になっているのかはよくわかりませんが、更新やコピーを行った後、ツリーが乱れるのではないかと思います。誰かがアドバイスしてくれませんか?それは私のせいですか、それともScala 2.9のTreeMapに何らかのバグがありますか?
ループの場合は更新
for (i <- 1 to (numEntries - 1)) {
交換された
for (i <- (numEntries - 1) to 1 by -1) {
すべてが正しく機能します。これがTreeMapのバグのように見えますか?
import org.scalatest.FlatSpec
import collection.immutable.TreeMap
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
@RunWith(classOf[JUnitRunner])
class TreeMapTest extends FlatSpec {
//val numEntries = 6;
val numEntries = 10;
sealed case class Entry(first: Option[Int], last: Int) extends Ordered[Entry] {
def compare(that: TreeMapTest.this.type#Entry) = {
if (first.isEmpty || that.first.isEmpty) {
last compare that.last
} else {
(first.get compare that.first.get) match {
case 0 => last compare that.last
case x => x
}
}
}
def increase() = copy(first = Some(this.first.getOrElse(0) + 1))
}
type Container = TreeMap[Entry, Entry]
"TreeMap" should "allow updates" in {
var dataMap: Container = new Container() ++ (for (i <- 1 to numEntries) yield Entry(Some(0), i) -> Entry(Some(0), i))
for (i <- 1 to (numEntries - 1)) {
val key = new Entry(None, i)
dataMap.get(new Entry(None, i)) match {
case Some(e) =>
val newEntry = e.increase()
dataMap = (dataMap - key) + (newEntry -> newEntry)
case None => fail("Can not find entry " + key)
}
}
}
}