Last active
March 2, 2025 01:42
-
-
Save andrewrk/04bb5b9485a2991f7e4f9b464f90f33a to your computer and use it in GitHub Desktop.
programming without pointers code snippets. https://www.hytradboi.com/2025/05c72e39-c07e-41bc-ac40-85e8308f2917-programming-without-pointers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ArrayList = std.ArrayListUnmanaged; | |
const StringHashMap = std.StringHashMapUnmanaged; | |
const Allocator = std.mem.Allocator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const State = struct { | |
clowns: ArrayHashMap(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.string_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 { | |
try state.string_bytes.ensureUnusedCapacity(gpa, bytes.len + 1); | |
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.*; | |
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.99, | |
}); | |
} | |
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 ArrayHashMap = std.AutoArrayHashMapUnmanaged; | |
const AutoHashMap = std.AutoHashMapUnmanaged; | |
const max_load_percent = std.hash_map.default_max_load_percentage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = 32.085996, .longitude = 110.500903 }, | |
.{ .lattitude = 27.225010, .longitude = 33.375462 }, | |
}); | |
} | |
{ | |
const disease = try state.diseases.addOne(gpa); | |
disease.* = .{ .occurrences = .empty }; | |
try disease.occurrences.appendSlice(gpa, &.{ | |
.{ .lattitude = 4.172149, .longitude = 31.234646 }, | |
}); | |
} | |
} | |
const std = @import("std"); | |
const ArrayList = std.ArrayListUnmanaged; | |
const StringHashMap = std.StringHashMapUnmanaged; | |
const Allocator = std.mem.Allocator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = 32.085996, .longitude = 110.500903 }, | |
.{ .lattitude = 27.225010, .longitude = 33.375462 }, | |
}), | |
}); | |
try state.diseases.append(gpa, .{ | |
.occurrences = try state.addLocations(gpa, &.{ | |
.{ .lattitude = 4.172149, .longitude = 31.234646 }, | |
}), | |
}); | |
} | |
const std = @import("std"); | |
const ArrayList = std.ArrayListUnmanaged; | |
const StringHashMap = std.StringHashMapUnmanaged; | |
const Allocator = std.mem.Allocator; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment