Skip to content

Commit

Permalink
Refactor: Make Connectd a directional graph (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanley Jones authored Mar 9, 2020
1 parent 2e5e0db commit aea69ba
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 63 deletions.
44 changes: 0 additions & 44 deletions src/connectd/database.mo

This file was deleted.

32 changes: 32 additions & 0 deletions src/connectd/digraph.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Array "mo:stdlib/array";
import Iter "mo:stdlib/iter";
import Types "./types";

module {
type Vertex = Types.Vertex;

public class Digraph() {

var vertexList : [Vertex] = [];
var edgeList : [(Vertex, Vertex)] = [];

public func addVertex(vertex : Vertex) {
vertexList := Array.append<Vertex>(vertexList, [vertex]);
};

public func addEdge(fromVertex : Vertex, toVertex : Vertex) {
edgeList := Array.append<(Vertex, Vertex)>(edgeList, [(fromVertex, toVertex)]);
};

public func getAdjacent(vertex : Vertex) : [Vertex] {
var adjacencyList : [Vertex] = [];
for ((fromVertex, toVertex) in Iter.fromArray<(Vertex, Vertex)>(edgeList)) {
if (fromVertex == vertex) {
adjacencyList := Array.append<Vertex>(adjacencyList, [toVertex]);
};
};
adjacencyList
};

};
};
20 changes: 8 additions & 12 deletions src/connectd/main.mo
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import Database "./database";
import Digraph "./digraph";
import Types "./types";

type Entry = Types.Entry;
type EntryId = Types.EntryId;
type Vertex = Types.Vertex;

actor Graph {
var entries : Database.Entries = Database.Entries();
actor Connectd {
var graph : Digraph.Digraph = Digraph.Digraph();

public func healthcheck() : async Bool { true };

// Connections

// "public" makes this method available as Graph.connect
public func connect(caller : Nat32, userId : Nat32) : async () {
entries.addConnection(caller, userId);
public func connect(userA : Vertex, userB : Vertex) : async () {
graph.addEdge(userA, userB);
};

public func getConnections(userId : EntryId) : async [EntryId] {
entries.getConnections(userId)
public func getConnections(user : Vertex) : async [Vertex] {
graph.getAdjacent(user)
};

};
8 changes: 1 addition & 7 deletions src/connectd/types.mo
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import Hash "mo:stdlib/hash";

module {
public type EntryId = Nat32;

public type Entry = {
id : EntryId;
connections : [EntryId];
invitations : [EntryId];
};
public type Vertex = Nat32;
};

0 comments on commit aea69ba

Please sign in to comment.