Video: Programming without pointers

Hi, does anyone have the code from the presentation? if not I could type it and send it here

Edit: here is example1-original

const State = struct {
    clowns: StringHashMap(Clown) = .empty,

    const Clown = struct {
        scariness: f32,
        funniness: f32,
    };

    fn deinit(state: *State, gpa: Allocator) void {
        var it = state.clowns.iterator();
        while (it.next()) |entry| {
            gpa.free(entry.key_ptr.*);
        }
        state.clowns.deinit(gpa);
    }
};

test {
    const gpa = std.testing.allocator;

    var state: State = .{};
    defer state.deinit(gpa);

    {
        const dimples = try gpa.dupe(u8, "Dimples");
        errdefer gpa.free(dimples);
        try state.clowns.put(gpa, dimples, .{
            .scariness = 0.50,
            .funniness = 0.10,
        });
    }
    {
        const humpty = try gpa.dupe(u8, "Humpty");
        errdefer gpa.free(humpty);
        try state.clowns.put(gpa, humpty, .{
            .scariness = 0.99,
            .funniness = 0.99,
        });
    }
}

const std = @import("std");
const mem = std.mem;
const assert = std.debug.assert;
const ArrayList = std.ArrayListUnmananged;
const StringHashMap = std.StringHashMapUnmanaged;
const Allocator = std.mem.Allocator;
const HashMap = std.HashMapUnamanged;
const max_load_percent = std.hash_map.default_max_load_percentage;

example1-pwd

const State = struct {
    clowns: AutoHashMap(String, Clown) = .empty,

    string_bytes: ArrayList(u8) = .empty,
    string_table: String.Table  = .empty,

    const Clown = struct {
        scariness: f32,
        funniness: f32,
    };

    fn deinit(state: *State, gpa: Allocator) void {
        state.string_bytes.deinit(gpa);
        state.string_table.deinit(gpa);
        state.clowns.deinit(gpa);
    }

    pub const String = enum(u32) {
        _,

        const Table = HashMap(String, void, TableContext, max_load_percent);

        const TableContext = struct {
            bytes: []const u8,

            pub fn eql(_: @This(), a: String, b: String) bool {
                return a == b;
            }

            pub fn hash(ctx: @This(), key: String) u64 {
                return std.hash_map.hashString(mem.sliceTo(ctx.bytes[@intFromEnum(key)..], 0));
            }
        };

        const TableIndexAdapter = struct {
            bytes: []const u8,

            pub fn eql(ctx: @This(), a: []const u8, b: String) bool {
                return mem.eql(u8, a, mem.sliceTo(ctx.bytes[@intFromEnum(b)..],0));
            }

            pub fn hash(_: @This(), adapted_key: []const u8) u64 {
                assert(mem.indexOfScalar(u8, adapted_key, 0) == null);
                return std.hash_map.hashString(adapted_key);
            }
        };

        pub fn slice(index: String, state: *const State) [:0]const u8 {
            const start_slice = state.stirng_bytes.items[@intFromEnum(index)..];
            return start_slice[0..mem.indexOfScalar(u8, start_slice, 0).? :0];
        }
    };

    pub fn internString(state: *State, gpa: Allocator, bytes: []const u8) !String {
        const gop = try state.string_table.getOrPutContextAdapted(
            gpa,
            @as([]const u8, bytes),
            @as(String.TableIndexAdapter, .{ .bytes = state.string_bytes.items }),
            @as(String.TableContext, .{ .bytes = state.string_bytes.items}),
        );
        if (gop.found_existing) return gop.key_ptr.*;

        try state.string_bytes.ensureUnusedCapacity(gpa, bytes.len + 1);
        const new_off: String = @enumFromInt(state.string_bytes.items.len);

        state.string_bytes.appendSliceAssumeCapacity(bytes);
        state.string_bytes.appendAssumeCapacity(0);

        gop.key_ptr.* = new_off;

        return new_off;
    }
};

