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,37 @@
package sequential
/* ---------------------------------------------------------------- *
* IMPORTS
* ---------------------------------------------------------------- */
import (
"ads/internal/core/logging"
"ads/internal/core/metrics"
)
/* ---------------------------------------------------------------- *
* GLOBAL VARIABLES/CONSTANTS
* ---------------------------------------------------------------- */
//
/* ---------------------------------------------------------------- *
* ALGORITHM sequential search
* ---------------------------------------------------------------- */
/*
Inputs: L = Liste von Zahlen, x = Zahl.
Outputs: Position von x in L, sonst 1 wenn x nicht in L.
*/
func SequentialSearch(L []int, x int) int {
n := len(L)
for i := 0; i < n; i++ {
metrics.AddTimeCost()
if L[i] == x {
logging.Debug("Element in Position %[1]v gefunden.", i)
return i
}
logging.Debug("Element nicht in Position %[1]v.", i)
}
return -1
}

View File

@@ -0,0 +1,104 @@
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
// 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 = SequentialSearch(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
}