| ... | @@ -461,8 +461,8 @@ pub fn binarySearch( | ... | @@ -461,8 +461,8 @@ pub fn binarySearch( |
| 461 | const mid = low + (high - low) / 2; | 461 | const mid = low + (high - low) / 2; |
| 462 | switch (compareFn(context, items[mid])) { | 462 | switch (compareFn(context, items[mid])) { |
| 463 | .eq => return mid, | 463 | .eq => return mid, |
| 464 | .lt => low = mid + 1, // item too small | 464 | .gt => low = mid + 1, |
| 465 | .gt => high = mid, // item too big | 465 | .lt => high = mid, |
| 466 | } | 466 | } |
| 467 | } | 467 | } |
| 468 | return null; | 468 | return null; |
| ... | @@ -471,13 +471,13 @@ pub fn binarySearch( | ... | @@ -471,13 +471,13 @@ pub fn binarySearch( |
| 471 | test binarySearch { | 471 | test binarySearch { |
| 472 | const S = struct { | 472 | const S = struct { |
| 473 | fn orderU32(context: u32, item: u32) std.math.Order { | 473 | fn orderU32(context: u32, item: u32) std.math.Order { |
| 474 | return std.math.order(item, context); | 474 | return std.math.order(context, item); |
| 475 | } | 475 | } |
| 476 | fn orderI32(context: i32, item: i32) std.math.Order { | 476 | fn orderI32(context: i32, item: i32) std.math.Order { |
| 477 | return std.math.order(item, context); | 477 | return std.math.order(context, item); |
| 478 | } | 478 | } |
| 479 | fn orderLength(context: usize, item: []const u8) std.math.Order { | 479 | fn orderLength(context: usize, item: []const u8) std.math.Order { |
| 480 | return std.math.order(item.len, context); | 480 | return std.math.order(context, item.len); |
| 481 | } | 481 | } |
| 482 | }; | 482 | }; |
| 483 | const R = struct { | 483 | const R = struct { |
| ... | @@ -489,9 +489,9 @@ test binarySearch { | ... | @@ -489,9 +489,9 @@ test binarySearch { |
| 489 | } | 489 | } |
| 490 | | 490 | |
| 491 | fn order(context: i32, item: @This()) std.math.Order { | 491 | fn order(context: i32, item: @This()) std.math.Order { |
| 492 | if (item.e < context) { | 492 | if (context < item.b) { |
| 493 | return .lt; | 493 | return .lt; |
| 494 | } else if (item.b > context) { | 494 | } else if (context > item.e) { |
| 495 | return .gt; | 495 | return .gt; |
| 496 | } else { | 496 | } else { |
| 497 | return .eq; | 497 | return .eq; |
| ... | @@ -513,9 +513,8 @@ test binarySearch { | ... | @@ -513,9 +513,8 @@ test binarySearch { |
| 513 | try std.testing.expectEqual(2, binarySearch([]const u8, &[_][]const u8{ "", "abc", "1234", "vwxyz" }, @as(usize, 4), S.orderLength)); | 513 | try std.testing.expectEqual(2, binarySearch([]const u8, &[_][]const u8{ "", "abc", "1234", "vwxyz" }, @as(usize, 4), S.orderLength)); |
| 514 | } | 514 | } |
| 515 | | 515 | |
| 516 | /// Returns the index of the first element in `items` returning `.eq` or `.gt` | 516 | /// Returns the index of the first element in `items` that is greater than or equal to `context`, |
| 517 | /// when given to `compareFn`. | 517 | /// as determined by `compareFn`. If no such element exists, returns `items.len`. |
| 518 | /// - Returns `items.len` if all elements return `.lt`. | | |
| 519 | /// | 518 | /// |
| 520 | /// `items` must be sorted in ascending order with respect to `compareFn`: | 519 | /// `items` must be sorted in ascending order with respect to `compareFn`: |
| 521 | /// ``` | 520 | /// ``` |
| ... | @@ -540,7 +539,7 @@ pub fn lowerBound( | ... | @@ -540,7 +539,7 @@ pub fn lowerBound( |
| 540 | ) usize { | 539 | ) usize { |
| 541 | const S = struct { | 540 | const S = struct { |
| 542 | fn predicate(ctx: @TypeOf(context), item: T) bool { | 541 | fn predicate(ctx: @TypeOf(context), item: T) bool { |
| 543 | return compareFn(ctx, item) == .lt; | 542 | return compareFn(ctx, item).invert() == .lt; |
| 544 | } | 543 | } |
| 545 | }; | 544 | }; |
| 546 | return partitionPoint(T, items, context, S.predicate); | 545 | return partitionPoint(T, items, context, S.predicate); |
| ... | @@ -549,13 +548,13 @@ pub fn lowerBound( | ... | @@ -549,13 +548,13 @@ pub fn lowerBound( |
| 549 | test lowerBound { | 548 | test lowerBound { |
| 550 | const S = struct { | 549 | const S = struct { |
| 551 | fn compareU32(context: u32, item: u32) std.math.Order { | 550 | fn compareU32(context: u32, item: u32) std.math.Order { |
| 552 | return std.math.order(item, context); | 551 | return std.math.order(context, item); |
| 553 | } | 552 | } |
| 554 | fn compareI32(context: i32, item: i32) std.math.Order { | 553 | fn compareI32(context: i32, item: i32) std.math.Order { |
| 555 | return std.math.order(item, context); | 554 | return std.math.order(context, item); |
| 556 | } | 555 | } |
| 557 | fn compareF32(context: f32, item: f32) std.math.Order { | 556 | fn compareF32(context: f32, item: f32) std.math.Order { |
| 558 | return std.math.order(item, context); | 557 | return std.math.order(context, item); |
| 559 | } | 558 | } |
| 560 | }; | 559 | }; |
| 561 | const R = struct { | 560 | const R = struct { |
| ... | @@ -566,7 +565,7 @@ test lowerBound { | ... | @@ -566,7 +565,7 @@ test lowerBound { |
| 566 | } | 565 | } |
| 567 | | 566 | |
| 568 | fn compareFn(context: i32, item: @This()) std.math.Order { | 567 | fn compareFn(context: i32, item: @This()) std.math.Order { |
| 569 | return std.math.order(item.val, context); | 568 | return std.math.order(context, item.val); |
| 570 | } | 569 | } |
| 571 | }; | 570 | }; |
| 572 | | 571 | |
| ... | @@ -584,9 +583,8 @@ test lowerBound { | ... | @@ -584,9 +583,8 @@ test lowerBound { |
| 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)); | 583 | 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 | } | 584 | } |
| 586 | | 585 | |
| 587 | /// Returns the index of the first element in `items` returning `.gt` | 586 | /// Returns the index of the first element in `items` that is greater than `context`, as determined |
| 588 | /// when given to `compareFn`. | 587 | /// by `compareFn`. If no such element exists, returns `items.len`. |
| 589 | /// - Returns `items.len` if none of the elements return `.gt`. | | |
| 590 | /// | 588 | /// |
| 591 | /// `items` must be sorted in ascending order with respect to `compareFn`: | 589 | /// `items` must be sorted in ascending order with respect to `compareFn`: |
| 592 | /// ``` | 590 | /// ``` |
| ... | @@ -611,7 +609,7 @@ pub fn upperBound( | ... | @@ -611,7 +609,7 @@ pub fn upperBound( |
| 611 | ) usize { | 609 | ) usize { |
| 612 | const S = struct { | 610 | const S = struct { |
| 613 | fn predicate(ctx: @TypeOf(context), item: T) bool { | 611 | fn predicate(ctx: @TypeOf(context), item: T) bool { |
| 614 | return compareFn(ctx, item) != .gt; | 612 | return compareFn(ctx, item).invert() != .gt; |
| 615 | } | 613 | } |
| 616 | }; | 614 | }; |
| 617 | return partitionPoint(T, items, context, S.predicate); | 615 | return partitionPoint(T, items, context, S.predicate); |
| ... | @@ -620,13 +618,13 @@ pub fn upperBound( | ... | @@ -620,13 +618,13 @@ pub fn upperBound( |
| 620 | test upperBound { | 618 | test upperBound { |
| 621 | const S = struct { | 619 | const S = struct { |
| 622 | fn compareU32(context: u32, item: u32) std.math.Order { | 620 | fn compareU32(context: u32, item: u32) std.math.Order { |
| 623 | return std.math.order(item, context); | 621 | return std.math.order(context, item); |
| 624 | } | 622 | } |
| 625 | fn compareI32(context: i32, item: i32) std.math.Order { | 623 | fn compareI32(context: i32, item: i32) std.math.Order { |
| 626 | return std.math.order(item, context); | 624 | return std.math.order(context, item); |
| 627 | } | 625 | } |
| 628 | fn compareF32(context: f32, item: f32) std.math.Order { | 626 | fn compareF32(context: f32, item: f32) std.math.Order { |
| 629 | return std.math.order(item, context); | 627 | return std.math.order(context, item); |
| 630 | } | 628 | } |
| 631 | }; | 629 | }; |
| 632 | const R = struct { | 630 | const R = struct { |
| ... | @@ -637,7 +635,7 @@ test upperBound { | ... | @@ -637,7 +635,7 @@ test upperBound { |
| 637 | } | 635 | } |
| 638 | | 636 | |
| 639 | fn compareFn(context: i32, item: @This()) std.math.Order { | 637 | fn compareFn(context: i32, item: @This()) std.math.Order { |
| 640 | return std.math.order(item.val, context); | 638 | return std.math.order(context, item.val); |
| 641 | } | 639 | } |
| 642 | }; | 640 | }; |
| 643 | | 641 | |
| ... | @@ -780,16 +778,16 @@ pub fn equalRange( | ... | @@ -780,16 +778,16 @@ pub fn equalRange( |
| 780 | test equalRange { | 778 | test equalRange { |
| 781 | const S = struct { | 779 | const S = struct { |
| 782 | fn orderU32(context: u32, item: u32) std.math.Order { | 780 | fn orderU32(context: u32, item: u32) std.math.Order { |
| 783 | return std.math.order(item, context); | 781 | return std.math.order(context, item); |
| 784 | } | 782 | } |
| 785 | fn orderI32(context: i32, item: i32) std.math.Order { | 783 | fn orderI32(context: i32, item: i32) std.math.Order { |
| 786 | return std.math.order(item, context); | 784 | return std.math.order(context, item); |
| 787 | } | 785 | } |
| 788 | fn orderF32(context: f32, item: f32) std.math.Order { | 786 | fn orderF32(context: f32, item: f32) std.math.Order { |
| 789 | return std.math.order(item, context); | 787 | return std.math.order(context, item); |
| 790 | } | 788 | } |
| 791 | fn orderLength(context: usize, item: []const u8) std.math.Order { | 789 | fn orderLength(context: usize, item: []const u8) std.math.Order { |
| 792 | return std.math.order(item.len, context); | 790 | return std.math.order(context, item.len); |
| 793 | } | 791 | } |
| 794 | }; | 792 | }; |
| 795 | | 793 | |