refactored data-types to reduce plugin sizes
This commit is contained in:
@@ -0,0 +1,222 @@
|
||||
// Copyright (c) 2024-2026 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// list-type.go
|
||||
package array
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.portale-stac.it/go-pkg/expr/kern"
|
||||
)
|
||||
|
||||
type ListType []any
|
||||
|
||||
func IsList(v any) (ok bool) {
|
||||
_, ok = v.(*ListType)
|
||||
return ok
|
||||
}
|
||||
|
||||
func NewListA(listAny ...any) (list *ListType) {
|
||||
if listAny == nil {
|
||||
listAny = []any{}
|
||||
}
|
||||
return NewList(listAny)
|
||||
}
|
||||
|
||||
func NewList(listAny []any) (list *ListType) {
|
||||
if listAny != nil {
|
||||
ls := make(ListType, len(listAny))
|
||||
copy(ls, listAny)
|
||||
list = &ls
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func MakeList(length, capacity int) (list *ListType) {
|
||||
if capacity < length {
|
||||
capacity = length
|
||||
}
|
||||
ls := make(ListType, length, capacity)
|
||||
list = &ls
|
||||
return
|
||||
}
|
||||
|
||||
func ListFromStrings(stringList []string) (list *ListType) {
|
||||
list = MakeList(len(stringList), 0)
|
||||
for i, s := range stringList {
|
||||
(*list)[i] = s
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (ls *ListType) ToString(opt kern.FmtOpt) (s string) {
|
||||
indent := kern.GetFormatIndent(opt)
|
||||
flags := kern.GetFormatFlags(opt)
|
||||
|
||||
var sb strings.Builder
|
||||
sb.WriteByte('[')
|
||||
if len(*ls) > 0 {
|
||||
innerOpt := kern.MakeFormatOptions(flags, indent+1)
|
||||
nest := strings.Repeat(" ", indent+1)
|
||||
|
||||
if flags&kern.MultiLine != 0 {
|
||||
sb.WriteByte('\n')
|
||||
sb.WriteString(nest)
|
||||
}
|
||||
for i, item := range []any(*ls) {
|
||||
if i > 0 {
|
||||
if flags&kern.MultiLine != 0 {
|
||||
sb.WriteString(",\n")
|
||||
sb.WriteString(nest)
|
||||
} else {
|
||||
sb.WriteString(", ")
|
||||
}
|
||||
}
|
||||
kern.Format(&sb, item, innerOpt)
|
||||
}
|
||||
if flags&kern.MultiLine != 0 {
|
||||
sb.WriteByte('\n')
|
||||
sb.WriteString(strings.Repeat(" ", indent))
|
||||
}
|
||||
}
|
||||
sb.WriteByte(']')
|
||||
s = sb.String()
|
||||
if flags&kern.Truncate != 0 && len(s) > kern.TruncateSize {
|
||||
s = kern.TruncateString(s)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (ls *ListType) String() string {
|
||||
return ls.ToString(0)
|
||||
}
|
||||
|
||||
func (ls *ListType) TypeName() string {
|
||||
return kern.TypeArray
|
||||
}
|
||||
|
||||
func (ls *ListType) Contains(t *ListType) (answer bool) {
|
||||
if len(*ls) >= len(*t) {
|
||||
answer = true
|
||||
for _, item := range *t {
|
||||
if answer = ls.IndexDeepSameCmp(item) >= 0; !answer {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (ls *ListType) EqualTo(other kern.Equaler) (equal bool) {
|
||||
if otherList, ok := other.(*ListType); ok {
|
||||
equal = ls.Equals(*otherList)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (ls *ListType) Equals(ls2 ListType) (answer bool) {
|
||||
if ls2 != nil && len(*ls) == len(ls2) {
|
||||
answer = true
|
||||
for index, i1 := range *ls {
|
||||
// if !reflect.DeepEqual(i1, ls2[index]) {
|
||||
// answer = false
|
||||
// break
|
||||
// }
|
||||
if !kern.Equal(i1, ls2[index]) {
|
||||
answer = false
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (ls1 *ListType) Clone() (ls2 *ListType) {
|
||||
ls := make(ListType, len(*ls1))
|
||||
for i, item := range *ls1 {
|
||||
ls[i] = kern.Clone(item)
|
||||
}
|
||||
ls2 = &ls
|
||||
return
|
||||
}
|
||||
|
||||
func (ls *ListType) IndexDeepSameCmp(target any) (index int) {
|
||||
index = -1
|
||||
for i, item := range *ls {
|
||||
if kern.Equal(item, target) {
|
||||
index = i
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// func (ls *ListType) IndexDeepSameCmp(target any) (index int) {
|
||||
// var eq bool
|
||||
// var err error
|
||||
// index = -1
|
||||
// for i, item := range *ls {
|
||||
// if eq, err = deepSame(item, target, SameContent); err != nil {
|
||||
// break
|
||||
// } else if eq {
|
||||
// index = i
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
|
||||
func SameContent(a, b any) (same bool, err error) {
|
||||
la, _ := a.(*ListType)
|
||||
lb, _ := b.(*ListType)
|
||||
if len(*la) == len(*lb) {
|
||||
same = true
|
||||
for _, item := range *la {
|
||||
if pos := lb.IndexDeepSameCmp(item); pos < 0 {
|
||||
same = false
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// func deepSame(a, b any, deepCmp kern.DeepFuncTemplate) (eq bool, err error) {
|
||||
// if IsNumOrFract(a) && IsNumOrFract(b) {
|
||||
// if IsNumber(a) && IsNumber(b) {
|
||||
// if IsInteger(a) && IsInteger(b) {
|
||||
// li, _ := a.(int64)
|
||||
// ri, _ := b.(int64)
|
||||
// eq = li == ri
|
||||
// } else {
|
||||
// eq = NumAsFloat(a) == NumAsFloat(b)
|
||||
// }
|
||||
// } else {
|
||||
// var cmp int
|
||||
// if cmp, err = fract.CmpAnyFract(a, b); err == nil {
|
||||
// eq = cmp == 0
|
||||
// }
|
||||
// }
|
||||
// } else if deepCmp != nil && IsList(a) && IsList(b) {
|
||||
// eq, err = deepCmp(a, b)
|
||||
// } else {
|
||||
// eq = reflect.DeepEqual(a, b)
|
||||
// }
|
||||
|
||||
// return
|
||||
// }
|
||||
|
||||
func (ls *ListType) SetItem(index int64, value any) (err error) {
|
||||
if index >= 0 && index < int64(len(*ls)) {
|
||||
(*ls)[index] = value
|
||||
} else {
|
||||
err = fmt.Errorf("index %d out of bounds (0, %d)", index, len(*ls)-1)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (ls *ListType) AppendItem(value any) {
|
||||
*ls = append(*ls, value)
|
||||
}
|
||||
Reference in New Issue
Block a user