@@ -461,8 +461,8 @@ pub fn binarySearch(
461
461
const mid = low + (high - low ) / 2 ;
462
462
switch (compareFn (context , items [mid ])) {
463
463
.eq = > return mid ,
464
- .lt = > low = mid + 1 , // item too small
465
- .gt = > high = mid , // item too big
464
+ .gt = > low = mid + 1 ,
465
+ .lt = > high = mid ,
466
466
}
467
467
}
468
468
return null ;
@@ -471,13 +471,13 @@ pub fn binarySearch(
471
471
test binarySearch {
472
472
const S = struct {
473
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
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
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
483
const R = struct {
@@ -489,9 +489,9 @@ test binarySearch {
489
489
}
490
490
491
491
fn order (context : i32 , item : @This ()) std.math.Order {
492
- if (item . e < context ) {
492
+ if (context < item . b ) {
493
493
return .lt ;
494
- } else if (item . b > context ) {
494
+ } else if (context > item . e ) {
495
495
return .gt ;
496
496
} else {
497
497
return .eq ;
@@ -513,9 +513,8 @@ test binarySearch {
513
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`
517
- /// when given to `compareFn`.
518
- /// - Returns `items.len` if all elements return `.lt`.
516
+ /// Returns the index of the first element in `items` that is greater than or equal to `context`,
517
+ /// as determined by `compareFn`. If no such element exists, returns `items.len`.
519
518
///
520
519
/// `items` must be sorted in ascending order with respect to `compareFn`:
521
520
/// ```
@@ -540,7 +539,7 @@ pub fn lowerBound(
540
539
) usize {
541
540
const S = struct {
542
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
545
return partitionPoint (T , items , context , S .predicate );
@@ -549,13 +548,13 @@ pub fn lowerBound(
549
548
test lowerBound {
550
549
const S = struct {
551
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
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
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
560
const R = struct {
@@ -566,7 +565,7 @@ test lowerBound {
566
565
}
567
566
568
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
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`
588
- /// when given to `compareFn`.
589
- /// - Returns `items.len` if none of the elements return `.gt`.
586
+ /// Returns the index of the first element in `items` that is greater than `context`, as determined
587
+ /// by `compareFn`. If no such element exists, returns `items.len`.
590
588
///
591
589
/// `items` must be sorted in ascending order with respect to `compareFn`:
592
590
/// ```
@@ -611,7 +609,7 @@ pub fn upperBound(
611
609
) usize {
612
610
const S = struct {
613
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
615
return partitionPoint (T , items , context , S .predicate );
@@ -620,13 +618,13 @@ pub fn upperBound(
620
618
test upperBound {
621
619
const S = struct {
622
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
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
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
630
const R = struct {
@@ -637,7 +635,7 @@ test upperBound {
637
635
}
638
636
639
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
778
test equalRange {
781
779
const S = struct {
782
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
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
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
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
0 commit comments