authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-05-04 23:48:14+02:00
committergravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-05-04 23:48:14+02:00
log87c0060e813be6ee9b449058b4288d148706f8a4
tree9b43ef28b91b4f24edc977a5cffc0d9c66bc4de1
parent0fc8885a8df6a44455535ba50c160bca90e8d998

Made container methods that can be const, const


5 files changed, 51 insertions(+), 23 deletions(-)

std/array_list.zig+12-4
...@@ -28,11 +28,11 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type{...@@ -28,11 +28,11 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type{
28 };28 };
29 }29 }
3030
31 pub fn deinit(l: &Self) void {31 pub fn deinit(l: &const Self) void {
32 l.allocator.free(l.items);32 l.allocator.free(l.items);
33 }33 }
3434
35 pub fn toSlice(l: &Self) []align(A) T {35 pub fn toSlice(l: &const Self) []align(A) T {
36 return l.items[0..l.len];36 return l.items[0..l.len];
37 }37 }
3838
...@@ -150,7 +150,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type{...@@ -150,7 +150,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type{
150 }150 }
151 };151 };
152152
153 pub fn iterator(self: &Self) Iterator {153 pub fn iterator(self: &const Self) Iterator {
154 return Iterator { .list = self, .count = 0 };154 return Iterator { .list = self, .count = 0 };
155 }155 }
156 };156 };
...@@ -168,6 +168,14 @@ test "basic ArrayList test" {...@@ -168,6 +168,14 @@ test "basic ArrayList test" {
168 assert(list.items[i] == i32(i + 1));168 assert(list.items[i] == i32(i + 1));
169 }}169 }}
170170
171 for (list.toSlice()) |v, i| {
172 assert(v == i32(i + 1));
173 }
174
175 for (list.toSliceConst()) |v, i| {
176 assert(v == i32(i + 1));
177 }
178
171 assert(list.pop() == 10);179 assert(list.pop() == 10);
172 assert(list.len == 9);180 assert(list.len == 9);
173181
...@@ -228,4 +236,4 @@ test "insert ArrayList test" {...@@ -228,4 +236,4 @@ test "insert ArrayList test" {
228 const items = []const i32 { 1 };236 const items = []const i32 { 1 };
229 try list.insertSlice(0, items[0..0]);237 try list.insertSlice(0, items[0..0]);
230 assert(list.items[0] == 5);238 assert(list.items[0] == 5);
231}
\ No newline at end of file
239}
std/buf_map.zig+6-6
...@@ -18,10 +18,10 @@ pub const BufMap = struct {...@@ -18,10 +18,10 @@ pub const BufMap = struct {
18 return self;18 return self;
19 }19 }
2020
21 pub fn deinit(self: &BufMap) void {21 pub fn deinit(self: &const BufMap) void {
22 var it = self.hash_map.iterator();22 var it = self.hash_map.iterator();
23 while (true) {23 while (true) {
24 const entry = it.next() ?? break; 24 const entry = it.next() ?? break;
25 self.free(entry.key);25 self.free(entry.key);
26 self.free(entry.value);26 self.free(entry.value);
27 }27 }
...@@ -38,7 +38,7 @@ pub const BufMap = struct {...@@ -38,7 +38,7 @@ pub const BufMap = struct {
38 _ = try self.hash_map.put(key_copy, value_copy);38 _ = try self.hash_map.put(key_copy, value_copy);
39 }39 }
4040
41 pub fn get(self: &BufMap, key: []const u8) ?[]const u8 {41 pub fn get(self: &const BufMap, key: []const u8) ?[]const u8 {
42 const entry = self.hash_map.get(key) ?? return null;42 const entry = self.hash_map.get(key) ?? return null;
43 return entry.value;43 return entry.value;
44 }44 }
...@@ -57,11 +57,11 @@ pub const BufMap = struct {...@@ -57,11 +57,11 @@ pub const BufMap = struct {
57 return self.hash_map.iterator();57 return self.hash_map.iterator();
58 }58 }
5959
60 fn free(self: &BufMap, value: []const u8) void {60 fn free(self: &const BufMap, value: []const u8) void {
61 self.hash_map.allocator.free(value);61 self.hash_map.allocator.free(value);
62 }62 }
6363
64 fn copy(self: &BufMap, value: []const u8) ![]const u8 {64 fn copy(self: &const BufMap, value: []const u8) ![]const u8 {
65 return mem.dupe(self.hash_map.allocator, u8, value);65 return mem.dupe(self.hash_map.allocator, u8, value);
66 }66 }
67};67};
...@@ -87,4 +87,4 @@ test "BufMap" {...@@ -87,4 +87,4 @@ test "BufMap" {
8787
88 bufmap.delete("x");88 bufmap.delete("x");
89 assert(0 == bufmap.count());89 assert(0 == bufmap.count());
90}
\ No newline at end of file
90}
std/buf_set.zig+23-4
...@@ -1,6 +1,8 @@...@@ -1,6 +1,8 @@
1const std = @import("index.zig");
1const HashMap = @import("hash_map.zig").HashMap;2const HashMap = @import("hash_map.zig").HashMap;
2const mem = @import("mem.zig");3const mem = @import("mem.zig");
3const Allocator = mem.Allocator;4const Allocator = mem.Allocator;
5const assert = std.debug.assert;
46
5pub const BufSet = struct {7pub const BufSet = struct {
6 hash_map: BufSetHashMap,8 hash_map: BufSetHashMap,
...@@ -14,10 +16,10 @@ pub const BufSet = struct {...@@ -14,10 +16,10 @@ pub const BufSet = struct {
14 return self;16 return self;
15 }17 }
1618
17 pub fn deinit(self: &BufSet) void {19 pub fn deinit(self: &const BufSet) void {
18 var it = self.hash_map.iterator();20 var it = self.hash_map.iterator();
19 while (true) {21 while (true) {
20 const entry = it.next() ?? break; 22 const entry = it.next() ?? break;
21 self.free(entry.key);23 self.free(entry.key);
22 }24 }
2325
...@@ -49,13 +51,30 @@ pub const BufSet = struct {...@@ -49,13 +51,30 @@ pub const BufSet = struct {
49 return self.hash_map.allocator;51 return self.hash_map.allocator;
50 }52 }
5153
52 fn free(self: &BufSet, value: []const u8) void {54 fn free(self: &const BufSet, value: []const u8) void {
53 self.hash_map.allocator.free(value);55 self.hash_map.allocator.free(value);
54 }56 }
5557
56 fn copy(self: &BufSet, value: []const u8) ![]const u8 {58 fn copy(self: &const BufSet, value: []const u8) ![]const u8 {
57 const result = try self.hash_map.allocator.alloc(u8, value.len);59 const result = try self.hash_map.allocator.alloc(u8, value.len);
58 mem.copy(u8, result, value);60 mem.copy(u8, result, value);
59 return result;61 return result;
60 }62 }
61};63};
64
65test "BufSet" {
66 var direct_allocator = std.heap.DirectAllocator.init();
67 defer direct_allocator.deinit();
68
69 var bufset = BufSet.init(&direct_allocator.allocator);
70 defer bufset.deinit();
71
72 try bufset.put("x");
73 assert(bufset.count() == 1);
74 bufset.delete("x");
75 assert(bufset.count() == 0);
76
77 try bufset.put("x");
78 try bufset.put("y");
79 try bufset.put("z");
80}
std/buffer.zig+2-2
...@@ -66,7 +66,7 @@ pub const Buffer = struct {...@@ -66,7 +66,7 @@ pub const Buffer = struct {
66 self.list.deinit();66 self.list.deinit();
67 }67 }
6868
69 pub fn toSlice(self: &Buffer) []u8 {69 pub fn toSlice(self: &const Buffer) []u8 {
70 return self.list.toSlice()[0..self.len()];70 return self.list.toSlice()[0..self.len()];
71 }71 }
7272
...@@ -166,5 +166,5 @@ test "simple Buffer" {...@@ -166,5 +166,5 @@ test "simple Buffer" {
166 assert(buf.endsWith("orld"));166 assert(buf.endsWith("orld"));
167167
168 try buf2.resize(4);168 try buf2.resize(4);
169 assert(buf.startsWith(buf2.toSliceConst()));169 assert(buf.startsWith(buf2.toSlice()));
170}170}
std/hash_map.zig+8-7
...@@ -74,7 +74,7 @@ pub fn HashMap(comptime K: type, comptime V: type,...@@ -74,7 +74,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
74 };74 };
75 }75 }
7676
77 pub fn deinit(hm: &Self) void {77 pub fn deinit(hm: &const Self) void {
78 hm.allocator.free(hm.entries);78 hm.allocator.free(hm.entries);
79 }79 }
8080
...@@ -114,14 +114,14 @@ pub fn HashMap(comptime K: type, comptime V: type,...@@ -114,14 +114,14 @@ pub fn HashMap(comptime K: type, comptime V: type,
114 return hm.internalPut(key, value);114 return hm.internalPut(key, value);
115 }115 }
116116
117 pub fn get(hm: &Self, key: K) ?&Entry {117 pub fn get(hm: &const Self, key: K) ?&Entry {
118 if (hm.entries.len == 0) {118 if (hm.entries.len == 0) {
119 return null;119 return null;
120 }120 }
121 return hm.internalGet(key);121 return hm.internalGet(key);
122 }122 }
123123
124 pub fn contains(hm: &Self, key: K) bool {124 pub fn contains(hm: &const Self, key: K) bool {
125 return hm.get(key) != null;125 return hm.get(key) != null;
126 }126 }
127127
...@@ -230,7 +230,7 @@ pub fn HashMap(comptime K: type, comptime V: type,...@@ -230,7 +230,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
230 unreachable; // put into a full map230 unreachable; // put into a full map
231 }231 }
232232
233 fn internalGet(hm: &Self, key: K) ?&Entry {233 fn internalGet(hm: &const Self, key: K) ?&Entry {
234 const start_index = hm.keyToIndex(key);234 const start_index = hm.keyToIndex(key);
235 {var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {235 {var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {
236 const index = (start_index + roll_over) % hm.entries.len;236 const index = (start_index + roll_over) % hm.entries.len;
...@@ -242,7 +242,7 @@ pub fn HashMap(comptime K: type, comptime V: type,...@@ -242,7 +242,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
242 return null;242 return null;
243 }243 }
244244
245 fn keyToIndex(hm: &Self, key: K) usize {245 fn keyToIndex(hm: &const Self, key: K) usize {
246 return usize(hash(key)) % hm.entries.len;246 return usize(hash(key)) % hm.entries.len;
247 }247 }
248 };248 };
...@@ -264,6 +264,7 @@ test "basic hash map usage" {...@@ -264,6 +264,7 @@ test "basic hash map usage" {
264 assert(??(map.put(5, 66) catch unreachable) == 55);264 assert(??(map.put(5, 66) catch unreachable) == 55);
265 assert(??(map.put(5, 55) catch unreachable) == 66);265 assert(??(map.put(5, 55) catch unreachable) == 66);
266266
267 assert(map.contains(2));
267 assert((??map.get(2)).value == 22);268 assert((??map.get(2)).value == 22);
268 _ = map.remove(2);269 _ = map.remove(2);
269 assert(map.remove(2) == null);270 assert(map.remove(2) == null);
...@@ -273,7 +274,7 @@ test "basic hash map usage" {...@@ -273,7 +274,7 @@ test "basic hash map usage" {
273test "iterator hash map" {274test "iterator hash map" {
274 var direct_allocator = std.heap.DirectAllocator.init();275 var direct_allocator = std.heap.DirectAllocator.init();
275 defer direct_allocator.deinit();276 defer direct_allocator.deinit();
276 277
277 var reset_map = HashMap(i32, i32, hash_i32, eql_i32).init(&direct_allocator.allocator);278 var reset_map = HashMap(i32, i32, hash_i32, eql_i32).init(&direct_allocator.allocator);
278 defer reset_map.deinit();279 defer reset_map.deinit();
279280
...@@ -315,4 +316,4 @@ fn hash_i32(x: i32) u32 {...@@ -315,4 +316,4 @@ fn hash_i32(x: i32) u32 {
315316
316fn eql_i32(a: i32, b: i32) bool {317fn eql_i32(a: i32, b: i32) bool {
317 return a == b;318 return a == b;
318}
\ No newline at end of file
319}