Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impr/add enums #7

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use enums instead of raw string values
## Description
This changes the use of raw strings to enums. With this users aware of
their algorithm options and can choose between them without having to
guess which ones have been implemented.
quamejnr committed Dec 11, 2024
commit 27c957934ed0f751033b88ab92c72acb8e999d64
4 changes: 2 additions & 2 deletions cmd/example-2/example_2.go
Original file line number Diff line number Diff line change
@@ -37,13 +37,13 @@ func main() {
3: []int{4, 5},
}

output, err = gorder.TopologicalSort(serialize(digraph), "kahn")
output, err = gorder.TopologicalSort(serialize(digraph), gorder.KAHN)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Solution (Kahn): %d\n", deserialize(output))

output, err = gorder.TopologicalSort(serialize(digraph), "dfsbased")
output, err = gorder.TopologicalSort(serialize(digraph), gorder.DFS)
if err != nil {
log.Fatal(err)
}
4 changes: 2 additions & 2 deletions cmd/example/example.go
Original file line number Diff line number Diff line change
@@ -23,13 +23,13 @@ func main() {
3: []interface{}{"4", "5"},
}

o, err = gorder.TopologicalSort(digraph, "kahn")
o, err = gorder.TopologicalSort(digraph, gorder.KAHN)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Solution (Kahn): %v\n", o)

o, err = gorder.TopologicalSort(digraph, "dfsbased")
o, err = gorder.TopologicalSort(digraph, gorder.DFS)
if err != nil {
log.Fatal(err)
}
4 changes: 2 additions & 2 deletions cmd/measurements/measurements.go
Original file line number Diff line number Diff line change
@@ -15,15 +15,15 @@ func main() {
log.Printf("DAG generation time: %v", time.Since(start))

start = time.Now()
s, err := gorder.TopologicalSort(graph, "kahn")
s, err := gorder.TopologicalSort(graph, gorder.KAHN)
log.Printf("Kahn resolve time: %v", time.Since(start))
if err != nil {
log.Fatal(err)
}
spew.Dump(s)

start = time.Now()
s, err = gorder.TopologicalSort(graph, "dfsbased")
s, err = gorder.TopologicalSort(graph, gorder.DFS)
log.Printf("DFS-based resolve time: %v", time.Since(start))
if err != nil {
log.Fatal(err)
30 changes: 18 additions & 12 deletions gorder.go
Original file line number Diff line number Diff line change
@@ -2,24 +2,30 @@ package gorder

import (
"errors"
"regexp"
)

func TopologicalSort[T comparable, V []T](digraph map[T]V, algorithm string) (solution V, err error) {
kahnRgxp, err := regexp.Compile(`[Kk]ahn\z`)
if err != nil {
return nil, err
}
dfsBasedRgxp, err := regexp.Compile(`[Dd][Ff][Ss]-?[Bb]ased\z`)
if err != nil {
return nil, err
}
type algo int

const (
DFS algo = iota
KAHN
)

func TopologicalSort[T comparable, V []T](digraph map[T]V, algorithm algo) (solution V, err error) {
// kahnRgxp, err := regexp.Compile(`[Kk]ahn\z`)
// if err != nil {
// return nil, err
// }
// dfsBasedRgxp, err := regexp.Compile(`[Dd][Ff][Ss]-?[Bb]ased\z`)
// if err != nil {
// return nil, err
// }

if kahnRgxp.MatchString(algorithm) {
if algorithm == KAHN {
if solution, err = kahn(digraph); err != nil {
return nil, err
}
} else if dfsBasedRgxp.MatchString(algorithm) {
} else if algorithm == DFS {
if solution, err = dfsBased(digraph); err != nil {
return nil, err
}
15 changes: 8 additions & 7 deletions gorder_test.go
Original file line number Diff line number Diff line change
@@ -15,12 +15,13 @@ func TestTopologicalSort(t *testing.T) {

want := []int{1, 2, 3, 5, 4}

_, err := TopologicalSort(digraph, "ultrasuperfast")

_, err := TopologicalSort(digraph, 3)
if err == nil {
t.Fatal("TopologicalSort: should have returned an error")
}

o, err := TopologicalSort(digraph, "kahn")
o, err := TopologicalSort(digraph, KAHN)
if err != nil {
t.Fatalf("Kahn: %v", err)
}
@@ -30,7 +31,7 @@ func TestTopologicalSort(t *testing.T) {
}
}

o, err = TopologicalSort(digraph, "dfsbased")
o, err = TopologicalSort(digraph, DFS)
if err != nil {
t.Fatalf("DFS-based: %v", err)
}
@@ -46,11 +47,11 @@ func TestTopologicalSort(t *testing.T) {
3: {4, 5},
4: {2},
}
_, err = TopologicalSort(graphWithCycles, "kahn")
_, err = TopologicalSort(graphWithCycles, KAHN)
if err == nil {
t.Fatal("Kahn: should have returned an error")
}
_, err = TopologicalSort(graphWithCycles, "dfsbased")
_, err = TopologicalSort(graphWithCycles, DFS)
if err == nil {
t.Fatal("DFS-based: should have returned an error")
}
@@ -60,15 +61,15 @@ func BenchmarkTopologicalSort(b *testing.B) {
digraph := dagenerator.Generate(10, 50, 30, 50, 30)
b.Run("kahn", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := TopologicalSort(digraph, "kahn")
_, err := TopologicalSort(digraph, KAHN)
if err != nil {
b.Errorf("Kahn: %v", err)
}
}
})
b.Run("dfs based", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := TopologicalSort(digraph, "dfsbased")
_, err := TopologicalSort(digraph, DFS)
if err != nil {
b.Errorf("DFS-based: %v", err)
}