diff --git a/src/location/Airports.zig b/src/location/Airports.zig index 99e3092..2f9db6f 100644 --- a/src/location/Airports.zig +++ b/src/location/Airports.zig @@ -9,7 +9,7 @@ pub const Airport = struct { const Airports = @This(); -allocator: std.mem.Allocator, +arena: std.heap.ArenaAllocator, airports: std.StringHashMap(Airport), pub fn init(allocator: std.mem.Allocator) !Airports { @@ -18,31 +18,28 @@ pub fn init(allocator: std.mem.Allocator) !Airports { } pub fn initFromData(allocator: std.mem.Allocator, csv_data: []const u8) !Airports { - var airports = std.StringHashMap(Airport).init(allocator); + var arena = std.heap.ArenaAllocator.init(allocator); + const alloc = arena.allocator(); + var airports = std.StringHashMap(Airport).init(alloc); var lines = std.mem.splitScalar(u8, csv_data, '\n'); while (lines.next()) |line| { if (line.len == 0) continue; - const airport = parseAirportLine(allocator, line) catch continue; - if (airport.iata.len == 3) { + const airport = parseAirportLine(alloc, line) catch continue; + if (airport.iata.len == 3 and airports.get(airport.iata) == null) { try airports.put(airport.iata, airport); } } return Airports{ - .allocator = allocator, + .arena = arena, .airports = airports, }; } pub fn deinit(self: *Airports) void { - var it = self.airports.iterator(); - while (it.next()) |entry| { - self.allocator.free(entry.key_ptr.*); - self.allocator.free(entry.value_ptr.name); - } - self.airports.deinit(); + self.arena.deinit(); } pub fn lookup(self: *Airports, iata_code: []const u8) ?Airport {