| author | |
| committer | |
| log | 87c0060e813be6ee9b449058b4288d148706f8a4 |
| tree | 9b43ef28b91b4f24edc977a5cffc0d9c66bc4de1 |
| parent | 0fc8885a8df6a44455535ba50c160bca90e8d998 |
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 | 28 | }; |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | pub fn deinit(l: &Self) void { | |
| 31 | pub fn deinit(l: &const Self) void { | |
| 32 | 32 | l.allocator.free(l.items); |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | pub fn toSlice(l: &Self) []align(A) T { | |
| 35 | pub fn toSlice(l: &const Self) []align(A) T { | |
| 36 | 36 | return l.items[0..l.len]; |
| 37 | 37 | } |
| 38 | 38 | |
| ... | ... | @@ -150,7 +150,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type{ |
| 150 | 150 | } |
| 151 | 151 | }; |
| 152 | 152 | |
| 153 | pub fn iterator(self: &Self) Iterator { | |
| 153 | pub fn iterator(self: &const Self) Iterator { | |
| 154 | 154 | return Iterator { .list = self, .count = 0 }; |
| 155 | 155 | } |
| 156 | 156 | }; |
| ... | ... | @@ -168,6 +168,14 @@ test "basic ArrayList test" { |
| 168 | 168 | assert(list.items[i] == i32(i + 1)); |
| 169 | 169 | }} |
| 170 | 170 | |
| 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 | 179 | assert(list.pop() == 10); |
| 172 | 180 | assert(list.len == 9); |
| 173 | 181 | |
| ... | ... | @@ -228,4 +236,4 @@ test "insert ArrayList test" { |
| 228 | 236 | const items = []const i32 { 1 }; |
| 229 | 237 | try list.insertSlice(0, items[0..0]); |
| 230 | 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 | 18 | return self; |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | pub fn deinit(self: &BufMap) void { | |
| 21 | pub fn deinit(self: &const BufMap) void { | |
| 22 | 22 | var it = self.hash_map.iterator(); |
| 23 | 23 | while (true) { |
| 24 | const entry = it.next() ?? break; | |
| 24 | const entry = it.next() ?? break; | |
| 25 | 25 | self.free(entry.key); |
| 26 | 26 | self.free(entry.value); |
| 27 | 27 | } |
| ... | ... | @@ -38,7 +38,7 @@ pub const BufMap = struct { |
| 38 | 38 | _ = try self.hash_map.put(key_copy, value_copy); |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | pub fn get(self: &BufMap, key: []const u8) ?[]const u8 { | |
| 41 | pub fn get(self: &const BufMap, key: []const u8) ?[]const u8 { | |
| 42 | 42 | const entry = self.hash_map.get(key) ?? return null; |
| 43 | 43 | return entry.value; |
| 44 | 44 | } |
| ... | ... | @@ -57,11 +57,11 @@ pub const BufMap = struct { |
| 57 | 57 | return self.hash_map.iterator(); |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | fn free(self: &BufMap, value: []const u8) void { | |
| 60 | fn free(self: &const BufMap, value: []const u8) void { | |
| 61 | 61 | self.hash_map.allocator.free(value); |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | fn copy(self: &BufMap, value: []const u8) ![]const u8 { | |
| 64 | fn copy(self: &const BufMap, value: []const u8) ![]const u8 { | |
| 65 | 65 | return mem.dupe(self.hash_map.allocator, u8, value); |
| 66 | 66 | } |
| 67 | 67 | }; |
| ... | ... | @@ -87,4 +87,4 @@ test "BufMap" { |
| 87 | 87 | |
| 88 | 88 | bufmap.delete("x"); |
| 89 | 89 | assert(0 == bufmap.count()); |
| 90 | } | |
| \ No newline at end of file | ||
| 90 | } |
std/buf_set.zig+23-4| ... | ... | @@ -1,6 +1,8 @@ |
| 1 | const std = @import("index.zig"); | |
| 1 | 2 | const HashMap = @import("hash_map.zig").HashMap; |
| 2 | 3 | const mem = @import("mem.zig"); |
| 3 | 4 | const Allocator = mem.Allocator; |
| 5 | const assert = std.debug.assert; | |
| 4 | 6 | |
| 5 | 7 | pub const BufSet = struct { |
| 6 | 8 | hash_map: BufSetHashMap, |
| ... | ... | @@ -14,10 +16,10 @@ pub const BufSet = struct { |
| 14 | 16 | return self; |
| 15 | 17 | } |
| 16 | 18 | |
| 17 | pub fn deinit(self: &BufSet) void { | |
| 19 | pub fn deinit(self: &const BufSet) void { | |
| 18 | 20 | var it = self.hash_map.iterator(); |
| 19 | 21 | while (true) { |
| 20 | const entry = it.next() ?? break; | |
| 22 | const entry = it.next() ?? break; | |
| 21 | 23 | self.free(entry.key); |
| 22 | 24 | } |
| 23 | 25 | |
| ... | ... | @@ -49,13 +51,30 @@ pub const BufSet = struct { |
| 49 | 51 | return self.hash_map.allocator; |
| 50 | 52 | } |
| 51 | 53 | |
| 52 | fn free(self: &BufSet, value: []const u8) void { | |
| 54 | fn free(self: &const BufSet, value: []const u8) void { | |
| 53 | 55 | self.hash_map.allocator.free(value); |
| 54 | 56 | } |
| 55 | 57 | |
| 56 | fn copy(self: &BufSet, value: []const u8) ![]const u8 { | |
| 58 | fn copy(self: &const BufSet, value: []const u8) ![]const u8 { | |
| 57 | 59 | const result = try self.hash_map.allocator.alloc(u8, value.len); |
| 58 | 60 | mem.copy(u8, result, value); |
| 59 | 61 | return result; |
| 60 | 62 | } |
| 61 | 63 | }; |
| 64 | ||
| 65 | test "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 | 66 | self.list.deinit(); |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | pub fn toSlice(self: &Buffer) []u8 { | |
| 69 | pub fn toSlice(self: &const Buffer) []u8 { | |
| 70 | 70 | return self.list.toSlice()[0..self.len()]; |
| 71 | 71 | } |
| 72 | 72 | |
| ... | ... | @@ -166,5 +166,5 @@ test "simple Buffer" { |
| 166 | 166 | assert(buf.endsWith("orld")); |
| 167 | 167 | |
| 168 | 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 | 74 | }; |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | pub fn deinit(hm: &Self) void { | |
| 77 | pub fn deinit(hm: &const Self) void { | |
| 78 | 78 | hm.allocator.free(hm.entries); |
| 79 | 79 | } |
| 80 | 80 | |
| ... | ... | @@ -114,14 +114,14 @@ pub fn HashMap(comptime K: type, comptime V: type, |
| 114 | 114 | return hm.internalPut(key, value); |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | pub fn get(hm: &Self, key: K) ?&Entry { | |
| 117 | pub fn get(hm: &const Self, key: K) ?&Entry { | |
| 118 | 118 | if (hm.entries.len == 0) { |
| 119 | 119 | return null; |
| 120 | 120 | } |
| 121 | 121 | return hm.internalGet(key); |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | pub fn contains(hm: &Self, key: K) bool { | |
| 124 | pub fn contains(hm: &const Self, key: K) bool { | |
| 125 | 125 | return hm.get(key) != null; |
| 126 | 126 | } |
| 127 | 127 | |
| ... | ... | @@ -230,7 +230,7 @@ pub fn HashMap(comptime K: type, comptime V: type, |
| 230 | 230 | unreachable; // put into a full map |
| 231 | 231 | } |
| 232 | 232 | |
| 233 | fn internalGet(hm: &Self, key: K) ?&Entry { | |
| 233 | fn internalGet(hm: &const Self, key: K) ?&Entry { | |
| 234 | 234 | const start_index = hm.keyToIndex(key); |
| 235 | 235 | {var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) { |
| 236 | 236 | const index = (start_index + roll_over) % hm.entries.len; |
| ... | ... | @@ -242,7 +242,7 @@ pub fn HashMap(comptime K: type, comptime V: type, |
| 242 | 242 | return null; |
| 243 | 243 | } |
| 244 | 244 | |
| 245 | fn keyToIndex(hm: &Self, key: K) usize { | |
| 245 | fn keyToIndex(hm: &const Self, key: K) usize { | |
| 246 | 246 | return usize(hash(key)) % hm.entries.len; |
| 247 | 247 | } |
| 248 | 248 | }; |
| ... | ... | @@ -264,6 +264,7 @@ test "basic hash map usage" { |
| 264 | 264 | assert(??(map.put(5, 66) catch unreachable) == 55); |
| 265 | 265 | assert(??(map.put(5, 55) catch unreachable) == 66); |
| 266 | 266 | |
| 267 | assert(map.contains(2)); | |
| 267 | 268 | assert((??map.get(2)).value == 22); |
| 268 | 269 | _ = map.remove(2); |
| 269 | 270 | assert(map.remove(2) == null); |
| ... | ... | @@ -273,7 +274,7 @@ test "basic hash map usage" { |
| 273 | 274 | test "iterator hash map" { |
| 274 | 275 | var direct_allocator = std.heap.DirectAllocator.init(); |
| 275 | 276 | defer direct_allocator.deinit(); |
| 276 | ||
| 277 | ||
| 277 | 278 | var reset_map = HashMap(i32, i32, hash_i32, eql_i32).init(&direct_allocator.allocator); |
| 278 | 279 | defer reset_map.deinit(); |
| 279 | 280 | |
| ... | ... | @@ -315,4 +316,4 @@ fn hash_i32(x: i32) u32 { |
| 315 | 316 | |
| 316 | 317 | fn eql_i32(a: i32, b: i32) bool { |
| 317 | 318 | return a == b; |
| 318 | } | |
| \ No newline at end of file | ||
| 319 | } |