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 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 {
const arena = try allocator.create(ArenaAllocator);
errdefer allocator.destroy(arena);
arena.* = ArenaAllocator.init(allocator);
arena: *ArenaAllocator,
map: *std.AutoHashMap(UUID, usize),
const map = try arena.allocator().create(std.AutoHashMap(UUID, usize));
map.* = std.AutoHashMap(UUID, usize).init(arena.allocator());
pub fn init(allocator: std.mem.Allocator) !UUIDIndexMap {
const arena = try allocator.create(ArenaAllocator);
errdefer allocator.destroy(arena);
arena.* = ArenaAllocator.init(allocator);
return UUIDIndexMap{
.map = map,
.arena = arena,
};
}
const map = try arena.allocator().create(std.AutoHashMap(UUID, usize));
map.* = std.AutoHashMap(UUID, usize).init(arena.allocator());
pub fn deinit(self: *UUIDIndexMap) void {
const allocator = self.arena.child_allocator;
self.arena.deinit();
allocator.destroy(self.arena);
}
return UUIDIndexMap{
.map = map,
.arena = arena,
};
}
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;
pub fn deinit(self: *UUIDIndexMap) void {
const allocator = self.arena.child_allocator;
self.arena.deinit();
allocator.destroy(self.arena);
}
const new_file_index = try allocator.create(usize);
new_file_index.* = file_index;
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;
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 {
return self.map.contains(uuid);
}
try self.map.*.put(new_uuid.*, new_file_index.*);
}
pub fn get(self: UUIDIndexMap, uuid: UUID) ?usize {
return self.map.get(uuid);
}
};
pub fn contains(self: UUIDIndexMap, uuid: UUID) bool {
return self.map.contains(uuid);
}
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 {
return AdditionalData{
.allocator = allocator,
.childrens = std.ArrayList(AdditionalDataMember).init(allocator),
};
}
allocator: Allocator,
limit: usize = 0,
childrens: std.ArrayList(AdditionalDataMember),
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 init(allocator: Allocator) AdditionalData {
return AdditionalData{
.allocator = allocator,
.childrens = std.ArrayList(AdditionalDataMember).init(allocator),
};
}
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;
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 {
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,48 +28,47 @@ 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 {
var uuid_bytes: [16]u8 = undefined;
var start: usize = 0;
while (std.mem.indexOf(u8, input[start..], "{|<")) |pos| {
const pattern_start = start + pos + 3;
const pattern_end = pattern_start + 16;
/// 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| {
const pattern_start = start + pos + 3;
const pattern_end = pattern_start + 16;
const member_end = if (input[pattern_start - 4] == '[') pattern_start - 6 else pattern_start - 5; // This should be ": {|<"
var member_start = member_end - 1;
while (input[member_start] != ' ') : (member_start -= 1) {}
member_start += 1;
const member_end = if (input[pattern_start - 4] == '[') pattern_start - 6 else pattern_start - 5; // This should be ": {|<"
var member_start = member_end - 1;
while (input[member_start] != ' ') : (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] == '[') {
start = try self.populateArray(input, pattern_start - 3);
continue;
}
@memcpy(uuid_bytes[0..], input[pattern_start..pattern_end]);
self.map.put(uuid_bytes, JsonString{}) catch return ZipponError.MemoryError;
start = pattern_end + 3;
if (input[pattern_start - 4] == '[') {
start = try self.populateArray(input, pattern_start - 3);
continue;
}
}
// 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;
@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>|},
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;
}