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 ArenaAllocator = std.heap.ArenaAllocator;
pub const UUIDIndexMap = struct {
arena: *ArenaAllocator,
map: *std.AutoHashMap(UUID, usize),
pub const UUIDIndexMap = @This();
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);
errdefer allocator.destroy(arena);
arena.* = ArenaAllocator.init(allocator);
@ -18,15 +19,15 @@ pub const UUIDIndexMap = struct {
.map = map,
.arena = arena,
};
}
}
pub fn deinit(self: *UUIDIndexMap) void {
pub fn deinit(self: *UUIDIndexMap) void {
const allocator = self.arena.child_allocator;
self.arena.deinit();
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 new_uuid = try allocator.create(UUID);
new_uuid.* = uuid;
@ -35,16 +36,15 @@ pub const UUIDIndexMap = struct {
new_file_index.* = 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);
}
}
pub fn get(self: UUIDIndexMap, uuid: UUID) ?usize {
pub fn get(self: UUIDIndexMap, uuid: UUID) ?usize {
return self.map.get(uuid);
}
};
}
test "Create empty UUIDIndexMap" {
const allocator = std.testing.allocator;

View File

@ -9,29 +9,29 @@ const DataType = dtype.DataType;
const ZipponError = @import("errors.zig").ZipponError;
/// This is the [] part
pub const AdditionalData = struct {
allocator: Allocator,
limit: usize = 0,
childrens: std.ArrayList(AdditionalDataMember),
pub const AdditionalData = @This();
pub fn init(allocator: Allocator) AdditionalData {
allocator: Allocator,
limit: usize = 0,
childrens: std.ArrayList(AdditionalDataMember),
pub fn init(allocator: Allocator) AdditionalData {
return AdditionalData{
.allocator = 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| {
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 {
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]
// 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,
};
pub const RelationMap = struct {
struct_name: []const u8,
member_name: []const u8,
additional_data: AdditionalData,
map: *std.AutoHashMap([16]u8, JsonString),
pub const RelationMap = @This();
struct_name: []const u8,
member_name: []const u8,
additional_data: AdditionalData,
map: *std.AutoHashMap([16]u8, JsonString),
/// 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
pub fn populate(self: *RelationMap, input: []const u8) ZipponError!void {
/// 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
pub fn populate(self: *RelationMap, input: []const u8) ZipponError!void {
var uuid_bytes: [16]u8 = undefined;
var start: usize = 0;
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;
start = pattern_end + 3;
}
}
}
// Array are pack in format {|<[16]u8>|},{|<[16]u8>|},{|<[16]u8>|},{|<[16]u8>|},
fn populateArray(self: *RelationMap, input: []const u8, origin: usize) ZipponError!usize {
// 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) {
@ -71,5 +71,4 @@ pub const RelationMap = struct {
self.map.put(uuid_bytes, JsonString{}) catch return ZipponError.MemoryError;
}
return start;
}
};
}