Made container methods that can be const, const

This commit is contained in:
Jimmi Holst Christensen 2018-05-04 23:48:14 +02:00
parent 0fc8885a8d
commit 87c0060e81
5 changed files with 51 additions and 23 deletions

View File

@ -28,11 +28,11 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type{
};
}
pub fn deinit(l: &Self) void {
pub fn deinit(l: &const Self) void {
l.allocator.free(l.items);
}
pub fn toSlice(l: &Self) []align(A) T {
pub fn toSlice(l: &const Self) []align(A) T {
return l.items[0..l.len];
}
@ -150,7 +150,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type{
}
};
pub fn iterator(self: &Self) Iterator {
pub fn iterator(self: &const Self) Iterator {
return Iterator { .list = self, .count = 0 };
}
};
@ -168,6 +168,14 @@ test "basic ArrayList test" {
assert(list.items[i] == i32(i + 1));
}}
for (list.toSlice()) |v, i| {
assert(v == i32(i + 1));
}
for (list.toSliceConst()) |v, i| {
assert(v == i32(i + 1));
}
assert(list.pop() == 10);
assert(list.len == 9);
@ -228,4 +236,4 @@ test "insert ArrayList test" {
const items = []const i32 { 1 };
try list.insertSlice(0, items[0..0]);
assert(list.items[0] == 5);
}
}

View File

@ -18,10 +18,10 @@ pub const BufMap = struct {
return self;
}
pub fn deinit(self: &BufMap) void {
pub fn deinit(self: &const BufMap) void {
var it = self.hash_map.iterator();
while (true) {
const entry = it.next() ?? break;
const entry = it.next() ?? break;
self.free(entry.key);
self.free(entry.value);
}
@ -38,7 +38,7 @@ pub const BufMap = struct {
_ = try self.hash_map.put(key_copy, value_copy);
}
pub fn get(self: &BufMap, key: []const u8) ?[]const u8 {
pub fn get(self: &const BufMap, key: []const u8) ?[]const u8 {
const entry = self.hash_map.get(key) ?? return null;
return entry.value;
}
@ -57,11 +57,11 @@ pub const BufMap = struct {
return self.hash_map.iterator();
}
fn free(self: &BufMap, value: []const u8) void {
fn free(self: &const BufMap, value: []const u8) void {
self.hash_map.allocator.free(value);
}
fn copy(self: &BufMap, value: []const u8) ![]const u8 {
fn copy(self: &const BufMap, value: []const u8) ![]const u8 {
return mem.dupe(self.hash_map.allocator, u8, value);
}
};
@ -87,4 +87,4 @@ test "BufMap" {
bufmap.delete("x");
assert(0 == bufmap.count());
}
}

View File

@ -1,6 +1,8 @@
const std = @import("index.zig");
const HashMap = @import("hash_map.zig").HashMap;
const mem = @import("mem.zig");
const Allocator = mem.Allocator;
const assert = std.debug.assert;
pub const BufSet = struct {
hash_map: BufSetHashMap,
@ -14,10 +16,10 @@ pub const BufSet = struct {
return self;
}
pub fn deinit(self: &BufSet) void {
pub fn deinit(self: &const BufSet) void {
var it = self.hash_map.iterator();
while (true) {
const entry = it.next() ?? break;
const entry = it.next() ?? break;
self.free(entry.key);
}
@ -49,13 +51,30 @@ pub const BufSet = struct {
return self.hash_map.allocator;
}
fn free(self: &BufSet, value: []const u8) void {
fn free(self: &const BufSet, value: []const u8) void {
self.hash_map.allocator.free(value);
}
fn copy(self: &BufSet, value: []const u8) ![]const u8 {
fn copy(self: &const BufSet, value: []const u8) ![]const u8 {
const result = try self.hash_map.allocator.alloc(u8, value.len);
mem.copy(u8, result, value);
return result;
}
};
test "BufSet" {
var direct_allocator = std.heap.DirectAllocator.init();
defer direct_allocator.deinit();
var bufset = BufSet.init(&direct_allocator.allocator);
defer bufset.deinit();
try bufset.put("x");
assert(bufset.count() == 1);
bufset.delete("x");
assert(bufset.count() == 0);
try bufset.put("x");
try bufset.put("y");
try bufset.put("z");
}

View File

@ -66,7 +66,7 @@ pub const Buffer = struct {
self.list.deinit();
}
pub fn toSlice(self: &Buffer) []u8 {
pub fn toSlice(self: &const Buffer) []u8 {
return self.list.toSlice()[0..self.len()];
}
@ -166,5 +166,5 @@ test "simple Buffer" {
assert(buf.endsWith("orld"));
try buf2.resize(4);
assert(buf.startsWith(buf2.toSliceConst()));
assert(buf.startsWith(buf2.toSlice()));
}

View File

@ -74,7 +74,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
};
}
pub fn deinit(hm: &Self) void {
pub fn deinit(hm: &const Self) void {
hm.allocator.free(hm.entries);
}
@ -114,14 +114,14 @@ pub fn HashMap(comptime K: type, comptime V: type,
return hm.internalPut(key, value);
}
pub fn get(hm: &Self, key: K) ?&Entry {
pub fn get(hm: &const Self, key: K) ?&Entry {
if (hm.entries.len == 0) {
return null;
}
return hm.internalGet(key);
}
pub fn contains(hm: &Self, key: K) bool {
pub fn contains(hm: &const Self, key: K) bool {
return hm.get(key) != null;
}
@ -230,7 +230,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
unreachable; // put into a full map
}
fn internalGet(hm: &Self, key: K) ?&Entry {
fn internalGet(hm: &const Self, key: K) ?&Entry {
const start_index = hm.keyToIndex(key);
{var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {
const index = (start_index + roll_over) % hm.entries.len;
@ -242,7 +242,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
return null;
}
fn keyToIndex(hm: &Self, key: K) usize {
fn keyToIndex(hm: &const Self, key: K) usize {
return usize(hash(key)) % hm.entries.len;
}
};
@ -264,6 +264,7 @@ test "basic hash map usage" {
assert(??(map.put(5, 66) catch unreachable) == 55);
assert(??(map.put(5, 55) catch unreachable) == 66);
assert(map.contains(2));
assert((??map.get(2)).value == 22);
_ = map.remove(2);
assert(map.remove(2) == null);
@ -273,7 +274,7 @@ test "basic hash map usage" {
test "iterator hash map" {
var direct_allocator = std.heap.DirectAllocator.init();
defer direct_allocator.deinit();
var reset_map = HashMap(i32, i32, hash_i32, eql_i32).init(&direct_allocator.allocator);
defer reset_map.deinit();
@ -315,4 +316,4 @@ fn hash_i32(x: i32) u32 {
fn eql_i32(a: i32, b: i32) bool {
return a == b;
}
}