move to arena allocation for airport data
This commit is contained in:
parent
4c678c1f7e
commit
f1c85205f9
1 changed files with 8 additions and 11 deletions
|
|
@ -9,7 +9,7 @@ pub const Airport = struct {
|
||||||
|
|
||||||
const Airports = @This();
|
const Airports = @This();
|
||||||
|
|
||||||
allocator: std.mem.Allocator,
|
arena: std.heap.ArenaAllocator,
|
||||||
airports: std.StringHashMap(Airport),
|
airports: std.StringHashMap(Airport),
|
||||||
|
|
||||||
pub fn init(allocator: std.mem.Allocator) !Airports {
|
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 {
|
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');
|
var lines = std.mem.splitScalar(u8, csv_data, '\n');
|
||||||
while (lines.next()) |line| {
|
while (lines.next()) |line| {
|
||||||
if (line.len == 0) continue;
|
if (line.len == 0) continue;
|
||||||
|
|
||||||
const airport = parseAirportLine(allocator, line) catch continue;
|
const airport = parseAirportLine(alloc, line) catch continue;
|
||||||
if (airport.iata.len == 3) {
|
if (airport.iata.len == 3 and airports.get(airport.iata) == null) {
|
||||||
try airports.put(airport.iata, airport);
|
try airports.put(airport.iata, airport);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Airports{
|
return Airports{
|
||||||
.allocator = allocator,
|
.arena = arena,
|
||||||
.airports = airports,
|
.airports = airports,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Airports) void {
|
pub fn deinit(self: *Airports) void {
|
||||||
var it = self.airports.iterator();
|
self.arena.deinit();
|
||||||
while (it.next()) |entry| {
|
|
||||||
self.allocator.free(entry.key_ptr.*);
|
|
||||||
self.allocator.free(entry.value_ptr.name);
|
|
||||||
}
|
|
||||||
self.airports.deinit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn lookup(self: *Airports, iata_code: []const u8) ?Airport {
|
pub fn lookup(self: *Airports, iata_code: []const u8) ?Airport {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue