master > master: src go - algorithmen + display methoden hinzugefügt
This commit is contained in:
50
code/golang/internal/core/metrics/metrics.go
Normal file
50
code/golang/internal/core/metrics/metrics.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package metrics
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"ads/internal/types"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* GLOBAL VARIABLES/CONSTANTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
var _ctr_time = types.NewCounter()
|
||||
var _ctr_space = types.NewCounter()
|
||||
var _tmr = types.NewTimer()
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHODS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func RestartMetrics() {
|
||||
_tmr.Reset()
|
||||
_ctr_time.Reset()
|
||||
_ctr_space.Reset()
|
||||
}
|
||||
|
||||
func AddTimeCost(options ...int) {
|
||||
_ctr_time.Add(options...)
|
||||
}
|
||||
|
||||
func AddSpaceCost(options ...int) {
|
||||
_ctr_space.Add(options...)
|
||||
}
|
||||
|
||||
func GetTimeCost() int {
|
||||
return _ctr_time.Size()
|
||||
}
|
||||
|
||||
func GetSpaceCost() int {
|
||||
return _ctr_space.Size()
|
||||
}
|
||||
|
||||
func GetTimeElapsed() time.Duration {
|
||||
_tmr.Stop()
|
||||
return _tmr.ElapsedTime()
|
||||
}
|
||||
@@ -24,26 +24,41 @@ func ArrayContains(x interface{}, elem interface{}) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// func ListComprehension(x interface{}, cond (interface{}) bool) bool {
|
||||
// xAsArray := reflect.ValueOf(x)
|
||||
// if xAsArray.Kind() == reflect.Slice {
|
||||
// for i := 0; i < xAsArray.Len(); i++ {
|
||||
// if xAsArray.Index(i).Interface() == elem {
|
||||
// return true
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return false
|
||||
// }
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHOD array contains
|
||||
* METHOD sort
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func UniqueListOfStrings(X []string) []string {
|
||||
func IsSortedListInt(X []int) bool {
|
||||
n := len(X)
|
||||
for i, x := range X {
|
||||
for j := i + 1; j < n; j++ {
|
||||
if X[j] < x {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHOD insert, pop
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func PopIndexListInt(X []int, index int) []int {
|
||||
// NOTE: aus irgendeinem Grund reicht es nicht aus mit X zu arbeiten, denn sonst die u. s. Zeile überschreibt X
|
||||
X_ := make([]int, len(X))
|
||||
copy(X_, X)
|
||||
return append(X_[:index], X_[(index+1):]...)
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHOD unique
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func UniqueListInt(X []int) []int {
|
||||
var ok bool
|
||||
m := map[string]bool{}
|
||||
X_unique := []string{}
|
||||
m := map[int]bool{}
|
||||
X_unique := []int{}
|
||||
for _, x := range X {
|
||||
if _, ok = m[x]; !ok {
|
||||
X_unique = append(X_unique, x)
|
||||
@@ -52,6 +67,10 @@ func UniqueListOfStrings(X []string) []string {
|
||||
return X_unique
|
||||
}
|
||||
|
||||
func ContainsNoDuplicatesListInt(X []int) bool {
|
||||
return len(X) <= len(UniqueListInt(X))
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHOD get value from array of unknown length
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
147
code/golang/internal/core/utils/utils_maths.go
Normal file
147
code/golang/internal/core/utils/utils_maths.go
Normal file
@@ -0,0 +1,147 @@
|
||||
package utils
|
||||
|
||||
/*
|
||||
In Golang sind manche (basic!) mathematischen Vorgänge etwas umständlich zu implementieren.
|
||||
Wir wollen diese Methoden komplett einhüllen, damit wir von ihrer Komplexität
|
||||
in den Algorithmen nicht abgelenkt werden.
|
||||
*/
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHOD floor, ceil
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func Floor(x float64) int {
|
||||
return int(math.Floor(x))
|
||||
}
|
||||
|
||||
func Ceil(x float64) int {
|
||||
return int(math.Ceil(x))
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHOD max, min
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func MinInt(x int, y ...int) int {
|
||||
if len(y) == 0 {
|
||||
return x
|
||||
} else if x < y[0] {
|
||||
return MinInt(x, y[1:]...)
|
||||
} else {
|
||||
return MinInt(y[0], y[1:]...)
|
||||
}
|
||||
}
|
||||
|
||||
func MaxInt(x int, y ...int) int {
|
||||
if len(y) == 0 {
|
||||
return x
|
||||
} else if x > y[0] {
|
||||
return MaxInt(x, y[1:]...)
|
||||
} else {
|
||||
return MaxInt(y[0], y[1:]...)
|
||||
}
|
||||
}
|
||||
|
||||
func ArgMinInt(X []int) int {
|
||||
if len(X) == 0 {
|
||||
return -1
|
||||
}
|
||||
minValue := X[0]
|
||||
index := 0
|
||||
for i, x := range X {
|
||||
if x < minValue {
|
||||
minValue = x
|
||||
index = i
|
||||
}
|
||||
}
|
||||
return index
|
||||
}
|
||||
|
||||
func ArgMaxInt(X []int) int {
|
||||
if len(X) == 0 {
|
||||
return -1
|
||||
}
|
||||
maxValue := X[0]
|
||||
index := 0
|
||||
for i, x := range X {
|
||||
if x > maxValue {
|
||||
maxValue = x
|
||||
index = i
|
||||
}
|
||||
}
|
||||
return index
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHOD sum
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func SumListInt(X []int) int {
|
||||
result := 0
|
||||
for _, x := range X {
|
||||
result += x
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func SumListFloat(X []float64) float64 {
|
||||
result := 0.
|
||||
for _, x := range X {
|
||||
result += x
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHODS binary
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
Für eine Zahl n != 0 liefert die Länge der binären Darstellung der Zahl,
|
||||
und für n == 0 liefert 0
|
||||
*/
|
||||
func LengthOfBinary(number int) int {
|
||||
if number == 0 {
|
||||
return 0
|
||||
}
|
||||
if number < 0 {
|
||||
number = -number
|
||||
}
|
||||
number_binary := fmt.Sprintf("%b", int64(number))
|
||||
return len([]rune(number_binary))
|
||||
}
|
||||
|
||||
/*
|
||||
Für eine Zahl n != 0 liefert die Bits der binären Darstellung
|
||||
*/
|
||||
func IntToBinary(number int) []int {
|
||||
if number == 0 || number == 1 {
|
||||
return []int{number}
|
||||
}
|
||||
number_binary := fmt.Sprintf("%b", int64(number))
|
||||
runes := []rune(number_binary)
|
||||
bits := make([]int, len(runes))
|
||||
for i, rune := range runes {
|
||||
bit, _ := strconv.ParseInt(fmt.Sprintf("%c", rune), 2, 64)
|
||||
bits[len(runes)-1-i] = int(bit)
|
||||
}
|
||||
return bits
|
||||
}
|
||||
|
||||
func NthBit(number int, digit int) int {
|
||||
bits := IntToBinary(number)
|
||||
if digit < len(bits) {
|
||||
return bits[digit]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
Reference in New Issue
Block a user