authorgravatar for john.schmidt.h@gmail.comJohn Schmidt <john.schmidt.h@gmail.com> 2024-01-27 22:43:35+01:00
committergravatar for john.schmidt.h@gmail.comJohn Schmidt <john.schmidt.h@gmail.com> 2024-02-07 21:00:24+01:00
loge487b576fadc2fd48100667ff233e4a0bb4f0aa5
tree27084584f2dd67cb33f5903d2c950f5149f6ce04
parent664c18544c2622352a4e38254496a349006d331b

Changes to lowerBound/upperBound/equalRange

The old definitions had some problems: - In `lowerBound`, the `lhs` (left hand side) argument was passed on the right hand side. - In `upperBound`, the `greaterThan` function needed to return `greaterThanOrEqual` for the function work, so either the name or the implementation is incorrect. To fix both problems, define the functions in terms of a `lessThan` function that returns `lhs < rhs`. The is more consistent with the rest of `sort.zig` and it's also how C++ implements lower/upperBound (1)(2). (1) https://en.cppreference.com/w/cpp/algorithm/lower_bound (2) https://en.cppreference.com/w/cpp/algorithm/upper_bound - Rewrite doc comments. - Add a couple of more test cases. - Add docstring for std.sort.binarySearch

1 files changed, 108 insertions(+), 124 deletions(-)

