ads1_2021/code/golang/internal/endpoints/run/run.go

174 lines
5.6 KiB
Go
Raw Normal View History

2021-11-04 10:36:31 +01:00
package run
/* ---------------------------------------------------------------- *
* IMPORTS
* ---------------------------------------------------------------- */
import (
"fmt"
2021-11-03 19:34:57 +01:00
"ads/internal/core/logging"
"ads/internal/setup"
"ads/internal/types"
algorithm_search_binary "ads/internal/algorithms/search/binary"
algorithm_search_interpol "ads/internal/algorithms/search/interpol"
algorithm_search_ith_element "ads/internal/algorithms/search/ith_element"
algorithm_search_jump "ads/internal/algorithms/search/jump"
algorithm_search_poison "ads/internal/algorithms/search/poison"
algorithm_search_sequential "ads/internal/algorithms/search/sequential"
algorithm_stacks_next_greater_element "ads/internal/algorithms/stacks/next_greater_element"
algorithm_sum_maxsubsum "ads/internal/algorithms/sum/maxsubsum"
)
2021-11-04 10:36:31 +01:00
/* ---------------------------------------------------------------- *
* ENDPOINT run interactive modus
* ---------------------------------------------------------------- */
// Startet App im interaktiven Modus
func RunInteractive() error {
logging.Plain(setup.Logo())
2021-11-04 10:36:31 +01:00
_, err := menuMain.ShowMenu()
logging.Info("Programm terminiert.")
2021-11-04 10:36:31 +01:00
return err
}
/* ---------------------------------------------------------------- *
* ENDPOINT run non-interactive modus
* ---------------------------------------------------------------- */
// Liest Config Datei ein und führt Algorithmen auf Fälle durch
func RunNonInteractive(path string) error {
var err error
var err_case error
// extrahiere user config
config := setup.NewUserConfig()
err = setup.GetUserConfig(path, &config)
if err != nil {
return err
}
logging.Plain(setup.Logo())
// Fälle extrahieren
cases := []types.UserConfigCase{}
if config.Parts != nil && config.Parts.Cases != nil {
cases = *config.Parts.Cases
}
for i := 0; i < len(cases); i++ {
err_case = nil
problem := cases[i]
setup.DisplayStartOfCase(i, problem.Description)
inputs := types.UserConfigInputs{}
if problem.Inputs != nil {
inputs = *problem.Inputs
}
if problem.Command == nil {
err_case = fmt.Errorf("Attribute 'command:' fehlt!")
} else {
switch *problem.Command {
case "algorithm-search-binary":
L := inputs.List
x := inputs.SearchValue
if L != nil && x != nil {
_, err_case = algorithm_search_binary.FancyBinarySearch(*L, *x)
} else {
err_case = fmt.Errorf("Fehlende Inputs für Befehl '%[1]s'.", *problem.Command)
}
case "algorithm-search-interpolation":
L := inputs.List
x := inputs.SearchValue
if L != nil && x != nil {
_, err_case = algorithm_search_interpol.FancyInterpolationSearch(*L, *x, 0, len(*L)-1)
} else {
err_case = fmt.Errorf("Fehlende Inputs für Befehl '%[1]s'.", *problem.Command)
}
case "algorithm-search-ith-element":
L := inputs.List
i := inputs.SearchRank
if L != nil && i != nil {
_, err_case = algorithm_search_ith_element.FancyFindIthSmallest(*L, *i)
} else {
err_case = fmt.Errorf("Fehlende Inputs für Befehl '%[1]s'.", *problem.Command)
}
case "algorithm-search-ith-element-dc":
L := inputs.List
i := inputs.SearchRank
if L != nil && i != nil {
_, err_case = algorithm_search_ith_element.FancyFindIthSmallestDC(*L, *i)
} else {
err_case = fmt.Errorf("Fehlende Inputs für Befehl '%[1]s'.", *problem.Command)
}
case "algorithm-search-jump":
L := inputs.List
x := inputs.SearchValue
m := inputs.JumpSize
if L != nil && x != nil {
_, err_case = algorithm_search_jump.FancyJumpSearchLinear(*L, *x, *m)
} else {
err_case = fmt.Errorf("Fehlende Inputs für Befehl '%[1]s'.", *problem.Command)
}
case "algorithm-search-jump-exp":
L := inputs.List
x := inputs.SearchValue
if L != nil && x != nil {
_, err_case = algorithm_search_jump.FancyJumpSearchExponentiell(*L, *x)
} else {
err_case = fmt.Errorf("Fehlende Inputs für Befehl '%[1]s'.", *problem.Command)
}
case "algorithm-search-poison":
L := inputs.List
if L != nil {
_, err_case = algorithm_search_poison.FancyFindPoison(*L)
} else {
err_case = fmt.Errorf("Fehlende Inputs für Befehl '%[1]s'.", *problem.Command)
}
case "algorithm-search-poison-fast":
L := inputs.List
if L != nil {
_, err_case = algorithm_search_poison.FancyFindPoisonFast(*L)
} else {
err_case = fmt.Errorf("Fehlende Inputs für Befehl '%[1]s'.", *problem.Command)
}
case "algorithm-search-sequential":
L := inputs.List
x := inputs.SearchValue
if L != nil && x != nil {
_, err_case = algorithm_search_sequential.FancySequentialSearch(*L, *x)
} else {
err_case = fmt.Errorf("Fehlende Inputs für Befehl '%[1]s'.", *problem.Command)
}
case "algorithm-stacks-next-greater-element":
L := inputs.List
if L != nil {
_, err_case = algorithm_stacks_next_greater_element.FancyNextGreaterElement(*L)
} else {
err_case = fmt.Errorf("Fehlende Inputs für Befehl '%[1]s'.", *problem.Command)
}
case "algorithm-sum-maxsub":
L := inputs.List
if L != nil {
_, _, _, err_case = algorithm_sum_maxsubsum.FancyMaxSubSum(*L)
} else {
err_case = fmt.Errorf("Fehlende Inputs für Befehl '%[1]s'.", *problem.Command)
}
case "algorithm-sum-maxsub-dc":
L := inputs.List
if L != nil {
_, _, _, err_case = algorithm_sum_maxsubsum.FancyMaxSubSumDC(*L)
} else {
err_case = fmt.Errorf("Fehlende Inputs für Befehl '%[1]s'.", *problem.Command)
}
default:
err_case = fmt.Errorf("Unbekannter Befehl '%[1]s'.", *problem.Command)
}
}
if err_case != nil {
logging.Error(err_case)
}
}
setup.DisplayEndOfCase()
return nil
}