forked from lempiy/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
clayman
committed
Jul 9, 2019
0 parents
commit 4e66616
Showing
12 changed files
with
670 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,111 @@ | ||
package core | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func isMultiple(m map[string][]string, id string) bool { | ||
list, ok := m[id] | ||
return ok && len(list) > 1; | ||
} | ||
|
||
type Set map[string]struct{} | ||
|
||
func (s *Set) Add(key string) { | ||
(*s)[key] = struct{}{} | ||
} | ||
|
||
func (s *Set) Has(key string) bool { | ||
_, ok := (*s)[key] | ||
return ok | ||
} | ||
|
||
type relationMap map[string][]string | ||
type nodesMap map[string]NodeInput | ||
|
||
type GraphBasic struct { | ||
list []NodeInput | ||
nodesMap nodesMap | ||
incomesByNodeIdMap relationMap | ||
outcomesByNodeIdMap relationMap | ||
loopsByNodeIdMap relationMap | ||
} | ||
|
||
func NewGraphBasic(list []NodeInput) (*GraphBasic, error) { | ||
g := &GraphBasic{ | ||
list: list, | ||
nodesMap: make(nodesMap), | ||
incomesByNodeIdMap: make(relationMap), | ||
outcomesByNodeIdMap: make(relationMap), | ||
loopsByNodeIdMap: make(relationMap), | ||
} | ||
for _, node := range g.list { | ||
if _, ok := g.nodesMap[node.Id]; ok { | ||
return nil, fmt.Errorf("Duplicate node id `%s`", node.Id) | ||
} | ||
g.nodesMap[node.Id] = node | ||
} | ||
} | ||
|
||
func (g *GraphBasic) detectIncomesAndOutcomes() (err error) { | ||
totalSet := make(Set) | ||
for _, node := range g.list { | ||
if totalSet.Has(node.Id) { | ||
continue | ||
} | ||
branchSet := make(Set) | ||
_ , err = g.traverseVertically(node, &branchSet, &totalSet) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (g *GraphBasic) traverseVertically(node NodeInput, branchSet *Set, totalSet *Set) (*Set, error) { | ||
if branchSet.Has(node.Id) { | ||
return nil, fmt.Errorf("Duplicate incomes for node id `%s`", node.Id) | ||
} | ||
branchSet.Add(node.Id) | ||
totalSet.Add(node.Id) | ||
for _, outcomeId := range node.Next { | ||
// skip loops which are already detected | ||
if g.IsLoopEdge(node.Id, outcomeId) { | ||
continue | ||
} | ||
// detect loops | ||
if branchSet.Has(outcomeId) { | ||
addUniqueRelation(g.loopsByNodeIdMap, node.Id, outcomeId) | ||
} | ||
// @TODO check if addUniqueRelation works | ||
} | ||
} | ||
|
||
func (g *GraphBasic) IsLoopEdge(nodeId string, outcomeId string) bool { | ||
loops, ok := g.loopsByNodeIdMap[nodeId] | ||
if !ok { | ||
return false | ||
} | ||
for _, id := range loops { | ||
if id == outcomeId { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func addUniqueRelation(rm relationMap, key, value string) { | ||
relations, ok := rm[key] | ||
if !ok { | ||
rm[key] = []string{value} | ||
} | ||
isUnique := true | ||
for _, v := range relations { | ||
if v == value { | ||
isUnique = false | ||
break | ||
} | ||
} | ||
if isUnique { | ||
rm[key] = append(relations, value) | ||
} | ||
} |
Oops, something went wrong.