Advertisement
Guest User

Untitled

a guest
Dec 4th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.71 KB | None | 0 0
  1. pub fn Graph(comptime T: type) type {
  2.     return struct {
  3.         constants: std.ArrayList(T),
  4.  
  5.         pub fn init(allocator: *std.mem.Allocator) Graph(T) {
  6.             return .{ .constants = std.ArrayList(T).init(allocator) };
  7.         }
  8.  
  9.         pub fn constant(graph: *Graph(T), value: T) !void {
  10.             try graph.constants.append(value);
  11.         }
  12.     };
  13. }
  14. const GraphT = Graph(i32);
  15. const constant = GraphT.constant;
  16.  
  17. test "create graph" {
  18.     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
  19.     defer arena.deinit();
  20.     const allocator = &arena.allocator;
  21.  
  22.     var graph = &GraphT.init(allocator);
  23.     const x = try constant(graph, 5);
  24.     const y = try constant(graph, 10);
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement