(zig) started implementing vector type
This commit is contained in:
+12
-9
@@ -3,18 +3,21 @@
|
|||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const fs = std.fs;
|
const fs = std.fs;
|
||||||
|
const vec = @import("vector.zig");
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
const stdout = std.io.getStdOut().writer();
|
var GPA = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
|
const allocator = GPA.allocator();
|
||||||
|
var temp = try vec.vector(u8).new(allocator);
|
||||||
|
|
||||||
var cwd: fs.Dir = try fs.cwd().openDir("../", .{});
|
for (0..12) |n|
|
||||||
defer cwd.close();
|
{
|
||||||
var file: fs.File = try cwd.openFile("small-lorem.md", .{.mode = .read_only});
|
try temp.push(@truncate(n));
|
||||||
defer file.close();
|
}
|
||||||
var buffer: [1024]u8 = undefined;
|
std.debug.print("{X}\n", .{temp.array[0..temp.length]});
|
||||||
|
|
||||||
const length: usize = try file.readAll(&buffer);
|
const blargh = try temp.drain(.{3, 6});
|
||||||
|
std.debug.print("drained slice: {X}\n", .{blargh});
|
||||||
try stdout.print("file ({}): {s}", .{length, buffer[0..length]});
|
std.debug.print("vector after drain: {X}\n", .{temp.array[0..temp.length]});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
pub fn vector(comptime T: type) type
|
||||||
|
{
|
||||||
|
return struct
|
||||||
|
{
|
||||||
|
const Self: type = @This();
|
||||||
|
const VectorType: type = T;
|
||||||
|
|
||||||
|
const VectorError: type = error {
|
||||||
|
OutOfBounds,
|
||||||
|
};
|
||||||
|
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
array: []VectorType,
|
||||||
|
length: usize,
|
||||||
|
|
||||||
|
pub fn new(allocator: std.mem.Allocator) !Self
|
||||||
|
{
|
||||||
|
const DEFAULT_SIZE: usize = 16;
|
||||||
|
return Self
|
||||||
|
{
|
||||||
|
.allocator = allocator,
|
||||||
|
.array = try allocator.alloc(VectorType, DEFAULT_SIZE),
|
||||||
|
.length = 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_with_capacity(allocator: std.mem.Allocator, capacity: usize) !Self
|
||||||
|
{
|
||||||
|
return Self
|
||||||
|
{
|
||||||
|
.allocator = allocator,
|
||||||
|
.array = try allocator.alloc(VectorType, capacity),
|
||||||
|
.length = 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn push(self: *Self, item: VectorType) !void
|
||||||
|
{
|
||||||
|
if (self.length >= self.array.len)
|
||||||
|
{
|
||||||
|
const new_buffer: []VectorType = try self.allocator.alloc(VectorType, 2 * self.array.len);
|
||||||
|
@memcpy(new_buffer, self.array);
|
||||||
|
self.allocator.free(self.array);
|
||||||
|
self.array = new_buffer;
|
||||||
|
}
|
||||||
|
self.array[self.length] = item;
|
||||||
|
self.length += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pop(self: *Self) VectorType
|
||||||
|
{
|
||||||
|
self.length -= 1;
|
||||||
|
return self.array[self.length];
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn drain(self: *Self, range: [2]usize) VectorError![]VectorType
|
||||||
|
{
|
||||||
|
if (2 != range.len
|
||||||
|
or range[1] <= range[0]
|
||||||
|
or range[0] > self.array.len
|
||||||
|
or range[1] > self.array.len)
|
||||||
|
{
|
||||||
|
return VectorError.OutOfBounds;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: return array instead of pointer
|
||||||
|
const slice: []VectorType = self.array[range[0]..range[1]];
|
||||||
|
@memmove(self.array.ptr, self.array[range[1]..]);
|
||||||
|
self.length -= range[1] - range[0] + 1;
|
||||||
|
|
||||||
|
return slice;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user