| ... | ... | @@ -427,375 +427,388 @@ test "sort fuzz testing" { |
| 427 | 427 | } |
| 428 | 428 | } |
| 429 | 429 | |
| 430 | | /// Returns the index of an element in `items` equal to `key`. |
| 431 | | /// If there are multiple such elements, returns the index of any one of them. |
| 432 | | /// If there are no such elements, returns `null`. |
| 430 | /// Returns the index of an element in `items` returning `.eq` when given to `compareFn`. |
| 431 | /// - If there are multiple such elements, returns the index of any one of them. |
| 432 | /// - If there are no such elements, returns `null`. |
| 433 | 433 | /// |
| 434 | | /// `items` must be sorted in ascending order with respect to `compareFn`. |
| 434 | /// `items` must be sorted in ascending order with respect to `compareFn`: |
| 435 | /// ``` |
| 436 | /// [0] [len] |
| 437 | /// ┌───┬───┬─/ /─┬───┬───┬───┬─/ /─┬───┬───┬───┬─/ /─┬───┐ |
| 438 | /// │.lt│.lt│ \ \ │.lt│.eq│.eq│ \ \ │.eq│.gt│.gt│ \ \ │.gt│ |
| 439 | /// └───┴───┴─/ /─┴───┴───┴───┴─/ /─┴───┴───┴───┴─/ /─┴───┘ |
| 440 | /// ├─────────────────┼─────────────────┼─────────────────┤ |
| 441 | /// ↳ zero or more ↳ zero or more ↳ zero or more |
| 442 | /// ├─────────────────┤ |
| 443 | /// ↳ if not null, returned |
| 444 | /// index is in this range |
| 445 | /// ``` |
| 435 | 446 | /// |
| 436 | | /// O(log n) complexity. |
| 447 | /// `O(log n)` time complexity. |
| 448 | /// |
| 449 | /// See also: `lowerBound, `upperBound`, `partitionPoint`, `equalRange`. |
| 437 | 450 | pub fn binarySearch( |
| 438 | 451 | comptime T: type, |
| 439 | | key: anytype, |
| 440 | 452 | items: []const T, |
| 441 | 453 | context: anytype, |
| 442 | | comptime compareFn: fn (context: @TypeOf(context), key: @TypeOf(key), mid_item: T) math.Order, |
| 454 | comptime compareFn: fn (@TypeOf(context), T) std.math.Order, |
| 443 | 455 | ) ?usize { |
| 444 | | var left: usize = 0; |
| 445 | | var right: usize = items.len; |
| 456 | var low: usize = 0; |
| 457 | var high: usize = items.len; |
| 446 | 458 | |
| 447 | | while (left < right) { |
| 459 | while (low < high) { |
| 448 | 460 | // Avoid overflowing in the midpoint calculation |
| 449 | | const mid = left + (right - left) / 2; |
| 450 | | // Compare the key with the midpoint element |
| 451 | | switch (compareFn(context, key, items[mid])) { |
| 461 | const mid = low + (high - low) / 2; |
| 462 | switch (compareFn(context, items[mid])) { |
| 452 | 463 | .eq => return mid, |
| 453 | | .gt => left = mid + 1, |
| 454 | | .lt => right = mid, |
| 464 | .lt => low = mid + 1, // item too small |
| 465 | .gt => high = mid, // item too big |
| 455 | 466 | } |
| 456 | 467 | } |
| 457 | | |
| 458 | 468 | return null; |
| 459 | 469 | } |
| 460 | 470 | |
| 461 | 471 | test binarySearch { |
| 462 | 472 | const S = struct { |
| 463 | | fn order_u32(context: void, lhs: u32, rhs: u32) math.Order { |
| 464 | | _ = context; |
| 465 | | return math.order(lhs, rhs); |
| 473 | fn orderU32(context: u32, item: u32) std.math.Order { |
| 474 | return std.math.order(item, context); |
| 475 | } |
| 476 | fn orderI32(context: i32, item: i32) std.math.Order { |
| 477 | return std.math.order(item, context); |
| 466 | 478 | } |
| 467 | | fn order_i32(context: void, lhs: i32, rhs: i32) math.Order { |
| 468 | | _ = context; |
| 469 | | return math.order(lhs, rhs); |
| 479 | fn orderLength(context: usize, item: []const u8) std.math.Order { |
| 480 | return std.math.order(item.len, context); |
| 470 | 481 | } |
| 471 | 482 | }; |
| 472 | | try testing.expectEqual( |
| 473 | | @as(?usize, null), |
| 474 | | binarySearch(u32, @as(u32, 1), &[_]u32{}, {}, S.order_u32), |
| 475 | | ); |
| 476 | | try testing.expectEqual( |
| 477 | | @as(?usize, 0), |
| 478 | | binarySearch(u32, @as(u32, 1), &[_]u32{1}, {}, S.order_u32), |
| 479 | | ); |
| 480 | | try testing.expectEqual( |
| 481 | | @as(?usize, null), |
| 482 | | binarySearch(u32, @as(u32, 1), &[_]u32{0}, {}, S.order_u32), |
| 483 | | ); |
| 484 | | try testing.expectEqual( |
| 485 | | @as(?usize, null), |
| 486 | | binarySearch(u32, @as(u32, 0), &[_]u32{1}, {}, S.order_u32), |
| 487 | | ); |
| 488 | | try testing.expectEqual( |
| 489 | | @as(?usize, 4), |
| 490 | | binarySearch(u32, @as(u32, 5), &[_]u32{ 1, 2, 3, 4, 5 }, {}, S.order_u32), |
| 491 | | ); |
| 492 | | try testing.expectEqual( |
| 493 | | @as(?usize, 0), |
| 494 | | binarySearch(u32, @as(u32, 2), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.order_u32), |
| 495 | | ); |
| 496 | | try testing.expectEqual( |
| 497 | | @as(?usize, 1), |
| 498 | | binarySearch(i32, @as(i32, -4), &[_]i32{ -7, -4, 0, 9, 10 }, {}, S.order_i32), |
| 499 | | ); |
| 500 | | try testing.expectEqual( |
| 501 | | @as(?usize, 3), |
| 502 | | binarySearch(i32, @as(i32, 98), &[_]i32{ -100, -25, 2, 98, 99, 100 }, {}, S.order_i32), |
| 503 | | ); |
| 504 | 483 | const R = struct { |
| 505 | 484 | b: i32, |
| 506 | 485 | e: i32, |
| 507 | 486 | |
| 508 | 487 | fn r(b: i32, e: i32) @This() { |
| 509 | | return @This(){ .b = b, .e = e }; |
| 488 | return .{ .b = b, .e = e }; |
| 510 | 489 | } |
| 511 | 490 | |
| 512 | | fn order(context: void, key: i32, mid_item: @This()) math.Order { |
| 513 | | _ = context; |
| 514 | | |
| 515 | | if (key < mid_item.b) { |
| 491 | fn order(context: i32, item: @This()) std.math.Order { |
| 492 | if (item.e < context) { |
| 516 | 493 | return .lt; |
| 517 | | } |
| 518 | | |
| 519 | | if (key > mid_item.e) { |
| 494 | } else if (item.b > context) { |
| 520 | 495 | return .gt; |
| 496 | } else { |
| 497 | return .eq; |
| 521 | 498 | } |
| 522 | | |
| 523 | | return .eq; |
| 524 | 499 | } |
| 525 | 500 | }; |
| 526 | | try testing.expectEqual( |
| 527 | | @as(?usize, null), |
| 528 | | binarySearch(R, @as(i32, -45), &[_]R{ R.r(-100, -50), R.r(-40, -20), R.r(-10, 20), R.r(30, 40) }, {}, R.order), |
| 529 | | ); |
| 530 | | try testing.expectEqual( |
| 531 | | @as(?usize, 2), |
| 532 | | binarySearch(R, @as(i32, 10), &[_]R{ R.r(-100, -50), R.r(-40, -20), R.r(-10, 20), R.r(30, 40) }, {}, R.order), |
| 533 | | ); |
| 534 | | try testing.expectEqual( |
| 535 | | @as(?usize, 1), |
| 536 | | binarySearch(R, @as(i32, -20), &[_]R{ R.r(-100, -50), R.r(-40, -20), R.r(-10, 20), R.r(30, 40) }, {}, R.order), |
| 537 | | ); |
| 538 | | } |
| 539 | | |
| 540 | | /// Returns the index of the first element in `items` greater than or equal to `key`, |
| 541 | | /// or `items.len` if all elements are less than `key`. |
| 501 | |
| 502 | try std.testing.expectEqual(null, binarySearch(u32, &[_]u32{}, @as(u32, 1), S.orderU32)); |
| 503 | try std.testing.expectEqual(0, binarySearch(u32, &[_]u32{1}, @as(u32, 1), S.orderU32)); |
| 504 | try std.testing.expectEqual(null, binarySearch(u32, &[_]u32{0}, @as(u32, 1), S.orderU32)); |
| 505 | try std.testing.expectEqual(null, binarySearch(u32, &[_]u32{1}, @as(u32, 0), S.orderU32)); |
| 506 | try std.testing.expectEqual(4, binarySearch(u32, &[_]u32{ 1, 2, 3, 4, 5 }, @as(u32, 5), S.orderU32)); |
| 507 | try std.testing.expectEqual(0, binarySearch(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 2), S.orderU32)); |
| 508 | try std.testing.expectEqual(1, binarySearch(i32, &[_]i32{ -7, -4, 0, 9, 10 }, @as(i32, -4), S.orderI32)); |
| 509 | try std.testing.expectEqual(3, binarySearch(i32, &[_]i32{ -100, -25, 2, 98, 99, 100 }, @as(i32, 98), S.orderI32)); |
| 510 | try std.testing.expectEqual(null, binarySearch(R, &[_]R{ R.r(-100, -50), R.r(-40, -20), R.r(-10, 20), R.r(30, 40) }, @as(i32, -45), R.order)); |
| 511 | try std.testing.expectEqual(2, binarySearch(R, &[_]R{ R.r(-100, -50), R.r(-40, -20), R.r(-10, 20), R.r(30, 40) }, @as(i32, 10), R.order)); |
| 512 | try std.testing.expectEqual(1, binarySearch(R, &[_]R{ R.r(-100, -50), R.r(-40, -20), R.r(-10, 20), R.r(30, 40) }, @as(i32, -20), R.order)); |
| 513 | try std.testing.expectEqual(2, binarySearch([]const u8, &[_][]const u8{ "", "abc", "1234", "vwxyz" }, @as(usize, 4), S.orderLength)); |
| 514 | } |
| 515 | |
| 516 | /// Returns the index of the first element in `items` returning `.eq` or `.gt` |
| 517 | /// when given to `compareFn`. |
| 518 | /// - Returns `items.len` if all elements return `.lt`. |
| 519 | /// |
| 520 | /// `items` must be sorted in ascending order with respect to `compareFn`: |
| 521 | /// ``` |
| 522 | /// [0] [len] |
| 523 | /// ┌───┬───┬─/ /─┬───┬───┬───┬─/ /─┬───┬───┬───┬─/ /─┬───┐ |
| 524 | /// │.lt│.lt│ \ \ │.lt│.eq│.eq│ \ \ │.eq│.gt│.gt│ \ \ │.gt│ |
| 525 | /// └───┴───┴─/ /─┴───┴───┴───┴─/ /─┴───┴───┴───┴─/ /─┴───┘ |
| 526 | /// ├─────────────────┼─────────────────┼─────────────────┤ |
| 527 | /// ↳ zero or more ↳ zero or more ↳ zero or more |
| 528 | /// ├───┤ |
| 529 | /// ↳ returned index |
| 530 | /// ``` |
| 542 | 531 | /// |
| 543 | | /// `items` must be sorted in ascending order with respect to `compareFn`. |
| 532 | /// `O(log n)` time complexity. |
| 544 | 533 | /// |
| 545 | | /// O(log n) complexity. |
| 534 | /// See also: `binarySearch`, `upperBound`, `partitionPoint`, `equalRange`. |
| 546 | 535 | pub fn lowerBound( |
| 547 | 536 | comptime T: type, |
| 548 | | key: anytype, |
| 549 | 537 | items: []const T, |
| 550 | 538 | context: anytype, |
| 551 | | comptime lessThan: fn (context: @TypeOf(context), lhs: @TypeOf(key), rhs: T) bool, |
| 539 | comptime compareFn: fn (@TypeOf(context), T) std.math.Order, |
| 552 | 540 | ) usize { |
| 553 | | var left: usize = 0; |
| 554 | | var right: usize = items.len; |
| 555 | | |
| 556 | | while (left < right) { |
| 557 | | const mid = left + (right - left) / 2; |
| 558 | | if (lessThan(context, items[mid], key)) { |
| 559 | | left = mid + 1; |
| 560 | | } else { |
| 561 | | right = mid; |
| 541 | const S = struct { |
| 542 | fn predicate(ctx: @TypeOf(context), item: T) bool { |
| 543 | return compareFn(ctx, item) == .lt; |
| 562 | 544 | } |
| 563 | | } |
| 564 | | |
| 565 | | return left; |
| 545 | }; |
| 546 | return partitionPoint(T, items, context, S.predicate); |
| 566 | 547 | } |
| 567 | 548 | |
| 568 | 549 | test lowerBound { |
| 569 | 550 | const S = struct { |
| 570 | | fn lower_u32(context: void, lhs: u32, rhs: u32) bool { |
| 571 | | _ = context; |
| 572 | | return lhs < rhs; |
| 551 | fn compareU32(context: u32, item: u32) std.math.Order { |
| 552 | return std.math.order(item, context); |
| 573 | 553 | } |
| 574 | | fn lower_i32(context: void, lhs: i32, rhs: i32) bool { |
| 575 | | _ = context; |
| 576 | | return lhs < rhs; |
| 554 | fn compareI32(context: i32, item: i32) std.math.Order { |
| 555 | return std.math.order(item, context); |
| 577 | 556 | } |
| 578 | | fn lower_f32(context: void, lhs: f32, rhs: f32) bool { |
| 579 | | _ = context; |
| 580 | | return lhs < rhs; |
| 557 | fn compareF32(context: f32, item: f32) std.math.Order { |
| 558 | return std.math.order(item, context); |
| 581 | 559 | } |
| 582 | 560 | }; |
| 561 | const R = struct { |
| 562 | val: i32, |
| 583 | 563 | |
| 584 | | try testing.expectEqual( |
| 585 | | @as(usize, 0), |
| 586 | | lowerBound(u32, @as(u32, 0), &[_]u32{}, {}, S.lower_u32), |
| 587 | | ); |
| 588 | | try testing.expectEqual( |
| 589 | | @as(usize, 0), |
| 590 | | lowerBound(u32, @as(u32, 0), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32), |
| 591 | | ); |
| 592 | | try testing.expectEqual( |
| 593 | | @as(usize, 0), |
| 594 | | lowerBound(u32, @as(u32, 2), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32), |
| 595 | | ); |
| 596 | | try testing.expectEqual( |
| 597 | | @as(usize, 2), |
| 598 | | lowerBound(u32, @as(u32, 5), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32), |
| 599 | | ); |
| 600 | | try testing.expectEqual( |
| 601 | | @as(usize, 2), |
| 602 | | lowerBound(u32, @as(u32, 8), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32), |
| 603 | | ); |
| 604 | | try testing.expectEqual( |
| 605 | | @as(usize, 6), |
| 606 | | lowerBound(u32, @as(u32, 8), &[_]u32{ 2, 4, 7, 7, 7, 7, 16, 32, 64 }, {}, S.lower_u32), |
| 607 | | ); |
| 608 | | try testing.expectEqual( |
| 609 | | @as(usize, 2), |
| 610 | | lowerBound(u32, @as(u32, 8), &[_]u32{ 2, 4, 8, 8, 8, 8, 16, 32, 64 }, {}, S.lower_u32), |
| 611 | | ); |
| 612 | | try testing.expectEqual( |
| 613 | | @as(usize, 5), |
| 614 | | lowerBound(u32, @as(u32, 64), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32), |
| 615 | | ); |
| 616 | | try testing.expectEqual( |
| 617 | | @as(usize, 6), |
| 618 | | lowerBound(u32, @as(u32, 100), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32), |
| 619 | | ); |
| 620 | | try testing.expectEqual( |
| 621 | | @as(usize, 2), |
| 622 | | lowerBound(i32, @as(i32, 5), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32), |
| 623 | | ); |
| 624 | | try testing.expectEqual( |
| 625 | | @as(usize, 1), |
| 626 | | lowerBound(f32, @as(f32, -33.4), &[_]f32{ -54.2, -26.7, 0.0, 56.55, 100.1, 322.0 }, {}, S.lower_f32), |
| 627 | | ); |
| 628 | | } |
| 629 | | |
| 630 | | /// Returns the index of the first element in `items` greater than `key`, |
| 631 | | /// or `items.len` if all elements are less than or equal to `key`. |
| 564 | fn r(val: i32) @This() { |
| 565 | return .{ .val = val }; |
| 566 | } |
| 567 | |
| 568 | fn compareFn(context: i32, item: @This()) std.math.Order { |
| 569 | return std.math.order(item.val, context); |
| 570 | } |
| 571 | }; |
| 572 | |
| 573 | try std.testing.expectEqual(0, lowerBound(u32, &[_]u32{}, @as(u32, 0), S.compareU32)); |
| 574 | try std.testing.expectEqual(0, lowerBound(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 0), S.compareU32)); |
| 575 | try std.testing.expectEqual(0, lowerBound(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 2), S.compareU32)); |
| 576 | try std.testing.expectEqual(2, lowerBound(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 5), S.compareU32)); |
| 577 | try std.testing.expectEqual(2, lowerBound(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 8), S.compareU32)); |
| 578 | try std.testing.expectEqual(6, lowerBound(u32, &[_]u32{ 2, 4, 7, 7, 7, 7, 16, 32, 64 }, @as(u32, 8), S.compareU32)); |
| 579 | try std.testing.expectEqual(2, lowerBound(u32, &[_]u32{ 2, 4, 8, 8, 8, 8, 16, 32, 64 }, @as(u32, 8), S.compareU32)); |
| 580 | try std.testing.expectEqual(5, lowerBound(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 64), S.compareU32)); |
| 581 | try std.testing.expectEqual(6, lowerBound(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 100), S.compareU32)); |
| 582 | try std.testing.expectEqual(2, lowerBound(i32, &[_]i32{ 2, 4, 8, 16, 32, 64 }, @as(i32, 5), S.compareI32)); |
| 583 | try std.testing.expectEqual(1, lowerBound(f32, &[_]f32{ -54.2, -26.7, 0.0, 56.55, 100.1, 322.0 }, @as(f32, -33.4), S.compareF32)); |
| 584 | try std.testing.expectEqual(2, lowerBound(R, &[_]R{ R.r(-100), R.r(-40), R.r(-10), R.r(30) }, @as(i32, -20), R.compareFn)); |
| 585 | } |
| 586 | |
| 587 | /// Returns the index of the first element in `items` returning `.gt` |
| 588 | /// when given to `compareFn`. |
| 589 | /// - Returns `items.len` if none of the elements return `.gt`. |
| 632 | 590 | /// |
| 633 | | /// `items` must be sorted in ascending order with respect to `compareFn`. |
| 591 | /// `items` must be sorted in ascending order with respect to `compareFn`: |
| 592 | /// ``` |
| 593 | /// [0] [len] |
| 594 | /// ┌───┬───┬─/ /─┬───┬───┬───┬─/ /─┬───┬───┬───┬─/ /─┬───┐ |
| 595 | /// │.lt│.lt│ \ \ │.lt│.eq│.eq│ \ \ │.eq│.gt│.gt│ \ \ │.gt│ |
| 596 | /// └───┴───┴─/ /─┴───┴───┴───┴─/ /─┴───┴───┴───┴─/ /─┴───┘ |
| 597 | /// ├─────────────────┼─────────────────┼─────────────────┤ |
| 598 | /// ↳ zero or more ↳ zero or more ↳ zero or more |
| 599 | /// ├───┤ |
| 600 | /// ↳ returned index |
| 601 | /// ``` |
| 634 | 602 | /// |
| 635 | | /// O(log n) complexity. |
| 603 | /// `O(log n)` time complexity. |
| 604 | /// |
| 605 | /// See also: `binarySearch`, `lowerBound`, `partitionPoint`, `equalRange`. |
| 636 | 606 | pub fn upperBound( |
| 637 | 607 | comptime T: type, |
| 638 | | key: anytype, |
| 639 | 608 | items: []const T, |
| 640 | 609 | context: anytype, |
| 641 | | comptime lessThan: fn (context: @TypeOf(context), lhs: @TypeOf(key), rhs: T) bool, |
| 610 | comptime compareFn: fn (@TypeOf(context), T) std.math.Order, |
| 642 | 611 | ) usize { |
| 643 | | var left: usize = 0; |
| 644 | | var right: usize = items.len; |
| 612 | const S = struct { |
| 613 | fn predicate(ctx: @TypeOf(context), item: T) bool { |
| 614 | return compareFn(ctx, item) != .gt; |
| 615 | } |
| 616 | }; |
| 617 | return partitionPoint(T, items, context, S.predicate); |
| 618 | } |
| 645 | 619 | |
| 646 | | while (left < right) { |
| 647 | | const mid = left + (right - left) / 2; |
| 648 | | if (!lessThan(context, key, items[mid])) { |
| 649 | | left = mid + 1; |
| 620 | test upperBound { |
| 621 | const S = struct { |
| 622 | fn compareU32(context: u32, item: u32) std.math.Order { |
| 623 | return std.math.order(item, context); |
| 624 | } |
| 625 | fn compareI32(context: i32, item: i32) std.math.Order { |
| 626 | return std.math.order(item, context); |
| 627 | } |
| 628 | fn compareF32(context: f32, item: f32) std.math.Order { |
| 629 | return std.math.order(item, context); |
| 630 | } |
| 631 | }; |
| 632 | const R = struct { |
| 633 | val: i32, |
| 634 | |
| 635 | fn r(val: i32) @This() { |
| 636 | return .{ .val = val }; |
| 637 | } |
| 638 | |
| 639 | fn compareFn(context: i32, item: @This()) std.math.Order { |
| 640 | return std.math.order(item.val, context); |
| 641 | } |
| 642 | }; |
| 643 | |
| 644 | try std.testing.expectEqual(0, upperBound(u32, &[_]u32{}, @as(u32, 0), S.compareU32)); |
| 645 | try std.testing.expectEqual(0, upperBound(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 0), S.compareU32)); |
| 646 | try std.testing.expectEqual(1, upperBound(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 2), S.compareU32)); |
| 647 | try std.testing.expectEqual(2, upperBound(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 5), S.compareU32)); |
| 648 | try std.testing.expectEqual(6, upperBound(u32, &[_]u32{ 2, 4, 7, 7, 7, 7, 16, 32, 64 }, @as(u32, 8), S.compareU32)); |
| 649 | try std.testing.expectEqual(6, upperBound(u32, &[_]u32{ 2, 4, 8, 8, 8, 8, 16, 32, 64 }, @as(u32, 8), S.compareU32)); |
| 650 | try std.testing.expectEqual(3, upperBound(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 8), S.compareU32)); |
| 651 | try std.testing.expectEqual(6, upperBound(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 64), S.compareU32)); |
| 652 | try std.testing.expectEqual(6, upperBound(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 100), S.compareU32)); |
| 653 | try std.testing.expectEqual(2, upperBound(i32, &[_]i32{ 2, 4, 8, 16, 32, 64 }, @as(i32, 5), S.compareI32)); |
| 654 | try std.testing.expectEqual(1, upperBound(f32, &[_]f32{ -54.2, -26.7, 0.0, 56.55, 100.1, 322.0 }, @as(f32, -33.4), S.compareF32)); |
| 655 | try std.testing.expectEqual(2, upperBound(R, &[_]R{ R.r(-100), R.r(-40), R.r(-10), R.r(30) }, @as(i32, -20), R.compareFn)); |
| 656 | } |
| 657 | |
| 658 | /// Returns the index of the partition point of `items` in relation to the given predicate. |
| 659 | /// - If all elements of `items` satisfy the predicate the returned value is `items.len`. |
| 660 | /// |
| 661 | /// `items` must contain a prefix for which all elements satisfy the predicate, |
| 662 | /// and beyond which none of the elements satisfy the predicate: |
| 663 | /// ``` |
| 664 | /// [0] [len] |
| 665 | /// ┌────┬────┬─/ /─┬────┬─────┬─────┬─/ /─┬─────┐ |
| 666 | /// │true│true│ \ \ │true│false│false│ \ \ │false│ |
| 667 | /// └────┴────┴─/ /─┴────┴─────┴─────┴─/ /─┴─────┘ |
| 668 | /// ├────────────────────┼───────────────────────┤ |
| 669 | /// ↳ zero or more ↳ zero or more |
| 670 | /// ├─────┤ |
| 671 | /// ↳ returned index |
| 672 | /// ``` |
| 673 | /// |
| 674 | /// `O(log n)` time complexity. |
| 675 | /// |
| 676 | /// See also: `binarySearch`, `lowerBound, `upperBound`, `equalRange`. |
| 677 | pub fn partitionPoint( |
| 678 | comptime T: type, |
| 679 | items: []const T, |
| 680 | context: anytype, |
| 681 | comptime predicate: fn (@TypeOf(context), T) bool, |
| 682 | ) usize { |
| 683 | var low: usize = 0; |
| 684 | var high: usize = items.len; |
| 685 | |
| 686 | while (low < high) { |
| 687 | const mid = low + (high - low) / 2; |
| 688 | if (predicate(context, items[mid])) { |
| 689 | low = mid + 1; |
| 650 | 690 | } else { |
| 651 | | right = mid; |
| 691 | high = mid; |
| 652 | 692 | } |
| 653 | 693 | } |
| 654 | | |
| 655 | | return left; |
| 694 | return low; |
| 656 | 695 | } |
| 657 | 696 | |
| 658 | | test upperBound { |
| 697 | test partitionPoint { |
| 659 | 698 | const S = struct { |
| 660 | | fn lower_u32(context: void, lhs: u32, rhs: u32) bool { |
| 661 | | _ = context; |
| 662 | | return lhs < rhs; |
| 699 | fn lowerU32(context: u32, item: u32) bool { |
| 700 | return item < context; |
| 701 | } |
| 702 | fn lowerI32(context: i32, item: i32) bool { |
| 703 | return item < context; |
| 704 | } |
| 705 | fn lowerF32(context: f32, item: f32) bool { |
| 706 | return item < context; |
| 707 | } |
| 708 | fn lowerEqU32(context: u32, item: u32) bool { |
| 709 | return item <= context; |
| 710 | } |
| 711 | fn lowerEqI32(context: i32, item: i32) bool { |
| 712 | return item <= context; |
| 663 | 713 | } |
| 664 | | fn lower_i32(context: void, lhs: i32, rhs: i32) bool { |
| 665 | | _ = context; |
| 666 | | return lhs < rhs; |
| 714 | fn lowerEqF32(context: f32, item: f32) bool { |
| 715 | return item <= context; |
| 667 | 716 | } |
| 668 | | fn lower_f32(context: void, lhs: f32, rhs: f32) bool { |
| 669 | | _ = context; |
| 670 | | return lhs < rhs; |
| 717 | fn isEven(_: void, item: u8) bool { |
| 718 | return item % 2 == 0; |
| 671 | 719 | } |
| 672 | 720 | }; |
| 673 | 721 | |
| 674 | | try testing.expectEqual( |
| 675 | | @as(usize, 0), |
| 676 | | upperBound(u32, @as(u32, 0), &[_]u32{}, {}, S.lower_u32), |
| 677 | | ); |
| 678 | | try testing.expectEqual( |
| 679 | | @as(usize, 0), |
| 680 | | upperBound(u32, @as(u32, 0), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32), |
| 681 | | ); |
| 682 | | try testing.expectEqual( |
| 683 | | @as(usize, 1), |
| 684 | | upperBound(u32, @as(u32, 2), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32), |
| 685 | | ); |
| 686 | | try testing.expectEqual( |
| 687 | | @as(usize, 2), |
| 688 | | upperBound(u32, @as(u32, 5), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32), |
| 689 | | ); |
| 690 | | try testing.expectEqual( |
| 691 | | @as(usize, 6), |
| 692 | | upperBound(u32, @as(u32, 8), &[_]u32{ 2, 4, 7, 7, 7, 7, 16, 32, 64 }, {}, S.lower_u32), |
| 693 | | ); |
| 694 | | try testing.expectEqual( |
| 695 | | @as(usize, 6), |
| 696 | | upperBound(u32, @as(u32, 8), &[_]u32{ 2, 4, 8, 8, 8, 8, 16, 32, 64 }, {}, S.lower_u32), |
| 697 | | ); |
| 698 | | try testing.expectEqual( |
| 699 | | @as(usize, 3), |
| 700 | | upperBound(u32, @as(u32, 8), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32), |
| 701 | | ); |
| 702 | | try testing.expectEqual( |
| 703 | | @as(usize, 6), |
| 704 | | upperBound(u32, @as(u32, 64), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32), |
| 705 | | ); |
| 706 | | try testing.expectEqual( |
| 707 | | @as(usize, 6), |
| 708 | | upperBound(u32, @as(u32, 100), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32), |
| 709 | | ); |
| 710 | | try testing.expectEqual( |
| 711 | | @as(usize, 2), |
| 712 | | upperBound(i32, @as(i32, 5), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32), |
| 713 | | ); |
| 714 | | try testing.expectEqual( |
| 715 | | @as(usize, 1), |
| 716 | | upperBound(f32, @as(f32, -33.4), &[_]f32{ -54.2, -26.7, 0.0, 56.55, 100.1, 322.0 }, {}, S.lower_f32), |
| 717 | | ); |
| 718 | | } |
| 719 | | |
| 720 | | /// Returns a tuple of the lower and upper indices in `items` between which all elements are equal to `key`. |
| 721 | | /// If no element in `items` is equal to `key`, both indices are the |
| 722 | | /// index of the first element in `items` greater than `key`. |
| 723 | | /// If no element in `items` is greater than `key`, both indices equal `items.len`. |
| 722 | try std.testing.expectEqual(0, partitionPoint(u32, &[_]u32{}, @as(u32, 0), S.lowerU32)); |
| 723 | try std.testing.expectEqual(0, partitionPoint(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 0), S.lowerU32)); |
| 724 | try std.testing.expectEqual(0, partitionPoint(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 2), S.lowerU32)); |
| 725 | try std.testing.expectEqual(2, partitionPoint(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 5), S.lowerU32)); |
| 726 | try std.testing.expectEqual(2, partitionPoint(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 8), S.lowerU32)); |
| 727 | try std.testing.expectEqual(6, partitionPoint(u32, &[_]u32{ 2, 4, 7, 7, 7, 7, 16, 32, 64 }, @as(u32, 8), S.lowerU32)); |
| 728 | try std.testing.expectEqual(2, partitionPoint(u32, &[_]u32{ 2, 4, 8, 8, 8, 8, 16, 32, 64 }, @as(u32, 8), S.lowerU32)); |
| 729 | try std.testing.expectEqual(5, partitionPoint(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 64), S.lowerU32)); |
| 730 | try std.testing.expectEqual(6, partitionPoint(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 100), S.lowerU32)); |
| 731 | try std.testing.expectEqual(2, partitionPoint(i32, &[_]i32{ 2, 4, 8, 16, 32, 64 }, @as(i32, 5), S.lowerI32)); |
| 732 | try std.testing.expectEqual(1, partitionPoint(f32, &[_]f32{ -54.2, -26.7, 0.0, 56.55, 100.1, 322.0 }, @as(f32, -33.4), S.lowerF32)); |
| 733 | try std.testing.expectEqual(0, partitionPoint(u32, &[_]u32{}, @as(u32, 0), S.lowerEqU32)); |
| 734 | try std.testing.expectEqual(0, partitionPoint(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 0), S.lowerEqU32)); |
| 735 | try std.testing.expectEqual(1, partitionPoint(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 2), S.lowerEqU32)); |
| 736 | try std.testing.expectEqual(2, partitionPoint(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 5), S.lowerEqU32)); |
| 737 | try std.testing.expectEqual(6, partitionPoint(u32, &[_]u32{ 2, 4, 7, 7, 7, 7, 16, 32, 64 }, @as(u32, 8), S.lowerEqU32)); |
| 738 | try std.testing.expectEqual(6, partitionPoint(u32, &[_]u32{ 2, 4, 8, 8, 8, 8, 16, 32, 64 }, @as(u32, 8), S.lowerEqU32)); |
| 739 | try std.testing.expectEqual(3, partitionPoint(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 8), S.lowerEqU32)); |
| 740 | try std.testing.expectEqual(6, partitionPoint(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 64), S.lowerEqU32)); |
| 741 | try std.testing.expectEqual(6, partitionPoint(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 100), S.lowerEqU32)); |
| 742 | try std.testing.expectEqual(2, partitionPoint(i32, &[_]i32{ 2, 4, 8, 16, 32, 64 }, @as(i32, 5), S.lowerEqI32)); |
| 743 | try std.testing.expectEqual(1, partitionPoint(f32, &[_]f32{ -54.2, -26.7, 0.0, 56.55, 100.1, 322.0 }, @as(f32, -33.4), S.lowerEqF32)); |
| 744 | try std.testing.expectEqual(4, partitionPoint(u8, &[_]u8{ 0, 50, 14, 2, 5, 71 }, {}, S.isEven)); |
| 745 | } |
| 746 | |
| 747 | /// Returns a tuple of the lower and upper indices in `items` between which all |
| 748 | /// elements return `.eq` when given to `compareFn`. |
| 749 | /// - If no element in `items` returns `.eq`, both indices are the |
| 750 | /// index of the first element in `items` returning `.gt`. |
| 751 | /// - If no element in `items` returns `.gt`, both indices equal `items.len`. |
| 724 | 752 | /// |
| 725 | | /// `items` must be sorted in ascending order with respect to `compareFn`. |
| 753 | /// `items` must be sorted in ascending order with respect to `compareFn`: |
| 754 | /// ``` |
| 755 | /// [0] [len] |
| 756 | /// ┌───┬───┬─/ /─┬───┬───┬───┬─/ /─┬───┬───┬───┬─/ /─┬───┐ |
| 757 | /// │.lt│.lt│ \ \ │.lt│.eq│.eq│ \ \ │.eq│.gt│.gt│ \ \ │.gt│ |
| 758 | /// └───┴───┴─/ /─┴───┴───┴───┴─/ /─┴───┴───┴───┴─/ /─┴───┘ |
| 759 | /// ├─────────────────┼─────────────────┼─────────────────┤ |
| 760 | /// ↳ zero or more ↳ zero or more ↳ zero or more |
| 761 | /// ├─────────────────┤ |
| 762 | /// ↳ returned range |
| 763 | /// ``` |
| 726 | 764 | /// |
| 727 | | /// O(log n) complexity. |
| 765 | /// `O(log n)` time complexity. |
| 728 | 766 | /// |
| 729 | | /// See also: `lowerBound` and `upperBound`. |
| 767 | /// See also: `binarySearch`, `lowerBound, `upperBound`, `partitionPoint`. |
| 730 | 768 | pub fn equalRange( |
| 731 | 769 | comptime T: type, |
| 732 | | key: anytype, |
| 733 | 770 | items: []const T, |
| 734 | 771 | context: anytype, |
| 735 | | comptime lessThan: fn (context: @TypeOf(context), lhs: @TypeOf(key), rhs: T) bool, |
| 772 | comptime compareFn: fn (@TypeOf(context), T) std.math.Order, |
| 736 | 773 | ) struct { usize, usize } { |
| 737 | 774 | return .{ |
| 738 | | lowerBound(T, key, items, context, lessThan), |
| 739 | | upperBound(T, key, items, context, lessThan), |
| 775 | lowerBound(T, items, context, compareFn), |
| 776 | upperBound(T, items, context, compareFn), |
| 740 | 777 | }; |
| 741 | 778 | } |
| 742 | 779 | |
| 743 | 780 | test equalRange { |
| 744 | 781 | const S = struct { |
| 745 | | fn lower_u32(context: void, lhs: u32, rhs: u32) bool { |
| 746 | | _ = context; |
| 747 | | return lhs < rhs; |
| 782 | fn orderU32(context: u32, item: u32) std.math.Order { |
| 783 | return std.math.order(item, context); |
| 748 | 784 | } |
| 749 | | fn lower_i32(context: void, lhs: i32, rhs: i32) bool { |
| 750 | | _ = context; |
| 751 | | return lhs < rhs; |
| 785 | fn orderI32(context: i32, item: i32) std.math.Order { |
| 786 | return std.math.order(item, context); |
| 752 | 787 | } |
| 753 | | fn lower_f32(context: void, lhs: f32, rhs: f32) bool { |
| 754 | | _ = context; |
| 755 | | return lhs < rhs; |
| 788 | fn orderF32(context: f32, item: f32) std.math.Order { |
| 789 | return std.math.order(item, context); |
| 790 | } |
| 791 | fn orderLength(context: usize, item: []const u8) std.math.Order { |
| 792 | return std.math.order(item.len, context); |
| 756 | 793 | } |
| 757 | 794 | }; |
| 758 | 795 | |
| 759 | | try testing.expectEqual( |
| 760 | | @as(struct { usize, usize }, .{ 0, 0 }), |
| 761 | | equalRange(i32, @as(i32, 0), &[_]i32{}, {}, S.lower_i32), |
| 762 | | ); |
| 763 | | try testing.expectEqual( |
| 764 | | @as(struct { usize, usize }, .{ 0, 0 }), |
| 765 | | equalRange(i32, @as(i32, 0), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32), |
| 766 | | ); |
| 767 | | try testing.expectEqual( |
| 768 | | @as(struct { usize, usize }, .{ 0, 1 }), |
| 769 | | equalRange(i32, @as(i32, 2), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32), |
| 770 | | ); |
| 771 | | try testing.expectEqual( |
| 772 | | @as(struct { usize, usize }, .{ 2, 2 }), |
| 773 | | equalRange(i32, @as(i32, 5), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32), |
| 774 | | ); |
| 775 | | try testing.expectEqual( |
| 776 | | @as(struct { usize, usize }, .{ 2, 3 }), |
| 777 | | equalRange(i32, @as(i32, 8), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32), |
| 778 | | ); |
| 779 | | try testing.expectEqual( |
| 780 | | @as(struct { usize, usize }, .{ 5, 6 }), |
| 781 | | equalRange(i32, @as(i32, 64), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32), |
| 782 | | ); |
| 783 | | try testing.expectEqual( |
| 784 | | @as(struct { usize, usize }, .{ 6, 6 }), |
| 785 | | equalRange(i32, @as(i32, 100), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32), |
| 786 | | ); |
| 787 | | try testing.expectEqual( |
| 788 | | @as(struct { usize, usize }, .{ 2, 6 }), |
| 789 | | equalRange(i32, @as(i32, 8), &[_]i32{ 2, 4, 8, 8, 8, 8, 15, 22 }, {}, S.lower_i32), |
| 790 | | ); |
| 791 | | try testing.expectEqual( |
| 792 | | @as(struct { usize, usize }, .{ 2, 2 }), |
| 793 | | equalRange(u32, @as(u32, 5), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32), |
| 794 | | ); |
| 795 | | try testing.expectEqual( |
| 796 | | @as(struct { usize, usize }, .{ 1, 1 }), |
| 797 | | equalRange(f32, @as(f32, -33.4), &[_]f32{ -54.2, -26.7, 0.0, 56.55, 100.1, 322.0 }, {}, S.lower_f32), |
| 798 | | ); |
| 796 | try std.testing.expectEqual(.{ 0, 0 }, equalRange(i32, &[_]i32{}, @as(i32, 0), S.orderI32)); |
| 797 | try std.testing.expectEqual(.{ 0, 0 }, equalRange(i32, &[_]i32{ 2, 4, 8, 16, 32, 64 }, @as(i32, 0), S.orderI32)); |
| 798 | try std.testing.expectEqual(.{ 0, 1 }, equalRange(i32, &[_]i32{ 2, 4, 8, 16, 32, 64 }, @as(i32, 2), S.orderI32)); |
| 799 | try std.testing.expectEqual(.{ 2, 2 }, equalRange(i32, &[_]i32{ 2, 4, 8, 16, 32, 64 }, @as(i32, 5), S.orderI32)); |
| 800 | try std.testing.expectEqual(.{ 2, 3 }, equalRange(i32, &[_]i32{ 2, 4, 8, 16, 32, 64 }, @as(i32, 8), S.orderI32)); |
| 801 | try std.testing.expectEqual(.{ 5, 6 }, equalRange(i32, &[_]i32{ 2, 4, 8, 16, 32, 64 }, @as(i32, 64), S.orderI32)); |
| 802 | try std.testing.expectEqual(.{ 6, 6 }, equalRange(i32, &[_]i32{ 2, 4, 8, 16, 32, 64 }, @as(i32, 100), S.orderI32)); |
| 803 | try std.testing.expectEqual(.{ 2, 6 }, equalRange(i32, &[_]i32{ 2, 4, 8, 8, 8, 8, 15, 22 }, @as(i32, 8), S.orderI32)); |
| 804 | try std.testing.expectEqual(.{ 2, 2 }, equalRange(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 5), S.orderU32)); |
| 805 | try std.testing.expectEqual(.{ 1, 1 }, equalRange(f32, &[_]f32{ -54.2, -26.7, 0.0, 56.55, 100.1, 322.0 }, @as(f32, -33.4), S.orderF32)); |
| 806 | try std.testing.expectEqual(.{ 3, 5 }, equalRange( |
| 807 | []const u8, |
| 808 | &[_][]const u8{ "Mars", "Venus", "Earth", "Saturn", "Uranus", "Mercury", "Jupiter", "Neptune" }, |
| 809 | @as(usize, 6), |
| 810 | S.orderLength, |
| 811 | )); |
| 799 | 812 | } |
| 800 | 813 | |
| 801 | 814 | pub fn argMin( |