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,11 +2,12 @@ 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,
map: *std.AutoHashMap(UUID, usize),
pub fn init(allocator: std.mem.Allocator) !UUIDIndexMap {
const arena = try allocator.create(ArenaAllocator); const arena = try allocator.create(ArenaAllocator);
errdefer allocator.destroy(arena); errdefer allocator.destroy(arena);
arena.* = ArenaAllocator.init(allocator); arena.* = ArenaAllocator.init(allocator);
@ -18,15 +19,15 @@ pub const UUIDIndexMap = struct {
.map = map, .map = map,
.arena = arena, .arena = arena,
}; };
} }
pub fn deinit(self: *UUIDIndexMap) void { pub fn deinit(self: *UUIDIndexMap) void {
const allocator = self.arena.child_allocator; const allocator = self.arena.child_allocator;
self.arena.deinit(); self.arena.deinit();
allocator.destroy(self.arena); allocator.destroy(self.arena);
} }
pub fn put(self: *UUIDIndexMap, uuid: UUID, file_index: usize) !void { pub fn put(self: *UUIDIndexMap, uuid: UUID, file_index: usize) !void {
const allocator = self.arena.allocator(); const allocator = self.arena.allocator();
const new_uuid = try allocator.create(UUID); const new_uuid = try allocator.create(UUID);
new_uuid.* = uuid; new_uuid.* = uuid;
@ -35,16 +36,15 @@ pub const UUIDIndexMap = struct {
new_file_index.* = file_index; new_file_index.* = file_index;
try self.map.*.put(new_uuid.*, new_file_index.*); try self.map.*.put(new_uuid.*, new_file_index.*);
} }
pub fn contains(self: UUIDIndexMap, uuid: UUID) bool { pub fn contains(self: UUIDIndexMap, uuid: UUID) bool {
return self.map.contains(uuid); return self.map.contains(uuid);
} }
pub fn get(self: UUIDIndexMap, uuid: UUID) ?usize { pub fn get(self: UUIDIndexMap, uuid: UUID) ?usize {
return self.map.get(uuid); 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,
limit: usize = 0,
childrens: std.ArrayList(AdditionalDataMember),
pub fn init(allocator: Allocator) AdditionalData {
return AdditionalData{ return AdditionalData{
.allocator = allocator, .allocator = allocator,
.childrens = std.ArrayList(AdditionalDataMember).init(allocator), .childrens = std.ArrayList(AdditionalDataMember).init(allocator),
}; };
} }
pub fn populateWithEverythingExceptLink(self: *AdditionalData, members: [][]const u8, dtypes: []DataType) !void { pub fn populateWithEverythingExceptLink(self: *AdditionalData, members: [][]const u8, dtypes: []DataType) !void {
for (members, dtypes, 0..) |member, dt, i| { for (members, dtypes, 0..) |member, dt, i| {
if (dt == .link or dt == .link_array) continue; if (dt == .link or dt == .link_array) continue;
try self.childrens.append(AdditionalDataMember.init(self.allocator, member, i)); try self.childrens.append(AdditionalDataMember.init(self.allocator, member, i));
} }
} }
pub fn addMember(self: *AdditionalData, name: []const u8, index: usize) ZipponError!void { 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; 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,15 +28,15 @@ 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| {
@ -60,10 +60,10 @@ pub const RelationMap = struct {
self.map.put(uuid_bytes, JsonString{}) catch return ZipponError.MemoryError; self.map.put(uuid_bytes, JsonString{}) catch return ZipponError.MemoryError;
start = pattern_end + 3; start = pattern_end + 3;
} }
} }
// Array are pack in format {|<[16]u8>|},{|<[16]u8>|},{|<[16]u8>|},{|<[16]u8>|}, // Array are pack in format {|<[16]u8>|},{|<[16]u8>|},{|<[16]u8>|},{|<[16]u8>|},
fn populateArray(self: *RelationMap, input: []const u8, origin: usize) ZipponError!usize { fn populateArray(self: *RelationMap, input: []const u8, origin: usize) ZipponError!usize {
var uuid_bytes: [16]u8 = undefined; var uuid_bytes: [16]u8 = undefined;
var start = origin; 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) { 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) {
@ -71,5 +71,4 @@ pub const RelationMap = struct {
self.map.put(uuid_bytes, JsonString{}) catch return ZipponError.MemoryError; self.map.put(uuid_bytes, JsonString{}) catch return ZipponError.MemoryError;
} }
return start; return start;
} }
};