-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Make Connectd a directional graph (#19)
- Loading branch information
Stanley Jones
authored
Mar 9, 2020
1 parent
2e5e0db
commit aea69ba
Showing
4 changed files
with
41 additions
and
63 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
|
||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |