rename structs

This commit is contained in:
falsycat 2025-04-08 23:03:19 +09:00
parent fc303ba553
commit a339d2a392
2 changed files with 30 additions and 32 deletions

View File

@ -1,7 +1,7 @@
const std = @import("std");
/// A data type to store connections of nodes in directional-graph.
pub fn Digraph(comptime T: type, comptime lessThanFn: LessThanFunc(T)) type {
pub fn Container(comptime T: type, comptime lessThanFn: LessThanFunc(T)) type {
return struct {
///
pub const Node = T;
@ -160,7 +160,7 @@ pub fn Digraph(comptime T: type, comptime lessThanFn: LessThanFunc(T)) type {
};
}
/// A type of comparator function for the type T, which is to be passed as an argument of `Digraph()`.
/// A type of comparator function for the type T, which is to be passed as an argument of `Container()`.
pub fn LessThanFunc(comptime T: type) type {
return fn (lhs: T, rhs: T) bool;
}
@ -179,15 +179,15 @@ pub fn lessThanFuncFor(comptime T: type) LessThanFunc(T) {
}
test "compile check for various types" {
_ = Digraph(u8, lessThanFuncFor(u8));
_ = Digraph(u16, lessThanFuncFor(u16));
_ = Digraph(i8, lessThanFuncFor(i8));
_ = Digraph(i16, lessThanFuncFor(i16));
_ = Digraph(*i8, lessThanFuncFor(*i8));
_ = Digraph(*anyopaque, lessThanFuncFor(*anyopaque));
_ = Container(u8, lessThanFuncFor(u8));
_ = Container(u16, lessThanFuncFor(u16));
_ = Container(i8, lessThanFuncFor(i8));
_ = Container(i16, lessThanFuncFor(i16));
_ = Container(*i8, lessThanFuncFor(*i8));
_ = Container(*anyopaque, lessThanFuncFor(*anyopaque));
}
test "check if connected" {
const Sut = Digraph(u8, lessThanFuncFor(u8));
const Sut = Container(u8, lessThanFuncFor(u8));
const map = [_]Sut.Conn {
.{ .from = 3, .to = 0, },
@ -214,7 +214,7 @@ test "check if connected" {
try std.testing.expect(!sut.isConnected(2, 1));
}
test "make new connection" {
const Sut = Digraph(u8, lessThanFuncFor(u8));
const Sut = Container(u8, lessThanFuncFor(u8));
var sut = try Sut.init(std.testing.allocator, &.{});
defer sut.deinit();
@ -230,7 +230,7 @@ test "make new connection" {
try std.testing.expect(!sut.isConnected(1, 3));
}
test "making an existing connection fails" {
const Sut = Digraph(u8, lessThanFuncFor(u8));
const Sut = Container(u8, lessThanFuncFor(u8));
const map = [_]Sut.Conn {
.{ .from = 0, .to = 1, },
@ -242,7 +242,7 @@ test "making an existing connection fails" {
try std.testing.expectError(Sut.Error.AlreadyConnected, sut.connect(0, 1));
}
test "disconnect an existing connection" {
const Sut = Digraph(u8, lessThanFuncFor(u8));
const Sut = Container(u8, lessThanFuncFor(u8));
const map = [_]Sut.Conn {
.{ .from = 0, .to = 1, },
@ -258,7 +258,7 @@ test "disconnect an existing connection" {
try std.testing.expect(!sut.isConnected(2, 3));
}
test "disconnecting a missing connection fails" {
const Sut = Digraph(u8, lessThanFuncFor(u8));
const Sut = Container(u8, lessThanFuncFor(u8));
var sut = try Sut.init(std.testing.allocator, &.{});
defer sut.deinit();
@ -267,7 +267,7 @@ test "disconnecting a missing connection fails" {
try std.testing.expectError(Sut.Error.NotConnected, sut.disconnect(1, 0));
}
test "chaotic operation" {
const Sut = Digraph(u16, lessThanFuncFor(u16));
const Sut = Container(u16, lessThanFuncFor(u16));
var sut = try Sut.init(std.testing.allocator, &.{});
defer sut.deinit();

View File

@ -1,29 +1,27 @@
const std = @import("std");
///
pub const Node = struct {
/// unique and immutable integer
id: usize,
/// unique and immutable integer
id: usize,
/// summary text of this node
summary: []const u8,
/// summary text of this node
summary: []const u8,
///
pub fn init(alloc: std.mem.Allocator, id: usize, summary: []const u8) !Node {
///
pub fn init(alloc: std.mem.Allocator, id: usize, summary: []const u8) !@This() {
return .{
.id = id,
.summary = try alloc.dupe(u8, summary),
};
}
/// pass the same allocator as init() call
pub fn deinit(self: *@This(), alloc: std.mem.Allocator) void {
}
/// pass the same allocator as init() call
pub fn deinit(self: *@This(), alloc: std.mem.Allocator) void {
alloc.free(self.summary);
}
};
}
test "serialize" {
const alloc = std.testing.allocator;
var node = try Node.init(alloc, 0, "helloworld");
var node = try init(alloc, 0, "helloworld");
defer node.deinit(alloc);
var json = std.ArrayList(u8).init(alloc);