master > master: code go - ordner von internal -> pkg umgezogen

This commit is contained in:
RD
2021-11-05 15:48:06 +01:00
parent dbdb6a8480
commit 9237dbfad0
20 changed files with 20 additions and 20 deletions

View File

@@ -0,0 +1,96 @@
package jump
/* ---------------------------------------------------------------- *
* IMPORTS
* ---------------------------------------------------------------- */
import (
"ads/internal/core/logging"
"ads/internal/core/metrics"
"ads/pkg/algorithms/search/sequential"
)
/* ---------------------------------------------------------------- *
* GLOBAL VARIABLES/CONSTANTS
* ---------------------------------------------------------------- */
//
/* ---------------------------------------------------------------- *
* ALGORITHM jump search
* ---------------------------------------------------------------- */
/*
Inputs: L = Liste von Zahlen, x = Zahl, m = lineare Sprunggröße.
Annahmen:
- L sei aufsteigend sortiert.
- Idealerweise: L enthält keine Duplikate.
- Idealerweise: Abstände zw. Elementen nicht uniform.
Outputs: Position von x in L, sonst 1 wenn x nicht in L.
*/
func JumpSearchLinear(L []int, x int, m int) int {
i0 := 0
i1 := m
// ACHTUNG: dies ist eine while-Schleife ist golang:
for i0 < len(L) {
metrics.AddTimeCost()
if i1 > len(L) {
i1 = len(L)
}
block := L[i0:i1]
elementAfterBlock := block[len(block)-1] + 1
if x < elementAfterBlock {
logging.Debug("Element muss sich im Block [%[1]v, %[2]v) befinden.", i0, i1)
index := sequential.SequentialSearch(block, x)
if index == -1 {
return -1 // wenn nicht gefunden
}
return index + i0 // NOTE: muss wegen Offset kompensieren
}
logging.Debug("Element befindet sich nicht im Block [%[1]v, %[2]v).", i0, i1)
i0 = i1
i1 += m
}
return -1
}
/* ---------------------------------------------------------------- *
* ALGORITHM jump search - exponentiell
* ---------------------------------------------------------------- */
/*
Inputs: L = Liste von Zahlen, x = Zahl.
Annahmen:
- L sei aufsteigend sortiert.
- L enthält keine Duplikate.
Outputs: Position von x in L, sonst 1 wenn x nicht in L.
*/
func JumpSearchExponentiell(L []int, x int) int {
i0 := 0
i1 := 1
// ACHTUNG: dies ist eine while-Schleife ist golang:
for i0 < len(L) {
metrics.AddTimeCost()
if i1 > len(L) {
i1 = len(L)
}
block := L[i0:i1]
elementAfterBlock := block[len(block)-1] + 1
if x < elementAfterBlock {
logging.Debug("Element muss sich im Block [%[1]v, %[2]v) befinden.", i0, i1)
index := sequential.SequentialSearch(block, x)
if index == -1 {
return -1 // wenn nicht gefunden
}
return index + i0 // NOTE: muss wegen Offset kompensieren
}
logging.Debug("Element befindet sich nicht im Block [%[1]v, %[2]v).", i0, i1)
i0 = i1
i1 *= 2
}
return -1
}

View File

@@ -0,0 +1,168 @@
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!")
}
// NOTE: nicht prüfen, ob Duplikate existieren. Das ist nur eine erwünschte aber keine notwendige Annahme.
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
// Start Message
setup.DisplayStartOfAlgorithm(name, inputs)
// Prechecks:
if setup.AppConfigPerformChecks() {
err = preChecks(input_L, input_x, input_m)
if err != nil {
break
}
}
// Ausführung des Algorithmus:
metrics.ResetMetrics()
metrics.StartMetrics()
output_index = JumpSearchLinear(input_L, input_x, input_m)
metrics.StopMetrics()
outputs = map[string]interface{}{
"index": output_index,
}
// Metriken anzeigen
if setup.AppConfigShowMetrics() {
setup.DisplayMetrics()
}
// End Message
setup.DisplayEndOfAlgorithm(outputs)
// Postchecks:
if setup.AppConfigPerformChecks() {
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
// Start Message
setup.DisplayStartOfAlgorithm(name, inputs)
// Prechecks:
if setup.AppConfigPerformChecks() {
err = preChecks(input_L, input_x)
if err != nil {
break
}
}
// Ausführung des Algorithmus:
metrics.ResetMetrics()
metrics.StartMetrics()
output_index = JumpSearchExponentiell(input_L, input_x)
metrics.StopMetrics()
outputs = map[string]interface{}{
"index": output_index,
}
// Metriken anzeigen
if setup.AppConfigShowMetrics() {
setup.DisplayMetrics()
}
// End Message
setup.DisplayEndOfAlgorithm(outputs)
// Postchecks:
if setup.AppConfigPerformChecks() {
err = postChecks(input_L, input_x, output_index)
if err != nil {
break
}
}
}
return output_index, err
}