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 {...@@ -1437,6 +1437,19 @@ pub const CompareOperator = enum {
1437 gt,1437 gt,
1438 /// Not equal (`!=`)1438 /// Not equal (`!=`)
1439 neq,1439 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 }
1440};1453};
14411454
1442/// This function does the same thing as comparison operators, however the1455/// This function does the same thing as comparison operators, however the
...@@ -1496,6 +1509,15 @@ test "order.compare" {...@@ -1496,6 +1509,15 @@ test "order.compare" {
1496 try testing.expect(order(1, 0).compare(.neq));1509 try testing.expect(order(1, 0).compare(.neq));
1497}1510}
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
1499/// Returns a mask of all ones if value is true,1521/// Returns a mask of all ones if value is true,
1500/// and a mask of all zeroes if value is false.1522/// and a mask of all zeroes if value is false.
1501/// Compiles to one instruction for register sized integers.1523/// Compiles to one instruction for register sized integers.