This repository has been archived by the owner on Jul 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.go
107 lines (95 loc) · 1.97 KB
/
vector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//-----------------------------------------------------------------------------
// Copyright (c) 2022 Detlef Stern
//
// This file is part of sxpf.
//
// sxpf is licensed under the latest version of the EUPL // (European Union
// Public License). Please see file LICENSE.txt for your rights and obligations
// under this license.
//-----------------------------------------------------------------------------
package sxpf
import "bytes"
// Vector is a sequence of values, including sub-vectors.
type Vector struct {
val []Value
frozen bool
}
// Empty is the defined value for an empty vector.
func Empty() *Vector { return &myNIL }
var myNIL = Vector{val: []Value{}, frozen: true}
// NewVector creates a new vector with the given values.
func NewVector(vals ...Value) *Vector {
if len(vals) == 0 {
return Empty()
}
for _, v := range vals {
if v == nil {
return Empty()
}
}
return &Vector{vals, false}
}
// Append some more value to a vector.
func (v *Vector) Append(lstVal ...Value) {
if v.frozen {
return
}
for _, v := range lstVal {
if v == nil {
return
}
}
v.val = append(v.val, lstVal...)
}
// Extend the vector by another
func (v *Vector) Extend(o *Vector) {
if v.frozen {
return
}
if o != nil {
for _, v := range o.val {
if v == nil {
return
}
}
v.val = append(v.val, o.val...)
}
}
func (v *Vector) GetSlice() []Value {
if v == nil {
return nil
}
return v.val
}
func (v *Vector) Equal(other Value) bool {
if v == nil || other == nil {
return v == other
}
o, ok := other.(*Vector)
if !ok || len(v.val) != len(o.val) {
return false
}
for i, val := range v.val {
if !val.Equal(o.val[i]) {
return false
}
}
return true
}
var (
space = []byte{' '}
lBracket = []byte{'['}
rBracket = []byte{']'}
)
func (v *Vector) String() string {
var buf bytes.Buffer
buf.Write(lBracket)
for i, val := range v.val {
if i > 0 {
buf.Write(space)
}
buf.WriteString(val.String())
}
buf.Write(rBracket)
return buf.String()
}