| ... | ... | @@ -806,9 +806,12 @@ fn eqlBytes(a: []const u8, b: []const u8) bool { |
| 806 | 806 | return !Scan.isNotEqual(last_a_chunk, last_b_chunk); |
| 807 | 807 | } |
| 808 | 808 | |
| 809 | /// Deprecated in favor of `findDiff`. |
| 810 | pub const indexOfDiff = findDiff; |
| 811 | |
| 809 | 812 | /// Compares two slices and returns the index of the first inequality. |
| 810 | 813 | /// Returns null if the slices are equal. |
| 811 | | pub fn indexOfDiff(comptime T: type, a: []const T, b: []const T) ?usize { |
| 814 | pub fn findDiff(comptime T: type, a: []const T, b: []const T) ?usize { |
| 812 | 815 | const shortest = @min(a.len, b.len); |
| 813 | 816 | if (a.ptr == b.ptr) |
| 814 | 817 | return if (a.len == b.len) null else shortest; |
| ... | ... | @@ -817,12 +820,12 @@ pub fn indexOfDiff(comptime T: type, a: []const T, b: []const T) ?usize { |
| 817 | 820 | return if (a.len == b.len) null else shortest; |
| 818 | 821 | } |
| 819 | 822 | |
| 820 | | test indexOfDiff { |
| 821 | | try testing.expectEqual(indexOfDiff(u8, "one", "one"), null); |
| 822 | | try testing.expectEqual(indexOfDiff(u8, "one two", "one"), 3); |
| 823 | | try testing.expectEqual(indexOfDiff(u8, "one", "one two"), 3); |
| 824 | | try testing.expectEqual(indexOfDiff(u8, "one twx", "one two"), 6); |
| 825 | | try testing.expectEqual(indexOfDiff(u8, "xne", "one"), 0); |
| 823 | test findDiff { |
| 824 | try testing.expectEqual(findDiff(u8, "one", "one"), null); |
| 825 | try testing.expectEqual(findDiff(u8, "one two", "one"), 3); |
| 826 | try testing.expectEqual(findDiff(u8, "one", "one two"), 3); |
| 827 | try testing.expectEqual(findDiff(u8, "one twx", "one two"), 6); |
| 828 | try testing.expectEqual(findDiff(u8, "xne", "one"), 0); |
| 826 | 829 | } |
| 827 | 830 | |
| 828 | 831 | /// Takes a sentinel-terminated pointer and returns a slice preserving pointer attributes. |
| ... | ... | @@ -1109,9 +1112,12 @@ test len { |
| 1109 | 1112 | try testing.expect(len(c_ptr) == 2); |
| 1110 | 1113 | } |
| 1111 | 1114 | |
| 1115 | /// Deprecated in favor of `findSentinel`. |
| 1116 | pub const indexOfSentinel = findSentinel; |
| 1117 | |
| 1112 | 1118 | /// Returns the index of the sentinel value in a sentinel-terminated pointer. |
| 1113 | 1119 | /// Linear search through memory until the sentinel is found. |
| 1114 | | pub fn indexOfSentinel(comptime T: type, comptime sentinel: T, p: [*:sentinel]const T) usize { |
| 1120 | pub fn findSentinel(comptime T: type, comptime sentinel: T, p: [*:sentinel]const T) usize { |
| 1115 | 1121 | var i: usize = 0; |
| 1116 | 1122 | |
| 1117 | 1123 | if (use_vectors_for_comparison and |
| ... | ... | @@ -1262,13 +1268,19 @@ test trim { |
| 1262 | 1268 | try testing.expectEqualSlices(u8, "foo", trim(u8, "foo", " \n")); |
| 1263 | 1269 | } |
| 1264 | 1270 | |
| 1271 | /// Deprecated in favor of `findScalar`. |
| 1272 | pub const indexOfScalar = findScalar; |
| 1273 | |
| 1265 | 1274 | /// Linear search for the index of a scalar value inside a slice. |
| 1266 | | pub fn indexOfScalar(comptime T: type, slice: []const T, value: T) ?usize { |
| 1275 | pub fn findScalar(comptime T: type, slice: []const T, value: T) ?usize { |
| 1267 | 1276 | return indexOfScalarPos(T, slice, 0, value); |
| 1268 | 1277 | } |
| 1269 | 1278 | |
| 1279 | /// Deprecated in favor of `findScalarLast`. |
| 1280 | pub const lastIndexOfScalar = findScalarLast; |
| 1281 | |
| 1270 | 1282 | /// Linear search for the last index of a scalar value inside a slice. |
| 1271 | | pub fn lastIndexOfScalar(comptime T: type, slice: []const T, value: T) ?usize { |
| 1283 | pub fn findScalarLast(comptime T: type, slice: []const T, value: T) ?usize { |
| 1272 | 1284 | var i: usize = slice.len; |
| 1273 | 1285 | while (i != 0) { |
| 1274 | 1286 | i -= 1; |
| ... | ... | @@ -1277,9 +1289,12 @@ pub fn lastIndexOfScalar(comptime T: type, slice: []const T, value: T) ?usize { |
| 1277 | 1289 | return null; |
| 1278 | 1290 | } |
| 1279 | 1291 | |
| 1292 | /// Deprecated in favor of `findScalarPos`. |
| 1293 | pub const indexOfScalarPos = findScalarPos; |
| 1294 | |
| 1280 | 1295 | /// Linear search for the index of a scalar value inside a slice, starting from a given position. |
| 1281 | 1296 | /// Returns null if the value is not found. |
| 1282 | | pub fn indexOfScalarPos(comptime T: type, slice: []const T, start_index: usize, value: T) ?usize { |
| 1297 | pub fn findScalarPos(comptime T: type, slice: []const T, start_index: usize, value: T) ?usize { |
| 1283 | 1298 | if (start_index >= slice.len) return null; |
| 1284 | 1299 | |
| 1285 | 1300 | var i: usize = start_index; |
| ... | ... | @@ -1355,15 +1370,21 @@ test indexOfScalarPos { |
| 1355 | 1370 | } |
| 1356 | 1371 | } |
| 1357 | 1372 | |
| 1373 | /// Deprecated in favor of `findAny`. |
| 1374 | pub const indexOfAny = findAny; |
| 1375 | |
| 1358 | 1376 | /// Linear search for the index of any value in the provided list inside a slice. |
| 1359 | 1377 | /// Returns null if no values are found. |
| 1360 | | pub fn indexOfAny(comptime T: type, slice: []const T, values: []const T) ?usize { |
| 1378 | pub fn findAny(comptime T: type, slice: []const T, values: []const T) ?usize { |
| 1361 | 1379 | return indexOfAnyPos(T, slice, 0, values); |
| 1362 | 1380 | } |
| 1363 | 1381 | |
| 1382 | /// Deprecated in favor of `findLastAny`. |
| 1383 | pub const lastIndexOfAny = findLastAny; |
| 1384 | |
| 1364 | 1385 | /// Linear search for the last index of any value in the provided list inside a slice. |
| 1365 | 1386 | /// Returns null if no values are found. |
| 1366 | | pub fn lastIndexOfAny(comptime T: type, slice: []const T, values: []const T) ?usize { |
| 1387 | pub fn findLastAny(comptime T: type, slice: []const T, values: []const T) ?usize { |
| 1367 | 1388 | var i: usize = slice.len; |
| 1368 | 1389 | while (i != 0) { |
| 1369 | 1390 | i -= 1; |
| ... | ... | @@ -1374,9 +1395,12 @@ pub fn lastIndexOfAny(comptime T: type, slice: []const T, values: []const T) ?us |
| 1374 | 1395 | return null; |
| 1375 | 1396 | } |
| 1376 | 1397 | |
| 1398 | /// Deprecated in favor of `findAnyPos`. |
| 1399 | pub const indexOfAnyPos = findAnyPos; |
| 1400 | |
| 1377 | 1401 | /// Linear search for the index of any value in the provided list inside a slice, starting from a given position. |
| 1378 | 1402 | /// Returns null if no values are found. |
| 1379 | | pub fn indexOfAnyPos(comptime T: type, slice: []const T, start_index: usize, values: []const T) ?usize { |
| 1403 | pub fn findAnyPos(comptime T: type, slice: []const T, start_index: usize, values: []const T) ?usize { |
| 1380 | 1404 | if (start_index >= slice.len) return null; |
| 1381 | 1405 | for (slice[start_index..], start_index..) |c, i| { |
| 1382 | 1406 | for (values) |value| { |
| ... | ... | @@ -1386,17 +1410,34 @@ pub fn indexOfAnyPos(comptime T: type, slice: []const T, start_index: usize, val |
| 1386 | 1410 | return null; |
| 1387 | 1411 | } |
| 1388 | 1412 | |
| 1413 | /// Deprecated in favor of `findNone`. |
| 1414 | pub const indexOfNone = findNone; |
| 1415 | |
| 1389 | 1416 | /// Find the first item in `slice` which is not contained in `values`. |
| 1390 | 1417 | /// |
| 1391 | 1418 | /// Comparable to `strspn` in the C standard library. |
| 1392 | | pub fn indexOfNone(comptime T: type, slice: []const T, values: []const T) ?usize { |
| 1419 | pub fn findNone(comptime T: type, slice: []const T, values: []const T) ?usize { |
| 1393 | 1420 | return indexOfNonePos(T, slice, 0, values); |
| 1394 | 1421 | } |
| 1395 | 1422 | |
| 1423 | test findNone { |
| 1424 | try testing.expect(findNone(u8, "abc123", "123").? == 0); |
| 1425 | try testing.expect(findLastNone(u8, "abc123", "123").? == 2); |
| 1426 | try testing.expect(findNone(u8, "123abc", "123").? == 3); |
| 1427 | try testing.expect(findLastNone(u8, "123abc", "123").? == 5); |
| 1428 | try testing.expect(findNone(u8, "123123", "123") == null); |
| 1429 | try testing.expect(findNone(u8, "333333", "123") == null); |
| 1430 | |
| 1431 | try testing.expect(indexOfNonePos(u8, "abc123", 3, "321") == null); |
| 1432 | } |
| 1433 | |
| 1434 | /// Deprecated in favor of `findLastNone`. |
| 1435 | pub const lastIndexOfNone = findLastNone; |
| 1436 | |
| 1396 | 1437 | /// Find the last item in `slice` which is not contained in `values`. |
| 1397 | 1438 | /// |
| 1398 | 1439 | /// Like `strspn` in the C standard library, but searches from the end. |
| 1399 | | pub fn lastIndexOfNone(comptime T: type, slice: []const T, values: []const T) ?usize { |
| 1440 | pub fn findLastNone(comptime T: type, slice: []const T, values: []const T) ?usize { |
| 1400 | 1441 | var i: usize = slice.len; |
| 1401 | 1442 | outer: while (i != 0) { |
| 1402 | 1443 | i -= 1; |
| ... | ... | @@ -1408,11 +1449,13 @@ pub fn lastIndexOfNone(comptime T: type, slice: []const T, values: []const T) ?u |
| 1408 | 1449 | return null; |
| 1409 | 1450 | } |
| 1410 | 1451 | |
| 1452 | pub const indexOfNonePos = findNonePos; |
| 1453 | |
| 1411 | 1454 | /// Find the first item in `slice[start_index..]` which is not contained in `values`. |
| 1412 | 1455 | /// The returned index will be relative to the start of `slice`, and never less than `start_index`. |
| 1413 | 1456 | /// |
| 1414 | 1457 | /// Comparable to `strspn` in the C standard library. |
| 1415 | | pub fn indexOfNonePos(comptime T: type, slice: []const T, start_index: usize, values: []const T) ?usize { |
| 1458 | pub fn findNonePos(comptime T: type, slice: []const T, start_index: usize, values: []const T) ?usize { |
| 1416 | 1459 | if (start_index >= slice.len) return null; |
| 1417 | 1460 | outer: for (slice[start_index..], start_index..) |c, i| { |
| 1418 | 1461 | for (values) |value| { |
| ... | ... | @@ -1423,29 +1466,24 @@ pub fn indexOfNonePos(comptime T: type, slice: []const T, start_index: usize, va |
| 1423 | 1466 | return null; |
| 1424 | 1467 | } |
| 1425 | 1468 | |
| 1426 | | test indexOfNone { |
| 1427 | | try testing.expect(indexOfNone(u8, "abc123", "123").? == 0); |
| 1428 | | try testing.expect(lastIndexOfNone(u8, "abc123", "123").? == 2); |
| 1429 | | try testing.expect(indexOfNone(u8, "123abc", "123").? == 3); |
| 1430 | | try testing.expect(lastIndexOfNone(u8, "123abc", "123").? == 5); |
| 1431 | | try testing.expect(indexOfNone(u8, "123123", "123") == null); |
| 1432 | | try testing.expect(indexOfNone(u8, "333333", "123") == null); |
| 1433 | | |
| 1434 | | try testing.expect(indexOfNonePos(u8, "abc123", 3, "321") == null); |
| 1435 | | } |
| 1469 | /// Deprecated in favor of `find`. |
| 1470 | pub const indexOf = find; |
| 1436 | 1471 | |
| 1437 | 1472 | /// Search for needle in haystack and return the index of the first occurrence. |
| 1438 | 1473 | /// Uses Boyer-Moore-Horspool algorithm on large inputs; linear search on small inputs. |
| 1439 | 1474 | /// Returns null if needle is not found. |
| 1440 | | pub fn indexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize { |
| 1475 | pub fn find(comptime T: type, haystack: []const T, needle: []const T) ?usize { |
| 1441 | 1476 | return indexOfPos(T, haystack, 0, needle); |
| 1442 | 1477 | } |
| 1443 | 1478 | |
| 1479 | /// Deprecated in favor of `findLastLinear`. |
| 1480 | pub const lastIndexOfLinear = findLastLinear; |
| 1481 | |
| 1444 | 1482 | /// Find the index in a slice of a sub-slice, searching from the end backwards. |
| 1445 | 1483 | /// To start looking at a different index, slice the haystack first. |
| 1446 | 1484 | /// Consider using `lastIndexOf` instead of this, which will automatically use a |
| 1447 | 1485 | /// more sophisticated algorithm on larger inputs. |
| 1448 | | pub fn lastIndexOfLinear(comptime T: type, haystack: []const T, needle: []const T) ?usize { |
| 1486 | pub fn findLastLinear(comptime T: type, haystack: []const T, needle: []const T) ?usize { |
| 1449 | 1487 | if (needle.len > haystack.len) return null; |
| 1450 | 1488 | var i: usize = haystack.len - needle.len; |
| 1451 | 1489 | while (true) : (i -= 1) { |
| ... | ... | @@ -1454,9 +1492,11 @@ pub fn lastIndexOfLinear(comptime T: type, haystack: []const T, needle: []const |
| 1454 | 1492 | } |
| 1455 | 1493 | } |
| 1456 | 1494 | |
| 1495 | pub const indexOfPosLinear = findPosLinear; |
| 1496 | |
| 1457 | 1497 | /// Consider using `indexOfPos` instead of this, which will automatically use a |
| 1458 | 1498 | /// more sophisticated algorithm on larger inputs. |
| 1459 | | pub fn indexOfPosLinear(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize { |
| 1499 | pub fn findPosLinear(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize { |
| 1460 | 1500 | if (needle.len > haystack.len) return null; |
| 1461 | 1501 | var i: usize = start_index; |
| 1462 | 1502 | const end = haystack.len - needle.len; |
| ... | ... | @@ -1466,24 +1506,24 @@ pub fn indexOfPosLinear(comptime T: type, haystack: []const T, start_index: usiz |
| 1466 | 1506 | return null; |
| 1467 | 1507 | } |
| 1468 | 1508 | |
| 1469 | | test indexOfPosLinear { |
| 1470 | | try testing.expectEqual(0, indexOfPosLinear(u8, "", 0, "")); |
| 1471 | | try testing.expectEqual(0, indexOfPosLinear(u8, "123", 0, "")); |
| 1509 | test findPosLinear { |
| 1510 | try testing.expectEqual(0, findPosLinear(u8, "", 0, "")); |
| 1511 | try testing.expectEqual(0, findPosLinear(u8, "123", 0, "")); |
| 1472 | 1512 | |
| 1473 | | try testing.expectEqual(null, indexOfPosLinear(u8, "", 0, "1")); |
| 1474 | | try testing.expectEqual(0, indexOfPosLinear(u8, "1", 0, "1")); |
| 1475 | | try testing.expectEqual(null, indexOfPosLinear(u8, "2", 0, "1")); |
| 1476 | | try testing.expectEqual(1, indexOfPosLinear(u8, "21", 0, "1")); |
| 1477 | | try testing.expectEqual(null, indexOfPosLinear(u8, "222", 0, "1")); |
| 1513 | try testing.expectEqual(null, findPosLinear(u8, "", 0, "1")); |
| 1514 | try testing.expectEqual(0, findPosLinear(u8, "1", 0, "1")); |
| 1515 | try testing.expectEqual(null, findPosLinear(u8, "2", 0, "1")); |
| 1516 | try testing.expectEqual(1, findPosLinear(u8, "21", 0, "1")); |
| 1517 | try testing.expectEqual(null, findPosLinear(u8, "222", 0, "1")); |
| 1478 | 1518 | |
| 1479 | | try testing.expectEqual(null, indexOfPosLinear(u8, "", 0, "12")); |
| 1480 | | try testing.expectEqual(null, indexOfPosLinear(u8, "1", 0, "12")); |
| 1481 | | try testing.expectEqual(null, indexOfPosLinear(u8, "2", 0, "12")); |
| 1482 | | try testing.expectEqual(0, indexOfPosLinear(u8, "12", 0, "12")); |
| 1483 | | try testing.expectEqual(null, indexOfPosLinear(u8, "21", 0, "12")); |
| 1484 | | try testing.expectEqual(1, indexOfPosLinear(u8, "212", 0, "12")); |
| 1485 | | try testing.expectEqual(0, indexOfPosLinear(u8, "122", 0, "12")); |
| 1486 | | try testing.expectEqual(1, indexOfPosLinear(u8, "212112", 0, "12")); |
| 1519 | try testing.expectEqual(null, findPosLinear(u8, "", 0, "12")); |
| 1520 | try testing.expectEqual(null, findPosLinear(u8, "1", 0, "12")); |
| 1521 | try testing.expectEqual(null, findPosLinear(u8, "2", 0, "12")); |
| 1522 | try testing.expectEqual(0, findPosLinear(u8, "12", 0, "12")); |
| 1523 | try testing.expectEqual(null, findPosLinear(u8, "21", 0, "12")); |
| 1524 | try testing.expectEqual(1, findPosLinear(u8, "212", 0, "12")); |
| 1525 | try testing.expectEqual(0, findPosLinear(u8, "122", 0, "12")); |
| 1526 | try testing.expectEqual(1, findPosLinear(u8, "212112", 0, "12")); |
| 1487 | 1527 | } |
| 1488 | 1528 | |
| 1489 | 1529 | fn boyerMooreHorspoolPreprocessReverse(pattern: []const u8, table: *[256]usize) void { |
| ... | ... | @@ -1512,11 +1552,14 @@ fn boyerMooreHorspoolPreprocess(pattern: []const u8, table: *[256]usize) void { |
| 1512 | 1552 | } |
| 1513 | 1553 | } |
| 1514 | 1554 | |
| 1555 | /// Deprecated in favor of `find`. |
| 1556 | pub const lastIndexOf = findLast; |
| 1557 | |
| 1515 | 1558 | /// Find the index in a slice of a sub-slice, searching from the end backwards. |
| 1516 | 1559 | /// To start looking at a different index, slice the haystack first. |
| 1517 | 1560 | /// Uses the Reverse Boyer-Moore-Horspool algorithm on large inputs; |
| 1518 | 1561 | /// `lastIndexOfLinear` on small inputs. |
| 1519 | | pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize { |
| 1562 | pub fn findLast(comptime T: type, haystack: []const T, needle: []const T) ?usize { |
| 1520 | 1563 | if (needle.len > haystack.len) return null; |
| 1521 | 1564 | if (needle.len == 0) return haystack.len; |
| 1522 | 1565 | |
| ... | ... | @@ -1542,8 +1585,11 @@ pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?us |
| 1542 | 1585 | return null; |
| 1543 | 1586 | } |
| 1544 | 1587 | |
| 1588 | /// Deprecated in favor of `findPos`. |
| 1589 | pub const indexOfPos = findPos; |
| 1590 | |
| 1545 | 1591 | /// Uses Boyer-Moore-Horspool algorithm on large inputs; `indexOfPosLinear` on small inputs. |
| 1546 | | pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize { |
| 1592 | pub fn findPos(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize { |
| 1547 | 1593 | if (needle.len > haystack.len) return null; |
| 1548 | 1594 | if (needle.len < 2) { |
| 1549 | 1595 | if (needle.len == 0) return start_index; |
| ... | ... | @@ -1593,7 +1639,7 @@ test indexOf { |
| 1593 | 1639 | try testing.expect(indexOf(u8, "foo foo", "foo").? == 0); |
| 1594 | 1640 | try testing.expect(lastIndexOf(u8, "foo foo", "foo").? == 4); |
| 1595 | 1641 | try testing.expect(lastIndexOfAny(u8, "boo, cat", "abo").? == 6); |
| 1596 | | try testing.expect(lastIndexOfScalar(u8, "boo", 'o').? == 2); |
| 1642 | try testing.expect(findScalarLast(u8, "boo", 'o').? == 2); |
| 1597 | 1643 | } |
| 1598 | 1644 | |
| 1599 | 1645 | test "indexOf multibyte" { |
| ... | ... | @@ -3306,7 +3352,7 @@ pub fn SplitBackwardsIterator(comptime T: type, comptime delimiter_type: Delimit |
| 3306 | 3352 | const start = if (switch (delimiter_type) { |
| 3307 | 3353 | .sequence => lastIndexOf(T, self.buffer[0..end], self.delimiter), |
| 3308 | 3354 | .any => lastIndexOfAny(T, self.buffer[0..end], self.delimiter), |
| 3309 | | .scalar => lastIndexOfScalar(T, self.buffer[0..end], self.delimiter), |
| 3355 | .scalar => findScalarLast(T, self.buffer[0..end], self.delimiter), |
| 3310 | 3356 | }) |delim_start| blk: { |
| 3311 | 3357 | self.index = delim_start; |
| 3312 | 3358 | break :blk delim_start + switch (delimiter_type) { |
| ... | ... | @@ -3620,9 +3666,12 @@ test minMax { |
| 3620 | 3666 | } |
| 3621 | 3667 | } |
| 3622 | 3668 | |
| 3669 | /// Deprecated in favor of `findMin`. |
| 3670 | pub const indexOfMin = findMin; |
| 3671 | |
| 3623 | 3672 | /// Returns the index of the smallest number in a slice. O(n). |
| 3624 | 3673 | /// `slice` must not be empty. |
| 3625 | | pub fn indexOfMin(comptime T: type, slice: []const T) usize { |
| 3674 | pub fn findMin(comptime T: type, slice: []const T) usize { |
| 3626 | 3675 | assert(slice.len > 0); |
| 3627 | 3676 | var best = slice[0]; |
| 3628 | 3677 | var index: usize = 0; |
| ... | ... | @@ -3635,15 +3684,17 @@ pub fn indexOfMin(comptime T: type, slice: []const T) usize { |
| 3635 | 3684 | return index; |
| 3636 | 3685 | } |
| 3637 | 3686 | |
| 3638 | | test indexOfMin { |
| 3639 | | try testing.expectEqual(indexOfMin(u8, "abcdefg"), 0); |
| 3640 | | try testing.expectEqual(indexOfMin(u8, "bcdefga"), 6); |
| 3641 | | try testing.expectEqual(indexOfMin(u8, "a"), 0); |
| 3687 | test findMin { |
| 3688 | try testing.expectEqual(findMin(u8, "abcdefg"), 0); |
| 3689 | try testing.expectEqual(findMin(u8, "bcdefga"), 6); |
| 3690 | try testing.expectEqual(findMin(u8, "a"), 0); |
| 3642 | 3691 | } |
| 3643 | 3692 | |
| 3693 | pub const indexOfMax = findMax; |
| 3694 | |
| 3644 | 3695 | /// Returns the index of the largest number in a slice. O(n). |
| 3645 | 3696 | /// `slice` must not be empty. |
| 3646 | | pub fn indexOfMax(comptime T: type, slice: []const T) usize { |
| 3697 | pub fn findMax(comptime T: type, slice: []const T) usize { |
| 3647 | 3698 | assert(slice.len > 0); |
| 3648 | 3699 | var best = slice[0]; |
| 3649 | 3700 | var index: usize = 0; |
| ... | ... | @@ -3656,16 +3707,19 @@ pub fn indexOfMax(comptime T: type, slice: []const T) usize { |
| 3656 | 3707 | return index; |
| 3657 | 3708 | } |
| 3658 | 3709 | |
| 3659 | | test indexOfMax { |
| 3660 | | try testing.expectEqual(indexOfMax(u8, "abcdefg"), 6); |
| 3661 | | try testing.expectEqual(indexOfMax(u8, "gabcdef"), 0); |
| 3662 | | try testing.expectEqual(indexOfMax(u8, "a"), 0); |
| 3710 | test findMax { |
| 3711 | try testing.expectEqual(findMax(u8, "abcdefg"), 6); |
| 3712 | try testing.expectEqual(findMax(u8, "gabcdef"), 0); |
| 3713 | try testing.expectEqual(findMax(u8, "a"), 0); |
| 3663 | 3714 | } |
| 3664 | 3715 | |
| 3716 | /// Deprecated in favor of `findMinMax`. |
| 3717 | pub const indexOfMinMax = findMinMax; |
| 3718 | |
| 3665 | 3719 | /// Finds the indices of the smallest and largest number in a slice. O(n). |
| 3666 | 3720 | /// Returns the indices of the smallest and largest numbers in that order. |
| 3667 | 3721 | /// `slice` must not be empty. |
| 3668 | | pub fn indexOfMinMax(comptime T: type, slice: []const T) struct { usize, usize } { |
| 3722 | pub fn findMinMax(comptime T: type, slice: []const T) struct { usize, usize } { |
| 3669 | 3723 | assert(slice.len > 0); |
| 3670 | 3724 | var minVal = slice[0]; |
| 3671 | 3725 | var maxVal = slice[0]; |
| ... | ... | @@ -3684,10 +3738,10 @@ pub fn indexOfMinMax(comptime T: type, slice: []const T) struct { usize, usize } |
| 3684 | 3738 | return .{ minIdx, maxIdx }; |
| 3685 | 3739 | } |
| 3686 | 3740 | |
| 3687 | | test indexOfMinMax { |
| 3688 | | try testing.expectEqual(.{ 0, 6 }, indexOfMinMax(u8, "abcdefg")); |
| 3689 | | try testing.expectEqual(.{ 1, 0 }, indexOfMinMax(u8, "gabcdef")); |
| 3690 | | try testing.expectEqual(.{ 0, 0 }, indexOfMinMax(u8, "a")); |
| 3741 | test findMinMax { |
| 3742 | try testing.expectEqual(.{ 0, 6 }, findMinMax(u8, "abcdefg")); |
| 3743 | try testing.expectEqual(.{ 1, 0 }, findMinMax(u8, "gabcdef")); |
| 3744 | try testing.expectEqual(.{ 0, 0 }, findMinMax(u8, "a")); |
| 3691 | 3745 | } |
| 3692 | 3746 | |
| 3693 | 3747 | /// Exchanges contents of two memory locations. |