authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-05-04 14:05:06-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-05-04 14:05:06-04:00
log5c094d7390a7225f942032992b6070dcb6b9f761
tree9edaf26aabc9a5117828e1af4cd2f5d3bab47c91
parentb6a679c0edd74d996bd0c1769cf4b161b4d42c4d

std: rename List to ArrayList and re-organize...

...the exports of std. closes #356

11 files changed, 201 insertions(+), 188 deletions(-)

CMakeLists.txt+1-1
......@@ -196,6 +196,7 @@ install(TARGETS zig DESTINATION bin)
196196
197197install(FILES ${C_HEADERS} DESTINATION ${C_HEADERS_DEST})
198198
199install(FILES "${CMAKE_SOURCE_DIR}/std/array_list.zig" DESTINATION "${ZIG_STD_DEST}")
199200install(FILES "${CMAKE_SOURCE_DIR}/std/base64.zig" DESTINATION "${ZIG_STD_DEST}")
200201install(FILES "${CMAKE_SOURCE_DIR}/std/buf_map.zig" DESTINATION "${ZIG_STD_DEST}")
201202install(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
216217install(FILES "${CMAKE_SOURCE_DIR}/std/index.zig" DESTINATION "${ZIG_STD_DEST}")
217218install(FILES "${CMAKE_SOURCE_DIR}/std/io.zig" DESTINATION "${ZIG_STD_DEST}")
218219install(FILES "${CMAKE_SOURCE_DIR}/std/linked_list.zig" DESTINATION "${ZIG_STD_DEST}")
219install(FILES "${CMAKE_SOURCE_DIR}/std/list.zig" DESTINATION "${ZIG_STD_DEST}")
220220install(FILES "${CMAKE_SOURCE_DIR}/std/math.zig" DESTINATION "${ZIG_STD_DEST}")
221221install(FILES "${CMAKE_SOURCE_DIR}/std/mem.zig" DESTINATION "${ZIG_STD_DEST}")
222222install(FILES "${CMAKE_SOURCE_DIR}/std/net.zig" DESTINATION "${ZIG_STD_DEST}")
std/array_list.zig created+91
......@@ -0,0 +1,91 @@
1const debug = @import("debug.zig");
2const assert = debug.assert;
3const mem = @import("mem.zig");
4const Allocator = mem.Allocator;
5
6pub fn ArrayList(comptime T: type) -> type{
7 struct {
8 const Self = this;
9
10 /// Use toSlice instead of slicing this directly, because if you don't
11 /// specify the end position of the slice, this will potentially give
12 /// you uninitialized memory.
13 items: []T,
14 len: usize,
15 allocator: &Allocator,
16
17 pub fn init(allocator: &Allocator) -> Self {
18 Self {
19 .items = []T{},
20 .len = 0,
21 .allocator = allocator,
22 }
23 }
24
25 pub fn deinit(l: &Self) {
26 l.allocator.free(l.items);
27 }
28
29 pub fn toSlice(l: &Self) -> []T {
30 return l.items[0...l.len];
31 }
32
33 pub fn toSliceConst(l: &const Self) -> []const T {
34 return l.items[0...l.len];
35 }
36
37 pub fn append(l: &Self, item: &const T) -> %void {
38 const new_item_ptr = %return l.addOne();
39 *new_item_ptr = *item;
40 }
41
42 pub fn resize(l: &Self, new_len: usize) -> %void {
43 %return l.ensureCapacity(new_len);
44 l.len = new_len;
45 }
46
47 pub fn resizeDown(l: &Self, new_len: usize) {
48 assert(new_len <= l.len);
49 l.len = new_len;
50 }
51
52 pub fn ensureCapacity(l: &Self, new_capacity: usize) -> %void {
53 var better_capacity = l.items.len;
54 if (better_capacity >= new_capacity) return;
55 while (true) {
56 better_capacity += better_capacity / 2 + 8;
57 if (better_capacity >= new_capacity) break;
58 }
59 l.items = %return l.allocator.realloc(T, l.items, better_capacity);
60 }
61
62 pub fn addOne(l: &Self) -> %&T {
63 const new_length = l.len + 1;
64 %return l.ensureCapacity(new_length);
65 const result = &l.items[l.len];
66 l.len = new_length;
67 return result;
68 }
69
70 pub fn pop(self: &Self) -> T {
71 self.len -= 1;
72 return self.items[self.len];
73 }
74 }
75}
76
77test "basic ArrayList test" {
78 var list = ArrayList(i32).init(&debug.global_allocator);
79 defer list.deinit();
80
81 {var i: usize = 0; while (i < 10) : (i += 1) {
82 %%list.append(i32(i + 1));
83 }}
84
85 {var i: usize = 0; while (i < 10) : (i += 1) {
86 assert(list.items[i] == i32(i + 1));
87 }}
88
89 assert(list.pop() == 10);
90 assert(list.len == 9);
91}
std/buffer.zig+3-3
......@@ -2,11 +2,11 @@ const debug = @import("debug.zig");
22const mem = @import("mem.zig");
33const Allocator = mem.Allocator;
44const assert = debug.assert;
5const List = @import("list.zig").List;
5const ArrayList = @import("array_list.zig").ArrayList;
66
77/// A buffer that allocates memory and maintains a null byte at the end.
88pub const Buffer = struct {
9 list: List(u8),
9 list: ArrayList(u8),
1010
1111 /// Must deinitialize with deinit.
1212 pub fn init(allocator: &Allocator, m: []const u8) -> %Buffer {
......@@ -29,7 +29,7 @@ pub const Buffer = struct {
2929 /// * ::resize
3030 pub fn initNull(allocator: &Allocator) -> Buffer {
3131 Buffer {
32 .list = List(u8).init(allocator),
32 .list = ArrayList(u8).init(allocator),
3333 }
3434 }
3535
std/build.zig+38-38
......@@ -3,7 +3,7 @@ const io = @import("io.zig");
33const mem = @import("mem.zig");
44const debug = @import("debug.zig");
55const assert = debug.assert;
6const List = @import("list.zig").List;
6const ArrayList = @import("array_list.zig").ArrayList;
77const HashMap = @import("hash_map.zig").HashMap;
88const Allocator = @import("mem.zig").Allocator;
99const os = @import("os/index.zig");
......@@ -26,22 +26,22 @@ pub const Builder = struct {
2626 have_uninstall_step: bool,
2727 have_install_step: bool,
2828 allocator: &Allocator,
29 lib_paths: List([]const u8),
30 include_paths: List([]const u8),
31 rpaths: List([]const u8),
29 lib_paths: ArrayList([]const u8),
30 include_paths: ArrayList([]const u8),
31 rpaths: ArrayList([]const u8),
3232 user_input_options: UserInputOptionsMap,
3333 available_options_map: AvailableOptionsMap,
34 available_options_list: List(AvailableOption),
34 available_options_list: ArrayList(AvailableOption),
3535 verbose: bool,
3636 invalid_user_input: bool,
3737 zig_exe: []const u8,
3838 default_step: &Step,
3939 env_map: BufMap,
40 top_level_steps: List(&TopLevelStep),
40 top_level_steps: ArrayList(&TopLevelStep),
4141 prefix: []const u8,
4242 lib_dir: []const u8,
4343 exe_dir: []const u8,
44 installed_files: List([]const u8),
44 installed_files: ArrayList([]const u8),
4545 build_root: []const u8,
4646 cache_root: []const u8,
4747
......@@ -63,7 +63,7 @@ pub const Builder = struct {
6363 const UserValue = enum {
6464 Flag,
6565 Scalar: []const u8,
66 List: List([]const u8),
66 List: ArrayList([]const u8),
6767 };
6868
6969 const TypeId = enum {
......@@ -89,19 +89,19 @@ pub const Builder = struct {
8989 .verbose = false,
9090 .invalid_user_input = false,
9191 .allocator = allocator,
92 .lib_paths = List([]const u8).init(allocator),
93 .include_paths = List([]const u8).init(allocator),
94 .rpaths = List([]const u8).init(allocator),
92 .lib_paths = ArrayList([]const u8).init(allocator),
93 .include_paths = ArrayList([]const u8).init(allocator),
94 .rpaths = ArrayList([]const u8).init(allocator),
9595 .user_input_options = UserInputOptionsMap.init(allocator),
9696 .available_options_map = AvailableOptionsMap.init(allocator),
97 .available_options_list = List(AvailableOption).init(allocator),
98 .top_level_steps = List(&TopLevelStep).init(allocator),
97 .available_options_list = ArrayList(AvailableOption).init(allocator),
98 .top_level_steps = ArrayList(&TopLevelStep).init(allocator),
9999 .default_step = undefined,
100100 .env_map = %%os.getEnvMap(allocator),
101101 .prefix = undefined,
102102 .lib_dir = undefined,
103103 .exe_dir = undefined,
104 .installed_files = List([]const u8).init(allocator),
104 .installed_files = ArrayList([]const u8).init(allocator),
105105 .uninstall_tls = TopLevelStep {
106106 .step = Step.init("uninstall", allocator, makeUninstall),
107107 .description = "Remove build artifacts from prefix path",
......@@ -225,7 +225,7 @@ pub const Builder = struct {
225225 }
226226
227227 pub fn make(self: &Builder, step_names: []const []const u8) -> %void {
228 var wanted_steps = List(&Step).init(self.allocator);
228 var wanted_steps = ArrayList(&Step).init(self.allocator);
229229 defer wanted_steps.deinit();
230230
231231 if (step_names.len == 0) {
......@@ -433,7 +433,7 @@ pub const Builder = struct {
433433 switch (prev_value.value) {
434434 UserValue.Scalar => |s| {
435435 // turn it into a list
436 var list = List([]const u8).init(self.allocator);
436 var list = ArrayList([]const u8).init(self.allocator);
437437 %%list.append(s);
438438 %%list.append(value);
439439 _ = %%self.user_input_options.put(name, UserInputOption {
......@@ -695,9 +695,9 @@ pub const LibExeObjStep = struct {
695695 out_filename: []const u8,
696696 major_only_filename: []const u8,
697697 name_only_filename: []const u8,
698 object_files: List([]const u8),
699 assembly_files: List([]const u8),
700 packages: List(Pkg),
698 object_files: ArrayList([]const u8),
699 assembly_files: ArrayList([]const u8),
700 packages: ArrayList(Pkg),
701701
702702 const Pkg = struct {
703703 name: []const u8,
......@@ -758,9 +758,9 @@ pub const LibExeObjStep = struct {
758758 .out_h_filename = builder.fmt("{}.h", name),
759759 .major_only_filename = undefined,
760760 .name_only_filename = undefined,
761 .object_files = List([]const u8).init(builder.allocator),
762 .assembly_files = List([]const u8).init(builder.allocator),
763 .packages = List(Pkg).init(builder.allocator),
761 .object_files = ArrayList([]const u8).init(builder.allocator),
762 .assembly_files = ArrayList([]const u8).init(builder.allocator),
763 .packages = ArrayList(Pkg).init(builder.allocator),
764764 };
765765 self.computeOutFileNames();
766766 return self;
......@@ -875,7 +875,7 @@ pub const LibExeObjStep = struct {
875875 return error.NeedAnObject;
876876 }
877877
878 var zig_args = List([]const u8).init(builder.allocator);
878 var zig_args = ArrayList([]const u8).init(builder.allocator);
879879 defer zig_args.deinit();
880880
881881 const cmd = switch (self.kind) {
......@@ -1043,7 +1043,7 @@ pub const TestStep = struct {
10431043 const self = @fieldParentPtr(TestStep, "step", step);
10441044 const builder = self.builder;
10451045
1046 var zig_args = List([]const u8).init(builder.allocator);
1046 var zig_args = ArrayList([]const u8).init(builder.allocator);
10471047 defer zig_args.deinit();
10481048
10491049 %%zig_args.append("test");
......@@ -1104,14 +1104,14 @@ pub const CLibExeObjStep = struct {
11041104 output_path: ?[]const u8,
11051105 static: bool,
11061106 version: Version,
1107 cflags: List([]const u8),
1108 source_files: List([]const u8),
1109 object_files: List([]const u8),
1107 cflags: ArrayList([]const u8),
1108 source_files: ArrayList([]const u8),
1109 object_files: ArrayList([]const u8),
11101110 link_libs: BufSet,
1111 full_path_libs: List([]const u8),
1111 full_path_libs: ArrayList([]const u8),
11121112 target: Target,
11131113 builder: &Builder,
1114 include_dirs: List([]const u8),
1114 include_dirs: ArrayList([]const u8),
11151115 major_only_filename: []const u8,
11161116 name_only_filename: []const u8,
11171117 object_src: []const u8,
......@@ -1158,13 +1158,13 @@ pub const CLibExeObjStep = struct {
11581158 .version = *version,
11591159 .static = static,
11601160 .target = Target.Native,
1161 .cflags = List([]const u8).init(builder.allocator),
1162 .source_files = List([]const u8).init(builder.allocator),
1163 .object_files = List([]const u8).init(builder.allocator),
1161 .cflags = ArrayList([]const u8).init(builder.allocator),
1162 .source_files = ArrayList([]const u8).init(builder.allocator),
1163 .object_files = ArrayList([]const u8).init(builder.allocator),
11641164 .step = Step.init(name, builder.allocator, make),
11651165 .link_libs = BufSet.init(builder.allocator),
1166 .full_path_libs = List([]const u8).init(builder.allocator),
1167 .include_dirs = List([]const u8).init(builder.allocator),
1166 .full_path_libs = ArrayList([]const u8).init(builder.allocator),
1167 .include_dirs = ArrayList([]const u8).init(builder.allocator),
11681168 .output_path = null,
11691169 .out_filename = undefined,
11701170 .major_only_filename = undefined,
......@@ -1267,7 +1267,7 @@ pub const CLibExeObjStep = struct {
12671267 }
12681268 }
12691269
1270 fn appendCompileFlags(self: &CLibExeObjStep, args: &List([]const u8)) {
1270 fn appendCompileFlags(self: &CLibExeObjStep, args: &ArrayList([]const u8)) {
12711271 if (!self.strip) {
12721272 %%args.append("-g");
12731273 }
......@@ -1300,7 +1300,7 @@ pub const CLibExeObjStep = struct {
13001300 const cc = os.getEnv("CC") ?? "cc";
13011301 const builder = self.builder;
13021302
1303 var cc_args = List([]const u8).init(builder.allocator);
1303 var cc_args = ArrayList([]const u8).init(builder.allocator);
13041304 defer cc_args.deinit();
13051305
13061306 switch (self.kind) {
......@@ -1646,7 +1646,7 @@ pub const RemoveDirStep = struct {
16461646pub const Step = struct {
16471647 name: []const u8,
16481648 makeFn: fn(self: &Step) -> %void,
1649 dependencies: List(&Step),
1649 dependencies: ArrayList(&Step),
16501650 loop_flag: bool,
16511651 done_flag: bool,
16521652
......@@ -1654,7 +1654,7 @@ pub const Step = struct {
16541654 Step {
16551655 .name = name,
16561656 .makeFn = makeFn,
1657 .dependencies = List(&Step).init(allocator),
1657 .dependencies = ArrayList(&Step).init(allocator),
16581658 .loop_flag = false,
16591659 .done_flag = false,
16601660 }
std/debug.zig+15-15
......@@ -3,7 +3,7 @@ const io = @import("io.zig");
33const os = @import("os/index.zig");
44const elf = @import("elf.zig");
55const DW = @import("dwarf.zig");
6const List = @import("list.zig").List;
6const ArrayList = @import("array_list.zig").ArrayList;
77const builtin = @import("builtin");
88
99error MissingDebugInfo;
......@@ -60,8 +60,8 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty
6060 .debug_abbrev = undefined,
6161 .debug_str = undefined,
6262 .debug_line = undefined,
63 .abbrev_table_list = List(AbbrevTableHeader).init(allocator),
64 .compile_unit_list = List(CompileUnit).init(allocator),
63 .abbrev_table_list = ArrayList(AbbrevTableHeader).init(allocator),
64 .compile_unit_list = ArrayList(CompileUnit).init(allocator),
6565 };
6666 const st = &stack_trace;
6767 st.self_exe_stream = %return io.openSelfExe();
......@@ -179,8 +179,8 @@ const ElfStackTrace = struct {
179179 debug_abbrev: &elf.SectionHeader,
180180 debug_str: &elf.SectionHeader,
181181 debug_line: &elf.SectionHeader,
182 abbrev_table_list: List(AbbrevTableHeader),
183 compile_unit_list: List(CompileUnit),
182 abbrev_table_list: ArrayList(AbbrevTableHeader),
183 compile_unit_list: ArrayList(CompileUnit),
184184
185185 pub fn allocator(self: &const ElfStackTrace) -> &mem.Allocator {
186186 return self.abbrev_table_list.allocator;
......@@ -204,7 +204,7 @@ const CompileUnit = struct {
204204 pc_range: ?PcRange,
205205};
206206
207const AbbrevTable = List(AbbrevTableEntry);
207const AbbrevTable = ArrayList(AbbrevTableEntry);
208208
209209const AbbrevTableHeader = struct {
210210 // offset from .debug_abbrev
......@@ -216,7 +216,7 @@ const AbbrevTableEntry = struct {
216216 has_children: bool,
217217 abbrev_code: u64,
218218 tag_id: u64,
219 attrs: List(AbbrevAttr),
219 attrs: ArrayList(AbbrevAttr),
220220};
221221
222222const AbbrevAttr = struct {
......@@ -254,7 +254,7 @@ const Constant = struct {
254254const Die = struct {
255255 tag_id: u64,
256256 has_children: bool,
257 attrs: List(Attr),
257 attrs: ArrayList(Attr),
258258
259259 const Attr = struct {
260260 id: u64,
......@@ -324,7 +324,7 @@ const LineNumberProgram = struct {
324324
325325 target_address: usize,
326326 include_dirs: []const []const u8,
327 file_entries: &List(FileEntry),
327 file_entries: &ArrayList(FileEntry),
328328
329329 prev_address: usize,
330330 prev_file: usize,
......@@ -335,7 +335,7 @@ const LineNumberProgram = struct {
335335 prev_end_sequence: bool,
336336
337337 pub fn init(is_stmt: bool, include_dirs: []const []const u8,
338 file_entries: &List(FileEntry), target_address: usize) -> LineNumberProgram
338 file_entries: &ArrayList(FileEntry), target_address: usize) -> LineNumberProgram
339339 {
340340 LineNumberProgram {
341341 .address = 0,
......@@ -394,7 +394,7 @@ const LineNumberProgram = struct {
394394};
395395
396396fn readStringRaw(allocator: &mem.Allocator, in_stream: &io.InStream) -> %[]u8 {
397 var buf = List(u8).init(allocator);
397 var buf = ArrayList(u8).init(allocator);
398398 while (true) {
399399 const byte = %return in_stream.readByte();
400400 if (byte == 0)
......@@ -525,7 +525,7 @@ fn parseAbbrevTable(st: &ElfStackTrace) -> %AbbrevTable {
525525 .abbrev_code = abbrev_code,
526526 .tag_id = %return readULeb128(in_stream),
527527 .has_children = (%return in_stream.readByte()) == DW.CHILDREN_yes,
528 .attrs = List(AbbrevAttr).init(st.allocator()),
528 .attrs = ArrayList(AbbrevAttr).init(st.allocator()),
529529 });
530530 const attrs = &result.items[result.len - 1].attrs;
531531
......@@ -574,7 +574,7 @@ fn parseDie(st: &ElfStackTrace, abbrev_table: &const AbbrevTable, is_64: bool) -
574574 var result = Die {
575575 .tag_id = table_entry.tag_id,
576576 .has_children = table_entry.has_children,
577 .attrs = List(Die.Attr).init(st.allocator()),
577 .attrs = ArrayList(Die.Attr).init(st.allocator()),
578578 };
579579 %return result.attrs.resize(table_entry.attrs.len);
580580 for (table_entry.attrs.toSliceConst()) |attr, i| {
......@@ -632,7 +632,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
632632 standard_opcode_lengths[i] = %return in_stream.readByte();
633633 }}
634634
635 var include_directories = List([]u8).init(st.allocator());
635 var include_directories = ArrayList([]u8).init(st.allocator());
636636 %return include_directories.append(compile_unit_cwd);
637637 while (true) {
638638 const dir = %return st.readString();
......@@ -641,7 +641,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
641641 %return include_directories.append(dir);
642642 }
643643
644 var file_entries = List(FileEntry).init(st.allocator());
644 var file_entries = ArrayList(FileEntry).init(st.allocator());
645645 var prog = LineNumberProgram.init(default_is_stmt, include_directories.toSliceConst(),
646646 &file_entries, target_address);
647647
std/index.zig+21-8
......@@ -1,15 +1,21 @@
1pub const ArrayList = @import("array_list.zig").ArrayList;
2pub const BufMap = @import("buf_map.zig").BufMap;
3pub const BufSet = @import("buf_set.zig").BufSet;
4pub const Buffer = @import("buffer.zig").Buffer;
5pub const HashMap = @import("hash_map.zig").HashMap;
6pub const LinkedList = @import("linked_list.zig").LinkedList;
7
18pub const base64 = @import("base64.zig");
2pub const buffer = @import("buffer.zig");
39pub const build = @import("build.zig");
410pub const c = @import("c/index.zig");
511pub const cstr = @import("cstr.zig");
612pub const debug = @import("debug.zig");
13pub const dwarf = @import("dwarf.zig");
14pub const elf = @import("elf.zig");
715pub const empty_import = @import("empty.zig");
16pub const endian = @import("endian.zig");
817pub const fmt = @import("fmt.zig");
9pub const hash_map = @import("hash_map.zig");
1018pub const io = @import("io.zig");
11pub const linked_list = @import("linked_list.zig");
12pub const list = @import("list.zig");
1319pub const math = @import("math.zig");
1420pub const mem = @import("mem.zig");
1521pub const net = @import("net.zig");
......@@ -20,17 +26,24 @@ pub const target = @import("target.zig");
2026
2127test "std" {
2228 // run tests from these
29 _ = @import("array_list.zig").ArrayList;
30 _ = @import("buf_map.zig").BufMap;
31 _ = @import("buf_set.zig").BufSet;
32 _ = @import("buffer.zig").Buffer;
33 _ = @import("hash_map.zig").HashMap;
34 _ = @import("linked_list.zig").LinkedList;
35
2336 _ = @import("base64.zig");
24 _ = @import("buffer.zig");
2537 _ = @import("build.zig");
2638 _ = @import("c/index.zig");
2739 _ = @import("cstr.zig");
2840 _ = @import("debug.zig");
41 _ = @import("dwarf.zig");
42 _ = @import("elf.zig");
43 _ = @import("empty.zig");
44 _ = @import("endian.zig");
2945 _ = @import("fmt.zig");
30 _ = @import("hash_map.zig");
3146 _ = @import("io.zig");
32 _ = @import("linked_list.zig");
33 _ = @import("list.zig");
3447 _ = @import("math.zig");
3548 _ = @import("mem.zig");
3649 _ = @import("net.zig");
std/linked_list.zig+13-13
......@@ -6,7 +6,7 @@ const Allocator = mem.Allocator;
66/// Generic doubly linked list.
77pub fn LinkedList(comptime T: type) -> type {
88 struct {
9 const List = this;
9 const Self = this;
1010
1111 /// Node inside the linked list wrapping the actual data.
1212 pub const Node = struct {
......@@ -27,8 +27,8 @@ pub fn LinkedList(comptime T: type) -> type {
2727 ///
2828 /// Returns:
2929 /// An empty linked list.
30 pub fn init(allocator: &Allocator) -> List {
31 List {
30 pub fn init(allocator: &Allocator) -> Self {
31 Self {
3232 .first = null,
3333 .last = null,
3434 .len = 0,
......@@ -41,7 +41,7 @@ pub fn LinkedList(comptime T: type) -> type {
4141 /// Arguments:
4242 /// node: Pointer to a node in the list.
4343 /// new_node: Pointer to the new node to insert.
44 pub fn insertAfter(list: &List, node: &Node, new_node: &Node) {
44 pub fn insertAfter(list: &Self, node: &Node, new_node: &Node) {
4545 new_node.prev = node;
4646 if (node.next) |next_node| {
4747 // Intermediate node.
......@@ -62,7 +62,7 @@ pub fn LinkedList(comptime T: type) -> type {
6262 /// Arguments:
6363 /// node: Pointer to a node in the list.
6464 /// new_node: Pointer to the new node to insert.
65 pub fn insertBefore(list: &List, node: &Node, new_node: &Node) {
65 pub fn insertBefore(list: &Self, node: &Node, new_node: &Node) {
6666 new_node.next = node;
6767 if (node.prev) |prev_node| {
6868 // Intermediate node.
......@@ -82,7 +82,7 @@ pub fn LinkedList(comptime T: type) -> type {
8282 ///
8383 /// Arguments:
8484 /// new_node: Pointer to the new node to insert.
85 pub fn append(list: &List, new_node: &Node) {
85 pub fn append(list: &Self, new_node: &Node) {
8686 if (list.last) |last| {
8787 // Insert after last.
8888 list.insertAfter(last, new_node);
......@@ -96,7 +96,7 @@ pub fn LinkedList(comptime T: type) -> type {
9696 ///
9797 /// Arguments:
9898 /// new_node: Pointer to the new node to insert.
99 pub fn prepend(list: &List, new_node: &Node) {
99 pub fn prepend(list: &Self, new_node: &Node) {
100100 if (list.first) |first| {
101101 // Insert before first.
102102 list.insertBefore(first, new_node);
......@@ -115,7 +115,7 @@ pub fn LinkedList(comptime T: type) -> type {
115115 ///
116116 /// Arguments:
117117 /// node: Pointer to the node to be removed.
118 pub fn remove(list: &List, node: &Node) {
118 pub fn remove(list: &Self, node: &Node) {
119119 if (node.prev) |prev_node| {
120120 // Intermediate node.
121121 prev_node.next = node.next;
......@@ -139,7 +139,7 @@ pub fn LinkedList(comptime T: type) -> type {
139139 ///
140140 /// Returns:
141141 /// A pointer to the last node in the list.
142 pub fn pop(list: &List) -> ?&Node {
142 pub fn pop(list: &Self) -> ?&Node {
143143 const last = list.last ?? return null;
144144 list.remove(last);
145145 return last;
......@@ -149,7 +149,7 @@ pub fn LinkedList(comptime T: type) -> type {
149149 ///
150150 /// Returns:
151151 /// A pointer to the first node in the list.
152 pub fn popFirst(list: &List) -> ?&Node {
152 pub fn popFirst(list: &Self) -> ?&Node {
153153 const first = list.first ?? return null;
154154 list.remove(first);
155155 return first;
......@@ -159,7 +159,7 @@ pub fn LinkedList(comptime T: type) -> type {
159159 ///
160160 /// Returns:
161161 /// A pointer to the new node.
162 pub fn allocateNode(list: &List) -> %&Node {
162 pub fn allocateNode(list: &Self) -> %&Node {
163163 list.allocator.create(Node)
164164 }
165165
......@@ -167,7 +167,7 @@ pub fn LinkedList(comptime T: type) -> type {
167167 ///
168168 /// Arguments:
169169 /// node: Pointer to the node to deallocate.
170 pub fn destroyNode(list: &List, node: &Node) {
170 pub fn destroyNode(list: &Self, node: &Node) {
171171 list.allocator.destroy(node);
172172 }
173173
......@@ -178,7 +178,7 @@ pub fn LinkedList(comptime T: type) -> type {
178178 ///
179179 /// Returns:
180180 /// A pointer to the new node.
181 pub fn createNode(list: &List, data: &const T) -> %&Node {
181 pub fn createNode(list: &Self, data: &const T) -> %&Node {
182182 var node = %return list.allocateNode();
183183 *node = Node {
184184 .prev = null,
std/list.zig deleted-91
......@@ -1,91 +0,0 @@
1const debug = @import("debug.zig");
2const assert = debug.assert;
3const mem = @import("mem.zig");
4const Allocator = mem.Allocator;
5
6pub fn List(comptime T: type) -> type{
7 struct {
8 const Self = this;
9
10 /// Use toSlice instead of slicing this directly, because if you don't
11 /// specify the end position of the slice, this will potentially give
12 /// you uninitialized memory.
13 items: []T,
14 len: usize,
15 allocator: &Allocator,
16
17 pub fn init(allocator: &Allocator) -> Self {
18 Self {
19 .items = []T{},
20 .len = 0,
21 .allocator = allocator,
22 }
23 }
24
25 pub fn deinit(l: &Self) {
26 l.allocator.free(l.items);
27 }
28
29 pub fn toSlice(l: &Self) -> []T {
30 return l.items[0...l.len];
31 }
32
33 pub fn toSliceConst(l: &const Self) -> []const T {
34 return l.items[0...l.len];
35 }
36
37 pub fn append(l: &Self, item: &const T) -> %void {
38 const new_item_ptr = %return l.addOne();
39 *new_item_ptr = *item;
40 }
41
42 pub fn resize(l: &Self, new_len: usize) -> %void {
43 %return l.ensureCapacity(new_len);
44 l.len = new_len;
45 }
46
47 pub fn resizeDown(l: &Self, new_len: usize) {
48 assert(new_len <= l.len);
49 l.len = new_len;
50 }
51
52 pub fn ensureCapacity(l: &Self, new_capacity: usize) -> %void {
53 var better_capacity = l.items.len;
54 if (better_capacity >= new_capacity) return;
55 while (true) {
56 better_capacity += better_capacity / 2 + 8;
57 if (better_capacity >= new_capacity) break;
58 }
59 l.items = %return l.allocator.realloc(T, l.items, better_capacity);
60 }
61
62 pub fn addOne(l: &Self) -> %&T {
63 const new_length = l.len + 1;
64 %return l.ensureCapacity(new_length);
65 const result = &l.items[l.len];
66 l.len = new_length;
67 return result;
68 }
69
70 pub fn pop(self: &Self) -> T {
71 self.len -= 1;
72 return self.items[self.len];
73 }
74 }
75}
76
77test "basic list test" {
78 var list = List(i32).init(&debug.global_allocator);
79 defer list.deinit();
80
81 {var i: usize = 0; while (i < 10) : (i += 1) {
82 %%list.append(i32(i + 1));
83 }}
84
85 {var i: usize = 0; while (i < 10) : (i += 1) {
86 assert(list.items[i] == i32(i + 1));
87 }}
88
89 assert(list.pop() == 10);
90 assert(list.len == 9);
91}
std/os/index.zig+2-2
......@@ -36,7 +36,7 @@ const cstr = @import("../cstr.zig");
3636
3737const io = @import("../io.zig");
3838const base64 = @import("../base64.zig");
39const List = @import("../list.zig").List;
39const ArrayList = @import("../array_list.zig").ArrayList;
4040
4141error Unexpected;
4242error SystemResources;
......@@ -683,7 +683,7 @@ start_over:
683683 };
684684 defer dir.close();
685685
686 var full_entry_buf = List(u8).init(allocator);
686 var full_entry_buf = ArrayList(u8).init(allocator);
687687 defer full_entry_buf.deinit();
688688
689689 while (%return dir.next()) |entry| {
std/special/build_runner.zig+2-2
......@@ -5,7 +5,7 @@ const fmt = std.fmt;
55const os = std.os;
66const Builder = std.build.Builder;
77const mem = std.mem;
8const List = std.list.List;
8const ArrayList = std.ArrayList;
99
1010error InvalidArgs;
1111
......@@ -51,7 +51,7 @@ pub fn main() -> %void {
5151 var builder = Builder.init(allocator, zig_exe, build_root, cache_root);
5252 defer builder.deinit();
5353
54 var targets = List([]const u8).init(allocator);
54 var targets = ArrayList([]const u8).init(allocator);
5555
5656 var prefix: ?[]const u8 = null;
5757
test/tests.zig+15-15
......@@ -4,11 +4,11 @@ const build = std.build;
44const os = std.os;
55const StdIo = os.ChildProcess.StdIo;
66const Term = os.ChildProcess.Term;
7const Buffer = std.buffer.Buffer;
7const Buffer = std.Buffer;
88const io = std.io;
99const mem = std.mem;
1010const fmt = std.fmt;
11const List = std.list.List;
11const ArrayList = std.ArrayList;
1212const Mode = @import("builtin").Mode;
1313
1414const compare_output = @import("compare_output.zig");
......@@ -138,7 +138,7 @@ pub const CompareOutputContext = struct {
138138
139139 const TestCase = struct {
140140 name: []const u8,
141 sources: List(SourceFile),
141 sources: ArrayList(SourceFile),
142142 expected_output: []const u8,
143143 link_libc: bool,
144144 special: Special,
......@@ -304,7 +304,7 @@ pub const CompareOutputContext = struct {
304304 {
305305 var tc = TestCase {
306306 .name = name,
307 .sources = List(TestCase.SourceFile).init(self.b.allocator),
307 .sources = ArrayList(TestCase.SourceFile).init(self.b.allocator),
308308 .expected_output = expected_output,
309309 .link_libc = false,
310310 .special = special,
......@@ -432,8 +432,8 @@ pub const CompileErrorContext = struct {
432432
433433 const TestCase = struct {
434434 name: []const u8,
435 sources: List(SourceFile),
436 expected_errors: List([]const u8),
435 sources: ArrayList(SourceFile),
436 expected_errors: ArrayList([]const u8),
437437 link_libc: bool,
438438 is_exe: bool,
439439
......@@ -486,7 +486,7 @@ pub const CompileErrorContext = struct {
486486 const root_src = %%os.path.join(b.allocator, b.cache_root, self.case.sources.items[0].filename);
487487 const obj_path = %%os.path.join(b.allocator, b.cache_root, "test.o");
488488
489 var zig_args = List([]const u8).init(b.allocator);
489 var zig_args = ArrayList([]const u8).init(b.allocator);
490490 %%zig_args.append(if (self.case.is_exe) "build_exe" else "build_obj");
491491 %%zig_args.append(b.pathFromRoot(root_src));
492492
......@@ -583,8 +583,8 @@ pub const CompileErrorContext = struct {
583583 const tc = %%self.b.allocator.create(TestCase);
584584 *tc = TestCase {
585585 .name = name,
586 .sources = List(TestCase.SourceFile).init(self.b.allocator),
587 .expected_errors = List([]const u8).init(self.b.allocator),
586 .sources = ArrayList(TestCase.SourceFile).init(self.b.allocator),
587 .expected_errors = ArrayList([]const u8).init(self.b.allocator),
588588 .link_libc = false,
589589 .is_exe = false,
590590 };
......@@ -660,7 +660,7 @@ pub const BuildExamplesContext = struct {
660660 return;
661661 }
662662
663 var zig_args = List([]const u8).init(b.allocator);
663 var zig_args = ArrayList([]const u8).init(b.allocator);
664664 %%zig_args.append("build");
665665
666666 %%zig_args.append("--build-file");
......@@ -713,8 +713,8 @@ pub const ParseHContext = struct {
713713
714714 const TestCase = struct {
715715 name: []const u8,
716 sources: List(SourceFile),
717 expected_lines: List([]const u8),
716 sources: ArrayList(SourceFile),
717 expected_lines: ArrayList([]const u8),
718718 allow_warnings: bool,
719719
720720 const SourceFile = struct {
......@@ -761,7 +761,7 @@ pub const ParseHContext = struct {
761761
762762 const root_src = %%os.path.join(b.allocator, b.cache_root, self.case.sources.items[0].filename);
763763
764 var zig_args = List([]const u8).init(b.allocator);
764 var zig_args = ArrayList([]const u8).init(b.allocator);
765765 %%zig_args.append("parseh");
766766 %%zig_args.append(b.pathFromRoot(root_src));
767767
......@@ -847,8 +847,8 @@ pub const ParseHContext = struct {
847847 const tc = %%self.b.allocator.create(TestCase);
848848 *tc = TestCase {
849849 .name = name,
850 .sources = List(TestCase.SourceFile).init(self.b.allocator),
851 .expected_lines = List([]const u8).init(self.b.allocator),
850 .sources = ArrayList(TestCase.SourceFile).init(self.b.allocator),
851 .expected_lines = ArrayList([]const u8).init(self.b.allocator),
852852 .allow_warnings = allow_warnings,
853853 };
854854 tc.addSourceFile("source.h", source);