|
1 | 1 | //! Vectors
|
2 | 2 |
|
| 3 | +import cmp::{Eq, Ord}; |
3 | 4 | import option::{Some, None};
|
4 | 5 | import ptr::addr_of;
|
5 | 6 | import libc::size_t;
|
@@ -1374,6 +1375,80 @@ pure fn as_mut_buf<T,U>(s: &[mut T],
|
1374 | 1375 | }
|
1375 | 1376 | }
|
1376 | 1377 |
|
| 1378 | +// Equality |
| 1379 | + |
| 1380 | +pure fn eq<T: Eq>(a: &[T], b: &[T]) -> bool { |
| 1381 | + let (a_len, b_len) = (a.len(), b.len()); |
| 1382 | + if a_len != b_len { return false; } |
| 1383 | + |
| 1384 | + let mut i = 0; |
| 1385 | + while i < a_len { |
| 1386 | + if a[i] != b[i] { return false; } |
| 1387 | + i += 1; |
| 1388 | + } |
| 1389 | + |
| 1390 | + return true; |
| 1391 | +} |
| 1392 | + |
| 1393 | +impl<T: Eq> &[T]: Eq { |
| 1394 | + #[inline(always)] |
| 1395 | + pure fn eq(&&other: &[T]) -> bool { |
| 1396 | + eq(self, other) |
| 1397 | + } |
| 1398 | +} |
| 1399 | + |
| 1400 | +impl<T: Eq> ~[T]: Eq { |
| 1401 | + #[inline(always)] |
| 1402 | + pure fn eq(&&other: ~[T]) -> bool { |
| 1403 | + eq(self, other) |
| 1404 | + } |
| 1405 | +} |
| 1406 | + |
| 1407 | +impl<T: Eq> @[T]: Eq { |
| 1408 | + #[inline(always)] |
| 1409 | + pure fn eq(&&other: @[T]) -> bool { |
| 1410 | + eq(self, other) |
| 1411 | + } |
| 1412 | +} |
| 1413 | + |
| 1414 | +// Lexicographical comparison |
| 1415 | + |
| 1416 | +pure fn lt<T: Ord>(a: &[T], b: &[T]) -> bool { |
| 1417 | + let (a_len, b_len) = (a.len(), b.len()); |
| 1418 | + let mut end = uint::min(a_len, b_len); |
| 1419 | + |
| 1420 | + let mut i = 0; |
| 1421 | + while i < end { |
| 1422 | + let (c_a, c_b) = (&a[i], &b[i]); |
| 1423 | + if *c_a < *c_b { return true; } |
| 1424 | + if *c_a > *c_b { return false; } |
| 1425 | + i += 1; |
| 1426 | + } |
| 1427 | + |
| 1428 | + return a_len < b_len; |
| 1429 | +} |
| 1430 | + |
| 1431 | +impl<T: Ord> &[T]: Ord { |
| 1432 | + #[inline(always)] |
| 1433 | + pure fn lt(&&other: &[T]) -> bool { |
| 1434 | + lt(self, other) |
| 1435 | + } |
| 1436 | +} |
| 1437 | + |
| 1438 | +impl<T: Ord> ~[T]: Ord { |
| 1439 | + #[inline(always)] |
| 1440 | + pure fn lt(&&other: ~[T]) -> bool { |
| 1441 | + lt(self, other) |
| 1442 | + } |
| 1443 | +} |
| 1444 | + |
| 1445 | +impl<T: Ord> @[T]: Ord { |
| 1446 | + #[inline(always)] |
| 1447 | + pure fn lt(&&other: @[T]) -> bool { |
| 1448 | + lt(self, other) |
| 1449 | + } |
| 1450 | +} |
| 1451 | + |
1377 | 1452 | #[cfg(notest)]
|
1378 | 1453 | impl<T: copy> ~[T]: add<&[const T],~[T]> {
|
1379 | 1454 | #[inline(always)]
|
|
0 commit comments