rename structs
This commit is contained in:
parent
fc303ba553
commit
a339d2a392
@ -1,7 +1,7 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
/// A data type to store connections of nodes in directional-graph.
|
/// 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 {
|
return struct {
|
||||||
///
|
///
|
||||||
pub const Node = T;
|
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 {
|
pub fn LessThanFunc(comptime T: type) type {
|
||||||
return fn (lhs: T, rhs: T) bool;
|
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" {
|
test "compile check for various types" {
|
||||||
_ = Digraph(u8, lessThanFuncFor(u8));
|
_ = Container(u8, lessThanFuncFor(u8));
|
||||||
_ = Digraph(u16, lessThanFuncFor(u16));
|
_ = Container(u16, lessThanFuncFor(u16));
|
||||||
_ = Digraph(i8, lessThanFuncFor(i8));
|
_ = Container(i8, lessThanFuncFor(i8));
|
||||||
_ = Digraph(i16, lessThanFuncFor(i16));
|
_ = Container(i16, lessThanFuncFor(i16));
|
||||||
_ = Digraph(*i8, lessThanFuncFor(*i8));
|
_ = Container(*i8, lessThanFuncFor(*i8));
|
||||||
_ = Digraph(*anyopaque, lessThanFuncFor(*anyopaque));
|
_ = Container(*anyopaque, lessThanFuncFor(*anyopaque));
|
||||||
}
|
}
|
||||||
test "check if connected" {
|
test "check if connected" {
|
||||||
const Sut = Digraph(u8, lessThanFuncFor(u8));
|
const Sut = Container(u8, lessThanFuncFor(u8));
|
||||||
|
|
||||||
const map = [_]Sut.Conn {
|
const map = [_]Sut.Conn {
|
||||||
.{ .from = 3, .to = 0, },
|
.{ .from = 3, .to = 0, },
|
||||||
@ -214,7 +214,7 @@ test "check if connected" {
|
|||||||
try std.testing.expect(!sut.isConnected(2, 1));
|
try std.testing.expect(!sut.isConnected(2, 1));
|
||||||
}
|
}
|
||||||
test "make new connection" {
|
test "make new connection" {
|
||||||
const Sut = Digraph(u8, lessThanFuncFor(u8));
|
const Sut = Container(u8, lessThanFuncFor(u8));
|
||||||
|
|
||||||
var sut = try Sut.init(std.testing.allocator, &.{});
|
var sut = try Sut.init(std.testing.allocator, &.{});
|
||||||
defer sut.deinit();
|
defer sut.deinit();
|
||||||
@ -230,7 +230,7 @@ test "make new connection" {
|
|||||||
try std.testing.expect(!sut.isConnected(1, 3));
|
try std.testing.expect(!sut.isConnected(1, 3));
|
||||||
}
|
}
|
||||||
test "making an existing connection fails" {
|
test "making an existing connection fails" {
|
||||||
const Sut = Digraph(u8, lessThanFuncFor(u8));
|
const Sut = Container(u8, lessThanFuncFor(u8));
|
||||||
|
|
||||||
const map = [_]Sut.Conn {
|
const map = [_]Sut.Conn {
|
||||||
.{ .from = 0, .to = 1, },
|
.{ .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));
|
try std.testing.expectError(Sut.Error.AlreadyConnected, sut.connect(0, 1));
|
||||||
}
|
}
|
||||||
test "disconnect an existing connection" {
|
test "disconnect an existing connection" {
|
||||||
const Sut = Digraph(u8, lessThanFuncFor(u8));
|
const Sut = Container(u8, lessThanFuncFor(u8));
|
||||||
|
|
||||||
const map = [_]Sut.Conn {
|
const map = [_]Sut.Conn {
|
||||||
.{ .from = 0, .to = 1, },
|
.{ .from = 0, .to = 1, },
|
||||||
@ -258,7 +258,7 @@ test "disconnect an existing connection" {
|
|||||||
try std.testing.expect(!sut.isConnected(2, 3));
|
try std.testing.expect(!sut.isConnected(2, 3));
|
||||||
}
|
}
|
||||||
test "disconnecting a missing connection fails" {
|
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, &.{});
|
var sut = try Sut.init(std.testing.allocator, &.{});
|
||||||
defer sut.deinit();
|
defer sut.deinit();
|
||||||
@ -267,7 +267,7 @@ test "disconnecting a missing connection fails" {
|
|||||||
try std.testing.expectError(Sut.Error.NotConnected, sut.disconnect(1, 0));
|
try std.testing.expectError(Sut.Error.NotConnected, sut.disconnect(1, 0));
|
||||||
}
|
}
|
||||||
test "chaotic operation" {
|
test "chaotic operation" {
|
||||||
const Sut = Digraph(u16, lessThanFuncFor(u16));
|
const Sut = Container(u16, lessThanFuncFor(u16));
|
||||||
|
|
||||||
var sut = try Sut.init(std.testing.allocator, &.{});
|
var sut = try Sut.init(std.testing.allocator, &.{});
|
||||||
defer sut.deinit();
|
defer sut.deinit();
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
///
|
///
|
||||||
pub const Node = struct {
|
|
||||||
/// unique and immutable integer
|
/// unique and immutable integer
|
||||||
id: usize,
|
id: usize,
|
||||||
|
|
||||||
@ -9,7 +8,7 @@ pub const Node = struct {
|
|||||||
summary: []const u8,
|
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 .{
|
return .{
|
||||||
.id = id,
|
.id = id,
|
||||||
.summary = try alloc.dupe(u8, summary),
|
.summary = try alloc.dupe(u8, summary),
|
||||||
@ -19,11 +18,10 @@ pub const Node = struct {
|
|||||||
pub fn deinit(self: *@This(), alloc: std.mem.Allocator) void {
|
pub fn deinit(self: *@This(), alloc: std.mem.Allocator) void {
|
||||||
alloc.free(self.summary);
|
alloc.free(self.summary);
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
test "serialize" {
|
test "serialize" {
|
||||||
const alloc = std.testing.allocator;
|
const alloc = std.testing.allocator;
|
||||||
var node = try Node.init(alloc, 0, "helloworld");
|
var node = try init(alloc, 0, "helloworld");
|
||||||
defer node.deinit(alloc);
|
defer node.deinit(alloc);
|
||||||
|
|
||||||
var json = std.ArrayList(u8).init(alloc);
|
var json = std.ArrayList(u8).init(alloc);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user