Переход на object_id

This commit is contained in:
2026-02-26 20:57:35 +03:00
parent f55a756132
commit 291dbd6f85
8 changed files with 80 additions and 26 deletions

View File

@@ -9,12 +9,14 @@ const shape = @import("shape/shape.zig");
size: Size_f,
allocator: std.mem.Allocator,
objects: std.ArrayList(Object),
next_object_id: u64,
pub fn init(allocator: std.mem.Allocator, size: Size_f) Document {
return .{
.size = size,
.allocator = allocator,
.objects = std.ArrayList(Object).empty,
.next_object_id = 1,
};
}
@@ -24,14 +26,14 @@ pub fn deinit(self: *Document) void {
}
pub fn addObject(self: *Document, template: Object) !void {
const obj = try template.clone(self.allocator);
const obj = try template.clone(self.allocator, &self.next_object_id);
try self.objects.append(self.allocator, obj);
}
pub fn addShape(self: *Document, parent: ?*Object, shape_kind: Object.ShapeKind) !void {
const obj = try shape.createObject(self.allocator, shape_kind);
if (parent) |p| {
try p.addChild(self.allocator, obj);
try p.addChild(self.allocator, obj, &self.next_object_id);
} else {
try self.addObject(obj);
}
@@ -50,6 +52,27 @@ pub fn removeObject(self: *Document, obj: *Object) bool {
return false;
}
/// Удаляет объект по id. Возвращает true, если объект был найден и удалён.
pub fn removeObjectById(self: *Document, obj_id: u64) bool {
for (self.objects.items, 0..) |*item, i| {
if (item.id == obj_id) {
var removed = self.objects.orderedRemove(i);
removed.deinit(self.allocator);
return true;
}
if (removeFromChildrenById(self.allocator, &item.children, obj_id)) return true;
}
return false;
}
pub fn findObjectById(self: *Document, obj_id: u64) ?*Object {
for (self.objects.items) |*item| {
if (item.id == obj_id) return item;
if (findInChildrenById(&item.children, obj_id)) |found| return found;
}
return null;
}
fn removeFromChildren(allocator: std.mem.Allocator, children: *std.ArrayList(Object), obj: *Object) bool {
for (children.items, 0..) |*item, i| {
if (item == obj) {
@@ -61,3 +84,23 @@ fn removeFromChildren(allocator: std.mem.Allocator, children: *std.ArrayList(Obj
}
return false;
}
fn removeFromChildrenById(allocator: std.mem.Allocator, children: *std.ArrayList(Object), obj_id: u64) bool {
for (children.items, 0..) |*item, i| {
if (item.id == obj_id) {
var removed = children.orderedRemove(i);
removed.deinit(allocator);
return true;
}
if (removeFromChildrenById(allocator, &item.children, obj_id)) return true;
}
return false;
}
fn findInChildrenById(children: *std.ArrayList(Object), obj_id: u64) ?*Object {
for (children.items) |*item| {
if (item.id == obj_id) return item;
if (findInChildrenById(&item.children, obj_id)) |found| return found;
}
return null;
}

View File

@@ -29,6 +29,7 @@ pub const defaultCommonProperties: [default_common_data.len]Property = blk: {
break :blk result;
};
id: u64,
shape: ShapeKind,
properties: std.ArrayList(Property),
children: std.ArrayList(Object),
@@ -51,12 +52,12 @@ pub fn setProperty(self: *Object, allocator: std.mem.Allocator, prop: Property)
return error.PropertyNotFound;
}
pub fn addChild(self: *Object, allocator: std.mem.Allocator, template: Object) !void {
const obj = try template.clone(allocator);
pub fn addChild(self: *Object, allocator: std.mem.Allocator, template: Object, next_id: *u64) !void {
const obj = try template.clone(allocator, next_id);
try self.children.append(allocator, obj);
}
pub fn clone(self: Object, allocator: std.mem.Allocator) !Object {
pub fn clone(self: Object, allocator: std.mem.Allocator, next_id: *u64) !Object {
var properties_list = std.ArrayList(Property).empty;
errdefer properties_list.deinit(allocator);
for (self.properties.items) |prop| {
@@ -66,16 +67,23 @@ pub fn clone(self: Object, allocator: std.mem.Allocator) !Object {
var children_list = std.ArrayList(Object).empty;
errdefer children_list.deinit(allocator);
for (self.children.items) |child| {
try children_list.append(allocator, try child.clone(allocator));
try children_list.append(allocator, try child.clone(allocator, next_id));
}
return .{
.id = allocId(next_id),
.shape = self.shape,
.properties = properties_list,
.children = children_list,
};
}
fn allocId(next_id: *u64) u64 {
const id = next_id.*;
next_id.* += 1;
return id;
}
pub fn deinit(self: *Object, allocator: std.mem.Allocator) void {
for (self.children.items) |*child| child.deinit(allocator);
self.children.deinit(allocator);

View File

@@ -29,6 +29,7 @@ fn createWithCommonProperties(allocator: std.mem.Allocator, shape_kind: Object.S
errdefer properties_list.deinit(allocator);
for (defaultCommonProperties) |prop| try properties_list.append(allocator, prop);
return .{
.id = 0,
.shape = shape_kind,
.properties = properties_list,
.children = std.ArrayList(Object).empty,