lib/std/sort.zig+108-124
...@@ -399,6 +399,13 @@ test "sort fuzz testing" {...@@ -399,6 +399,13 @@ test "sort fuzz testing" {
399 }399 }
400}400}
401401
402/// Returns the index of an element in `items` equal to `key`.
403/// If there are multiple such elements, returns the index of any one of them.
404/// If there are no such elements, returns `null`.
405///
406/// `items` must be sorted in ascending order with respect to `compareFn`.
407///
408/// O(log n) complexity.
402pub fn binarySearch(409pub fn binarySearch(
403 comptime T: type,410 comptime T: type,
404 key: anytype,411 key: anytype,
...@@ -502,25 +509,25 @@ test "binarySearch" {...@@ -502,25 +509,25 @@ test "binarySearch" {
502 );509 );
503}510}
504511
505/// Returns the index pointing to the first element in the range [first,last)512/// Returns the index of the first element in `items` greater than or equal to `key`,
506/// which does not compare less than val.513/// or `items.len` if all elements are less than `key`.
507/// The function optimizes the number of comparisons performed by using a binary search O(log n).514///
508/// An example lessThan function:515/// `items` must be sorted in ascending order with respect to `compareFn`.
509/// fn lower_u32(_: void, key: u32, lhs: u32) bool { return lhs < key; }516///
517/// O(log n) complexity.
510pub fn lowerBound(518pub fn lowerBound(
511 comptime T: type,519 comptime T: type,
512 key: anytype,520 key: anytype,
513 items: []const T,521 items: []const T,
514 context: anytype,522 context: anytype,
515 comptime lessThan: fn (context: @TypeOf(context), key: @TypeOf(key), lhs: T) bool,523 comptime lessThan: fn (context: @TypeOf(context), lhs: @TypeOf(key), rhs: T) bool,
516) usize {524) usize {
517 var left: usize = 0;525 var left: usize = 0;
518 var right: usize = items.len;526 var right: usize = items.len;
519 var mid: usize = undefined;
520527
521 while (left < right) {528 while (left < right) {
522 mid = left + (right - left) / 2;529 const mid = left + (right - left) / 2;
523 if (lessThan(context, key, items[mid])) {530 if (lessThan(context, items[mid], key)) {
524 left = mid + 1;531 left = mid + 1;
525 } else {532 } else {
526 right = mid;533 right = mid;
...@@ -532,87 +539,85 @@ pub fn lowerBound(...@@ -532,87 +539,85 @@ pub fn lowerBound(
532539
533test "lowerBound" {540test "lowerBound" {
534 const S = struct {541 const S = struct {
535 fn lower_u32(context: void, key: u32, lhs: u32) bool {542 fn lower_u32(context: void, lhs: u32, rhs: u32) bool {
536 _ = context;543 _ = context;
537 return lhs < key;544 return lhs < rhs;
538 }545 }
539 fn lower_i32(context: void, key: i32, lhs: i32) bool {546 fn lower_i32(context: void, lhs: i32, rhs: i32) bool {
540 _ = context;547 _ = context;
541 return lhs < key;548 return lhs < rhs;
542 }549 }
543 fn lower_f32(context: void, key: f32, lhs: f32) bool {550 fn lower_f32(context: void, lhs: f32, rhs: f32) bool {
544 _ = context;551 _ = context;
545 return lhs < key;552 return lhs < rhs;
546 }553 }
547 };554 };
548555
549 // u32
550 // test no data
551 try testing.expectEqual(556 try testing.expectEqual(
552 @as(?usize, 0),557 @as(usize, 0),
553 lowerBound(u32, @as(u32, 0), &[_]u32{}, {}, S.lower_u32),558 lowerBound(u32, @as(u32, 0), &[_]u32{}, {}, S.lower_u32),
554 );559 );
555 // test below the first element
556 try testing.expectEqual(560 try testing.expectEqual(
557 @as(?usize, 0),561 @as(usize, 0),
558 lowerBound(u32, @as(u32, 0), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32),562 lowerBound(u32, @as(u32, 0), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32),
559 );563 );
560 // test equal to the first element
561 try testing.expectEqual(564 try testing.expectEqual(
562 @as(?usize, 0),565 @as(usize, 0),
563 lowerBound(u32, @as(u32, 2), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32),566 lowerBound(u32, @as(u32, 2), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32),
564 );567 );
565 // test between two numbers
566 try testing.expectEqual(568 try testing.expectEqual(
567 @as(?usize, 2),569 @as(usize, 2),
568 lowerBound(u32, @as(u32, 5), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32),570 lowerBound(u32, @as(u32, 5), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32),
569 );571 );
570 // test equal to a number not at the ends
571 try testing.expectEqual(572 try testing.expectEqual(
572 @as(?usize, 2),573 @as(usize, 2),
573 lowerBound(u32, @as(u32, 8), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32),574 lowerBound(u32, @as(u32, 8), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32),
574 );575 );
575 // test equal to the last element
576 try testing.expectEqual(576 try testing.expectEqual(
577 @as(?usize, 5),577 @as(usize, 6),
578 lowerBound(u32, @as(u32, 8), &[_]u32{ 2, 4, 7, 7, 7, 7, 16, 32, 64 }, {}, S.lower_u32),
579 );
580 try testing.expectEqual(
581 @as(usize, 2),
582 lowerBound(u32, @as(u32, 8), &[_]u32{ 2, 4, 8, 8, 8, 8, 16, 32, 64 }, {}, S.lower_u32),
583 );
584 try testing.expectEqual(
585 @as(usize, 5),
578 lowerBound(u32, @as(u32, 64), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32),586 lowerBound(u32, @as(u32, 64), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32),
579 );587 );
580 // test above the last element
581 try testing.expectEqual(588 try testing.expectEqual(
582 @as(?usize, 6),589 @as(usize, 6),
583 lowerBound(u32, @as(u32, 100), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32),590 lowerBound(u32, @as(u32, 100), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32),
584 );591 );
585 // i32
586 try testing.expectEqual(592 try testing.expectEqual(
587 @as(?usize, 2),593 @as(usize, 2),
588 lowerBound(i32, @as(i32, 5), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32),594 lowerBound(i32, @as(i32, 5), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32),
589 );595 );
590 // f32
591 try testing.expectEqual(596 try testing.expectEqual(
592 @as(?usize, 1),597 @as(usize, 1),
593 lowerBound(f32, @as(f32, -33.4), &[_]f32{ -54.2, -26.7, 0.0, 56.55, 100.1, 322.0 }, {}, S.lower_f32),598 lowerBound(f32, @as(f32, -33.4), &[_]f32{ -54.2, -26.7, 0.0, 56.55, 100.1, 322.0 }, {}, S.lower_f32),
594 );599 );
595}600}
596601
597/// Returns the index pointing to the first element in the range [first,last)602/// Returns the index of the first element in `items` greater than `key`,
598/// which compares greater than val.603/// or `items.len` if all elements are less than or equal to `key`.
599/// The function optimizes the number of comparisons performed by using a binary search O(log n).604///
600/// An example greaterThan function:605/// `items` must be sorted in ascending order with respect to `compareFn`.
601/// fn upper_u32(_: void, key: u32, rhs: u32) bool { return key >= rhs; }606///
607/// O(log n) complexity.
602pub fn upperBound(608pub fn upperBound(
603 comptime T: type,609 comptime T: type,
604 key: anytype,610 key: anytype,
605 items: []const T,611 items: []const T,
606 context: anytype,612 context: anytype,
607 comptime greaterThan: fn (context: @TypeOf(context), key: @TypeOf(key), rhs: T) bool,613 comptime lessThan: fn (context: @TypeOf(context), lhs: @TypeOf(key), rhs: T) bool,
608) usize {614) usize {
609 var left: usize = 0;615 var left: usize = 0;
610 var right: usize = items.len;616 var right: usize = items.len;
611 var mid: usize = undefined;
612617
613 while (left < right) {618 while (left < right) {
614 mid = (right + left) / 2;619 const mid = (right + left) / 2;
615 if (greaterThan(context, key, items[mid])) {620 if (!lessThan(context, key, items[mid])) {
616 left = mid + 1;621 left = mid + 1;
617 } else {622 } else {
618 right = mid;623 right = mid;
...@@ -624,165 +629,144 @@ pub fn upperBound(...@@ -624,165 +629,144 @@ pub fn upperBound(
624629
625test "upperBound" {630test "upperBound" {
626 const S = struct {631 const S = struct {
627 fn upper_u32(context: void, key: u32, rhs: u32) bool {632 fn lower_u32(context: void, lhs: u32, rhs: u32) bool {
628 _ = context;633 _ = context;
629 return key >= rhs;634 return lhs < rhs;
630 }635 }
631 fn upper_i32(context: void, key: i32, rhs: i32) bool {636 fn lower_i32(context: void, lhs: i32, rhs: i32) bool {
632 _ = context;637 _ = context;
633 return key >= rhs;638 return lhs < rhs;
634 }639 }
635 fn upper_f32(context: void, key: f32, rhs: f32) bool {640 fn lower_f32(context: void, lhs: f32, rhs: f32) bool {
636 _ = context;641 _ = context;
637 return key >= rhs;642 return lhs < rhs;
638 }643 }
639 };644 };
640645
641 // u32
642 // test no data
643 try testing.expectEqual(646 try testing.expectEqual(
644 @as(?usize, 0),647 @as(usize, 0),
645 upperBound(u32, @as(u32, 0), &[_]u32{}, {}, S.upper_u32),648 upperBound(u32, @as(u32, 0), &[_]u32{}, {}, S.lower_u32),
646 );649 );
647 // test below the first element
648 try testing.expectEqual(650 try testing.expectEqual(
649 @as(?usize, 0),651 @as(usize, 0),
650 upperBound(u32, @as(u32, 0), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.upper_u32),652 upperBound(u32, @as(u32, 0), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32),
651 );653 );
652 // test equal to the first element
653 try testing.expectEqual(654 try testing.expectEqual(
654 @as(?usize, 1),655 @as(usize, 1),
655 upperBound(u32, @as(u32, 2), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.upper_u32),656 upperBound(u32, @as(u32, 2), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32),
656 );657 );
657 // test between two numbers
658 try testing.expectEqual(658 try testing.expectEqual(
659 @as(?usize, 2),659 @as(usize, 2),
660 upperBound(u32, @as(u32, 5), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.upper_u32),660 upperBound(u32, @as(u32, 5), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32),
661 );661 );
662 // test equal to a number not at the ends
663 try testing.expectEqual(662 try testing.expectEqual(
664 @as(?usize, 3),663 @as(usize, 6),
665 upperBound(u32, @as(u32, 8), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.upper_u32),664 upperBound(u32, @as(u32, 8), &[_]u32{ 2, 4, 7, 7, 7, 7, 16, 32, 64 }, {}, S.lower_u32),
666 );665 );
667 // test equal to the last element
668 try testing.expectEqual(666 try testing.expectEqual(
669 @as(?usize, 6),667 @as(usize, 6),
670 upperBound(u32, @as(u32, 64), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.upper_u32),668 upperBound(u32, @as(u32, 8), &[_]u32{ 2, 4, 8, 8, 8, 8, 16, 32, 64 }, {}, S.lower_u32),
671 );669 );
672 // test above the last element
673 try testing.expectEqual(670 try testing.expectEqual(
674 @as(?usize, 6),671 @as(usize, 3),
675 upperBound(u32, @as(u32, 100), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.upper_u32),672 upperBound(u32, @as(u32, 8), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32),
676 );673 );
677 // i32
678 try testing.expectEqual(674 try testing.expectEqual(
679 @as(?usize, 2),675 @as(usize, 6),
680 upperBound(i32, @as(i32, 5), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.upper_i32),676 upperBound(u32, @as(u32, 64), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32),
681 );677 );
682 // f32
683 try testing.expectEqual(678 try testing.expectEqual(
684 @as(?usize, 1),679 @as(usize, 6),
685 upperBound(f32, @as(f32, -33.4), &[_]f32{ -54.2, -26.7, 0.0, 56.55, 100.1, 322.0 }, {}, S.upper_f32),680 upperBound(u32, @as(u32, 100), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32),
681 );
682 try testing.expectEqual(
683 @as(usize, 2),
684 upperBound(i32, @as(i32, 5), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32),
685 );
686 try testing.expectEqual(
687 @as(usize, 1),
688 upperBound(f32, @as(f32, -33.4), &[_]f32{ -54.2, -26.7, 0.0, 56.55, 100.1, 322.0 }, {}, S.lower_f32),
686 );689 );
687}690}
688691
689/// Returns a range containing all elements equivalent to value in the range [first,last)692/// Returns a tuple of the lower and upper indices in `items` between which all elements are equal to `key`.
690/// The function optimizes the number of comparisons performed by using a binary search O(2 * log n).693/// If no element in `items` is equal to `key`, both indices are the
691/// Example lessThan & greaterThan functions:694/// index of the first element in `items` greater than `key`.
692/// fn lower_u32(_: void, key: u32, lhs: u32) bool { return lhs < key; }695/// If no element in `items` is greater than `key`, both indices equal `items.len`.
693/// fn upper_u32(_: void, key: u32, rhs: u32) bool { return key >= rhs; }696///
697/// `items` must be sorted in ascending order with respect to `compareFn`.
698///
699/// O(log n) complexity.
700///
701/// See also: `lowerBound` and `upperBound`.
694pub fn equalRange(702pub fn equalRange(
695 comptime T: type,703 comptime T: type,
696 key: anytype,704 key: anytype,
697 items: []const T,705 items: []const T,
698 context: anytype,706 context: anytype,
699 comptime lessThan: fn (context: @TypeOf(context), key: @TypeOf(key), lhs: T) bool,707 comptime lessThan: fn (context: @TypeOf(context), lhs: @TypeOf(key), rhs: T) bool,
700 comptime greaterThan: fn (context: @TypeOf(context), key: @TypeOf(key), rhs: T) bool,
701) struct { usize, usize } {708) struct { usize, usize } {
702 return .{709 return .{
703 lowerBound(T, key, items, context, lessThan),710 lowerBound(T, key, items, context, lessThan),
704 upperBound(T, key, items, context, greaterThan),711 upperBound(T, key, items, context, lessThan),
705 };712 };
706}713}
707714
708test "equalRange" {715test "equalRange" {
709 const S = struct {716 const S = struct {
710 fn lower_i32(context: void, key: i32, lhs: i32) bool {717 fn lower_u32(context: void, lhs: u32, rhs: u32) bool {
711 _ = context;
712 return lhs < key;
713 }
714 fn upper_i32(context: void, key: i32, rhs: i32) bool {
715 _ = context;
716 return key >= rhs;
717 }
718 fn lower_u32(context: void, key: u32, lhs: u32) bool {
719 _ = context;
720 return lhs < key;
721 }
722 fn upper_u32(context: void, key: u32, rhs: u32) bool {
723 _ = context;718 _ = context;
724 return key >= rhs;719 return lhs < rhs;
725 }720 }
726 fn lower_f32(context: void, key: f32, lhs: f32) bool {721 fn lower_i32(context: void, lhs: i32, rhs: i32) bool {
727 _ = context;722 _ = context;
728 return lhs < key;723 return lhs < rhs;
729 }724 }
730 fn upper_f32(context: void, key: f32, rhs: f32) bool {725 fn lower_f32(context: void, lhs: f32, rhs: f32) bool {
731 _ = context;726 _ = context;
732 return key >= rhs;727 return lhs < rhs;
733 }728 }
734 };729 };
735730
736 // i32
737 // test no data
738 try testing.expectEqual(731 try testing.expectEqual(
739 @as(struct { usize, usize }, .{ 0, 0 }),732 @as(struct { usize, usize }, .{ 0, 0 }),
740 equalRange(i32, @as(i32, 0), &[_]i32{}, {}, S.lower_i32, S.upper_i32),733 equalRange(i32, @as(i32, 0), &[_]i32{}, {}, S.lower_i32),
741 );734 );
742 // test below the first element
743 try testing.expectEqual(735 try testing.expectEqual(
744 @as(struct { usize, usize }, .{ 0, 0 }),736 @as(struct { usize, usize }, .{ 0, 0 }),
745 equalRange(i32, @as(i32, 0), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32, S.upper_i32),737 equalRange(i32, @as(i32, 0), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32),
746 );738 );
747 // test equal to the first element
748 try testing.expectEqual(739 try testing.expectEqual(
749 @as(struct { usize, usize }, .{ 0, 1 }),740 @as(struct { usize, usize }, .{ 0, 1 }),
750 equalRange(i32, @as(i32, 2), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32, S.upper_i32),741 equalRange(i32, @as(i32, 2), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32),
751 );742 );
752 // test between two numbers
753 try testing.expectEqual(743 try testing.expectEqual(
754 @as(struct { usize, usize }, .{ 2, 2 }),744 @as(struct { usize, usize }, .{ 2, 2 }),
755 equalRange(i32, @as(i32, 5), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32, S.upper_i32),745 equalRange(i32, @as(i32, 5), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32),
756 );746 );
757 // test equal to a number not at the ends
758 try testing.expectEqual(747 try testing.expectEqual(
759 @as(struct { usize, usize }, .{ 2, 3 }),748 @as(struct { usize, usize }, .{ 2, 3 }),
760 equalRange(i32, @as(i32, 8), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32, S.upper_i32),749 equalRange(i32, @as(i32, 8), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32),
761 );750 );
762 // test equal to the last element
763 try testing.expectEqual(751 try testing.expectEqual(
764 @as(struct { usize, usize }, .{ 5, 6 }),752 @as(struct { usize, usize }, .{ 5, 6 }),
765 equalRange(i32, @as(i32, 64), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32, S.upper_i32),753 equalRange(i32, @as(i32, 64), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32),
766 );754 );
767 // test above the last element
768 try testing.expectEqual(755 try testing.expectEqual(
769 @as(struct { usize, usize }, .{ 6, 6 }),756 @as(struct { usize, usize }, .{ 6, 6 }),
770 equalRange(i32, @as(i32, 100), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32, S.upper_i32),757 equalRange(i32, @as(i32, 100), &[_]i32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_i32),
771 );758 );
772 // test many of the same element
773 try testing.expectEqual(759 try testing.expectEqual(
774 @as(struct { usize, usize }, .{ 2, 6 }),760 @as(struct { usize, usize }, .{ 2, 6 }),
775 equalRange(i32, @as(i32, 8), &[_]i32{ 2, 4, 8, 8, 8, 8, 15, 22 }, {}, S.lower_i32, S.upper_i32),761 equalRange(i32, @as(i32, 8), &[_]i32{ 2, 4, 8, 8, 8, 8, 15, 22 }, {}, S.lower_i32),
776 );762 );
777 // u32
778 try testing.expectEqual(763 try testing.expectEqual(
779 @as(struct { usize, usize }, .{ 2, 2 }),764 @as(struct { usize, usize }, .{ 2, 2 }),
780 equalRange(u32, @as(u32, 5), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32, S.upper_u32),765 equalRange(u32, @as(u32, 5), &[_]u32{ 2, 4, 8, 16, 32, 64 }, {}, S.lower_u32),
781 );766 );
782 // f32
783 try testing.expectEqual(767 try testing.expectEqual(
784 @as(struct { usize, usize }, .{ 1, 1 }),768 @as(struct { usize, usize }, .{ 1, 1 }),
785 equalRange(f32, @as(f32, -33.4), &[_]f32{ -54.2, -26.7, 0.0, 56.55, 100.1, 322.0 }, {}, S.lower_f32, S.upper_f32),769 equalRange(f32, @as(f32, -33.4), &[_]f32{ -54.2, -26.7, 0.0, 56.55, 100.1, 322.0 }, {}, S.lower_f32),
786 );770 );
787}771}
788772