test {
    const gpa = std.testing.allocator;

    var state: State = .{};
    defer state.deinit(gpa);

    try state.clowns.put(gpa, try state.internString(gpa, "Dimples"), .{
        .scariness = 0.50,
        .funniness = 0.10,
    });

    try state.clowns.put(gpa, try state.internString(gpa, "Humpty"), .{
        .scariness = 0.99,
        .funniness = 0.10,
    });
}

const std = @import("std");
const mem = std.mem;
const assert = std.debug.assert;
const ArrayList = std.ArrayListUnmanaged;
const StringHashMap = std.StringHashMapUnmanaged;
const Allocator = std.mem.Allocator;
const HashMap = std.HashMapUnmanaged;
const AutoHashMap = std.AutoHashMapUnmanaged;
const max_load_percent = std.hash_map.default_max_load_percentage;

example2-original

const State = struct {
    diseases: ArrayList(Disease) = .empty,

    const Disease = struct {
        occurrences: ArrayList(Location),
    };

    const Location = struct {
        lattitude: f32,
        longitude: f32,
    };

    fn deinit(state: *State, gpa: Allocator) void {
        for (state.diseases.items) |*disease| {
            disease.occurrences.deinit(gpa);
        }
        state.diseases.deinit(gpa);
    }
};

test {
    const gpa = std.testing.allocator;

    var state: State = .{};
    defer state.deinit(gpa);

    {
        const disease = try state.diseases.addOne(gpa);
        disease.* = .{ .occurrences = .empty };
        try disease.occurrences.appendSlice(gpa, &.{
            .{ .lattitude = 31.77635, .longitude = 35.23559 },
            .{ .lattitude = 30.04443, .longitude = 31.23575 },
        });
    }
    {
        const disease = try state.diseases.addOne(gpa);
        disease.* = .{ .occurrences = .empty };
        try disease.occurrences.appendSlice(gpa, &.{
            .{ .lattitude = 31.50504, .longitude = 34.46107 },
        });
    }
}


const std = @import("std");
const mem = std.mem;
const assert = std.debug.assert;
const ArrayList = std.ArrayListUnmanaged;
const StringHashMap = std.StringHashMapUnmanaged;
const Allocator = std.mem.Allocator;
const HashMap = std.HashMapUnmanaged;
const AutoHashMap = std.AutoHashMapUnmanaged;
const max_load_percent = std.hash_map.default_max_load_percentage;

and example2-pwd

const State = struct {
    diseases: ArrayList(Disease) = .empty,
    locations: ArrayList(Location) = .empty,

    const Disease = struct {
        occurrences: Location.Slice,
    };

    const Location = struct {
        lattitude: f32,
        longitude: f32,

        const Slice = struct {
            off: u32,
            len: u32,
        };
    };

    fn deinit(state: *State, gpa: Allocator) void {
        state.locations.deinit(gpa);
        state.diseases.deinit(gpa);
    }

    fn addLocations(state: *State, gpa: Allocator, locs: []const Location) !Location.Slice {
        try state.locations.appendSlice(gpa, locs);
        return .{
            .off = @intCast(state.locations.items.len - locs.len),
            .len = @intCast(locs.len),
        };
    }
};

test {
    const gpa = std.testing.allocator;

    var state: State = .{};
    defer state.deinit(gpa);

    try state.diseases.append(gpa, .{
        .occurrences = try state.addLocations(gpa, &.{
            .{ .lattitude = 31.77635, .longitude = 35.23559 },
            .{ .lattitude = 30.04443, .longitude = 31.23575 },
        }),
    });

    try state.diseases.append(gpa, .{
        .occurrences = try state.addLocations(gpa, &.{
            .{ .lattitude = 31.50504, .longitude = 34.46107 },
        }),
    });
}

const std = @import("std");
const mem = std.mem;
const assert = std.debug.assert;
const ArrayList = std.ArrayListUnmanaged;
const StringHashMap = std.StringHashMapUnmanaged;
const Allocator = std.mem.Allocator;
const HashMap = std.HashMapUnmanaged;
const AutoHashMap = std.AutoHashMapUnmanaged;
const max_load_percent = std.hash_map.default_max_load_percentage;

hope it helps someone else

2 Likes