| ... | @@ -427,375 +427,388 @@ test "sort fuzz testing" { | ... | @@ -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`. | 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. | 431 | /// - If there are multiple such elements, returns the index of any one of them. |
| 432 | /// If there are no such elements, returns `null`. | 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 | pub fn binarySearch( | 450 | pub fn binarySearch( |
| 438 | comptime T: type, | 451 | comptime T: type, |
| 439 | key: anytype, | | |
| 440 | items: []const T, | 452 | items: []const T, |
| 441 | context: anytype, | 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 | ) ?usize { | 455 | ) ?usize { |
| 444 | var left: usize = 0; | 456 | var low: usize = 0; |
| 445 | var right: usize = items.len; | 457 | var high: usize = items.len; |
| 446 | | 458 | |
| 447 | while (left < right) { | 459 | while (low < high) { |
| 448 | // Avoid overflowing in the midpoint calculation | 460 | // Avoid overflowing in the midpoint calculation |
| 449 | const mid = left + (right - left) / 2; | 461 | const mid = low + (high - low) / 2; |
| 450 | // Compare the key with the midpoint element | 462 | switch (compareFn(context, items[mid])) { |
| 451 | switch (compareFn(context, key, items[mid])) { | | |
| 452 | .eq => return mid, | 463 | .eq => return mid, |
| 453 | .gt => left = mid + 1, | 464 | .lt => low = mid + 1, // item too small |
| 454 | .lt => right = mid, | 465 | .gt => high = mid, // item too big |
| 455 | } | 466 | } |
| 456 | } | 467 | } |
| 457 | | | |
| 458 | return null; | 468 | return null; |
| 459 | } | 469 | } |
| 460 | | 470 | |
| 461 | test binarySearch { | 471 | test binarySearch { |
| 462 | const S = struct { | 472 | const S = struct { |
| 463 | fn order_u32(context: void, lhs: u32, rhs: u32) math.Order { | 473 | fn orderU32(context: u32, item: u32) std.math.Order { |
| 464 | _ = context; | 474 | return std.math.order(item, context); |
| 465 | return math.order(lhs, rhs); | 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 { | 479 | fn orderLength(context: usize, item: []const u8) std.math.Order { |
| 468 | _ = context; | 480 | return std.math.order(item.len, context); |
| 469 | return math.order(lhs, rhs); | | |
| 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 | const R = struct { | 483 | const R = struct { |
| 505 | b: i32, | 484 | b: i32, |
| 506 | e: i32, | 485 | e: i32, |
| 507 | | 486 | |
| 508 | fn r(b: i32, e: i32) @This() { | 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 { | 491 | fn order(context: i32, item: @This()) std.math.Order { |
| 513 | _ = context; | 492 | if (item.e < context) { |
| 514 | | | |
| 515 | if (key < mid_item.b) { | | |
| 516 | return .lt; | 493 | return .lt; |
| 517 | } | 494 | } else if (item.b > context) { |
| 518 | | | |
| 519 | if (key > mid_item.e) { | | |
| 520 | return .gt; | 495 | return .gt; |
| | 496 | } else { |
| | 497 | return .eq; |
| 521 | } | 498 | } |
| 522 | | | |
| 523 | return .eq; | | |
| 524 | } | 499 | } |
| 525 | }; | 500 | }; |
| 526 | try testing.expectEqual( | 501 | |
| 527 | @as(?usize, null), | 502 | try std.testing.expectEqual(null, binarySearch(u32, &[_]u32{}, @as(u32, 1), S.orderU32)); |
| 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), | 503 | try std.testing.expectEqual(0, binarySearch(u32, &[_]u32{1}, @as(u32, 1), S.orderU32)); |
| 529 | ); | 504 | try std.testing.expectEqual(null, binarySearch(u32, &[_]u32{0}, @as(u32, 1), S.orderU32)); |
| 530 | try testing.expectEqual( | 505 | try std.testing.expectEqual(null, binarySearch(u32, &[_]u32{1}, @as(u32, 0), S.orderU32)); |
| 531 | @as(?usize, 2), | 506 | try std.testing.expectEqual(4, binarySearch(u32, &[_]u32{ 1, 2, 3, 4, 5 }, @as(u32, 5), S.orderU32)); |
| 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), | 507 | try std.testing.expectEqual(0, binarySearch(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 2), S.orderU32)); |
| 533 | ); | 508 | try std.testing.expectEqual(1, binarySearch(i32, &[_]i32{ -7, -4, 0, 9, 10 }, @as(i32, -4), S.orderI32)); |
| 534 | try testing.expectEqual( | 509 | try std.testing.expectEqual(3, binarySearch(i32, &[_]i32{ -100, -25, 2, 98, 99, 100 }, @as(i32, 98), S.orderI32)); |
| 535 | @as(?usize, 1), | 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)); |
| 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), | 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)); |
| 537 | ); | 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)); |
| 538 | } | 513 | try std.testing.expectEqual(2, binarySearch([]const u8, &[_][]const u8{ "", "abc", "1234", "vwxyz" }, @as(usize, 4), S.orderLength)); |
| 539 | | 514 | } |
| 540 | /// Returns the index of the first element in `items` greater than or equal to `key`, | 515 | |
| 541 | /// or `items.len` if all elements are less than `key`. | 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 | pub fn lowerBound( | 535 | pub fn lowerBound( |
| 547 | comptime T: type, | 536 | comptime T: type, |
| 548 | key: anytype, | | |
| 549 | items: []const T, | 537 | items: []const T, |
| 550 | context: anytype, | 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 | ) usize { | 540 | ) usize { |
| 553 | var left: usize = 0; | 541 | const S = struct { |
| 554 | var right: usize = items.len; | 542 | fn predicate(ctx: @TypeOf(context), item: T) bool { |
| 555 | | 543 | return compareFn(ctx, item) == .lt; |
| 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; | | |
| 562 | } | 544 | } |
| 563 | } | 545 | }; |
| 564 | | 546 | return partitionPoint(T, items, context, S.predicate); |
| 565 | return left; | | |
| 566 | } | 547 | } |
| 567 | | 548 | |
| 568 | test lowerBound { | 549 | test lowerBound { |
| 569 | const S = struct { | 550 | const S = struct { |
| 570 | fn lower_u32(context: void, lhs: u32, rhs: u32) bool { | 551 | fn compareU32(context: u32, item: u32) std.math.Order { |
| 571 | _ = context; | 552 | return std.math.order(item, context); |
| 572 | return lhs < rhs; | | |
| 573 | } | 553 | } |
| 574 | fn lower_i32(context: void, lhs: i32, rhs: i32) bool { | 554 | fn compareI32(context: i32, item: i32) std.math.Order { |
| 575 | _ = context; | 555 | return std.math.order(item, context); |
| 576 | return lhs < rhs; | | |
| 577 | } | 556 | } |
| 578 | fn lower_f32(context: void, lhs: f32, rhs: f32) bool { | 557 | fn compareF32(context: f32, item: f32) std.math.Order { |
| 579 | _ = context; | 558 | return std.math.order(item, context); |
| 580 | return lhs < rhs; | | |
| 581 | } | 559 | } |
| 582 | }; | 560 | }; |
| | 561 | const R = struct { |
| | 562 | val: i32, |
| 583 | | 563 | |
| 584 | try testing.expectEqual( | 564 | fn r(val: i32) @This() { |
| 585 | @as(usize, 0), | 565 | return .{ .val = val }; |
| 586 | lowerBound(u32, @as(u32, 0), &[_]u32{}, {}, S.lower_u32), | 566 | } |
| 587 | ); | 567 | |
| 588 | try testing.expectEqual( | 568 | fn compareFn(context: i32, item: @This()) std.math.Order { |
| 589 | @as(usize, 0), | 569 | return std.math.order(item.val, context); |
| 590 | lowerBound(u32, @as(u32, 0), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32), | 570 | } |
| 591 | ); | 571 | }; |
| 592 | try testing.expectEqual( | 572 | |
| 593 | @as(usize, 0), | 573 | try std.testing.expectEqual(0, lowerBound(u32, &[_]u32{}, @as(u32, 0), S.compareU32)); |
| 594 | lowerBound(u32, @as(u32, 2), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32), | 574 | try std.testing.expectEqual(0, lowerBound(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 0), S.compareU32)); |
| 595 | ); | 575 | try std.testing.expectEqual(0, lowerBound(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 2), S.compareU32)); |
| 596 | try testing.expectEqual( | 576 | try std.testing.expectEqual(2, lowerBound(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 5), S.compareU32)); |
| 597 | @as(usize, 2), | 577 | try std.testing.expectEqual(2, lowerBound(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 8), S.compareU32)); |
| 598 | lowerBound(u32, @as(u32, 5), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32), | 578 | try std.testing.expectEqual(6, lowerBound(u32, &[_]u32{ 2, 4, 7, 7, 7, 7, 16, 32, 64 }, @as(u32, 8), S.compareU32)); |
| 599 | ); | 579 | try std.testing.expectEqual(2, lowerBound(u32, &[_]u32{ 2, 4, 8, 8, 8, 8, 16, 32, 64 }, @as(u32, 8), S.compareU32)); |
| 600 | try testing.expectEqual( | 580 | try std.testing.expectEqual(5, lowerBound(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 64), S.compareU32)); |
| 601 | @as(usize, 2), | 581 | try std.testing.expectEqual(6, lowerBound(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 100), S.compareU32)); |
| 602 | lowerBound(u32, @as(u32, 8), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32), | 582 | try std.testing.expectEqual(2, lowerBound(i32, &[_]i32{ 2, 4, 8, 16, 32, 64 }, @as(i32, 5), S.compareI32)); |
| 603 | ); | 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)); |
| 604 | try testing.expectEqual( | 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)); |
| 605 | @as(usize, 6), | 585 | } |
| 606 | lowerBound(u32, @as(u32, 8), &[_]u32{ 2, 4, 7, 7, 7, 7, 16, 32, 64 }, {}, S.lower_u32), | 586 | |
| 607 | ); | 587 | /// Returns the index of the first element in `items` returning `.gt` |
| 608 | try testing.expectEqual( | 588 | /// when given to `compareFn`. |
| 609 | @as(usize, 2), | 589 | /// - Returns `items.len` if none of the elements return `.gt`. |
| 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`. | | |
| 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 | pub fn upperBound( | 606 | pub fn upperBound( |
| 637 | comptime T: type, | 607 | comptime T: type, |
| 638 | key: anytype, | | |
| 639 | items: []const T, | 608 | items: []const T, |
| 640 | context: anytype, | 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 | ) usize { | 611 | ) usize { |
| 643 | var left: usize = 0; | 612 | const S = struct { |
| 644 | var right: usize = items.len; | 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) { | 620 | test upperBound { |
| 647 | const mid = left + (right - left) / 2; | 621 | const S = struct { |
| 648 | if (!lessThan(context, key, items[mid])) { | 622 | fn compareU32(context: u32, item: u32) std.math.Order { |
| 649 | left = mid + 1; | 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 | } else { | 690 | } else { |
| 651 | right = mid; | 691 | high = mid; |
| 652 | } | 692 | } |
| 653 | } | 693 | } |
| 654 | | 694 | return low; |
| 655 | return left; | | |
| 656 | } | 695 | } |
| 657 | | 696 | |
| 658 | test upperBound { | 697 | test partitionPoint { |
| 659 | const S = struct { | 698 | const S = struct { |
| 660 | fn lower_u32(context: void, lhs: u32, rhs: u32) bool { | 699 | fn lowerU32(context: u32, item: u32) bool { |
| 661 | _ = context; | 700 | return item < context; |
| 662 | return lhs < rhs; | 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 { | 714 | fn lowerEqF32(context: f32, item: f32) bool { |
| 665 | _ = context; | 715 | return item <= context; |
| 666 | return lhs < rhs; | | |
| 667 | } | 716 | } |
| 668 | fn lower_f32(context: void, lhs: f32, rhs: f32) bool { | 717 | fn isEven(_: void, item: u8) bool { |
| 669 | _ = context; | 718 | return item % 2 == 0; |
| 670 | return lhs < rhs; | | |
| 671 | } | 719 | } |
| 672 | }; | 720 | }; |
| 673 | | 721 | |
| 674 | try testing.expectEqual( | 722 | try std.testing.expectEqual(0, partitionPoint(u32, &[_]u32{}, @as(u32, 0), S.lowerU32)); |
| 675 | @as(usize, 0), | 723 | try std.testing.expectEqual(0, partitionPoint(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 0), S.lowerU32)); |
| 676 | upperBound(u32, @as(u32, 0), &[_]u32{}, {}, S.lower_u32), | 724 | try std.testing.expectEqual(0, partitionPoint(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 2), S.lowerU32)); |
| 677 | ); | 725 | try std.testing.expectEqual(2, partitionPoint(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 5), S.lowerU32)); |
| 678 | try testing.expectEqual( | 726 | try std.testing.expectEqual(2, partitionPoint(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 8), S.lowerU32)); |
| 679 | @as(usize, 0), | 727 | try std.testing.expectEqual(6, partitionPoint(u32, &[_]u32{ 2, 4, 7, 7, 7, 7, 16, 32, 64 }, @as(u32, 8), S.lowerU32)); |
| 680 | upperBound(u32, @as(u32, 0), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32), | 728 | try std.testing.expectEqual(2, partitionPoint(u32, &[_]u32{ 2, 4, 8, 8, 8, 8, 16, 32, 64 }, @as(u32, 8), S.lowerU32)); |
| 681 | ); | 729 | try std.testing.expectEqual(5, partitionPoint(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 64), S.lowerU32)); |
| 682 | try testing.expectEqual( | 730 | try std.testing.expectEqual(6, partitionPoint(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 100), S.lowerU32)); |
| 683 | @as(usize, 1), | 731 | try std.testing.expectEqual(2, partitionPoint(i32, &[_]i32{ 2, 4, 8, 16, 32, 64 }, @as(i32, 5), S.lowerI32)); |
| 684 | upperBound(u32, @as(u32, 2), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32), | 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)); |
| 685 | ); | 733 | try std.testing.expectEqual(0, partitionPoint(u32, &[_]u32{}, @as(u32, 0), S.lowerEqU32)); |
| 686 | try testing.expectEqual( | 734 | try std.testing.expectEqual(0, partitionPoint(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 0), S.lowerEqU32)); |
| 687 | @as(usize, 2), | 735 | try std.testing.expectEqual(1, partitionPoint(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 2), S.lowerEqU32)); |
| 688 | upperBound(u32, @as(u32, 5), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32), | 736 | try std.testing.expectEqual(2, partitionPoint(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 5), S.lowerEqU32)); |
| 689 | ); | 737 | try std.testing.expectEqual(6, partitionPoint(u32, &[_]u32{ 2, 4, 7, 7, 7, 7, 16, 32, 64 }, @as(u32, 8), S.lowerEqU32)); |
| 690 | try testing.expectEqual( | 738 | try std.testing.expectEqual(6, partitionPoint(u32, &[_]u32{ 2, 4, 8, 8, 8, 8, 16, 32, 64 }, @as(u32, 8), S.lowerEqU32)); |
| 691 | @as(usize, 6), | 739 | try std.testing.expectEqual(3, partitionPoint(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 8), S.lowerEqU32)); |
| 692 | upperBound(u32, @as(u32, 8), &[_]u32{ 2, 4, 7, 7, 7, 7, 16, 32, 64 }, {}, S.lower_u32), | 740 | try std.testing.expectEqual(6, partitionPoint(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 64), S.lowerEqU32)); |
| 693 | ); | 741 | try std.testing.expectEqual(6, partitionPoint(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 100), S.lowerEqU32)); |
| 694 | try testing.expectEqual( | 742 | try std.testing.expectEqual(2, partitionPoint(i32, &[_]i32{ 2, 4, 8, 16, 32, 64 }, @as(i32, 5), S.lowerEqI32)); |
| 695 | @as(usize, 6), | 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)); |
| 696 | upperBound(u32, @as(u32, 8), &[_]u32{ 2, 4, 8, 8, 8, 8, 16, 32, 64 }, {}, S.lower_u32), | 744 | try std.testing.expectEqual(4, partitionPoint(u8, &[_]u8{ 0, 50, 14, 2, 5, 71 }, {}, S.isEven)); |
| 697 | ); | 745 | } |
| 698 | try testing.expectEqual( | 746 | |
| 699 | @as(usize, 3), | 747 | /// Returns a tuple of the lower and upper indices in `items` between which all |
| 700 | upperBound(u32, @as(u32, 8), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32), | 748 | /// elements return `.eq` when given to `compareFn`. |
| 701 | ); | 749 | /// - If no element in `items` returns `.eq`, both indices are the |
| 702 | try testing.expectEqual( | 750 | /// index of the first element in `items` returning `.gt`. |
| 703 | @as(usize, 6), | 751 | /// - If no element in `items` returns `.gt`, both indices equal `items.len`. |
| 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`. | | |
| 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 | pub fn equalRange( | 768 | pub fn equalRange( |
| 731 | comptime T: type, | 769 | comptime T: type, |
| 732 | key: anytype, | | |
| 733 | items: []const T, | 770 | items: []const T, |
| 734 | context: anytype, | 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 | ) struct { usize, usize } { | 773 | ) struct { usize, usize } { |
| 737 | return .{ | 774 | return .{ |
| 738 | lowerBound(T, key, items, context, lessThan), | 775 | lowerBound(T, items, context, compareFn), |
| 739 | upperBound(T, key, items, context, lessThan), | 776 | upperBound(T, items, context, compareFn), |
| 740 | }; | 777 | }; |
| 741 | } | 778 | } |
| 742 | | 779 | |
| 743 | test equalRange { | 780 | test equalRange { |
| 744 | const S = struct { | 781 | const S = struct { |
| 745 | fn lower_u32(context: void, lhs: u32, rhs: u32) bool { | 782 | fn orderU32(context: u32, item: u32) std.math.Order { |
| 746 | _ = context; | 783 | return std.math.order(item, context); |
| 747 | return lhs < rhs; | | |
| 748 | } | 784 | } |
| 749 | fn lower_i32(context: void, lhs: i32, rhs: i32) bool { | 785 | fn orderI32(context: i32, item: i32) std.math.Order { |
| 750 | _ = context; | 786 | return std.math.order(item, context); |
| 751 | return lhs < rhs; | | |
| 752 | } | 787 | } |
| 753 | fn lower_f32(context: void, lhs: f32, rhs: f32) bool { | 788 | fn orderF32(context: f32, item: f32) std.math.Order { |
| 754 | _ = context; | 789 | return std.math.order(item, context); |
| 755 | return lhs < rhs; | 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( | 796 | try std.testing.expectEqual(.{ 0, 0 }, equalRange(i32, &[_]i32{}, @as(i32, 0), S.orderI32)); |
| 760 | @as(struct { usize, usize }, .{ 0, 0 }), | 797 | try std.testing.expectEqual(.{ 0, 0 }, equalRange(i32, &[_]i32{ 2, 4, 8, 16, 32, 64 }, @as(i32, 0), S.orderI32)); |
| 761 | equalRange(i32, @as(i32, 0), &[_]i32{}, {}, S.lower_i32), | 798 | try std.testing.expectEqual(.{ 0, 1 }, equalRange(i32, &[_]i32{ 2, 4, 8, 16, 32, 64 }, @as(i32, 2), S.orderI32)); |
| 762 | ); | 799 | try std.testing.expectEqual(.{ 2, 2 }, equalRange(i32, &[_]i32{ 2, 4, 8, 16, 32, 64 }, @as(i32, 5), S.orderI32)); |
| 763 | try testing.expectEqual( | 800 | try std.testing.expectEqual(.{ 2, 3 }, equalRange(i32, &[_]i32{ 2, 4, 8, 16, 32, 64 }, @as(i32, 8), S.orderI32)); |
| 764 | @as(struct { usize, usize }, .{ 0, 0 }), | 801 | try std.testing.expectEqual(.{ 5, 6 }, equalRange(i32, &[_]i32{ 2, 4, 8, 16, 32, 64 }, @as(i32, 64), S.orderI32)); |
| 765 | equalRange(i32, @as(i32, 0), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32), | 802 | try std.testing.expectEqual(.{ 6, 6 }, equalRange(i32, &[_]i32{ 2, 4, 8, 16, 32, 64 }, @as(i32, 100), S.orderI32)); |
| 766 | ); | 803 | try std.testing.expectEqual(.{ 2, 6 }, equalRange(i32, &[_]i32{ 2, 4, 8, 8, 8, 8, 15, 22 }, @as(i32, 8), S.orderI32)); |
| 767 | try testing.expectEqual( | 804 | try std.testing.expectEqual(.{ 2, 2 }, equalRange(u32, &[_]u32{ 2, 4, 8, 16, 32, 64 }, @as(u32, 5), S.orderU32)); |
| 768 | @as(struct { usize, usize }, .{ 0, 1 }), | 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)); |
| 769 | equalRange(i32, @as(i32, 2), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32), | 806 | try std.testing.expectEqual(.{ 3, 5 }, equalRange( |
| 770 | ); | 807 | []const u8, |
| 771 | try testing.expectEqual( | 808 | &[_][]const u8{ "Mars", "Venus", "Earth", "Saturn", "Uranus", "Mercury", "Jupiter", "Neptune" }, |
| 772 | @as(struct { usize, usize }, .{ 2, 2 }), | 809 | @as(usize, 6), |
| 773 | equalRange(i32, @as(i32, 5), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32), | 810 | S.orderLength, |
| 774 | ); | 811 | )); |
| 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 | ); | | |
| 799 | } | 812 | } |
| 800 | | 813 | |
| 801 | pub fn argMin( | 814 | pub fn argMin( |