diff --git a/CMakeLists.txt b/CMakeLists.txt index 8e610dd7b9b7f70532be1cdfeca577cca8f00105..5b3a7eb25ac1502e6481906ba027ecc91a9adf53 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -196,6 +196,7 @@ install(TARGETS zig DESTINATION bin) install(FILES ${C_HEADERS} DESTINATION ${C_HEADERS_DEST}) +install(FILES "${CMAKE_SOURCE_DIR}/std/array_list.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/base64.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/buf_map.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/buf_set.zig" DESTINATION "${ZIG_STD_DEST}") @@ -216,7 +217,6 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/hash_map.zig" DESTINATION "${ZIG_STD_DEST install(FILES "${CMAKE_SOURCE_DIR}/std/index.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/io.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/linked_list.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/list.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/math.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/mem.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/net.zig" DESTINATION "${ZIG_STD_DEST}") diff --git a/std/array_list.zig b/std/array_list.zig new file mode 100644 index 0000000000000000000000000000000000000000..a8e35f2b2088286e97295d6af213fb8aacd1cd16 --- /dev/null +++ b/std/array_list.zig @@ -0,0 +1,91 @@ +const debug = @import("debug.zig"); +const assert = debug.assert; +const mem = @import("mem.zig"); +const Allocator = mem.Allocator; + +pub fn ArrayList(comptime T: type) -> type{ + struct { + const Self = this; + + /// Use toSlice instead of slicing this directly, because if you don't + /// specify the end position of the slice, this will potentially give + /// you uninitialized memory. + items: []T, + len: usize, + allocator: &Allocator, + + pub fn init(allocator: &Allocator) -> Self { + Self { + .items = []T{}, + .len = 0, + .allocator = allocator, + } + } + + pub fn deinit(l: &Self) { + l.allocator.free(l.items); + } + + pub fn toSlice(l: &Self) -> []T { + return l.items[0...l.len]; + } + + pub fn toSliceConst(l: &const Self) -> []const T { + return l.items[0...l.len]; + } + + pub fn append(l: &Self, item: &const T) -> %void { + const new_item_ptr = %return l.addOne(); + *new_item_ptr = *item; + } + + pub fn resize(l: &Self, new_len: usize) -> %void { + %return l.ensureCapacity(new_len); + l.len = new_len; + } + + pub fn resizeDown(l: &Self, new_len: usize) { + assert(new_len <= l.len); + l.len = new_len; + } + + pub fn ensureCapacity(l: &Self, new_capacity: usize) -> %void { + var better_capacity = l.items.len; + if (better_capacity >= new_capacity) return; + while (true) { + better_capacity += better_capacity / 2 + 8; + if (better_capacity >= new_capacity) break; + } + l.items = %return l.allocator.realloc(T, l.items, better_capacity); + } + + pub fn addOne(l: &Self) -> %&T { + const new_length = l.len + 1; + %return l.ensureCapacity(new_length); + const result = &l.items[l.len]; + l.len = new_length; + return result; + } + + pub fn pop(self: &Self) -> T { + self.len -= 1; + return self.items[self.len]; + } + } +} + +test "basic ArrayList test" { + var list = ArrayList(i32).init(&debug.global_allocator); + defer list.deinit(); + + {var i: usize = 0; while (i < 10) : (i += 1) { + %%list.append(i32(i + 1)); + }} + + {var i: usize = 0; while (i < 10) : (i += 1) { + assert(list.items[i] == i32(i + 1)); + }} + + assert(list.pop() == 10); + assert(list.len == 9); +} diff --git a/std/buffer.zig b/std/buffer.zig index c7f086135615606caa99902e46c60d76a1aa3206..5afd5c2de90c8031b47d59456117838a2b49950e 100644 --- a/std/buffer.zig +++ b/std/buffer.zig @@ -2,11 +2,11 @@ const debug = @import("debug.zig"); const mem = @import("mem.zig"); const Allocator = mem.Allocator; const assert = debug.assert; -const List = @import("list.zig").List; +const ArrayList = @import("array_list.zig").ArrayList; /// A buffer that allocates memory and maintains a null byte at the end. pub const Buffer = struct { - list: List(u8), + list: ArrayList(u8), /// Must deinitialize with deinit. pub fn init(allocator: &Allocator, m: []const u8) -> %Buffer { @@ -29,7 +29,7 @@ pub const Buffer = struct { /// * ::resize pub fn initNull(allocator: &Allocator) -> Buffer { Buffer { - .list = List(u8).init(allocator), + .list = ArrayList(u8).init(allocator), } } diff --git a/std/build.zig b/std/build.zig index 00e8963338cf18b799e7a8d2e796921b37ba8f20..e15030b2038c07a3b865fb81e7582f46a90be5e5 100644 --- a/std/build.zig +++ b/std/build.zig @@ -3,7 +3,7 @@ const io = @import("io.zig"); const mem = @import("mem.zig"); const debug = @import("debug.zig"); const assert = debug.assert; -const List = @import("list.zig").List; +const ArrayList = @import("array_list.zig").ArrayList; const HashMap = @import("hash_map.zig").HashMap; const Allocator = @import("mem.zig").Allocator; const os = @import("os/index.zig"); @@ -26,22 +26,22 @@ pub const Builder = struct { have_uninstall_step: bool, have_install_step: bool, allocator: &Allocator, - lib_paths: List([]const u8), - include_paths: List([]const u8), - rpaths: List([]const u8), + lib_paths: ArrayList([]const u8), + include_paths: ArrayList([]const u8), + rpaths: ArrayList([]const u8), user_input_options: UserInputOptionsMap, available_options_map: AvailableOptionsMap, - available_options_list: List(AvailableOption), + available_options_list: ArrayList(AvailableOption), verbose: bool, invalid_user_input: bool, zig_exe: []const u8, default_step: &Step, env_map: BufMap, - top_level_steps: List(&TopLevelStep), + top_level_steps: ArrayList(&TopLevelStep), prefix: []const u8, lib_dir: []const u8, exe_dir: []const u8, - installed_files: List([]const u8), + installed_files: ArrayList([]const u8), build_root: []const u8, cache_root: []const u8, @@ -63,7 +63,7 @@ pub const Builder = struct { const UserValue = enum { Flag, Scalar: []const u8, - List: List([]const u8), + List: ArrayList([]const u8), }; const TypeId = enum { @@ -89,19 +89,19 @@ pub const Builder = struct { .verbose = false, .invalid_user_input = false, .allocator = allocator, - .lib_paths = List([]const u8).init(allocator), - .include_paths = List([]const u8).init(allocator), - .rpaths = List([]const u8).init(allocator), + .lib_paths = ArrayList([]const u8).init(allocator), + .include_paths = ArrayList([]const u8).init(allocator), + .rpaths = ArrayList([]const u8).init(allocator), .user_input_options = UserInputOptionsMap.init(allocator), .available_options_map = AvailableOptionsMap.init(allocator), - .available_options_list = List(AvailableOption).init(allocator), - .top_level_steps = List(&TopLevelStep).init(allocator), + .available_options_list = ArrayList(AvailableOption).init(allocator), + .top_level_steps = ArrayList(&TopLevelStep).init(allocator), .default_step = undefined, .env_map = %%os.getEnvMap(allocator), .prefix = undefined, .lib_dir = undefined, .exe_dir = undefined, - .installed_files = List([]const u8).init(allocator), + .installed_files = ArrayList([]const u8).init(allocator), .uninstall_tls = TopLevelStep { .step = Step.init("uninstall", allocator, makeUninstall), .description = "Remove build artifacts from prefix path", @@ -225,7 +225,7 @@ pub const Builder = struct { } pub fn make(self: &Builder, step_names: []const []const u8) -> %void { - var wanted_steps = List(&Step).init(self.allocator); + var wanted_steps = ArrayList(&Step).init(self.allocator); defer wanted_steps.deinit(); if (step_names.len == 0) { @@ -433,7 +433,7 @@ pub const Builder = struct { switch (prev_value.value) { UserValue.Scalar => |s| { // turn it into a list - var list = List([]const u8).init(self.allocator); + var list = ArrayList([]const u8).init(self.allocator); %%list.append(s); %%list.append(value); _ = %%self.user_input_options.put(name, UserInputOption { @@ -695,9 +695,9 @@ pub const LibExeObjStep = struct { out_filename: []const u8, major_only_filename: []const u8, name_only_filename: []const u8, - object_files: List([]const u8), - assembly_files: List([]const u8), - packages: List(Pkg), + object_files: ArrayList([]const u8), + assembly_files: ArrayList([]const u8), + packages: ArrayList(Pkg), const Pkg = struct { name: []const u8, @@ -758,9 +758,9 @@ pub const LibExeObjStep = struct { .out_h_filename = builder.fmt("{}.h", name), .major_only_filename = undefined, .name_only_filename = undefined, - .object_files = List([]const u8).init(builder.allocator), - .assembly_files = List([]const u8).init(builder.allocator), - .packages = List(Pkg).init(builder.allocator), + .object_files = ArrayList([]const u8).init(builder.allocator), + .assembly_files = ArrayList([]const u8).init(builder.allocator), + .packages = ArrayList(Pkg).init(builder.allocator), }; self.computeOutFileNames(); return self; @@ -875,7 +875,7 @@ pub const LibExeObjStep = struct { return error.NeedAnObject; } - var zig_args = List([]const u8).init(builder.allocator); + var zig_args = ArrayList([]const u8).init(builder.allocator); defer zig_args.deinit(); const cmd = switch (self.kind) { @@ -1043,7 +1043,7 @@ pub const TestStep = struct { const self = @fieldParentPtr(TestStep, "step", step); const builder = self.builder; - var zig_args = List([]const u8).init(builder.allocator); + var zig_args = ArrayList([]const u8).init(builder.allocator); defer zig_args.deinit(); %%zig_args.append("test"); @@ -1104,14 +1104,14 @@ pub const CLibExeObjStep = struct { output_path: ?[]const u8, static: bool, version: Version, - cflags: List([]const u8), - source_files: List([]const u8), - object_files: List([]const u8), + cflags: ArrayList([]const u8), + source_files: ArrayList([]const u8), + object_files: ArrayList([]const u8), link_libs: BufSet, - full_path_libs: List([]const u8), + full_path_libs: ArrayList([]const u8), target: Target, builder: &Builder, - include_dirs: List([]const u8), + include_dirs: ArrayList([]const u8), major_only_filename: []const u8, name_only_filename: []const u8, object_src: []const u8, @@ -1158,13 +1158,13 @@ pub const CLibExeObjStep = struct { .version = *version, .static = static, .target = Target.Native, - .cflags = List([]const u8).init(builder.allocator), - .source_files = List([]const u8).init(builder.allocator), - .object_files = List([]const u8).init(builder.allocator), + .cflags = ArrayList([]const u8).init(builder.allocator), + .source_files = ArrayList([]const u8).init(builder.allocator), + .object_files = ArrayList([]const u8).init(builder.allocator), .step = Step.init(name, builder.allocator, make), .link_libs = BufSet.init(builder.allocator), - .full_path_libs = List([]const u8).init(builder.allocator), - .include_dirs = List([]const u8).init(builder.allocator), + .full_path_libs = ArrayList([]const u8).init(builder.allocator), + .include_dirs = ArrayList([]const u8).init(builder.allocator), .output_path = null, .out_filename = undefined, .major_only_filename = undefined, @@ -1267,7 +1267,7 @@ pub const CLibExeObjStep = struct { } } - fn appendCompileFlags(self: &CLibExeObjStep, args: &List([]const u8)) { + fn appendCompileFlags(self: &CLibExeObjStep, args: &ArrayList([]const u8)) { if (!self.strip) { %%args.append("-g"); } @@ -1300,7 +1300,7 @@ pub const CLibExeObjStep = struct { const cc = os.getEnv("CC") ?? "cc"; const builder = self.builder; - var cc_args = List([]const u8).init(builder.allocator); + var cc_args = ArrayList([]const u8).init(builder.allocator); defer cc_args.deinit(); switch (self.kind) { @@ -1646,7 +1646,7 @@ pub const RemoveDirStep = struct { pub const Step = struct { name: []const u8, makeFn: fn(self: &Step) -> %void, - dependencies: List(&Step), + dependencies: ArrayList(&Step), loop_flag: bool, done_flag: bool, @@ -1654,7 +1654,7 @@ pub const Step = struct { Step { .name = name, .makeFn = makeFn, - .dependencies = List(&Step).init(allocator), + .dependencies = ArrayList(&Step).init(allocator), .loop_flag = false, .done_flag = false, } diff --git a/std/debug.zig b/std/debug.zig index 03b172e5d0b09d3cadf642e9f5bcbf4b3525806b..0b66802ced45a6210c0c7b60d665b87caf538403 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -3,7 +3,7 @@ const io = @import("io.zig"); const os = @import("os/index.zig"); const elf = @import("elf.zig"); const DW = @import("dwarf.zig"); -const List = @import("list.zig").List; +const ArrayList = @import("array_list.zig").ArrayList; const builtin = @import("builtin"); error MissingDebugInfo; @@ -60,8 +60,8 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty .debug_abbrev = undefined, .debug_str = undefined, .debug_line = undefined, - .abbrev_table_list = List(AbbrevTableHeader).init(allocator), - .compile_unit_list = List(CompileUnit).init(allocator), + .abbrev_table_list = ArrayList(AbbrevTableHeader).init(allocator), + .compile_unit_list = ArrayList(CompileUnit).init(allocator), }; const st = &stack_trace; st.self_exe_stream = %return io.openSelfExe(); @@ -179,8 +179,8 @@ const ElfStackTrace = struct { debug_abbrev: &elf.SectionHeader, debug_str: &elf.SectionHeader, debug_line: &elf.SectionHeader, - abbrev_table_list: List(AbbrevTableHeader), - compile_unit_list: List(CompileUnit), + abbrev_table_list: ArrayList(AbbrevTableHeader), + compile_unit_list: ArrayList(CompileUnit), pub fn allocator(self: &const ElfStackTrace) -> &mem.Allocator { return self.abbrev_table_list.allocator; @@ -204,7 +204,7 @@ const CompileUnit = struct { pc_range: ?PcRange, }; -const AbbrevTable = List(AbbrevTableEntry); +const AbbrevTable = ArrayList(AbbrevTableEntry); const AbbrevTableHeader = struct { // offset from .debug_abbrev @@ -216,7 +216,7 @@ const AbbrevTableEntry = struct { has_children: bool, abbrev_code: u64, tag_id: u64, - attrs: List(AbbrevAttr), + attrs: ArrayList(AbbrevAttr), }; const AbbrevAttr = struct { @@ -254,7 +254,7 @@ const Constant = struct { const Die = struct { tag_id: u64, has_children: bool, - attrs: List(Attr), + attrs: ArrayList(Attr), const Attr = struct { id: u64, @@ -324,7 +324,7 @@ const LineNumberProgram = struct { target_address: usize, include_dirs: []const []const u8, - file_entries: &List(FileEntry), + file_entries: &ArrayList(FileEntry), prev_address: usize, prev_file: usize, @@ -335,7 +335,7 @@ const LineNumberProgram = struct { prev_end_sequence: bool, pub fn init(is_stmt: bool, include_dirs: []const []const u8, - file_entries: &List(FileEntry), target_address: usize) -> LineNumberProgram + file_entries: &ArrayList(FileEntry), target_address: usize) -> LineNumberProgram { LineNumberProgram { .address = 0, @@ -394,7 +394,7 @@ const LineNumberProgram = struct { }; fn readStringRaw(allocator: &mem.Allocator, in_stream: &io.InStream) -> %[]u8 { - var buf = List(u8).init(allocator); + var buf = ArrayList(u8).init(allocator); while (true) { const byte = %return in_stream.readByte(); if (byte == 0) @@ -525,7 +525,7 @@ fn parseAbbrevTable(st: &ElfStackTrace) -> %AbbrevTable { .abbrev_code = abbrev_code, .tag_id = %return readULeb128(in_stream), .has_children = (%return in_stream.readByte()) == DW.CHILDREN_yes, - .attrs = List(AbbrevAttr).init(st.allocator()), + .attrs = ArrayList(AbbrevAttr).init(st.allocator()), }); const attrs = &result.items[result.len - 1].attrs; @@ -574,7 +574,7 @@ fn parseDie(st: &ElfStackTrace, abbrev_table: &const AbbrevTable, is_64: bool) - var result = Die { .tag_id = table_entry.tag_id, .has_children = table_entry.has_children, - .attrs = List(Die.Attr).init(st.allocator()), + .attrs = ArrayList(Die.Attr).init(st.allocator()), }; %return result.attrs.resize(table_entry.attrs.len); for (table_entry.attrs.toSliceConst()) |attr, i| { @@ -632,7 +632,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe standard_opcode_lengths[i] = %return in_stream.readByte(); }} - var include_directories = List([]u8).init(st.allocator()); + var include_directories = ArrayList([]u8).init(st.allocator()); %return include_directories.append(compile_unit_cwd); while (true) { const dir = %return st.readString(); @@ -641,7 +641,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe %return include_directories.append(dir); } - var file_entries = List(FileEntry).init(st.allocator()); + var file_entries = ArrayList(FileEntry).init(st.allocator()); var prog = LineNumberProgram.init(default_is_stmt, include_directories.toSliceConst(), &file_entries, target_address); diff --git a/std/index.zig b/std/index.zig index 8397741b46a0052aa6440932f832ea3aebffd612..b8b6029b55935abbfdd951d454c734a42a5285bb 100644 --- a/std/index.zig +++ b/std/index.zig @@ -1,15 +1,21 @@ +pub const ArrayList = @import("array_list.zig").ArrayList; +pub const BufMap = @import("buf_map.zig").BufMap; +pub const BufSet = @import("buf_set.zig").BufSet; +pub const Buffer = @import("buffer.zig").Buffer; +pub const HashMap = @import("hash_map.zig").HashMap; +pub const LinkedList = @import("linked_list.zig").LinkedList; + pub const base64 = @import("base64.zig"); -pub const buffer = @import("buffer.zig"); pub const build = @import("build.zig"); pub const c = @import("c/index.zig"); pub const cstr = @import("cstr.zig"); pub const debug = @import("debug.zig"); +pub const dwarf = @import("dwarf.zig"); +pub const elf = @import("elf.zig"); pub const empty_import = @import("empty.zig"); +pub const endian = @import("endian.zig"); pub const fmt = @import("fmt.zig"); -pub const hash_map = @import("hash_map.zig"); pub const io = @import("io.zig"); -pub const linked_list = @import("linked_list.zig"); -pub const list = @import("list.zig"); pub const math = @import("math.zig"); pub const mem = @import("mem.zig"); pub const net = @import("net.zig"); @@ -20,17 +26,24 @@ pub const target = @import("target.zig"); test "std" { // run tests from these + _ = @import("array_list.zig").ArrayList; + _ = @import("buf_map.zig").BufMap; + _ = @import("buf_set.zig").BufSet; + _ = @import("buffer.zig").Buffer; + _ = @import("hash_map.zig").HashMap; + _ = @import("linked_list.zig").LinkedList; + _ = @import("base64.zig"); - _ = @import("buffer.zig"); _ = @import("build.zig"); _ = @import("c/index.zig"); _ = @import("cstr.zig"); _ = @import("debug.zig"); + _ = @import("dwarf.zig"); + _ = @import("elf.zig"); + _ = @import("empty.zig"); + _ = @import("endian.zig"); _ = @import("fmt.zig"); - _ = @import("hash_map.zig"); _ = @import("io.zig"); - _ = @import("linked_list.zig"); - _ = @import("list.zig"); _ = @import("math.zig"); _ = @import("mem.zig"); _ = @import("net.zig"); diff --git a/std/linked_list.zig b/std/linked_list.zig index 1b933d8058582943b66a111a9dcc261069db9a3f..6811f5d99fc9247d47fb92ae4e81cc99cb37bef3 100644 --- a/std/linked_list.zig +++ b/std/linked_list.zig @@ -6,7 +6,7 @@ const Allocator = mem.Allocator; /// Generic doubly linked list. pub fn LinkedList(comptime T: type) -> type { struct { - const List = this; + const Self = this; /// Node inside the linked list wrapping the actual data. pub const Node = struct { @@ -27,8 +27,8 @@ pub fn LinkedList(comptime T: type) -> type { /// /// Returns: /// An empty linked list. - pub fn init(allocator: &Allocator) -> List { - List { + pub fn init(allocator: &Allocator) -> Self { + Self { .first = null, .last = null, .len = 0, @@ -41,7 +41,7 @@ pub fn LinkedList(comptime T: type) -> type { /// Arguments: /// node: Pointer to a node in the list. /// new_node: Pointer to the new node to insert. - pub fn insertAfter(list: &List, node: &Node, new_node: &Node) { + pub fn insertAfter(list: &Self, node: &Node, new_node: &Node) { new_node.prev = node; if (node.next) |next_node| { // Intermediate node. @@ -62,7 +62,7 @@ pub fn LinkedList(comptime T: type) -> type { /// Arguments: /// node: Pointer to a node in the list. /// new_node: Pointer to the new node to insert. - pub fn insertBefore(list: &List, node: &Node, new_node: &Node) { + pub fn insertBefore(list: &Self, node: &Node, new_node: &Node) { new_node.next = node; if (node.prev) |prev_node| { // Intermediate node. @@ -82,7 +82,7 @@ pub fn LinkedList(comptime T: type) -> type { /// /// Arguments: /// new_node: Pointer to the new node to insert. - pub fn append(list: &List, new_node: &Node) { + pub fn append(list: &Self, new_node: &Node) { if (list.last) |last| { // Insert after last. list.insertAfter(last, new_node); @@ -96,7 +96,7 @@ pub fn LinkedList(comptime T: type) -> type { /// /// Arguments: /// new_node: Pointer to the new node to insert. - pub fn prepend(list: &List, new_node: &Node) { + pub fn prepend(list: &Self, new_node: &Node) { if (list.first) |first| { // Insert before first. list.insertBefore(first, new_node); @@ -115,7 +115,7 @@ pub fn LinkedList(comptime T: type) -> type { /// /// Arguments: /// node: Pointer to the node to be removed. - pub fn remove(list: &List, node: &Node) { + pub fn remove(list: &Self, node: &Node) { if (node.prev) |prev_node| { // Intermediate node. prev_node.next = node.next; @@ -139,7 +139,7 @@ pub fn LinkedList(comptime T: type) -> type { /// /// Returns: /// A pointer to the last node in the list. - pub fn pop(list: &List) -> ?&Node { + pub fn pop(list: &Self) -> ?&Node { const last = list.last ?? return null; list.remove(last); return last; @@ -149,7 +149,7 @@ pub fn LinkedList(comptime T: type) -> type { /// /// Returns: /// A pointer to the first node in the list. - pub fn popFirst(list: &List) -> ?&Node { + pub fn popFirst(list: &Self) -> ?&Node { const first = list.first ?? return null; list.remove(first); return first; @@ -159,7 +159,7 @@ pub fn LinkedList(comptime T: type) -> type { /// /// Returns: /// A pointer to the new node. - pub fn allocateNode(list: &List) -> %&Node { + pub fn allocateNode(list: &Self) -> %&Node { list.allocator.create(Node) } @@ -167,7 +167,7 @@ pub fn LinkedList(comptime T: type) -> type { /// /// Arguments: /// node: Pointer to the node to deallocate. - pub fn destroyNode(list: &List, node: &Node) { + pub fn destroyNode(list: &Self, node: &Node) { list.allocator.destroy(node); } @@ -178,7 +178,7 @@ pub fn LinkedList(comptime T: type) -> type { /// /// Returns: /// A pointer to the new node. - pub fn createNode(list: &List, data: &const T) -> %&Node { + pub fn createNode(list: &Self, data: &const T) -> %&Node { var node = %return list.allocateNode(); *node = Node { .prev = null, diff --git a/std/list.zig b/std/list.zig deleted file mode 100644 index 54d32413ec06288b2b00054b3e1d5840906214e6..0000000000000000000000000000000000000000 --- a/std/list.zig +++ /dev/null @@ -1,91 +0,0 @@ -const debug = @import("debug.zig"); -const assert = debug.assert; -const mem = @import("mem.zig"); -const Allocator = mem.Allocator; - -pub fn List(comptime T: type) -> type{ - struct { - const Self = this; - - /// Use toSlice instead of slicing this directly, because if you don't - /// specify the end position of the slice, this will potentially give - /// you uninitialized memory. - items: []T, - len: usize, - allocator: &Allocator, - - pub fn init(allocator: &Allocator) -> Self { - Self { - .items = []T{}, - .len = 0, - .allocator = allocator, - } - } - - pub fn deinit(l: &Self) { - l.allocator.free(l.items); - } - - pub fn toSlice(l: &Self) -> []T { - return l.items[0...l.len]; - } - - pub fn toSliceConst(l: &const Self) -> []const T { - return l.items[0...l.len]; - } - - pub fn append(l: &Self, item: &const T) -> %void { - const new_item_ptr = %return l.addOne(); - *new_item_ptr = *item; - } - - pub fn resize(l: &Self, new_len: usize) -> %void { - %return l.ensureCapacity(new_len); - l.len = new_len; - } - - pub fn resizeDown(l: &Self, new_len: usize) { - assert(new_len <= l.len); - l.len = new_len; - } - - pub fn ensureCapacity(l: &Self, new_capacity: usize) -> %void { - var better_capacity = l.items.len; - if (better_capacity >= new_capacity) return; - while (true) { - better_capacity += better_capacity / 2 + 8; - if (better_capacity >= new_capacity) break; - } - l.items = %return l.allocator.realloc(T, l.items, better_capacity); - } - - pub fn addOne(l: &Self) -> %&T { - const new_length = l.len + 1; - %return l.ensureCapacity(new_length); - const result = &l.items[l.len]; - l.len = new_length; - return result; - } - - pub fn pop(self: &Self) -> T { - self.len -= 1; - return self.items[self.len]; - } - } -} - -test "basic list test" { - var list = List(i32).init(&debug.global_allocator); - defer list.deinit(); - - {var i: usize = 0; while (i < 10) : (i += 1) { - %%list.append(i32(i + 1)); - }} - - {var i: usize = 0; while (i < 10) : (i += 1) { - assert(list.items[i] == i32(i + 1)); - }} - - assert(list.pop() == 10); - assert(list.len == 9); -} diff --git a/std/os/index.zig b/std/os/index.zig index 9f2d0e326a2cae22b6d40847c4f301b9f61c938f..c00cc5bda1cb29e92d74b438a38b1514164ddabd 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -36,7 +36,7 @@ const cstr = @import("../cstr.zig"); const io = @import("../io.zig"); const base64 = @import("../base64.zig"); -const List = @import("../list.zig").List; +const ArrayList = @import("../array_list.zig").ArrayList; error Unexpected; error SystemResources; @@ -683,7 +683,7 @@ start_over: }; defer dir.close(); - var full_entry_buf = List(u8).init(allocator); + var full_entry_buf = ArrayList(u8).init(allocator); defer full_entry_buf.deinit(); while (%return dir.next()) |entry| { diff --git a/std/special/build_runner.zig b/std/special/build_runner.zig index 4c55b01ed84adf2fe6a399bd2e8cca993b5df076..d193b332e6ac75f2dbb03175b6dab44f922a5ffa 100644 --- a/std/special/build_runner.zig +++ b/std/special/build_runner.zig @@ -5,7 +5,7 @@ const fmt = std.fmt; const os = std.os; const Builder = std.build.Builder; const mem = std.mem; -const List = std.list.List; +const ArrayList = std.ArrayList; error InvalidArgs; @@ -51,7 +51,7 @@ pub fn main() -> %void { var builder = Builder.init(allocator, zig_exe, build_root, cache_root); defer builder.deinit(); - var targets = List([]const u8).init(allocator); + var targets = ArrayList([]const u8).init(allocator); var prefix: ?[]const u8 = null; diff --git a/test/tests.zig b/test/tests.zig index 28a4a5b7094bd8c5602b35b0402e3c6adb4b8b6e..bdf2010fe7829d56017cb494b7099f2fb6fac361 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -4,11 +4,11 @@ const build = std.build; const os = std.os; const StdIo = os.ChildProcess.StdIo; const Term = os.ChildProcess.Term; -const Buffer = std.buffer.Buffer; +const Buffer = std.Buffer; const io = std.io; const mem = std.mem; const fmt = std.fmt; -const List = std.list.List; +const ArrayList = std.ArrayList; const Mode = @import("builtin").Mode; const compare_output = @import("compare_output.zig"); @@ -138,7 +138,7 @@ pub const CompareOutputContext = struct { const TestCase = struct { name: []const u8, - sources: List(SourceFile), + sources: ArrayList(SourceFile), expected_output: []const u8, link_libc: bool, special: Special, @@ -304,7 +304,7 @@ pub const CompareOutputContext = struct { { var tc = TestCase { .name = name, - .sources = List(TestCase.SourceFile).init(self.b.allocator), + .sources = ArrayList(TestCase.SourceFile).init(self.b.allocator), .expected_output = expected_output, .link_libc = false, .special = special, @@ -432,8 +432,8 @@ pub const CompileErrorContext = struct { const TestCase = struct { name: []const u8, - sources: List(SourceFile), - expected_errors: List([]const u8), + sources: ArrayList(SourceFile), + expected_errors: ArrayList([]const u8), link_libc: bool, is_exe: bool, @@ -486,7 +486,7 @@ pub const CompileErrorContext = struct { const root_src = %%os.path.join(b.allocator, b.cache_root, self.case.sources.items[0].filename); const obj_path = %%os.path.join(b.allocator, b.cache_root, "test.o"); - var zig_args = List([]const u8).init(b.allocator); + var zig_args = ArrayList([]const u8).init(b.allocator); %%zig_args.append(if (self.case.is_exe) "build_exe" else "build_obj"); %%zig_args.append(b.pathFromRoot(root_src)); @@ -583,8 +583,8 @@ pub const CompileErrorContext = struct { const tc = %%self.b.allocator.create(TestCase); *tc = TestCase { .name = name, - .sources = List(TestCase.SourceFile).init(self.b.allocator), - .expected_errors = List([]const u8).init(self.b.allocator), + .sources = ArrayList(TestCase.SourceFile).init(self.b.allocator), + .expected_errors = ArrayList([]const u8).init(self.b.allocator), .link_libc = false, .is_exe = false, }; @@ -660,7 +660,7 @@ pub const BuildExamplesContext = struct { return; } - var zig_args = List([]const u8).init(b.allocator); + var zig_args = ArrayList([]const u8).init(b.allocator); %%zig_args.append("build"); %%zig_args.append("--build-file"); @@ -713,8 +713,8 @@ pub const ParseHContext = struct { const TestCase = struct { name: []const u8, - sources: List(SourceFile), - expected_lines: List([]const u8), + sources: ArrayList(SourceFile), + expected_lines: ArrayList([]const u8), allow_warnings: bool, const SourceFile = struct { @@ -761,7 +761,7 @@ pub const ParseHContext = struct { const root_src = %%os.path.join(b.allocator, b.cache_root, self.case.sources.items[0].filename); - var zig_args = List([]const u8).init(b.allocator); + var zig_args = ArrayList([]const u8).init(b.allocator); %%zig_args.append("parseh"); %%zig_args.append(b.pathFromRoot(root_src)); @@ -847,8 +847,8 @@ pub const ParseHContext = struct { const tc = %%self.b.allocator.create(TestCase); *tc = TestCase { .name = name, - .sources = List(TestCase.SourceFile).init(self.b.allocator), - .expected_lines = List([]const u8).init(self.b.allocator), + .sources = ArrayList(TestCase.SourceFile).init(self.b.allocator), + .expected_lines = ArrayList([]const u8).init(self.b.allocator), .allow_warnings = allow_warnings, }; tc.addSourceFile("source.h", source);