2021-10-30 10:19:16 +02:00
|
|
|
package utils
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------- *
|
|
|
|
* IMPORTS
|
|
|
|
* ---------------------------------------------------------------- */
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------- *
|
|
|
|
* METHOD array contains
|
|
|
|
* ---------------------------------------------------------------- */
|
|
|
|
|
|
|
|
func ArrayContains(x interface{}, elem interface{}) 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
|
|
|
|
}
|
|
|
|
|
2021-11-01 19:58:55 +01:00
|
|
|
/* ---------------------------------------------------------------- *
|
|
|
|
* METHOD sort
|
|
|
|
* ---------------------------------------------------------------- */
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2021-10-30 10:19:16 +02:00
|
|
|
|
|
|
|
/* ---------------------------------------------------------------- *
|
2021-11-01 19:58:55 +01:00
|
|
|
* METHOD insert, pop
|
2021-10-30 10:19:16 +02:00
|
|
|
* ---------------------------------------------------------------- */
|
|
|
|
|
2021-11-01 19:58:55 +01:00
|
|
|
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 {
|
2021-10-30 10:19:16 +02:00
|
|
|
var ok bool
|
2021-11-01 19:58:55 +01:00
|
|
|
m := map[int]bool{}
|
|
|
|
X_unique := []int{}
|
2021-10-30 10:19:16 +02:00
|
|
|
for _, x := range X {
|
|
|
|
if _, ok = m[x]; !ok {
|
|
|
|
X_unique = append(X_unique, x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return X_unique
|
|
|
|
}
|
|
|
|
|
2021-11-01 19:58:55 +01:00
|
|
|
func ContainsNoDuplicatesListInt(X []int) bool {
|
|
|
|
return len(X) <= len(UniqueListInt(X))
|
|
|
|
}
|
|
|
|
|
2021-10-30 10:19:16 +02:00
|
|
|
/* ---------------------------------------------------------------- *
|
|
|
|
* METHOD get value from array of unknown length
|
|
|
|
* ---------------------------------------------------------------- */
|
|
|
|
|
|
|
|
func GetArrayStringValue(arr *[]string, index int, Default string) string {
|
|
|
|
if arr != nil && len(*arr) > index {
|
|
|
|
return (*arr)[index]
|
|
|
|
}
|
|
|
|
return Default
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetArrayBoolValue(arr *[]bool, index int, Default bool) bool {
|
|
|
|
if arr != nil && len(*arr) > index {
|
|
|
|
return (*arr)[index]
|
|
|
|
}
|
|
|
|
return Default
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetArrayIntValue(arr *[]int, index int, Default int) int {
|
|
|
|
if arr != nil && len(*arr) > index {
|
|
|
|
return (*arr)[index]
|
|
|
|
}
|
|
|
|
return Default
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetArrayInterfaceValue(arr *[](interface{}), index int, Default interface{}) interface{} {
|
|
|
|
if arr != nil && len(*arr) > index {
|
|
|
|
return (*arr)[index]
|
|
|
|
}
|
|
|
|
return Default
|
|
|
|
}
|