Improve some RocList functions in zig

This commit is contained in:
Richard Feldman 2022-07-01 18:21:37 -04:00
parent 6f5baa5021
commit ce6c2f330c
No known key found for this signature in database
GPG key ID: 7E4127D1E4241798

View file

@ -31,11 +31,16 @@ pub const RocList = extern struct {
return RocList{ .bytes = null, .length = 0, .capacity = 0 };
}
pub fn isEq(self: RocList, other: RocList) bool {
if ((self.len() != other.len()) or self.isEmpty()) {
pub fn eql(self: RocList, other: RocList) bool {
if (self.len() != other.len()) {
return false;
}
// Their lengths are the same, and one is empty; they're both empty!
if (self.isEmpty()) {
return true;
}
var index: usize = 0;
const self_bytes = self.bytes orelse unreachable;
const other_bytes = other.bytes orelse unreachable;
@ -69,12 +74,10 @@ pub const RocList = extern struct {
utils.decref(self.bytes, self.len(), @alignOf(T));
}
pub fn elements(self: RocList, comptime T: type) [*]T {
const refcount_byte_count = math.max(@alignOf(usize), @alignOf(T));
const addr = @ptrToInt(self.bytes) + refcount_byte_count;
// The first element is stored right after the refcount.
return @intToPtr([*]T, addr);
pub fn elements(self: RocList, comptime T: type) ?[*]T {
// Is there a better way to make this cast happen?
// @ptrCast gives an error because this increases alignment.
return @intToPtr(?[*]T, @ptrToInt(self.bytes));
}
pub fn isUnique(self: RocList) bool {