2021-11-01 19:58:55 +01:00
|
|
|
package interpol
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------- *
|
|
|
|
* 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 interpolation + Display
|
|
|
|
* ---------------------------------------------------------------- */
|
|
|
|
|
|
|
|
func FancyInterpolationSearch(input_L []int, input_x int, input_u int, input_v int) (int, error) {
|
|
|
|
var name = "Interpolationssuchalgorithmus"
|
|
|
|
var inputs = map[string]interface{}{
|
|
|
|
"L": input_L,
|
|
|
|
"x": input_x,
|
|
|
|
"u": input_u,
|
|
|
|
"v": input_v,
|
|
|
|
}
|
|
|
|
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, input_u, input_v)
|
|
|
|
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 = InterpolationSearch(input_L, input_x, input_u, input_v)
|
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
|
|
|
|
}
|