Some data struct now use file instead of = struct {

This commit is contained in:
Adrien Bouvais 2025-01-11 19:24:15 +01:00
parent 78213df3ff
commit ee9d5c92f2
3 changed files with 90 additions and 91 deletions

View File

@ -2,49 +2,49 @@ const std = @import("std");
const UUID = @import("dtype").UUID; const UUID = @import("dtype").UUID;
const ArenaAllocator = std.heap.ArenaAllocator; const ArenaAllocator = std.heap.ArenaAllocator;
pub const UUIDIndexMap = struct { pub const UUIDIndexMap = @This();
arena: *ArenaAllocator,
map: *std.AutoHashMap(UUID, usize),
pub fn init(allocator: std.mem.Allocator) !UUIDIndexMap { arena: *ArenaAllocator,
const arena = try allocator.create(ArenaAllocator); map: *std.AutoHashMap(UUID, usize),
errdefer allocator.destroy(arena);
arena.* = ArenaAllocator.init(allocator);
const map = try arena.allocator().create(std.AutoHashMap(UUID, usize)); pub fn init(allocator: std.mem.Allocator) !UUIDIndexMap {
map.* = std.AutoHashMap(UUID, usize).init(arena.allocator()); const arena = try allocator.create(ArenaAllocator);
errdefer allocator.destroy(arena);
arena.* = ArenaAllocator.init(allocator);
return UUIDIndexMap{ const map = try arena.allocator().create(std.AutoHashMap(UUID, usize));
.map = map, map.* = std.AutoHashMap(UUID, usize).init(arena.allocator());
.arena = arena,
};
}
pub fn deinit(self: *UUIDIndexMap) void { return UUIDIndexMap{
const allocator = self.arena.child_allocator; .map = map,
self.arena.deinit(); .arena = arena,
allocator.destroy(self.arena); };
} }
pub fn put(self: *UUIDIndexMap, uuid: UUID, file_index: usize) !void { pub fn deinit(self: *UUIDIndexMap) void {
const allocator = self.arena.allocator(); const allocator = self.arena.child_allocator;
const new_uuid = try allocator.create(UUID); self.arena.deinit();
new_uuid.* = uuid; allocator.destroy(self.arena);
}
const new_file_index = try allocator.create(usize); pub fn put(self: *UUIDIndexMap, uuid: UUID, file_index: usize) !void {
new_file_index.* = file_index; const allocator = self.arena.allocator();
const new_uuid = try allocator.create(UUID);
new_uuid.* = uuid;
try self.map.*.put(new_uuid.*, new_file_index.*); const new_file_index = try allocator.create(usize);
} new_file_index.* = file_index;
pub fn contains(self: UUIDIndexMap, uuid: UUID) bool { try self.map.*.put(new_uuid.*, new_file_index.*);
return self.map.contains(uuid); }
}
pub fn get(self: UUIDIndexMap, uuid: UUID) ?usize { pub fn contains(self: UUIDIndexMap, uuid: UUID) bool {
return self.map.get(uuid); return self.map.contains(uuid);
} }
};
pub fn get(self: UUIDIndexMap, uuid: UUID) ?usize {
return self.map.get(uuid);
}
test "Create empty UUIDIndexMap" { test "Create empty UUIDIndexMap" {
const allocator = std.testing.allocator; const allocator = std.testing.allocator;

View File

@ -9,29 +9,29 @@ const DataType = dtype.DataType;
const ZipponError = @import("errors.zig").ZipponError; const ZipponError = @import("errors.zig").ZipponError;
/// This is the [] part /// This is the [] part
pub const AdditionalData = struct { pub const AdditionalData = @This();
allocator: Allocator,
limit: usize = 0,
childrens: std.ArrayList(AdditionalDataMember),
pub fn init(allocator: Allocator) AdditionalData { allocator: Allocator,
return AdditionalData{ limit: usize = 0,
.allocator = allocator, childrens: std.ArrayList(AdditionalDataMember),
.childrens = std.ArrayList(AdditionalDataMember).init(allocator),
};
}
pub fn populateWithEverythingExceptLink(self: *AdditionalData, members: [][]const u8, dtypes: []DataType) !void { pub fn init(allocator: Allocator) AdditionalData {
for (members, dtypes, 0..) |member, dt, i| { return AdditionalData{
if (dt == .link or dt == .link_array) continue; .allocator = allocator,
try self.childrens.append(AdditionalDataMember.init(self.allocator, member, i)); .childrens = std.ArrayList(AdditionalDataMember).init(allocator),
} };
} }
pub fn addMember(self: *AdditionalData, name: []const u8, index: usize) ZipponError!void { pub fn populateWithEverythingExceptLink(self: *AdditionalData, members: [][]const u8, dtypes: []DataType) !void {
self.childrens.append(AdditionalDataMember.init(self.allocator, name, index)) catch return ZipponError.MemoryError; for (members, dtypes, 0..) |member, dt, i| {
if (dt == .link or dt == .link_array) continue;
try self.childrens.append(AdditionalDataMember.init(self.allocator, member, i));
} }
}; }
pub fn addMember(self: *AdditionalData, name: []const u8, index: usize) ZipponError!void {
self.childrens.append(AdditionalDataMember.init(self.allocator, name, index)) catch return ZipponError.MemoryError;
}
// This is name in: [name] // This is name in: [name]
// There is an additional data because it can be [friend [1; name]] // There is an additional data because it can be [friend [1; name]]

View File

@ -28,48 +28,47 @@ pub const JsonString = struct {
init: bool = false, init: bool = false,
}; };
pub const RelationMap = struct { pub const RelationMap = @This();
struct_name: []const u8, struct_name: []const u8,
member_name: []const u8, member_name: []const u8,
additional_data: AdditionalData, additional_data: AdditionalData,
map: *std.AutoHashMap([16]u8, JsonString), map: *std.AutoHashMap([16]u8, JsonString),
/// Will use a string in the JSON format and look for {|<[16]u8>|} /// Will use a string in the JSON format and look for {|<[16]u8>|}
/// It will then check if it is for the right member name and if so, add an empty JSON string at the key /// It will then check if it is for the right member name and if so, add an empty JSON string at the key
pub fn populate(self: *RelationMap, input: []const u8) ZipponError!void { pub fn populate(self: *RelationMap, input: []const u8) ZipponError!void {
var uuid_bytes: [16]u8 = undefined; var uuid_bytes: [16]u8 = undefined;
var start: usize = 0; var start: usize = 0;
while (std.mem.indexOf(u8, input[start..], "{|<")) |pos| { while (std.mem.indexOf(u8, input[start..], "{|<")) |pos| {
const pattern_start = start + pos + 3; const pattern_start = start + pos + 3;
const pattern_end = pattern_start + 16; const pattern_end = pattern_start + 16;
const member_end = if (input[pattern_start - 4] == '[') pattern_start - 6 else pattern_start - 5; // This should be ": {|<" const member_end = if (input[pattern_start - 4] == '[') pattern_start - 6 else pattern_start - 5; // This should be ": {|<"
var member_start = member_end - 1; var member_start = member_end - 1;
while (input[member_start] != ' ') : (member_start -= 1) {} while (input[member_start] != ' ') : (member_start -= 1) {}
member_start += 1; member_start += 1;
if (!std.mem.eql(u8, input[member_start..member_end], self.member_name)) continue; if (!std.mem.eql(u8, input[member_start..member_end], self.member_name)) continue;
if (input[pattern_start - 4] == '[') { if (input[pattern_start - 4] == '[') {
start = try self.populateArray(input, pattern_start - 3); start = try self.populateArray(input, pattern_start - 3);
continue; continue;
}
@memcpy(uuid_bytes[0..], input[pattern_start..pattern_end]);
self.map.put(uuid_bytes, JsonString{}) catch return ZipponError.MemoryError;
start = pattern_end + 3;
} }
}
// Array are pack in format {|<[16]u8>|},{|<[16]u8>|},{|<[16]u8>|},{|<[16]u8>|}, @memcpy(uuid_bytes[0..], input[pattern_start..pattern_end]);
fn populateArray(self: *RelationMap, input: []const u8, origin: usize) ZipponError!usize {
var uuid_bytes: [16]u8 = undefined; self.map.put(uuid_bytes, JsonString{}) catch return ZipponError.MemoryError;
var start = origin; start = pattern_end + 3;
while (input.len > start + 23 and std.mem.eql(u8, input[start .. start + 3], "{|<") and std.mem.eql(u8, input[start + 19 .. start + 23], ">|},")) : (start += 23) {
for (start + 3..start + 19, 0..) |i, j| uuid_bytes[j] = input[i];
self.map.put(uuid_bytes, JsonString{}) catch return ZipponError.MemoryError;
}
return start;
} }
}; }
// Array are pack in format {|<[16]u8>|},{|<[16]u8>|},{|<[16]u8>|},{|<[16]u8>|},
fn populateArray(self: *RelationMap, input: []const u8, origin: usize) ZipponError!usize {
var uuid_bytes: [16]u8 = undefined;
var start = origin;
while (input.len > start + 23 and std.mem.eql(u8, input[start .. start + 3], "{|<") and std.mem.eql(u8, input[start + 19 .. start + 23], ">|},")) : (start += 23) {
for (start + 3..start + 19, 0..) |i, j| uuid_bytes[j] = input[i];
self.map.put(uuid_bytes, JsonString{}) catch return ZipponError.MemoryError;
}
return start;
}