authorgravatar for codroid@gmail.comStevie Hryciw <codroid@gmail.com> 2022-11-14 15:51:44-08:00
committergravatar for codroid@gmail.comStevie Hryciw <codroid@gmail.com> 2022-12-15 00:56:26-08:00
log69b784ff52248663df210e9899bdbf9a4923a40e
tree3f905160353ed40b974a893b143d2bc94267b516
parent88d927a511922efd82c4ec862c8e2aa83df84391

std: add CompareOperator.reverse


1 files changed, 22 insertions(+), 0 deletions(-)

lib/std/math.zig+22
......@@ -1437,6 +1437,19 @@ pub const CompareOperator = enum {
14371437 gt,
14381438 /// Not equal (`!=`)
14391439 neq,
1440
1441 /// Reverse the direction of the comparison.
1442 /// Use when swapping the left and right hand operands.
1443 pub fn reverse(op: CompareOperator) CompareOperator {
1444 return switch (op) {
1445 .lt => .gt,
1446 .lte => .gte,
1447 .gt => .lt,
1448 .gte => .lte,
1449 .eq => .eq,
1450 .neq => .neq,
1451 };
1452 }
14401453};
14411454
14421455/// This function does the same thing as comparison operators, however the
......@@ -1496,6 +1509,15 @@ test "order.compare" {
14961509 try testing.expect(order(1, 0).compare(.neq));
14971510}
14981511
1512test "compare.reverse" {
1513 inline for (@typeInfo(CompareOperator).Enum.fields) |op_field| {
1514 const op = @intToEnum(CompareOperator, op_field.value);
1515 try testing.expect(compare(2, op, 3) == compare(3, op.reverse(), 2));
1516 try testing.expect(compare(3, op, 3) == compare(3, op.reverse(), 3));
1517 try testing.expect(compare(4, op, 3) == compare(3, op.reverse(), 4));
1518 }
1519}
1520
14991521/// Returns a mask of all ones if value is true,
15001522/// and a mask of all zeroes if value is false.
15011523/// Compiles to one instruction for register sized integers.