#Can/Should I use a tuple as a key in a Hashset?

1 messages · Page 1 of 1 (latest)

rough stone
#
const std = @import("std");

const alloc = std.testing.allocator;

test "tuples as keys" {
    var map: std.AutoHashMap(struct { usize, usize }, void) = std.AutoHashMap(struct { usize, usize }, void).init(alloc);
    defer map.deinit();

    try map.put(.{ 10, 12 }, {});
    try map.put(.{ 12, 11 }, {});

    try std.testing.expect(map.contains(.{ 10, 12 }));

    _ = map.remove(.{ 10, 12 });

    try std.testing.expect(!map.contains(.{ 10, 12 }));
}

The test does run but I was wondering if this is a reasonable thing to do.

gloomy venture
#

yeah that's fine to do

tender oxide
#

Whether it’s reasonable or not to do really depends on your problem

rough stone
#

I was thinking whether the hash function works well with tuples

gloomy venture