The name of 'list' has been changed to 'array'; from now on, 'list' will refer to the linked list.

This commit is contained in:
2026-07-12 07:35:28 +02:00
parent b6da9bcad4
commit 77f36642b4
37 changed files with 180 additions and 196 deletions
+50 -46
View File
@@ -11,53 +11,53 @@ import (
"git.portale-stac.it/go-pkg/expr/kern"
)
type ListType []any
type ArrayType []any
func IsList(v any) (ok bool) {
_, ok = v.(*ListType)
_, ok = v.(*ArrayType)
return ok
}
func NewListA(listAny ...any) (list *ListType) {
func NewArrayA(listAny ...any) (list *ArrayType) {
if listAny == nil {
listAny = []any{}
}
return NewList(listAny)
return NewArray(listAny)
}
func NewList(listAny []any) (list *ListType) {
if listAny != nil {
ls := make(ListType, len(listAny))
copy(ls, listAny)
list = &ls
func NewArray(arrayAny []any) (aRef *ArrayType) {
if arrayAny != nil {
a := make(ArrayType, len(arrayAny))
copy(a, arrayAny)
aRef = &a
}
return
}
func MakeList(length, capacity int) (list *ListType) {
func MakeArray(length, capacity int) (list *ArrayType) {
if capacity < length {
capacity = length
}
ls := make(ListType, length, capacity)
ls := make(ArrayType, length, capacity)
list = &ls
return
}
func ListFromStrings(stringList []string) (list *ListType) {
list = MakeList(len(stringList), 0)
for i, s := range stringList {
(*list)[i] = s
func ArrayFromStrings(stringSlice []string) (aRef *ArrayType) {
aRef = MakeArray(len(stringSlice), 0)
for i, s := range stringSlice {
(*aRef)[i] = s
}
return
}
func (ls *ListType) ToString(opt kern.FmtOpt) (s string) {
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(*ls) > 0 {
if len(*aRef) > 0 {
innerOpt := kern.MakeFormatOptions(flags, indent+1)
nest := strings.Repeat(" ", indent+1)
@@ -65,7 +65,7 @@ func (ls *ListType) ToString(opt kern.FmtOpt) (s string) {
sb.WriteByte('\n')
sb.WriteString(nest)
}
for i, item := range []any(*ls) {
for i, item := range []any(*aRef) {
if i > 0 {
if flags&kern.MultiLine != 0 {
sb.WriteString(",\n")
@@ -89,19 +89,19 @@ func (ls *ListType) ToString(opt kern.FmtOpt) (s string) {
return
}
func (ls *ListType) String() string {
return ls.ToString(0)
func (aRef *ArrayType) String() string {
return aRef.ToString(0)
}
func (ls *ListType) TypeName() string {
func (aRef *ArrayType) TypeName() string {
return kern.TypeArray
}
func (ls *ListType) Contains(t *ListType) (answer bool) {
if len(*ls) >= len(*t) {
func (aRef *ArrayType) Contains(t *ArrayType) (answer bool) {
if len(*aRef) >= len(*t) {
answer = true
for _, item := range *t {
if answer = ls.IndexDeepSameCmp(item) >= 0; !answer {
if answer = aRef.IndexDeepSameCmp(item) >= 0; !answer {
break
}
}
@@ -109,17 +109,17 @@ func (ls *ListType) Contains(t *ListType) (answer bool) {
return
}
func (ls *ListType) EqualTo(other kern.Equaler) (equal bool) {
if otherList, ok := other.(*ListType); ok {
equal = ls.Equals(*otherList)
func (aRef *ArrayType) EqualTo(other kern.Equaler) (equal bool) {
if otherList, ok := other.(*ArrayType); ok {
equal = aRef.Equals(*otherList)
}
return
}
func (ls *ListType) Equals(ls2 ListType) (answer bool) {
if ls2 != nil && len(*ls) == len(ls2) {
func (aRef *ArrayType) Equals(ls2 ArrayType) (answer bool) {
if ls2 != nil && len(*aRef) == len(ls2) {
answer = true
for index, i1 := range *ls {
for index, i1 := range *aRef {
// if !reflect.DeepEqual(i1, ls2[index]) {
// answer = false
// break
@@ -133,18 +133,18 @@ func (ls *ListType) Equals(ls2 ListType) (answer bool) {
return
}
func (ls1 *ListType) Clone() (ls2 *ListType) {
ls := make(ListType, len(*ls1))
for i, item := range *ls1 {
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 (ls *ListType) IndexDeepSameCmp(target any) (index int) {
func (aRef *ArrayType) IndexDeepSameCmp(target any) (index int) {
index = -1
for i, item := range *ls {
for i, item := range *aRef {
if kern.Equal(item, target) {
index = i
break
@@ -169,12 +169,16 @@ func (ls *ListType) IndexDeepSameCmp(target any) (index int) {
// }
func SameContent(a, b any) (same bool, err error) {
la, _ := a.(*ListType)
lb, _ := b.(*ListType)
if len(*la) == len(*lb) {
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 *la {
if pos := lb.IndexDeepSameCmp(item); pos < 0 {
for _, item := range *aRef {
if pos := bRef.IndexDeepSameCmp(item); pos < 0 {
same = false
break
}
@@ -208,15 +212,15 @@ func SameContent(a, b any) (same bool, err error) {
// return
// }
func (ls *ListType) SetItem(index int64, value any) (err error) {
if index >= 0 && index < int64(len(*ls)) {
(*ls)[index] = value
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(*ls)-1)
err = fmt.Errorf("index %d out of bounds (0, %d)", index, len(*aRef)-1)
}
return
}
func (ls *ListType) AppendItem(value any) {
*ls = append(*ls, value)
func (aRef *ArrayType) AppendItem(value any) {
*aRef = append(*aRef, value)
}
+2 -2
View File
@@ -24,8 +24,8 @@ func Equal(value1, value2 any) (equal bool) {
} else if boolean.IsBool(value1) && boolean.IsBool(value2) {
equal = value1.(bool) == value2.(bool)
} else if array.IsList(value1) && array.IsList(value2) {
ls1 := value1.(*array.ListType)
ls2 := value2.(*array.ListType)
ls1 := value1.(*array.ArrayType)
ls2 := value2.(*array.ArrayType)
equal = ls1.Equals(*ls2)
} else if dict.IsDict(value1) && dict.IsDict(value2) {
d1 := value1.(*dict.DictType)