authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-27 13:40:39-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-11-27 13:40:39-05:00
log0f2a9af4aacc018a06839993475991a034aa137f
treefb8b8a345d5f0f73061956203eef3a2e43e10b59
parent83c664eaa080669cff83f7c30046376f3399eafb
parent17abb7ef7f75705c1195022076e95cdde6ab2243
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #3769 from MCRusher/initcapacity-for-buffer-arraylist

Add initCapacity for buffer & arraylist

2 files changed, 52 insertions(+), 1 deletions(-)

lib/std/array_list.zig+17
...@@ -40,6 +40,14 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {...@@ -40,6 +40,14 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
40 .allocator = allocator,40 .allocator = allocator,
41 };41 };
42 }42 }
43
44 /// Initialize with capacity to hold at least num elements.
45 /// Deinitialize with `deinit` or use `toOwnedSlice`.
46 pub fn initCapacity(allocator: *Allocator, num: usize) !Self {
47 var self = Self.init(allocator);
48 try self.ensureCapacity(num);
49 return self;
50 }
4351
44 /// Release all allocated memory.52 /// Release all allocated memory.
45 pub fn deinit(self: Self) void {53 pub fn deinit(self: Self) void {
...@@ -271,6 +279,15 @@ test "std.ArrayList.init" {...@@ -271,6 +279,15 @@ test "std.ArrayList.init" {
271 testing.expect(list.capacity() == 0);279 testing.expect(list.capacity() == 0);
272}280}
273281
282test "std.ArrayList.initCapacity" {
283 var bytes: [1024]u8 = undefined;
284 const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator;
285 var list = try ArrayList(i8).initCapacity(allocator, 200);
286 defer list.deinit();
287 testing.expect(list.count() == 0);
288 testing.expect(list.capacity() >= 200);
289}
290
274test "std.ArrayList.basic" {291test "std.ArrayList.basic" {
275 var bytes: [1024]u8 = undefined;292 var bytes: [1024]u8 = undefined;
276 const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator;293 const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator;
lib/std/buffer.zig+35-1
...@@ -16,13 +16,22 @@ pub const Buffer = struct {...@@ -16,13 +16,22 @@ pub const Buffer = struct {
16 mem.copy(u8, self.list.items, m);16 mem.copy(u8, self.list.items, m);
17 return self;17 return self;
18 }18 }
1919
20 /// Initialize memory to size bytes of undefined values.
20 /// Must deinitialize with deinit.21 /// Must deinitialize with deinit.
21 pub fn initSize(allocator: *Allocator, size: usize) !Buffer {22 pub fn initSize(allocator: *Allocator, size: usize) !Buffer {
22 var self = initNull(allocator);23 var self = initNull(allocator);
23 try self.resize(size);24 try self.resize(size);
24 return self;25 return self;
25 }26 }
27
28 /// Initialize with capacity to hold at least num bytes.
29 /// Must deinitialize with deinit.
30 pub fn initCapacity(allocator: *Allocator, num: usize) !Buffer {
31 var self = Buffer{ .list = try ArrayList(u8).initCapacity(allocator, num + 1) };
32 self.list.appendAssumeCapacity(0);
33 return self;
34 }
2635
27 /// Must deinitialize with deinit.36 /// Must deinitialize with deinit.
28 /// None of the other operations are valid until you do one of these:37 /// None of the other operations are valid until you do one of these:
...@@ -98,6 +107,13 @@ pub const Buffer = struct {...@@ -98,6 +107,13 @@ pub const Buffer = struct {
98 pub fn len(self: Buffer) usize {107 pub fn len(self: Buffer) usize {
99 return self.list.len - 1;108 return self.list.len - 1;
100 }109 }
110
111 pub fn capacity(self: Buffer) usize {
112 return if (self.list.items.len > 0)
113 self.list.items.len - 1
114 else
115 0;
116 }
101117
102 pub fn append(self: *Buffer, m: []const u8) !void {118 pub fn append(self: *Buffer, m: []const u8) !void {
103 const old_len = self.len();119 const old_len = self.len();
...@@ -151,3 +167,21 @@ test "simple Buffer" {...@@ -151,3 +167,21 @@ test "simple Buffer" {
151 try buf2.resize(4);167 try buf2.resize(4);
152 testing.expect(buf.startsWith(buf2.toSlice()));168 testing.expect(buf.startsWith(buf2.toSlice()));
153}169}
170
171test "Buffer.initSize" {
172 var buf = try Buffer.initSize(debug.global_allocator, 3);
173 testing.expect(buf.len() == 3);
174 try buf.append("hello");
175 testing.expect(mem.eql(u8, buf.toSliceConst()[3..], "hello"));
176}
177
178test "Buffer.initCapacity" {
179 var buf = try Buffer.initCapacity(debug.global_allocator, 10);
180 testing.expect(buf.len() == 0);
181 testing.expect(buf.capacity() >= 10);
182 const old_cap = buf.capacity();
183 try buf.append("hello");
184 testing.expect(buf.len() == 5);
185 testing.expect(buf.capacity() == old_cap);
186 testing.expect(mem.eql(u8, buf.toSliceConst(), "hello"));
187}