From aea69ba61b7071ec5495cc476f1a55281629e1f7 Mon Sep 17 00:00:00 2001 From: Stanley Jones <49165812+stanleygjones@users.noreply.github.com> Date: Mon, 9 Mar 2020 13:49:54 -0700 Subject: [PATCH] Refactor: Make Connectd a directional graph (#19) --- src/connectd/database.mo | 44 ---------------------------------------- src/connectd/digraph.mo | 32 +++++++++++++++++++++++++++++ src/connectd/main.mo | 20 ++++++++---------- src/connectd/types.mo | 8 +------- 4 files changed, 41 insertions(+), 63 deletions(-) delete mode 100644 src/connectd/database.mo create mode 100644 src/connectd/digraph.mo diff --git a/src/connectd/database.mo b/src/connectd/database.mo deleted file mode 100644 index cffd741..0000000 --- a/src/connectd/database.mo +++ /dev/null @@ -1,44 +0,0 @@ -import Array "mo:stdlib/array"; -import HashMap "mo:stdlib/hashMap"; -import Hash "mo:stdlib/hash"; -import Iter "mo:stdlib/iter"; -import List "mo:stdlib/list"; -import Nat "mo:stdlib/nat"; -import Option "mo:stdlib/option"; - -import Types "./types"; - -module { - type Entry = Types.Entry; - type EntryId = Types.EntryId; - - public class Entries() { - - func natEq(x : Nat32, y : Nat32) : Bool { x == y }; - func hashEntryId(x : EntryId) : Hash.Hash { Nat.toWord32(Nat.fromNat32(x)) }; - let hashMap = HashMap.HashMap(1, natEq, hashEntryId); - - public func addConnection(fromUser : EntryId, toUser : EntryId) { - let entry = getEntry(fromUser); - let updated : Entry = { - id = entry.id; - connections = Array.append(entry.connections, [toUser]); - invitations = entry.invitations; - }; - ignore hashMap.set(fromUser, updated); - }; - - func getEntry(entryId : EntryId) : Entry { - let existing = hashMap.get(entryId); - switch (existing) { - case (?existing) { existing }; - case (null) { { id = entryId; connections = []; invitations = []; } }; - }; - }; - - public func getConnections(userId : EntryId) : [EntryId] { - let entry = getEntry(userId); - entry.connections - }; - }; -}; diff --git a/src/connectd/digraph.mo b/src/connectd/digraph.mo new file mode 100644 index 0000000..f1296da --- /dev/null +++ b/src/connectd/digraph.mo @@ -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(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(adjacencyList, [toVertex]); + }; + }; + adjacencyList + }; + + }; +}; diff --git a/src/connectd/main.mo b/src/connectd/main.mo index baeb91d..98c52f6 100644 --- a/src/connectd/main.mo +++ b/src/connectd/main.mo @@ -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) }; }; diff --git a/src/connectd/types.mo b/src/connectd/types.mo index f8261f2..b55d640 100644 --- a/src/connectd/types.mo +++ b/src/connectd/types.mo @@ -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; };