144 lines
3.2 KiB
Go
144 lines
3.2 KiB
Go
|
package jump
|
||
|
|
||
|
/* ---------------------------------------------------------------- *
|
||
|
* IMPORTS
|
||
|
* ---------------------------------------------------------------- */
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
"ads/internal/core/metrics"
|
||
|
"ads/internal/core/utils"
|
||
|
"ads/internal/setup"
|
||
|
)
|
||
|
|
||
|
/* ---------------------------------------------------------------- *
|
||
|
* GLOBAL VARIABLES/CONSTANTS
|
||
|
* ---------------------------------------------------------------- */
|
||
|
|
||
|
//
|
||
|
|
||
|
/* ---------------------------------------------------------------- *
|
||
|
* CHECKS
|
||
|
* ---------------------------------------------------------------- */
|
||
|
|
||
|
func preChecks(L []int, _ ...interface{}) error {
|
||
|
if !utils.IsSortedListInt(L) {
|
||
|
return fmt.Errorf("Ungültiger Input: L muss aufsteigend sortiert sein!")
|
||
|
}
|
||
|
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
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------- *
|
||
|
* ALGORITHM jump search + Display
|
||
|
* ---------------------------------------------------------------- */
|
||
|
|
||
|
func FancyJumpSearchLinear(input_L []int, input_x int, input_m int) (int, error) {
|
||
|
var name = "Sprungsuche"
|
||
|
var inputs = map[string]interface{}{
|
||
|
"L": input_L,
|
||
|
"x": input_x,
|
||
|
"m": input_m,
|
||
|
}
|
||
|
var outputs map[string]interface{}
|
||
|
var (
|
||
|
output_index int
|
||
|
)
|
||
|
var err error
|
||
|
|
||
|
do_once := true
|
||
|
for do_once {
|
||
|
do_once = false
|
||
|
setup.DisplayStartOfAlgorithm(name, inputs)
|
||
|
metrics.RestartMetrics()
|
||
|
|
||
|
// Prechecks:
|
||
|
err = preChecks(input_L, input_x, input_m)
|
||
|
if err != nil {
|
||
|
break
|
||
|
}
|
||
|
|
||
|
// Ausführung des Algorithmus:
|
||
|
output_index = JumpSearchLinear(input_L, input_x, input_m)
|
||
|
outputs = map[string]interface{}{
|
||
|
"index": output_index,
|
||
|
}
|
||
|
|
||
|
// Letzte Messages:
|
||
|
setup.DisplayMetrics()
|
||
|
setup.DisplayEndOfAlgorithm(outputs)
|
||
|
|
||
|
// Postchecks:
|
||
|
err = postChecks(input_L, input_x, output_index)
|
||
|
if err != nil {
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return output_index, err
|
||
|
}
|
||
|
|
||
|
/* ---------------------------------------------------------------- *
|
||
|
* ALGORITHM jump search - exponentiell + Display
|
||
|
* ---------------------------------------------------------------- */
|
||
|
|
||
|
func FancyJumpSearchExponentiell(input_L []int, input_x int) (int, error) {
|
||
|
var name = "Sprungsuche (exponentiell)"
|
||
|
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
|
||
|
setup.DisplayStartOfAlgorithm(name, inputs)
|
||
|
metrics.RestartMetrics()
|
||
|
|
||
|
// Prechecks:
|
||
|
err = preChecks(input_L, input_x)
|
||
|
if err != nil {
|
||
|
break
|
||
|
}
|
||
|
|
||
|
// Ausführung des Algorithmus:
|
||
|
output_index = JumpSearchExponentiell(input_L, input_x)
|
||
|
outputs = map[string]interface{}{
|
||
|
"index": output_index,
|
||
|
}
|
||
|
|
||
|
// Letzte Messages:
|
||
|
setup.DisplayMetrics()
|
||
|
setup.DisplayEndOfAlgorithm(outputs)
|
||
|
|
||
|
// Postchecks:
|
||
|
err = postChecks(input_L, input_x, output_index)
|
||
|
if err != nil {
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return output_index, err
|
||
|
}
|