aboutsummaryrefslogtreecommitdiff
path: root/std/buf_map.zig
blob: e913b8765d7ad0d2e771f527ba7306daafe9d084 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const HashMap = @import("hash_map.zig").HashMap;
const mem = @import("mem.zig");
const Allocator = mem.Allocator;

/// BufMap copies keys and values before they go into the map, and
/// frees them when they get removed.
pub const BufMap = struct {
    hash_map: BufMapHashMap,

    const BufMapHashMap = HashMap([]const u8, []const u8, mem.hash_slice_u8, mem.eql_slice_u8);

    pub fn init(allocator: &Allocator) -> BufMap {
        var self = BufMap {
            .hash_map = BufMapHashMap.init(allocator),
        };
        return self;
    }

    pub fn deinit(self: &BufMap) {
        var it = self.hash_map.iterator();
        while (true) {
            const entry = it.next() ?? break; 
            self.free(entry.key);
            self.free(entry.value);
        }

        self.hash_map.deinit();
    }

    pub fn set(self: &BufMap, key: []const u8, value: []const u8) -> %void {
        if (self.hash_map.get(key)) |entry| {
            const value_copy = %return self.copy(value);
            %defer self.free(value_copy);
            _ = %return self.hash_map.put(key, value_copy);
            self.free(entry.value);
        } else {
            const key_copy = %return self.copy(key);
            %defer self.free(key_copy);
            const value_copy = %return self.copy(value);
            %defer self.free(value_copy);
            _ = %return self.hash_map.put(key_copy, value_copy);
        }
    }

    pub fn delete(self: &BufMap, key: []const u8) {
        const entry = self.hash_map.remove(key) ?? return;
        self.free(entry.key);
        self.free(entry.value);
    }

    pub fn count(self: &const BufMap) -> usize {
        return self.hash_map.size;
    }

    pub fn iterator(self: &const BufMap) -> BufMapHashMap.Iterator {
        return self.hash_map.iterator();
    }

    fn free(self: &BufMap, value: []const u8) {
        // remove the const
        const mut_value = @ptrCast(&u8, value.ptr)[0..value.len];
        self.hash_map.allocator.free(mut_value);
    }

    fn copy(self: &BufMap, value: []const u8) -> %[]const u8 {
        const result = %return self.hash_map.allocator.alloc(u8, value.len);
        mem.copy(u8, result, value);
        return result;
    }
};