| ... | ... | @@ -3,8 +3,6 @@ const assert = debug.assert; |
| 3 | 3 | const math = @import("math/index.zig"); |
| 4 | 4 | const builtin = @import("builtin"); |
| 5 | 5 | |
| 6 | | pub const Cmp = math.Cmp; |
| 7 | | |
| 8 | 6 | pub const Allocator = struct { |
| 9 | 7 | /// Allocate byte_count bytes and return them in a slice, with the |
| 10 | 8 | /// slice's pointer aligned at least to alignment bytes. |
| ... | ... | @@ -166,17 +164,24 @@ pub fn set(comptime T: type, dest: []T, value: T) { |
| 166 | 164 | for (dest) |*d| *d = value; |
| 167 | 165 | } |
| 168 | 166 | |
| 169 | | /// Return < 0, == 0, or > 0 if memory a is less than, equal to, or greater than, |
| 170 | | /// memory b, respectively. |
| 171 | | pub fn cmp(comptime T: type, a: []const T, b: []const T) -> Cmp { |
| 172 | | const n = math.min(a.len, b.len); |
| 167 | /// Returns true if lhs < rhs, false otherwise |
| 168 | pub fn lessThan(comptime T: type, lhs: []const T, rhs: []const T) -> bool { |
| 169 | const n = math.min(lhs.len, rhs.len); |
| 173 | 170 | var i: usize = 0; |
| 174 | 171 | while (i < n) : (i += 1) { |
| 175 | | if (a[i] == b[i]) continue; |
| 176 | | return if (a[i] > b[i]) Cmp.Greater else if (a[i] < b[i]) Cmp.Less else Cmp.Equal; |
| 172 | if (lhs[i] == rhs[i]) continue; |
| 173 | return lhs[i] < rhs[i]; |
| 177 | 174 | } |
| 178 | 175 | |
| 179 | | return if (a.len > b.len) Cmp.Greater else if (a.len < b.len) Cmp.Less else Cmp.Equal; |
| 176 | return lhs.len < rhs.len; |
| 177 | } |
| 178 | |
| 179 | test "mem.lessThan" { |
| 180 | assert(lessThan(u8, "abcd", "bee")); |
| 181 | assert(!lessThan(u8, "abc", "abc")); |
| 182 | assert(lessThan(u8, "abc", "abc0")); |
| 183 | assert(!lessThan(u8, "", "")); |
| 184 | assert(lessThan(u8, "", "a")); |
| 180 | 185 | } |
| 181 | 186 | |
| 182 | 187 | /// Compares two slices and returns whether they are equal. |