Skip to content

Commit 87c0060

Browse files
committedMay 4, 2018
Made container methods that can be const, const
·
0.15.20.3.0
1 parent 0fc8885 commit 87c0060

File tree

5 files changed

+51
-23
lines changed

5 files changed

+51
-23
lines changed
 

‎std/array_list.zig‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type{
2828
};
2929
}
3030

31-
pub fn deinit(l: &Self) void {
31+
pub fn deinit(l: &const Self) void {
3232
l.allocator.free(l.items);
3333
}
3434

35-
pub fn toSlice(l: &Self) []align(A) T {
35+
pub fn toSlice(l: &const Self) []align(A) T {
3636
return l.items[0..l.len];
3737
}
3838

@@ -150,7 +150,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type{
150150
}
151151
};
152152

153-
pub fn iterator(self: &Self) Iterator {
153+
pub fn iterator(self: &const Self) Iterator {
154154
return Iterator { .list = self, .count = 0 };
155155
}
156156
};
@@ -168,6 +168,14 @@ test "basic ArrayList test" {
168168
assert(list.items[i] == i32(i + 1));
169169
}}
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+
171179
assert(list.pop() == 10);
172180
assert(list.len == 9);
173181

@@ -228,4 +236,4 @@ test "insert ArrayList test" {
228236
const items = []const i32 { 1 };
229237
try list.insertSlice(0, items[0..0]);
230238
assert(list.items[0] == 5);
231-
}
239+
}

‎std/buf_map.zig‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ pub const BufMap = struct {
1818
return self;
1919
}
2020

21-
pub fn deinit(self: &BufMap) void {
21+
pub fn deinit(self: &const BufMap) void {
2222
var it = self.hash_map.iterator();
2323
while (true) {
24-
const entry = it.next() ?? break;
24+
const entry = it.next() ?? break;
2525
self.free(entry.key);
2626
self.free(entry.value);
2727
}
@@ -38,7 +38,7 @@ pub const BufMap = struct {
3838
_ = try self.hash_map.put(key_copy, value_copy);
3939
}
4040

41-
pub fn get(self: &BufMap, key: []const u8) ?[]const u8 {
41+
pub fn get(self: &const BufMap, key: []const u8) ?[]const u8 {
4242
const entry = self.hash_map.get(key) ?? return null;
4343
return entry.value;
4444
}
@@ -57,11 +57,11 @@ pub const BufMap = struct {
5757
return self.hash_map.iterator();
5858
}
5959

60-
fn free(self: &BufMap, value: []const u8) void {
60+
fn free(self: &const BufMap, value: []const u8) void {
6161
self.hash_map.allocator.free(value);
6262
}
6363

64-
fn copy(self: &BufMap, value: []const u8) ![]const u8 {
64+
fn copy(self: &const BufMap, value: []const u8) ![]const u8 {
6565
return mem.dupe(self.hash_map.allocator, u8, value);
6666
}
6767
};
@@ -87,4 +87,4 @@ test "BufMap" {
8787

8888
bufmap.delete("x");
8989
assert(0 == bufmap.count());
90-
}
90+
}

‎std/buf_set.zig‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
const std = @import("index.zig");
12
const HashMap = @import("hash_map.zig").HashMap;
23
const mem = @import("mem.zig");
34
const Allocator = mem.Allocator;
5+
const assert = std.debug.assert;
46

57
pub const BufSet = struct {
68
hash_map: BufSetHashMap,
@@ -14,10 +16,10 @@ pub const BufSet = struct {
1416
return self;
1517
}
1618

17-
pub fn deinit(self: &BufSet) void {
19+
pub fn deinit(self: &const BufSet) void {
1820
var it = self.hash_map.iterator();
1921
while (true) {
20-
const entry = it.next() ?? break;
22+
const entry = it.next() ?? break;
2123
self.free(entry.key);
2224
}
2325

@@ -49,13 +51,30 @@ pub const BufSet = struct {
4951
return self.hash_map.allocator;
5052
}
5153

52-
fn free(self: &BufSet, value: []const u8) void {
54+
fn free(self: &const BufSet, value: []const u8) void {
5355
self.hash_map.allocator.free(value);
5456
}
5557

56-
fn copy(self: &BufSet, value: []const u8) ![]const u8 {
58+
fn copy(self: &const BufSet, value: []const u8) ![]const u8 {
5759
const result = try self.hash_map.allocator.alloc(u8, value.len);
5860
mem.copy(u8, result, value);
5961
return result;
6062
}
6163
};
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‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub const Buffer = struct {
6666
self.list.deinit();
6767
}
6868

69-
pub fn toSlice(self: &Buffer) []u8 {
69+
pub fn toSlice(self: &const Buffer) []u8 {
7070
return self.list.toSlice()[0..self.len()];
7171
}
7272

@@ -166,5 +166,5 @@ test "simple Buffer" {
166166
assert(buf.endsWith("orld"));
167167

168168
try buf2.resize(4);
169-
assert(buf.startsWith(buf2.toSliceConst()));
169+
assert(buf.startsWith(buf2.toSlice()));
170170
}

‎std/hash_map.zig‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
7474
};
7575
}
7676

77-
pub fn deinit(hm: &Self) void {
77+
pub fn deinit(hm: &const Self) void {
7878
hm.allocator.free(hm.entries);
7979
}
8080

@@ -114,14 +114,14 @@ pub fn HashMap(comptime K: type, comptime V: type,
114114
return hm.internalPut(key, value);
115115
}
116116

117-
pub fn get(hm: &Self, key: K) ?&Entry {
117+
pub fn get(hm: &const Self, key: K) ?&Entry {
118118
if (hm.entries.len == 0) {
119119
return null;
120120
}
121121
return hm.internalGet(key);
122122
}
123123

124-
pub fn contains(hm: &Self, key: K) bool {
124+
pub fn contains(hm: &const Self, key: K) bool {
125125
return hm.get(key) != null;
126126
}
127127

@@ -230,7 +230,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
230230
unreachable; // put into a full map
231231
}
232232

233-
fn internalGet(hm: &Self, key: K) ?&Entry {
233+
fn internalGet(hm: &const Self, key: K) ?&Entry {
234234
const start_index = hm.keyToIndex(key);
235235
{var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {
236236
const index = (start_index + roll_over) % hm.entries.len;
@@ -242,7 +242,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
242242
return null;
243243
}
244244

245-
fn keyToIndex(hm: &Self, key: K) usize {
245+
fn keyToIndex(hm: &const Self, key: K) usize {
246246
return usize(hash(key)) % hm.entries.len;
247247
}
248248
};
@@ -264,6 +264,7 @@ test "basic hash map usage" {
264264
assert(??(map.put(5, 66) catch unreachable) == 55);
265265
assert(??(map.put(5, 55) catch unreachable) == 66);
266266

267+
assert(map.contains(2));
267268
assert((??map.get(2)).value == 22);
268269
_ = map.remove(2);
269270
assert(map.remove(2) == null);
@@ -273,7 +274,7 @@ test "basic hash map usage" {
273274
test "iterator hash map" {
274275
var direct_allocator = std.heap.DirectAllocator.init();
275276
defer direct_allocator.deinit();
276-
277+
277278
var reset_map = HashMap(i32, i32, hash_i32, eql_i32).init(&direct_allocator.allocator);
278279
defer reset_map.deinit();
279280

@@ -315,4 +316,4 @@ fn hash_i32(x: i32) u32 {
315316

316317
fn eql_i32(a: i32, b: i32) bool {
317318
return a == b;
318-
}
319+
}

0 commit comments

Comments
 (0)
Please sign in to comment.