(zig) fixed output of vector.drain()
This commit is contained in:
+8
-7
@@ -6,18 +6,19 @@ const fs = std.fs;
|
|||||||
const vec = @import("vector.zig");
|
const vec = @import("vector.zig");
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
var GPA = std.heap.GeneralPurposeAllocator(.{}){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
const allocator = GPA.allocator();
|
const allocator = gpa.allocator();
|
||||||
var temp = try vec.vector(u8).new(allocator);
|
var test_vector = try vec.vector(u8).new(allocator);
|
||||||
|
|
||||||
for (0..12) |n|
|
for (0..12) |n|
|
||||||
{
|
{
|
||||||
try temp.push(@truncate(n));
|
try test_vector.push(@truncate(n));
|
||||||
}
|
}
|
||||||
std.debug.print("{X}\n", .{temp.array[0..temp.length]});
|
std.debug.print("{X}\n", .{test_vector.array[0..test_vector.length]});
|
||||||
|
|
||||||
const blargh = try temp.drain(.{3, 6});
|
const blargh: []u8 = try test_vector.drain(.{3, 6});
|
||||||
|
defer allocator.free(blargh);
|
||||||
std.debug.print("drained slice: {X}\n", .{blargh});
|
std.debug.print("drained slice: {X}\n", .{blargh});
|
||||||
std.debug.print("vector after drain: {X}\n", .{temp.array[0..temp.length]});
|
std.debug.print("vector after drain: {X}\n", .{test_vector.array[0..test_vector.length]});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+16
-3
@@ -9,6 +9,7 @@ pub fn vector(comptime T: type) type
|
|||||||
|
|
||||||
const VectorError: type = error {
|
const VectorError: type = error {
|
||||||
OutOfBounds,
|
OutOfBounds,
|
||||||
|
FailedToAllocateMemory,
|
||||||
};
|
};
|
||||||
|
|
||||||
allocator: std.mem.Allocator,
|
allocator: std.mem.Allocator,
|
||||||
@@ -36,6 +37,11 @@ pub fn vector(comptime T: type) type
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn len(self: *Self) usize
|
||||||
|
{
|
||||||
|
return self.length;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn push(self: *Self, item: VectorType) !void
|
pub fn push(self: *Self, item: VectorType) !void
|
||||||
{
|
{
|
||||||
if (self.length >= self.array.len)
|
if (self.length >= self.array.len)
|
||||||
@@ -65,10 +71,17 @@ pub fn vector(comptime T: type) type
|
|||||||
return VectorError.OutOfBounds;
|
return VectorError.OutOfBounds;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const slice_length: usize = range[1] - range[0] + 1;
|
||||||
|
const slice: []VectorType = self.allocator.alloc(VectorType, slice_length)
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return VectorError.FailedToAllocateMemory;
|
||||||
|
};
|
||||||
|
|
||||||
// TODO: return array instead of pointer
|
// TODO: return array instead of pointer
|
||||||
const slice: []VectorType = self.array[range[0]..range[1]];
|
@memcpy(slice.ptr, self.array[range[0]..range[1] + 1]);
|
||||||
@memmove(self.array.ptr, self.array[range[1]..]);
|
@memmove(self.array.ptr + range[0], self.array[range[1]..self.length]);
|
||||||
self.length -= range[1] - range[0] + 1;
|
self.length -= slice_length;
|
||||||
|
|
||||||
return slice;
|
return slice;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user