Zig utility library
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

new features and reorganize library

IamPyu 2e3fbb3a 922ae8ed

+150 -25
+1 -3
src/BinaryTree.zig src/data/BinaryTree.zig
··· 3 3 const Allocator = mem.Allocator; 4 4 const ArrayList = std.ArrayList; 5 5 6 - const ARRAY_LIST_CAPACITY = 30; 7 - 8 6 pub fn BinaryTree(comptime T: type) type { 9 7 return struct { 10 8 const Self = @This(); ··· 101 99 return Self{ 102 100 .allocator = gpa, 103 101 .binary_tree = BT.init(), 104 - .nodes = try NodeList.initCapacity(gpa, ARRAY_LIST_CAPACITY), 102 + .nodes = try NodeList.initCapacity(gpa, 10), 105 103 }; 106 104 } 107 105
src/Queue.zig src/data/Queue.zig
src/RingBuffer.zig src/data/RingBuffer.zig
+1 -1
src/SparseSet.zig src/data/SparseSet.zig
··· 136 136 _ = list.remove(i); 137 137 _ = try list.insert("it's a beautiful day"); 138 138 139 - const SliceIterator = @import("./SliceIterator.zig").SliceIterator; 139 + const SliceIterator = @import("../SliceIterator.zig").SliceIterator; 140 140 141 141 var iter = SliceIterator([]const u8).init(list.asSlice()); 142 142
+20
src/data/root.zig
··· 1 + const sparse_set = @import("./SparseSet.zig"); 2 + pub const SparseSet = sparse_set.SparseSet; 3 + 4 + const queue = @import("./Queue.zig"); 5 + pub const Queue = queue.Queue; 6 + pub const ArrayQueue = queue.ArrayQueue; 7 + 8 + const binary_tree = @import("./BinaryTree.zig"); 9 + 10 + // TODO: rope data structure based on `BinaryTree` 11 + 12 + const ring_buffer = @import("./RingBuffer.zig"); 13 + pub const RingBuffer = ring_buffer.RingBuffer; 14 + 15 + test { 16 + _ = queue; 17 + _ = sparse_set; 18 + // _ = binary_tree; // failing 19 + _ = ring_buffer; 20 + }
+47
src/gamedev/ResourcePath.zig
··· 1 + //! Resource Path 2 + 3 + const std = @import("std"); 4 + const Allocator = std.mem.Allocator; 5 + const mem = @import("../mem.zig"); 6 + 7 + const Self = @This(); 8 + 9 + pub const namespace_delim = ':'; 10 + pub const path_delim = '/'; 11 + 12 + pub fn isValidChar(c: u8) bool { 13 + return std.ascii.isAlphanumeric(c) || c == '_'; 14 + } 15 + 16 + pub fn isValidStr(s: []const u8) bool { 17 + if (s.len == 0) 18 + return false; 19 + 20 + if (s[0] == path_delim) 21 + return false; 22 + 23 + for (s) |c| { 24 + if (!isValidChar(c)) { 25 + return false; 26 + } 27 + } 28 + 29 + return true; 30 + } 31 + 32 + namespace: []const u8, 33 + path: []const u8, 34 + 35 + pub fn new(id: []const u8) !?Self { 36 + const tuple = mem.splitOnce(u8, id, &.{namespace_delim}) orelse return null; 37 + 38 + // TODO: should own its data. 39 + const namespace_ref = tuple.@"0"; 40 + const path_ref = tuple.@"1"; 41 + 42 + if (!isValidStr(namespace_ref) || !isValidStr(path_ref)) { 43 + return null; 44 + } 45 + } 46 + 47 + // TODO: not ready
+5
src/gamedev/root.zig
··· 1 + pub const tick = @import("./tick.zig"); 2 + 3 + test { 4 + _ = tick; 5 + }
+62
src/gamedev/tick.zig
··· 1 + //! Game scheduling with ticks 2 + 3 + const std = @import("std"); 4 + 5 + /// A game tick. 6 + /// 7 + /// `MS` describes how much milliseconds are in 1 tick. 8 + pub fn Tick(comptime MS: comptime_int) type { 9 + if (MS == 0) { 10 + @compileError("`MS` cannot be 0 in `Tick`"); 11 + } 12 + 13 + return struct { 14 + _inner: usize, 15 + 16 + const Self = @This(); 17 + 18 + /// Unit tick, equivelant to `1t` 19 + pub const single_tick = Self.new(1); 20 + 21 + /// Create a `Tick` of `n` ticks 22 + pub fn new(n: usize) Self { 23 + return .{ ._inner = n }; 24 + } 25 + 26 + /// Create a `Tick` from milliseconds 27 + pub fn fromMillis(ms: u64) Self { 28 + return Self.new(ms / MS); 29 + } 30 + 31 + /// Convert `self` to milliseconds 32 + pub fn asMillis(self: *const Self) u64 { 33 + return self._inner * MS; 34 + } 35 + 36 + /// Step by 1 tick 37 + pub fn step(self: *Self) void { 38 + self._inner += 1; 39 + } 40 + 41 + /// Sleep current thread for the amount of ticks in `self`. 42 + pub fn sleep(self: *const Self) void { 43 + std.Thread.sleep(self.asMillis() * 1_000_000); 44 + } 45 + }; 46 + } 47 + 48 + /// A tick type where `1t = 50ms` 49 + pub const Tick50 = Tick(50); 50 + 51 + /// A tick type where `1t = 25ms` 52 + pub const Tick25 = Tick(25); 53 + 54 + test "millis" { 55 + const t = Tick50.new(5); 56 + try std.testing.expectEqual(t.asMillis(), 250); 57 + } 58 + 59 + test "sleep" { 60 + const t = Tick25.new(5); 61 + t.sleep(); 62 + }
+5
src/net/root.zig
··· 1 + pub const packet = @import("./packet.zig"); 2 + 3 + test { 4 + _ = packet; 5 + }
src/packet.zig src/net/packet.zig
+9 -21
src/root.zig
··· 1 1 const std = @import("std"); 2 2 3 3 pub const math = @import("./math.zig"); 4 - 5 - pub const packet = @import("./packet.zig"); 6 - 7 - const sparse_set = @import("./SparseSet.zig"); 8 - pub const SparseSet = sparse_set.SparseSet; 9 - 4 + pub const mem = @import("./mem.zig"); 10 5 pub const StringBuilder = @import("./StringBuilder.zig"); 11 6 12 - pub const mem = @import("./mem.zig"); 13 - 14 7 const slice_iterator = @import("./SliceIterator.zig"); 15 8 pub const SliceIterator = slice_iterator.SliceIterator; 16 9 17 - pub const queue = @import("./Queue.zig"); 18 - const binary_tree = @import("./BinaryTree.zig"); 19 - 20 - // TODO: rope data structure based on `BinaryTree` 21 - 22 - const ring_buffer = @import("./RingBuffer.zig"); 23 - pub const RingBuffer = ring_buffer.RingBuffer; 10 + pub const net = @import("./net/root.zig"); 11 + pub const data = @import("./data/root.zig"); 12 + pub const gamedev = @import("./gamedev/root.zig"); 24 13 25 14 test { 26 15 _ = math; 27 - _ = packet; 28 - _ = sparse_set; 29 - _ = StringBuilder; 30 16 _ = mem; 17 + _ = StringBuilder; 31 18 _ = slice_iterator; 32 - _ = queue; 33 - _ = binary_tree; 34 - _ = ring_buffer; 19 + 20 + _ = net; 21 + _ = data; 22 + _ = gamedev; 35 23 }