new type LinkedList, preliminary implementation
This commit is contained in:
@@ -21,6 +21,8 @@ func Clone(v any) (c any) {
|
||||
c = unboxed.Clone()
|
||||
case *DictType:
|
||||
c = unboxed.Clone()
|
||||
case *LinkedList:
|
||||
c = unboxed.Clone()
|
||||
default:
|
||||
c = v
|
||||
}
|
||||
|
||||
@@ -20,4 +20,5 @@ const (
|
||||
TypeDict = "dict"
|
||||
TypeListOf = "list-of-"
|
||||
TypeListOfStrings = "list-of-strings"
|
||||
TypeLinkedList = "linked-list"
|
||||
)
|
||||
|
||||
@@ -20,6 +20,10 @@ func Equal(value1, value2 any) (equal bool) {
|
||||
d1 := value1.(*DictType)
|
||||
d2 := value2.(*DictType)
|
||||
equal = d1.Equals(*d2)
|
||||
} else if IsLinkedList(value1) && IsLinkedList(value2) {
|
||||
ll1 := value1.(*LinkedList)
|
||||
ll2 := value2.(*LinkedList)
|
||||
equal = ll1.Equals(ll2)
|
||||
} else if IsInteger(value1) && IsInteger(value2) {
|
||||
equal = value1.(int64) == value2.(int64)
|
||||
} else if IsString(value1) && IsString(value2) {
|
||||
|
||||
+16
-1
@@ -4,7 +4,10 @@
|
||||
// formatter.go
|
||||
package kern
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type FmtOpt uint32 // lower 16 bits hold a bit-mask, higher 16 bits hold an indentation number
|
||||
|
||||
@@ -46,6 +49,18 @@ type Formatter interface {
|
||||
ToString(options FmtOpt) string
|
||||
}
|
||||
|
||||
func Format(sb *strings.Builder, item any, opt FmtOpt) {
|
||||
if s, ok := item.(string); ok {
|
||||
sb.WriteByte('"')
|
||||
sb.WriteString(s)
|
||||
sb.WriteByte('"')
|
||||
} else if formatter, ok := item.(Formatter); ok {
|
||||
sb.WriteString(formatter.ToString(opt))
|
||||
} else {
|
||||
fmt.Fprintf(sb, "%v", item)
|
||||
}
|
||||
}
|
||||
|
||||
func GetFormatted(v any, opt FmtOpt) (text string) {
|
||||
if v == nil {
|
||||
text = "(nil)"
|
||||
|
||||
+15
-23
@@ -73,15 +73,7 @@ func (ls *ListType) ToString(opt FmtOpt) (s string) {
|
||||
sb.WriteString(", ")
|
||||
}
|
||||
}
|
||||
if s, ok := item.(string); ok {
|
||||
sb.WriteByte('"')
|
||||
sb.WriteString(s)
|
||||
sb.WriteByte('"')
|
||||
} else if formatter, ok := item.(Formatter); ok {
|
||||
sb.WriteString(formatter.ToString(innerOpt))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf("%v", item))
|
||||
}
|
||||
Format(&sb, item, innerOpt)
|
||||
}
|
||||
if flags&MultiLine != 0 {
|
||||
sb.WriteByte('\n')
|
||||
@@ -104,11 +96,11 @@ func (ls *ListType) TypeName() string {
|
||||
return "list"
|
||||
}
|
||||
|
||||
func (dict *ListType) Contains(t *ListType) (answer bool) {
|
||||
if len(*dict) >= len(*t) {
|
||||
func (ls *ListType) Contains(t *ListType) (answer bool) {
|
||||
if len(*ls) >= len(*t) {
|
||||
answer = true
|
||||
for _, item := range *t {
|
||||
if answer = dict.IndexDeepSameCmp(item) >= 0; !answer {
|
||||
if answer = ls.IndexDeepSameCmp(item) >= 0; !answer {
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -116,10 +108,10 @@ func (dict *ListType) Contains(t *ListType) (answer bool) {
|
||||
return
|
||||
}
|
||||
|
||||
func (ls1 *ListType) Equals(ls2 ListType) (answer bool) {
|
||||
if ls2 != nil && len(*ls1) == len(ls2) {
|
||||
func (ls *ListType) Equals(ls2 ListType) (answer bool) {
|
||||
if ls2 != nil && len(*ls) == len(ls2) {
|
||||
answer = true
|
||||
for index, i1 := range *ls1 {
|
||||
for index, i1 := range *ls {
|
||||
// if !reflect.DeepEqual(i1, ls2[index]) {
|
||||
// answer = false
|
||||
// break
|
||||
@@ -142,11 +134,11 @@ func (ls1 *ListType) Clone() (ls2 *ListType) {
|
||||
return
|
||||
}
|
||||
|
||||
func (dict *ListType) IndexDeepSameCmp(target any) (index int) {
|
||||
func (ls *ListType) IndexDeepSameCmp(target any) (index int) {
|
||||
var eq bool
|
||||
var err error
|
||||
index = -1
|
||||
for i, item := range *dict {
|
||||
for i, item := range *ls {
|
||||
if eq, err = deepSame(item, target, SameContent); err != nil {
|
||||
break
|
||||
} else if eq {
|
||||
@@ -197,15 +189,15 @@ func deepSame(a, b any, deepCmp DeepFuncTemplate) (eq bool, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (dict *ListType) SetItem(index int64, value any) (err error) {
|
||||
if index >= 0 && index < int64(len(*dict)) {
|
||||
(*dict)[index] = value
|
||||
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(*dict)-1)
|
||||
err = fmt.Errorf("index %d out of bounds (0, %d)", index, len(*ls)-1)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (dict *ListType) AppendItem(value any) {
|
||||
*dict = append(*dict, value)
|
||||
func (ls *ListType) AppendItem(value any) {
|
||||
*ls = append(*ls, value)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user