completed list-type to array-type conversion and fixed common test module

This commit is contained in:
2026-07-13 10:42:17 +02:00
parent 77f36642b4
commit 072fd9af39
39 changed files with 160 additions and 318 deletions
-226
View File
@@ -1,226 +0,0 @@
// 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 ArrayType []any
func IsList(v any) (ok bool) {
_, ok = v.(*ArrayType)
return ok
}
func NewArrayA(listAny ...any) (list *ArrayType) {
if listAny == nil {
listAny = []any{}
}
return NewArray(listAny)
}
func NewArray(arrayAny []any) (aRef *ArrayType) {
if arrayAny != nil {
a := make(ArrayType, len(arrayAny))
copy(a, arrayAny)
aRef = &a
}
return
}
func MakeArray(length, capacity int) (list *ArrayType) {
if capacity < length {
capacity = length
}
ls := make(ArrayType, length, capacity)
list = &ls
return
}
func ArrayFromStrings(stringSlice []string) (aRef *ArrayType) {
aRef = MakeArray(len(stringSlice), 0)
for i, s := range stringSlice {
(*aRef)[i] = s
}
return
}
func (aRef *ArrayType) ToString(opt kern.FmtOpt) (s string) {
indent := kern.GetFormatIndent(opt)
flags := kern.GetFormatFlags(opt)
var sb strings.Builder
sb.WriteByte('[')
if len(*aRef) > 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(*aRef) {
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 (aRef *ArrayType) String() string {
return aRef.ToString(0)
}
func (aRef *ArrayType) TypeName() string {
return kern.TypeArray
}
func (aRef *ArrayType) Contains(t *ArrayType) (answer bool) {
if len(*aRef) >= len(*t) {
answer = true
for _, item := range *t {
if answer = aRef.IndexDeepSameCmp(item) >= 0; !answer {
break
}
}
}
return
}
func (aRef *ArrayType) EqualTo(other kern.Equaler) (equal bool) {
if otherList, ok := other.(*ArrayType); ok {
equal = aRef.Equals(*otherList)
}
return
}
func (aRef *ArrayType) Equals(ls2 ArrayType) (answer bool) {
if ls2 != nil && len(*aRef) == len(ls2) {
answer = true
for index, i1 := range *aRef {
// if !reflect.DeepEqual(i1, ls2[index]) {
// answer = false
// break
// }
if !kern.Equal(i1, ls2[index]) {
answer = false
break
}
}
}
return
}
func (aRef *ArrayType) Clone() (ls2 *ArrayType) {
ls := make(ArrayType, len(*aRef))
for i, item := range *aRef {
ls[i] = kern.Clone(item)
}
ls2 = &ls
return
}
func (aRef *ArrayType) IndexDeepSameCmp(target any) (index int) {
index = -1
for i, item := range *aRef {
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) {
aRef, aValid := a.(*ArrayType)
bRef, bValid := b.(*ArrayType)
if !aValid || !bValid {
err = fmt.Errorf("invalid type for comparison")
return
}
if len(*aRef) == len(*bRef) {
same = true
for _, item := range *aRef {
if pos := bRef.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 (aRef *ArrayType) SetItem(index int64, value any) (err error) {
if index >= 0 && index < int64(len(*aRef)) {
(*aRef)[index] = value
} else {
err = fmt.Errorf("index %d out of bounds (0, %d)", index, len(*aRef)-1)
}
return
}
func (aRef *ArrayType) AppendItem(value any) {
*aRef = append(*aRef, value)
}
+1 -1
View File
@@ -23,7 +23,7 @@ func Equal(value1, value2 any) (equal bool) {
equal = false
} else if boolean.IsBool(value1) && boolean.IsBool(value2) {
equal = value1.(bool) == value2.(bool)
} else if array.IsList(value1) && array.IsList(value2) {
} else if array.IsArray(value1) && array.IsArray(value2) {
ls1 := value1.(*array.ArrayType)
ls2 := value2.(*array.ArrayType)
equal = ls1.Equals(*ls2)
+10 -3
View File
@@ -160,12 +160,12 @@ func (dict *DictType) GetItem(key any) (value any, exists bool) {
return
}
func (dict *DictType) Clone() (c *DictType) {
c = newDict(nil)
func (dict *DictType) Clone() kern.Cloner {
c := newDict(nil)
for k, v := range *dict {
(*c)[k] = kern.Clone(v)
}
return
return c
}
func (dict *DictType) Merge(second *DictType) {
@@ -176,6 +176,13 @@ func (dict *DictType) Merge(second *DictType) {
}
}
func (dict *DictType) EqualTo(other kern.Equaler) (answer bool) {
if dict2, ok := other.(*DictType); ok {
answer = dict.Equals(*dict2)
}
return
}
func (dict *DictType) Equals(dict2 DictType) (answer bool) {
if dict2 != nil && len(*dict) == len(dict2) {
answer = true
+7
View File
@@ -98,6 +98,13 @@ func (ls *LinkedList) TypeName() string {
// return
// }
func (ls1 *LinkedList) EqualTo(other kern.Equaler) (answer bool) {
if ls2, ok := other.(*LinkedList); ok {
answer = ls1.Equals(ls2)
}
return
}
func (ls1 *LinkedList) Equals(ls2 *LinkedList) (answer bool) {
if ls2 != nil && ls1.Len() == ls2.Len() {
answer = true