2021-05-09 18:22:12 +02:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2021-05-12 18:44:53 +02:00
|
|
|
"encoding/json"
|
2021-05-09 18:22:12 +02:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------- *
|
2021-05-12 18:44:53 +02:00
|
|
|
* METHODS: json
|
2021-05-09 18:22:12 +02:00
|
|
|
* ---------------------------------------------------------------- */
|
|
|
|
|
2021-05-12 18:44:53 +02:00
|
|
|
func JsonToArrayOfStrings(s string, value *[]string) {
|
|
|
|
var err error
|
|
|
|
err = json.Unmarshal([]byte(s), &value)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2021-05-09 18:22:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 18:44:53 +02:00
|
|
|
/* ---------------------------------------------------------------- *
|
|
|
|
* METHODS: conversion
|
|
|
|
* ---------------------------------------------------------------- */
|
|
|
|
|
|
|
|
func BoolToInt(cond bool) int {
|
|
|
|
if cond {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------- *
|
|
|
|
* METHODS: for integer lists
|
|
|
|
* ---------------------------------------------------------------- */
|
|
|
|
|
2021-05-09 18:22:12 +02:00
|
|
|
func Min2(x int, y int) int {
|
|
|
|
if x <= y {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
|
|
|
|
func Max2(x int, y int) int {
|
|
|
|
if x >= y {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
|
2021-05-12 18:44:53 +02:00
|
|
|
func MinList(x []int) int {
|
|
|
|
if len(x) == 0 {
|
2021-05-12 22:35:10 +02:00
|
|
|
panic("Cannot compute the maximum of an empty array.")
|
2021-05-12 18:44:53 +02:00
|
|
|
}
|
2021-05-12 22:35:10 +02:00
|
|
|
var val int = x[0]
|
2021-05-12 18:44:53 +02:00
|
|
|
for i, val_ := range x {
|
|
|
|
if i > 0 {
|
|
|
|
val = Min2(val, val_)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
func MaxList(x []int) int {
|
|
|
|
if len(x) == 0 {
|
2021-05-12 22:35:10 +02:00
|
|
|
panic("Cannot compute the maximum of an empty array.")
|
2021-05-12 18:44:53 +02:00
|
|
|
}
|
2021-05-12 22:35:10 +02:00
|
|
|
var val int = x[0]
|
2021-05-12 18:44:53 +02:00
|
|
|
for i, val_ := range x {
|
|
|
|
if i > 0 {
|
|
|
|
val = Max2(val, val_)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2021-05-12 22:35:10 +02:00
|
|
|
func SumList(x []int) int {
|
|
|
|
var val int = 0
|
|
|
|
for _, val_ := range x {
|
|
|
|
val += val_
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2021-05-12 18:44:53 +02:00
|
|
|
/* ---------------------------------------------------------------- *
|
|
|
|
* METHODS: for string lists
|
|
|
|
* ---------------------------------------------------------------- */
|
|
|
|
|
2021-05-18 11:32:32 +02:00
|
|
|
func CopyStringList(list []string) []string {
|
|
|
|
var listCopy = make([]string, len(list))
|
|
|
|
for i, value := range list {
|
|
|
|
listCopy[i] = value
|
|
|
|
}
|
|
|
|
return listCopy
|
|
|
|
}
|
|
|
|
|
2021-05-12 18:44:53 +02:00
|
|
|
func StrListContains(list []string, x string) bool {
|
|
|
|
for _, obj := range list {
|
|
|
|
if obj == x {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-05-09 18:22:12 +02:00
|
|
|
func SortStrings(list *[]string) {
|
|
|
|
sort.Slice(*list, func(i int, j int) bool {
|
|
|
|
u := strings.ToLower((*list)[i])
|
|
|
|
v := strings.ToLower((*list)[j])
|
|
|
|
cmp := strings.Compare(u, v)
|
|
|
|
return (cmp < 0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func FilterStrings(list *[]string, f func(string) bool) []string {
|
|
|
|
var listFiltered = []string{}
|
|
|
|
for _, val := range *list {
|
|
|
|
if f(val) {
|
|
|
|
listFiltered = append(listFiltered, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return listFiltered
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnionStrings2(list1 []string, list2 []string) []string {
|
|
|
|
var mark = make(map[string]bool)
|
|
|
|
for _, item := range list1 {
|
|
|
|
mark[item] = true
|
|
|
|
}
|
|
|
|
for _, item := range list2 {
|
|
|
|
mark[item] = true
|
|
|
|
}
|
|
|
|
var list = make([]string, len(mark))
|
|
|
|
var i int = 0
|
|
|
|
for item, _ := range mark {
|
|
|
|
list[i] = item
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2021-05-12 18:44:53 +02:00
|
|
|
// assumes that listTo contains no duplicates
|
2021-05-14 16:58:27 +02:00
|
|
|
func UnionStringsInPlace(listTo *[]string, listFrom []string) {
|
2021-05-09 18:22:12 +02:00
|
|
|
var mark = make(map[string]bool)
|
|
|
|
for _, item := range listFrom {
|
|
|
|
mark[item] = true
|
|
|
|
}
|
|
|
|
for _, item := range *listTo {
|
|
|
|
mark[item] = false // signals suppression of duplicate addition
|
|
|
|
}
|
|
|
|
for item, isNew := range mark {
|
|
|
|
if isNew {
|
|
|
|
*listTo = append(*listTo, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-12 18:44:53 +02:00
|
|
|
|
|
|
|
func UnionStringsList(lists [][]string) []string {
|
|
|
|
var list = []string{}
|
|
|
|
for _, list_ := range lists {
|
2021-05-14 16:58:27 +02:00
|
|
|
UnionStringsInPlace(&list, list_)
|
2021-05-12 18:44:53 +02:00
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
2021-05-18 11:32:32 +02:00
|
|
|
|
|
|
|
/* ---------------------------------------------------------------- *
|
|
|
|
* METHODS: for maps
|
|
|
|
* ---------------------------------------------------------------- */
|
|
|
|
|
|
|
|
func CopyMapStringBool(m map[string]bool) map[string]bool {
|
|
|
|
var mCopy = map[string]bool{}
|
|
|
|
for key, value := range m {
|
|
|
|
mCopy[key] = value
|
|
|
|
}
|
|
|
|
return mCopy
|
|
|
|
}
|