| 1 |
// Copyright 2014-2022 Google Inc. |
| 2 |
// Copyright 2025 The Kubernetes Authors. |
| 3 |
// |
| 4 |
// This file is derived from github.com/google/btree and has been |
| 5 |
// modified for use in the Kubernetes util package. |
| 6 |
// |
| 7 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 |
// you may not use this file except in compliance with the License. |
| 9 |
// You may obtain a copy of the License at |
| 10 |
// |
| 11 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
// |
| 13 |
// Unless required by applicable law or agreed to in writing, software |
| 14 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 15 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 |
// See the License for the specific language governing permissions and |
| 17 |
// limitations under the License. |
| 18 |
|
| 19 |
package btree |
| 20 |
|
| 21 |
import ( |
| 22 |
"flag" |
| 23 |
"fmt" |
| 24 |
"math/rand" |
| 25 |
"reflect" |
| 26 |
"sort" |
| 27 |
"sync" |
| 28 |
"testing" |
| 29 |
) |
| 30 |
|
| 31 |
func intRange(s int, reverse bool) []int { |
| 32 |
out := make([]int, s) |
| 33 |
for i := range s { |
| 34 |
v := i |
| 35 |
if reverse { |
| 36 |
v = s - i - 1 |
| 37 |
} |
| 38 |
out[i] = v |
| 39 |
} |
| 40 |
return out |
| 41 |
} |
| 42 |
|
| 43 |
func intAll(t *BTree[int]) (out []int) { |
| 44 |
t.Ascend(func(a int) bool { |
| 45 |
out = append(out, a) |
| 46 |
return true |
| 47 |
}) |
| 48 |
return |
| 49 |
} |
| 50 |
|
| 51 |
func intAllRev(t *BTree[int]) (out []int) { |
| 52 |
t.Descend(func(a int) bool { |
| 53 |
out = append(out, a) |
| 54 |
return true |
| 55 |
}) |
| 56 |
return |
| 57 |
} |
| 58 |
|
| 59 |
var btreeDegree = flag.Int("degree", 32, "B-Tree degree") |
| 60 |
|
| 61 |
func TestBTree(t *testing.T) { |
| 62 |
tr := NewOrdered[int](*btreeDegree) |
| 63 |
const treeSize = 10000 |
| 64 |
for range 10 { |
| 65 |
if min, ok := tr.Min(); ok || min != 0 { |
| 66 |
t.Fatalf("empty min, got %+v", min) |
| 67 |
} |
| 68 |
if max, ok := tr.Max(); ok || max != 0 { |
| 69 |
t.Fatalf("empty max, got %+v", max) |
| 70 |
} |
| 71 |
for _, item := range rand.Perm(treeSize) { |
| 72 |
if x, ok := tr.ReplaceOrInsert(item); ok || x != 0 { |
| 73 |
t.Fatal("insert found item", item) |
| 74 |
} |
| 75 |
} |
| 76 |
for _, item := range rand.Perm(treeSize) { |
| 77 |
if x, ok := tr.ReplaceOrInsert(item); !ok || x != item { |
| 78 |
t.Fatal("insert didn't find item", item) |
| 79 |
} |
| 80 |
} |
| 81 |
want := 0 |
| 82 |
if min, ok := tr.Min(); !ok || min != want { |
| 83 |
t.Fatalf("min: ok %v want %+v, got %+v", ok, want, min) |
| 84 |
} |
| 85 |
want = treeSize - 1 |
| 86 |
if max, ok := tr.Max(); !ok || max != want { |
| 87 |
t.Fatalf("max: ok %v want %+v, got %+v", ok, want, max) |
| 88 |
} |
| 89 |
got := intAll(tr) |
| 90 |
wantRange := intRange(treeSize, false) |
| 91 |
if !reflect.DeepEqual(got, wantRange) { |
| 92 |
t.Fatalf("mismatch:\n got: %v\nwant: %v", got, wantRange) |
| 93 |
} |
| 94 |
|
| 95 |
gotrev := intAllRev(tr) |
| 96 |
wantrev := intRange(treeSize, true) |
| 97 |
if !reflect.DeepEqual(gotrev, wantrev) { |
| 98 |
t.Fatalf("mismatch:\n got: %v\nwant: %v", gotrev, wantrev) |
| 99 |
} |
| 100 |
|
| 101 |
for _, item := range rand.Perm(treeSize) { |
| 102 |
if x, ok := tr.Delete(item); !ok || x != item { |
| 103 |
t.Fatalf("didn't find %v", item) |
| 104 |
} |
| 105 |
} |
| 106 |
if got = intAll(tr); len(got) > 0 { |
| 107 |
t.Fatalf("some left!: %v", got) |
| 108 |
} |
| 109 |
if got = intAllRev(tr); len(got) > 0 { |
| 110 |
t.Fatalf("some left!: %v", got) |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
func ExampleBTree() { |
| 116 |
tr := NewOrdered[int](*btreeDegree) |
| 117 |
for i := range 10 { |
| 118 |
tr.ReplaceOrInsert(i) |
| 119 |
} |
| 120 |
fmt.Println("len: ", tr.Len()) |
| 121 |
v, ok := tr.Get(3) |
| 122 |
fmt.Println("get3: ", v, ok) |
| 123 |
v, ok = tr.Get(100) |
| 124 |
fmt.Println("get100: ", v, ok) |
| 125 |
v, ok = tr.Delete(4) |
| 126 |
fmt.Println("del4: ", v, ok) |
| 127 |
v, ok = tr.Delete(100) |
| 128 |
fmt.Println("del100: ", v, ok) |
| 129 |
v, ok = tr.ReplaceOrInsert(5) |
| 130 |
fmt.Println("replace5: ", v, ok) |
| 131 |
v, ok = tr.ReplaceOrInsert(100) |
| 132 |
fmt.Println("replace100:", v, ok) |
| 133 |
v, ok = tr.Min() |
| 134 |
fmt.Println("min: ", v, ok) |
| 135 |
v, ok = tr.DeleteMin() |
| 136 |
fmt.Println("delmin: ", v, ok) |
| 137 |
v, ok = tr.Max() |
| 138 |
fmt.Println("max: ", v, ok) |
| 139 |
v, ok = tr.DeleteMax() |
| 140 |
fmt.Println("delmax: ", v, ok) |
| 141 |
fmt.Println("len: ", tr.Len()) |
| 142 |
// Output: |
| 143 |
// len: 10 |
| 144 |
// get3: 3 true |
| 145 |
// get100: 0 false |
| 146 |
// del4: 4 true |
| 147 |
// del100: 0 false |
| 148 |
// replace5: 5 true |
| 149 |
// replace100: 0 false |
| 150 |
// min: 0 true |
| 151 |
// delmin: 0 true |
| 152 |
// max: 100 true |
| 153 |
// delmax: 100 true |
| 154 |
// len: 8 |
| 155 |
} |
| 156 |
|
| 157 |
func TestDeleteMin(t *testing.T) { |
| 158 |
tr := NewOrdered[int](3) |
| 159 |
for _, v := range rand.Perm(100) { |
| 160 |
tr.ReplaceOrInsert(v) |
| 161 |
} |
| 162 |
var got []int |
| 163 |
for v, ok := tr.DeleteMin(); ok; v, ok = tr.DeleteMin() { |
| 164 |
got = append(got, v) |
| 165 |
} |
| 166 |
if want := intRange(100, false); !reflect.DeepEqual(got, want) { |
| 167 |
t.Fatalf("ascendrange:\n got: %v\nwant: %v", got, want) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
func TestDeleteMax(t *testing.T) { |
| 172 |
tr := NewOrdered[int](3) |
| 173 |
for _, v := range rand.Perm(100) { |
| 174 |
tr.ReplaceOrInsert(v) |
| 175 |
} |
| 176 |
var got []int |
| 177 |
for v, ok := tr.DeleteMax(); ok; v, ok = tr.DeleteMax() { |
| 178 |
got = append(got, v) |
| 179 |
} |
| 180 |
if want := intRange(100, true); !reflect.DeepEqual(got, want) { |
| 181 |
t.Fatalf("ascendrange:\n got: %v\nwant: %v", got, want) |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
func TestAscendRange(t *testing.T) { |
| 186 |
tr := NewOrdered[int](2) |
| 187 |
for _, v := range rand.Perm(100) { |
| 188 |
tr.ReplaceOrInsert(v) |
| 189 |
} |
| 190 |
var got []int |
| 191 |
tr.AscendRange(40, 60, func(a int) bool { |
| 192 |
got = append(got, a) |
| 193 |
return true |
| 194 |
}) |
| 195 |
if want := intRange(100, false)[40:60]; !reflect.DeepEqual(got, want) { |
| 196 |
t.Fatalf("ascendrange:\n got: %v\nwant: %v", got, want) |
| 197 |
} |
| 198 |
got = got[:0] |
| 199 |
tr.AscendRange(40, 60, func(a int) bool { |
| 200 |
if a > 50 { |
| 201 |
return false |
| 202 |
} |
| 203 |
got = append(got, a) |
| 204 |
return true |
| 205 |
}) |
| 206 |
if want := intRange(100, false)[40:51]; !reflect.DeepEqual(got, want) { |
| 207 |
t.Fatalf("ascendrange:\n got: %v\nwant: %v", got, want) |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
func TestDescendRange(t *testing.T) { |
| 212 |
tr := NewOrdered[int](2) |
| 213 |
for _, v := range rand.Perm(100) { |
| 214 |
tr.ReplaceOrInsert(v) |
| 215 |
} |
| 216 |
var got []int |
| 217 |
tr.DescendRange(60, 40, func(a int) bool { |
| 218 |
got = append(got, a) |
| 219 |
return true |
| 220 |
}) |
| 221 |
if want := intRange(100, true)[39:59]; !reflect.DeepEqual(got, want) { |
| 222 |
t.Fatalf("descendrange:\n got: %v\nwant: %v", got, want) |
| 223 |
} |
| 224 |
got = got[:0] |
| 225 |
tr.DescendRange(60, 40, func(a int) bool { |
| 226 |
if a < 50 { |
| 227 |
return false |
| 228 |
} |
| 229 |
got = append(got, a) |
| 230 |
return true |
| 231 |
}) |
| 232 |
if want := intRange(100, true)[39:50]; !reflect.DeepEqual(got, want) { |
| 233 |
t.Fatalf("descendrange:\n got: %v\nwant: %v", got, want) |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
func TestAscendLessThan(t *testing.T) { |
| 238 |
tr := NewOrdered[int](*btreeDegree) |
| 239 |
for _, v := range rand.Perm(100) { |
| 240 |
tr.ReplaceOrInsert(v) |
| 241 |
} |
| 242 |
var got []int |
| 243 |
tr.AscendLessThan(60, func(a int) bool { |
| 244 |
got = append(got, a) |
| 245 |
return true |
| 246 |
}) |
| 247 |
if want := intRange(100, false)[:60]; !reflect.DeepEqual(got, want) { |
| 248 |
t.Fatalf("ascendrange:\n got: %v\nwant: %v", got, want) |
| 249 |
} |
| 250 |
got = got[:0] |
| 251 |
tr.AscendLessThan(60, func(a int) bool { |
| 252 |
if a > 50 { |
| 253 |
return false |
| 254 |
} |
| 255 |
got = append(got, a) |
| 256 |
return true |
| 257 |
}) |
| 258 |
if want := intRange(100, false)[:51]; !reflect.DeepEqual(got, want) { |
| 259 |
t.Fatalf("ascendrange:\n got: %v\nwant: %v", got, want) |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
func TestDescendLessOrEqual(t *testing.T) { |
| 264 |
tr := NewOrdered[int](*btreeDegree) |
| 265 |
for _, v := range rand.Perm(100) { |
| 266 |
tr.ReplaceOrInsert(v) |
| 267 |
} |
| 268 |
var got []int |
| 269 |
tr.DescendLessOrEqual(40, func(a int) bool { |
| 270 |
got = append(got, a) |
| 271 |
return true |
| 272 |
}) |
| 273 |
if want := intRange(100, true)[59:]; !reflect.DeepEqual(got, want) { |
| 274 |
t.Fatalf("descendlessorequal:\n got: %v\nwant: %v", got, want) |
| 275 |
} |
| 276 |
got = got[:0] |
| 277 |
tr.DescendLessOrEqual(60, func(a int) bool { |
| 278 |
if a < 50 { |
| 279 |
return false |
| 280 |
} |
| 281 |
got = append(got, a) |
| 282 |
return true |
| 283 |
}) |
| 284 |
if want := intRange(100, true)[39:50]; !reflect.DeepEqual(got, want) { |
| 285 |
t.Fatalf("descendlessorequal:\n got: %v\nwant: %v", got, want) |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
func TestAscendGreaterOrEqual(t *testing.T) { |
| 290 |
tr := NewOrdered[int](*btreeDegree) |
| 291 |
for _, v := range rand.Perm(100) { |
| 292 |
tr.ReplaceOrInsert(v) |
| 293 |
} |
| 294 |
var got []int |
| 295 |
tr.AscendGreaterOrEqual(40, func(a int) bool { |
| 296 |
got = append(got, a) |
| 297 |
return true |
| 298 |
}) |
| 299 |
if want := intRange(100, false)[40:]; !reflect.DeepEqual(got, want) { |
| 300 |
t.Fatalf("ascendrange:\n got: %v\nwant: %v", got, want) |
| 301 |
} |
| 302 |
got = got[:0] |
| 303 |
tr.AscendGreaterOrEqual(40, func(a int) bool { |
| 304 |
if a > 50 { |
| 305 |
return false |
| 306 |
} |
| 307 |
got = append(got, a) |
| 308 |
return true |
| 309 |
}) |
| 310 |
if want := intRange(100, false)[40:51]; !reflect.DeepEqual(got, want) { |
| 311 |
t.Fatalf("ascendrange:\n got: %v\nwant: %v", got, want) |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
func TestDescendGreaterThan(t *testing.T) { |
| 316 |
tr := NewOrdered[int](*btreeDegree) |
| 317 |
for _, v := range rand.Perm(100) { |
| 318 |
tr.ReplaceOrInsert(v) |
| 319 |
} |
| 320 |
var got []int |
| 321 |
tr.DescendGreaterThan(40, func(a int) bool { |
| 322 |
got = append(got, a) |
| 323 |
return true |
| 324 |
}) |
| 325 |
if want := intRange(100, true)[:59]; !reflect.DeepEqual(got, want) { |
| 326 |
t.Fatalf("descendgreaterthan:\n got: %v\nwant: %v", got, want) |
| 327 |
} |
| 328 |
got = got[:0] |
| 329 |
tr.DescendGreaterThan(40, func(a int) bool { |
| 330 |
if a < 50 { |
| 331 |
return false |
| 332 |
} |
| 333 |
got = append(got, a) |
| 334 |
return true |
| 335 |
}) |
| 336 |
if want := intRange(100, true)[:50]; !reflect.DeepEqual(got, want) { |
| 337 |
t.Fatalf("descendgreaterthan:\n got: %v\nwant: %v", got, want) |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
const benchmarkTreeSize = 10000 |
| 342 |
|
| 343 |
func BenchmarkInsert(b *testing.B) { |
| 344 |
b.StopTimer() |
| 345 |
insertP := rand.Perm(benchmarkTreeSize) |
| 346 |
b.StartTimer() |
| 347 |
i := 0 |
| 348 |
for i < b.N { |
| 349 |
tr := NewOrdered[int](*btreeDegree) |
| 350 |
for _, item := range insertP { |
| 351 |
tr.ReplaceOrInsert(item) |
| 352 |
i++ |
| 353 |
if i >= b.N { |
| 354 |
return |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
func BenchmarkSeek(b *testing.B) { |
| 361 |
b.StopTimer() |
| 362 |
size := 100000 |
| 363 |
insertP := rand.Perm(size) |
| 364 |
tr := NewOrdered[int](*btreeDegree) |
| 365 |
for _, item := range insertP { |
| 366 |
tr.ReplaceOrInsert(item) |
| 367 |
} |
| 368 |
b.StartTimer() |
| 369 |
|
| 370 |
for i := 0; i < b.N; i++ { |
| 371 |
tr.AscendGreaterOrEqual(i%size, func(_ int) bool { return false }) |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
func BenchmarkDeleteInsert(b *testing.B) { |
| 376 |
b.StopTimer() |
| 377 |
insertP := rand.Perm(benchmarkTreeSize) |
| 378 |
tr := NewOrdered[int](*btreeDegree) |
| 379 |
for _, item := range insertP { |
| 380 |
tr.ReplaceOrInsert(item) |
| 381 |
} |
| 382 |
b.StartTimer() |
| 383 |
for i := 0; i < b.N; i++ { |
| 384 |
tr.Delete(insertP[i%benchmarkTreeSize]) |
| 385 |
tr.ReplaceOrInsert(insertP[i%benchmarkTreeSize]) |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
func BenchmarkDeleteInsertCloneOnce(b *testing.B) { |
| 390 |
b.StopTimer() |
| 391 |
insertP := rand.Perm(benchmarkTreeSize) |
| 392 |
tr := NewOrdered[int](*btreeDegree) |
| 393 |
for _, item := range insertP { |
| 394 |
tr.ReplaceOrInsert(item) |
| 395 |
} |
| 396 |
tr = tr.Clone() |
| 397 |
b.StartTimer() |
| 398 |
for i := 0; i < b.N; i++ { |
| 399 |
tr.Delete(insertP[i%benchmarkTreeSize]) |
| 400 |
tr.ReplaceOrInsert(insertP[i%benchmarkTreeSize]) |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
func BenchmarkDeleteInsertCloneEachTime(b *testing.B) { |
| 405 |
b.StopTimer() |
| 406 |
insertP := rand.Perm(benchmarkTreeSize) |
| 407 |
tr := NewOrdered[int](*btreeDegree) |
| 408 |
for _, item := range insertP { |
| 409 |
tr.ReplaceOrInsert(item) |
| 410 |
} |
| 411 |
b.StartTimer() |
| 412 |
for i := 0; i < b.N; i++ { |
| 413 |
tr = tr.Clone() |
| 414 |
tr.Delete(insertP[i%benchmarkTreeSize]) |
| 415 |
tr.ReplaceOrInsert(insertP[i%benchmarkTreeSize]) |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
func BenchmarkDelete(b *testing.B) { |
| 420 |
b.StopTimer() |
| 421 |
insertP := rand.Perm(benchmarkTreeSize) |
| 422 |
removeP := rand.Perm(benchmarkTreeSize) |
| 423 |
b.StartTimer() |
| 424 |
i := 0 |
| 425 |
for i < b.N { |
| 426 |
b.StopTimer() |
| 427 |
tr := NewOrdered[int](*btreeDegree) |
| 428 |
for _, v := range insertP { |
| 429 |
tr.ReplaceOrInsert(v) |
| 430 |
} |
| 431 |
b.StartTimer() |
| 432 |
for _, item := range removeP { |
| 433 |
tr.Delete(item) |
| 434 |
i++ |
| 435 |
if i >= b.N { |
| 436 |
return |
| 437 |
} |
| 438 |
} |
| 439 |
if tr.Len() > 0 { |
| 440 |
panic(tr.Len()) |
| 441 |
} |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
func BenchmarkGet(b *testing.B) { |
| 446 |
b.StopTimer() |
| 447 |
insertP := rand.Perm(benchmarkTreeSize) |
| 448 |
removeP := rand.Perm(benchmarkTreeSize) |
| 449 |
b.StartTimer() |
| 450 |
i := 0 |
| 451 |
for i < b.N { |
| 452 |
b.StopTimer() |
| 453 |
tr := NewOrdered[int](*btreeDegree) |
| 454 |
for _, v := range insertP { |
| 455 |
tr.ReplaceOrInsert(v) |
| 456 |
} |
| 457 |
b.StartTimer() |
| 458 |
for _, item := range removeP { |
| 459 |
tr.Get(item) |
| 460 |
i++ |
| 461 |
if i >= b.N { |
| 462 |
return |
| 463 |
} |
| 464 |
} |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
func BenchmarkGetCloneEachTime(b *testing.B) { |
| 469 |
b.StopTimer() |
| 470 |
insertP := rand.Perm(benchmarkTreeSize) |
| 471 |
removeP := rand.Perm(benchmarkTreeSize) |
| 472 |
b.StartTimer() |
| 473 |
i := 0 |
| 474 |
for i < b.N { |
| 475 |
b.StopTimer() |
| 476 |
tr := NewOrdered[int](*btreeDegree) |
| 477 |
for _, v := range insertP { |
| 478 |
tr.ReplaceOrInsert(v) |
| 479 |
} |
| 480 |
b.StartTimer() |
| 481 |
for _, item := range removeP { |
| 482 |
tr = tr.Clone() |
| 483 |
tr.Get(item) |
| 484 |
i++ |
| 485 |
if i >= b.N { |
| 486 |
return |
| 487 |
} |
| 488 |
} |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
func BenchmarkAscend(b *testing.B) { |
| 493 |
arr := rand.Perm(benchmarkTreeSize) |
| 494 |
tr := NewOrdered[int](*btreeDegree) |
| 495 |
for _, v := range arr { |
| 496 |
tr.ReplaceOrInsert(v) |
| 497 |
} |
| 498 |
sort.Ints(arr) |
| 499 |
b.ResetTimer() |
| 500 |
for i := 0; i < b.N; i++ { |
| 501 |
j := 0 |
| 502 |
tr.Ascend(func(item int) bool { |
| 503 |
if item != arr[j] { |
| 504 |
b.Fatalf("mismatch: expected: %v, got %v", arr[j], item) |
| 505 |
} |
| 506 |
j++ |
| 507 |
return true |
| 508 |
}) |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
func BenchmarkDescend(b *testing.B) { |
| 513 |
arr := rand.Perm(benchmarkTreeSize) |
| 514 |
tr := NewOrdered[int](*btreeDegree) |
| 515 |
for _, v := range arr { |
| 516 |
tr.ReplaceOrInsert(v) |
| 517 |
} |
| 518 |
sort.Ints(arr) |
| 519 |
b.ResetTimer() |
| 520 |
for i := 0; i < b.N; i++ { |
| 521 |
j := len(arr) - 1 |
| 522 |
tr.Descend(func(item int) bool { |
| 523 |
if item != arr[j] { |
| 524 |
b.Fatalf("mismatch: expected: %v, got %v", arr[j], item) |
| 525 |
} |
| 526 |
j-- |
| 527 |
return true |
| 528 |
}) |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
func BenchmarkAscendRange(b *testing.B) { |
| 533 |
arr := rand.Perm(benchmarkTreeSize) |
| 534 |
tr := NewOrdered[int](*btreeDegree) |
| 535 |
for _, v := range arr { |
| 536 |
tr.ReplaceOrInsert(v) |
| 537 |
} |
| 538 |
sort.Ints(arr) |
| 539 |
b.ResetTimer() |
| 540 |
for i := 0; i < b.N; i++ { |
| 541 |
j := 100 |
| 542 |
tr.AscendRange(100, arr[len(arr)-100], func(item int) bool { |
| 543 |
if item != arr[j] { |
| 544 |
b.Fatalf("mismatch: expected: %v, got %v", arr[j], item) |
| 545 |
} |
| 546 |
j++ |
| 547 |
return true |
| 548 |
}) |
| 549 |
if j != len(arr)-100 { |
| 550 |
b.Fatalf("expected: %v, got %v", len(arr)-100, j) |
| 551 |
} |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
func BenchmarkDescendRange(b *testing.B) { |
| 556 |
arr := rand.Perm(benchmarkTreeSize) |
| 557 |
tr := NewOrdered[int](*btreeDegree) |
| 558 |
for _, v := range arr { |
| 559 |
tr.ReplaceOrInsert(v) |
| 560 |
} |
| 561 |
sort.Ints(arr) |
| 562 |
b.ResetTimer() |
| 563 |
for i := 0; i < b.N; i++ { |
| 564 |
j := len(arr) - 100 |
| 565 |
tr.DescendRange(arr[len(arr)-100], 100, func(item int) bool { |
| 566 |
if item != arr[j] { |
| 567 |
b.Fatalf("mismatch: expected: %v, got %v", arr[j], item) |
| 568 |
} |
| 569 |
j-- |
| 570 |
return true |
| 571 |
}) |
| 572 |
if j != 100 { |
| 573 |
b.Fatalf("expected: %v, got %v", len(arr)-100, j) |
| 574 |
} |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
func BenchmarkAscendGreaterOrEqual(b *testing.B) { |
| 579 |
arr := rand.Perm(benchmarkTreeSize) |
| 580 |
tr := NewOrdered[int](*btreeDegree) |
| 581 |
for _, v := range arr { |
| 582 |
tr.ReplaceOrInsert(v) |
| 583 |
} |
| 584 |
sort.Ints(arr) |
| 585 |
b.ResetTimer() |
| 586 |
for i := 0; i < b.N; i++ { |
| 587 |
j := 100 |
| 588 |
k := 0 |
| 589 |
tr.AscendGreaterOrEqual(100, func(item int) bool { |
| 590 |
if item != arr[j] { |
| 591 |
b.Fatalf("mismatch: expected: %v, got %v", arr[j], item) |
| 592 |
} |
| 593 |
j++ |
| 594 |
k++ |
| 595 |
return true |
| 596 |
}) |
| 597 |
if j != len(arr) { |
| 598 |
b.Fatalf("expected: %v, got %v", len(arr), j) |
| 599 |
} |
| 600 |
if k != len(arr)-100 { |
| 601 |
b.Fatalf("expected: %v, got %v", len(arr)-100, k) |
| 602 |
} |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
func BenchmarkDescendLessOrEqual(b *testing.B) { |
| 607 |
arr := rand.Perm(benchmarkTreeSize) |
| 608 |
tr := NewOrdered[int](*btreeDegree) |
| 609 |
for _, v := range arr { |
| 610 |
tr.ReplaceOrInsert(v) |
| 611 |
} |
| 612 |
sort.Ints(arr) |
| 613 |
b.ResetTimer() |
| 614 |
for i := 0; i < b.N; i++ { |
| 615 |
j := len(arr) - 100 |
| 616 |
k := len(arr) |
| 617 |
tr.DescendLessOrEqual(arr[len(arr)-100], func(item int) bool { |
| 618 |
if item != arr[j] { |
| 619 |
b.Fatalf("mismatch: expected: %v, got %v", arr[j], item) |
| 620 |
} |
| 621 |
j-- |
| 622 |
k-- |
| 623 |
return true |
| 624 |
}) |
| 625 |
if j != -1 { |
| 626 |
b.Fatalf("expected: %v, got %v", -1, j) |
| 627 |
} |
| 628 |
if k != 99 { |
| 629 |
b.Fatalf("expected: %v, got %v", 99, k) |
| 630 |
} |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
const cloneTestSize = 10000 |
| 635 |
|
| 636 |
func cloneTest(t *testing.T, b *BTree[int], start int, p []int, wg *sync.WaitGroup, trees *[]*BTree[int], lock *sync.Mutex) { |
| 637 |
t.Logf("Starting new clone at %v", start) |
| 638 |
lock.Lock() |
| 639 |
*trees = append(*trees, b) |
| 640 |
lock.Unlock() |
| 641 |
for i := start; i < cloneTestSize; i++ { |
| 642 |
b.ReplaceOrInsert(p[i]) |
| 643 |
if i%(cloneTestSize/5) == 0 { |
| 644 |
wg.Add(1) |
| 645 |
go cloneTest(t, b.Clone(), i+1, p, wg, trees, lock) |
| 646 |
} |
| 647 |
} |
| 648 |
wg.Done() |
| 649 |
} |
| 650 |
|
| 651 |
func TestCloneConcurrentOperations(t *testing.T) { |
| 652 |
b := NewOrdered[int](*btreeDegree) |
| 653 |
trees := []*BTree[int]{} |
| 654 |
p := rand.Perm(cloneTestSize) |
| 655 |
var wg sync.WaitGroup |
| 656 |
wg.Add(1) |
| 657 |
go cloneTest(t, b, 0, p, &wg, &trees, &sync.Mutex{}) |
| 658 |
wg.Wait() |
| 659 |
want := intRange(cloneTestSize, false) |
| 660 |
t.Logf("Starting equality checks on %d trees", len(trees)) |
| 661 |
for i, tree := range trees { |
| 662 |
if !reflect.DeepEqual(want, intAll(tree)) { |
| 663 |
t.Errorf("tree %v mismatch", i) |
| 664 |
} |
| 665 |
} |
| 666 |
t.Log("Removing half from first half") |
| 667 |
toRemove := intRange(cloneTestSize, false)[cloneTestSize/2:] |
| 668 |
for i := 0; i < len(trees)/2; i++ { |
| 669 |
tree := trees[i] |
| 670 |
wg.Go(func() { |
| 671 |
for _, item := range toRemove { |
| 672 |
tree.Delete(item) |
| 673 |
} |
| 674 |
}) |
| 675 |
} |
| 676 |
wg.Wait() |
| 677 |
t.Log("Checking all values again") |
| 678 |
for i, tree := range trees { |
| 679 |
var wantpart []int |
| 680 |
if i < len(trees)/2 { |
| 681 |
wantpart = want[:cloneTestSize/2] |
| 682 |
} else { |
| 683 |
wantpart = want |
| 684 |
} |
| 685 |
if got := intAll(tree); !reflect.DeepEqual(wantpart, got) { |
| 686 |
t.Errorf("tree %v mismatch, want %v got %v", i, len(want), len(got)) |
| 687 |
} |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
func BenchmarkDeleteAndRestore(b *testing.B) { |
| 692 |
items := rand.Perm(16392) |
| 693 |
b.ResetTimer() |
| 694 |
b.Run(`CopyBigFreeList`, func(b *testing.B) { |
| 695 |
fl := NewFreeList[int](16392) |
| 696 |
tr := NewWithFreeList[int](*btreeDegree, Less[int](), fl) |
| 697 |
for _, v := range items { |
| 698 |
tr.ReplaceOrInsert(v) |
| 699 |
} |
| 700 |
b.ReportAllocs() |
| 701 |
b.ResetTimer() |
| 702 |
for i := 0; i < b.N; i++ { |
| 703 |
dels := make([]int, 0, tr.Len()) |
| 704 |
tr.Ascend(func(b int) bool { |
| 705 |
dels = append(dels, b) |
| 706 |
return true |
| 707 |
}) |
| 708 |
for _, del := range dels { |
| 709 |
tr.Delete(del) |
| 710 |
} |
| 711 |
// tr is now empty, we make a new empty copy of it. |
| 712 |
tr = NewWithFreeList[int](*btreeDegree, Less[int](), fl) |
| 713 |
for _, v := range items { |
| 714 |
tr.ReplaceOrInsert(v) |
| 715 |
} |
| 716 |
} |
| 717 |
}) |
| 718 |
b.Run(`Copy`, func(b *testing.B) { |
| 719 |
tr := NewOrdered[int](*btreeDegree) |
| 720 |
for _, v := range items { |
| 721 |
tr.ReplaceOrInsert(v) |
| 722 |
} |
| 723 |
b.ReportAllocs() |
| 724 |
b.ResetTimer() |
| 725 |
for i := 0; i < b.N; i++ { |
| 726 |
dels := make([]int, 0, tr.Len()) |
| 727 |
tr.Ascend(func(b int) bool { |
| 728 |
dels = append(dels, b) |
| 729 |
return true |
| 730 |
}) |
| 731 |
for _, del := range dels { |
| 732 |
tr.Delete(del) |
| 733 |
} |
| 734 |
// tr is now empty, we make a new empty copy of it. |
| 735 |
tr = NewOrdered[int](*btreeDegree) |
| 736 |
for _, v := range items { |
| 737 |
tr.ReplaceOrInsert(v) |
| 738 |
} |
| 739 |
} |
| 740 |
}) |
| 741 |
b.Run(`ClearBigFreelist`, func(b *testing.B) { |
| 742 |
fl := NewFreeList[int](16392) |
| 743 |
tr := NewWithFreeList[int](*btreeDegree, Less[int](), fl) |
| 744 |
for _, v := range items { |
| 745 |
tr.ReplaceOrInsert(v) |
| 746 |
} |
| 747 |
b.ReportAllocs() |
| 748 |
b.ResetTimer() |
| 749 |
for i := 0; i < b.N; i++ { |
| 750 |
tr.Clear(true) |
| 751 |
for _, v := range items { |
| 752 |
tr.ReplaceOrInsert(v) |
| 753 |
} |
| 754 |
} |
| 755 |
}) |
| 756 |
b.Run(`Clear`, func(b *testing.B) { |
| 757 |
tr := NewOrdered[int](*btreeDegree) |
| 758 |
for _, v := range items { |
| 759 |
tr.ReplaceOrInsert(v) |
| 760 |
} |
| 761 |
b.ReportAllocs() |
| 762 |
b.ResetTimer() |
| 763 |
for i := 0; i < b.N; i++ { |
| 764 |
tr.Clear(true) |
| 765 |
for _, v := range items { |
| 766 |
tr.ReplaceOrInsert(v) |
| 767 |
} |
| 768 |
} |
| 769 |
}) |
| 770 |
} |