structstd.buf_map.BufMap[src]

BufMap copies keys and values before they go into the map and frees them when they get removed.

Fields

hash_map: BufMapHashMap

Functions

Functioninit[src]

pub fn init(allocator: Allocator) BufMap

Create a BufMap backed by a specific allocator. That allocator will be used for both backing allocations and string deduplication.

Parameters

allocator: Allocator

Source Code

Source code
pub fn init(allocator: Allocator) BufMap {
    return .{ .hash_map = BufMapHashMap.init(allocator) };
}

Functiondeinit[src]

pub fn deinit(self: *BufMap) void

Free the backing storage of the map, as well as all of the stored keys and values.

Parameters

self: *BufMap

Source Code

Source code
pub fn deinit(self: *BufMap) void {
    var it = self.hash_map.iterator();
    while (it.next()) |entry| {
        self.free(entry.key_ptr.*);
        self.free(entry.value_ptr.*);
    }

    self.hash_map.deinit();
}

FunctionputMove[src]

pub fn putMove(self: *BufMap, key: []u8, value: []u8) !void

Same as put but the key and value become owned by the BufMap rather than being copied. If putMove fails, the ownership of key and value does not transfer.

Parameters

self: *BufMap
key: []u8
value: []u8

Source Code

Source code
pub fn putMove(self: *BufMap, key: []u8, value: []u8) !void {
    const get_or_put = try self.hash_map.getOrPut(key);
    if (get_or_put.found_existing) {
        self.free(get_or_put.key_ptr.*);
        self.free(get_or_put.value_ptr.*);
        get_or_put.key_ptr.* = key;
    }
    get_or_put.value_ptr.* = value;
}

Functionput[src]

pub fn put(self: *BufMap, key: []const u8, value: []const u8) !void

key and value are copied into the BufMap.

Parameters

self: *BufMap
key: []const u8
value: []const u8

Source Code

Source code
pub fn put(self: *BufMap, key: []const u8, value: []const u8) !void {
    const value_copy = try self.copy(value);
    errdefer self.free(value_copy);
    const get_or_put = try self.hash_map.getOrPut(key);
    if (get_or_put.found_existing) {
        self.free(get_or_put.value_ptr.*);
    } else {
        get_or_put.key_ptr.* = self.copy(key) catch |err| {
            _ = self.hash_map.remove(key);
            return err;
        };
    }
    get_or_put.value_ptr.* = value_copy;
}

FunctiongetPtr[src]

pub fn getPtr(self: BufMap, key: []const u8) ?*[]const u8

Find the address of the value associated with a key. The returned pointer is invalidated if the map resizes.

Parameters

self: BufMap
key: []const u8

Source Code

Source code
pub fn getPtr(self: BufMap, key: []const u8) ?*[]const u8 {
    return self.hash_map.getPtr(key);
}

Functionget[src]

pub fn get(self: BufMap, key: []const u8) ?[]const u8

Return the map's copy of the value associated with a key. The returned string is invalidated if this key is removed from the map.

Parameters

self: BufMap
key: []const u8

Source Code

Source code
pub fn get(self: BufMap, key: []const u8) ?[]const u8 {
    return self.hash_map.get(key);
}

Functionremove[src]

pub fn remove(self: *BufMap, key: []const u8) void

Removes the item from the map and frees its value. This invalidates the value returned by get() for this key.

Parameters

self: *BufMap
key: []const u8

Source Code

Source code
pub fn remove(self: *BufMap, key: []const u8) void {
    const kv = self.hash_map.fetchRemove(key) orelse return;
    self.free(kv.key);
    self.free(kv.value);
}

Functioncount[src]

pub fn count(self: BufMap) BufMapHashMap.Size

Returns the number of KV pairs stored in the map.

Parameters

self: BufMap

Source Code

Source code
pub fn count(self: BufMap) BufMapHashMap.Size {
    return self.hash_map.count();
}

Functioniterator[src]

pub fn iterator(self: *const BufMap) BufMapHashMap.Iterator

Returns an iterator over entries in the map.

Parameters

self: *const BufMap

Source Code

Source code
pub fn iterator(self: *const BufMap) BufMapHashMap.Iterator {
    return self.hash_map.iterator();
}

Source Code

Source code
pub const BufMap = struct {
    hash_map: BufMapHashMap,

    const BufMapHashMap = StringHashMap([]const u8);

    /// Create a BufMap backed by a specific allocator.
    /// That allocator will be used for both backing allocations
    /// and string deduplication.
    pub fn init(allocator: Allocator) BufMap {
        return .{ .hash_map = BufMapHashMap.init(allocator) };
    }

    /// Free the backing storage of the map, as well as all
    /// of the stored keys and values.
    pub fn deinit(self: *BufMap) void {
        var it = self.hash_map.iterator();
        while (it.next()) |entry| {
            self.free(entry.key_ptr.*);
            self.free(entry.value_ptr.*);
        }

        self.hash_map.deinit();
    }

    /// Same as `put` but the key and value become owned by the BufMap rather
    /// than being copied.
    /// If `putMove` fails, the ownership of key and value does not transfer.
    pub fn putMove(self: *BufMap, key: []u8, value: []u8) !void {
        const get_or_put = try self.hash_map.getOrPut(key);
        if (get_or_put.found_existing) {
            self.free(get_or_put.key_ptr.*);
            self.free(get_or_put.value_ptr.*);
            get_or_put.key_ptr.* = key;
        }
        get_or_put.value_ptr.* = value;
    }

    /// `key` and `value` are copied into the BufMap.
    pub fn put(self: *BufMap, key: []const u8, value: []const u8) !void {
        const value_copy = try self.copy(value);
        errdefer self.free(value_copy);
        const get_or_put = try self.hash_map.getOrPut(key);
        if (get_or_put.found_existing) {
            self.free(get_or_put.value_ptr.*);
        } else {
            get_or_put.key_ptr.* = self.copy(key) catch |err| {
                _ = self.hash_map.remove(key);
                return err;
            };
        }
        get_or_put.value_ptr.* = value_copy;
    }

    /// Find the address of the value associated with a key.
    /// The returned pointer is invalidated if the map resizes.
    pub fn getPtr(self: BufMap, key: []const u8) ?*[]const u8 {
        return self.hash_map.getPtr(key);
    }

    /// Return the map's copy of the value associated with
    /// a key.  The returned string is invalidated if this
    /// key is removed from the map.
    pub fn get(self: BufMap, key: []const u8) ?[]const u8 {
        return self.hash_map.get(key);
    }

    /// Removes the item from the map and frees its value.
    /// This invalidates the value returned by get() for this key.
    pub fn remove(self: *BufMap, key: []const u8) void {
        const kv = self.hash_map.fetchRemove(key) orelse return;
        self.free(kv.key);
        self.free(kv.value);
    }

    /// Returns the number of KV pairs stored in the map.
    pub fn count(self: BufMap) BufMapHashMap.Size {
        return self.hash_map.count();
    }

    /// Returns an iterator over entries in the map.
    pub fn iterator(self: *const BufMap) BufMapHashMap.Iterator {
        return self.hash_map.iterator();
    }

    fn free(self: BufMap, value: []const u8) void {
        self.hash_map.allocator.free(value);
    }

    fn copy(self: BufMap, value: []const u8) ![]u8 {
        return self.hash_map.allocator.dupe(u8, value);
    }
}