2021-11-01 19:58:55 +01:00
|
|
|
package sequential
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------- *
|
|
|
|
* IMPORTS
|
|
|
|
* ---------------------------------------------------------------- */
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"ads/internal/core/metrics"
|
|
|
|
"ads/internal/core/utils"
|
|
|
|
"ads/internal/setup"
|
|
|
|
)
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------- *
|
|
|
|
* GLOBAL VARIABLES/CONSTANTS
|
|
|
|
* ---------------------------------------------------------------- */
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------- *
|
|
|
|
* CHECKS
|
|
|
|
* ---------------------------------------------------------------- */
|
|
|
|
|
|
|
|
func preChecks(L []int, _ ...interface{}) error {
|
|
|
|
// Keine Checks!
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func postChecks(L []int, x int, index int, _ ...interface{}) error {
|
|
|
|
if utils.ArrayContains(L, x) {
|
|
|
|
if !(index >= 0) {
|
|
|
|
return fmt.Errorf("Der Algorithmus sollte nicht -1 zurückgeben.")
|
|
|
|
}
|
|
|
|
if L[index] != x {
|
|
|
|
return fmt.Errorf("Der Algorithmus hat den falschen Index bestimmt.")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if index != -1 {
|
|
|
|
return fmt.Errorf("Der Algorithmus sollte -1 zurückgeben.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------- *
|
|
|
|
* METHOD sequential search + Display
|
|
|
|
* ---------------------------------------------------------------- */
|
|
|
|
|
|
|
|
func FancySequentialSearch(input_L []int, input_x int) (int, error) {
|
|
|
|
var name = "Sequenziellsuchalgorithmus"
|
|
|
|
var inputs = map[string]interface{}{
|
|
|
|
"L": input_L,
|
|
|
|
"x": input_x,
|
|
|
|
}
|
|
|
|
var outputs map[string]interface{}
|
|
|
|
var (
|
|
|
|
output_index int
|
|
|
|
)
|
|
|
|
var err error
|
|
|
|
|
|
|
|
do_once := true
|
|
|
|
for do_once {
|
|
|
|
do_once = false
|
2021-11-02 09:07:04 +01:00
|
|
|
|
|
|
|
// Start Message
|
2021-11-01 19:58:55 +01:00
|
|
|
setup.DisplayStartOfAlgorithm(name, inputs)
|
2021-11-02 09:07:04 +01:00
|
|
|
|
2021-11-01 19:58:55 +01:00
|
|
|
// Prechecks:
|
2021-11-02 09:07:04 +01:00
|
|
|
if setup.AppConfigPerformChecks() {
|
|
|
|
err = preChecks(input_L, input_x)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2021-11-01 19:58:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ausführung des Algorithmus:
|
2021-11-02 13:05:34 +01:00
|
|
|
metrics.ResetMetrics()
|
|
|
|
metrics.StartMetrics()
|
2021-11-01 19:58:55 +01:00
|
|
|
output_index = SequentialSearch(input_L, input_x)
|
2021-11-02 13:05:34 +01:00
|
|
|
metrics.StopMetrics()
|
2021-11-01 19:58:55 +01:00
|
|
|
outputs = map[string]interface{}{
|
|
|
|
"index": output_index,
|
|
|
|
}
|
|
|
|
|
2021-11-02 09:07:04 +01:00
|
|
|
// Metriken anzeigen
|
|
|
|
if setup.AppConfigShowMetrics() {
|
|
|
|
setup.DisplayMetrics()
|
|
|
|
}
|
|
|
|
|
|
|
|
// End Message
|
2021-11-01 19:58:55 +01:00
|
|
|
setup.DisplayEndOfAlgorithm(outputs)
|
|
|
|
|
|
|
|
// Postchecks:
|
2021-11-02 09:07:04 +01:00
|
|
|
if setup.AppConfigPerformChecks() {
|
|
|
|
err = postChecks(input_L, input_x, output_index)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2021-11-01 19:58:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return output_index, err
|
|
|
|
}
|