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 {
4040 .allocator = allocator,
4141 };
4242 }
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
4452 /// Release all allocated memory.
4553 pub fn deinit(self: Self) void {
......@@ -271,6 +279,15 @@ test "std.ArrayList.init" {
271279 testing.expect(list.capacity() == 0);
272280}
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
274291test "std.ArrayList.basic" {
275292 var bytes: [1024]u8 = undefined;
276293 const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator;
lib/std/buffer.zig+35-1
......@@ -16,13 +16,22 @@ pub const Buffer = struct {
1616 mem.copy(u8, self.list.items, m);
1717 return self;
1818 }
19
19
20 /// Initialize memory to size bytes of undefined values.
2021 /// Must deinitialize with deinit.
2122 pub fn initSize(allocator: *Allocator, size: usize) !Buffer {
2223 var self = initNull(allocator);
2324 try self.resize(size);
2425 return self;
2526 }
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
2736 /// Must deinitialize with deinit.
2837 /// None of the other operations are valid until you do one of these:
......@@ -98,6 +107,13 @@ pub const Buffer = struct {
98107 pub fn len(self: Buffer) usize {
99108 return self.list.len - 1;
100109 }
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
102118 pub fn append(self: *Buffer, m: []const u8) !void {
103119 const old_len = self.len();
......@@ -151,3 +167,21 @@ test "simple Buffer" {
151167 try buf2.resize(4);
152168 testing.expect(buf.startsWith(buf2.toSlice()));
153169}
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}