-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathuniform.go
More file actions
142 lines (135 loc) · 3.35 KB
/
Copy pathuniform.go
File metadata and controls
142 lines (135 loc) · 3.35 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package apl
import (
"fmt"
"reflect"
)
// Unify tries to convert the array to a uniform array, if possible.
// If uptype is true, it uptypes numeric values, if that helps.
func (a *Apl) Unify(A Array, uptype bool) (resultarray Array, resultok bool) {
if _, ok := A.(EmptyArray); ok {
return A, false // An empty array is defined to be not uniform.
}
if _, ok := A.(Uniform); ok {
return A, true
}
noNumber := -10
boolType := reflect.TypeOf(Bool(false))
indexType := reflect.TypeOf(Int(0))
class := func(t reflect.Type) int {
n, ok := a.Tower.Numbers[t]
if ok == false {
if t == indexType {
return -1
} else if t == boolType {
return -2
}
return noNumber
}
return n.Class
}
// If all values of the array are the same type, it is uniform.
// This includes a List who's primary values are lists as well.
size := A.Size()
if size < 1 {
return A, true
}
v0 := A.At(0)
t0 := reflect.TypeOf(v0)
max := class(t0)
var maxnumber Number
if max != noNumber {
maxnumber = v0.(Number)
}
sametype := true
for i := 1; i < size; i++ {
v := A.At(i)
t := reflect.TypeOf(v)
if t != t0 {
sametype = false
if uptype == false {
return A, false
}
}
if max != noNumber {
if c := class(t); c == noNumber {
max = noNumber
} else if c > max {
max = c
maxnumber = v.(Number)
}
}
}
// All values have the same type.
// Try to convert them to a compact uniform type.
if sametype {
// Some uniform types are defined in the numeric implementation.
// E.g. numbers/{FloatArray;ComplexArray;TimeArray}.
if max != noNumber {
var values []Value
switch v := A.(type) {
case MixedArray:
values = v.Values
case List:
values = []Value(v)
default:
return A, true
}
if u, ok := a.Tower.Uniform(values); ok == false {
return A, true
} else if rs, ok := u.(Reshaper); ok {
return rs.Reshape(CopyShape(A)).(Array), true
}
}
// Some uniform types are defined in array.go.
if t0 == reflect.TypeOf(String("")) {
ar := StringArray{}.Make(CopyShape(A))
for i := 0; i < A.Size(); i++ {
v := A.At(i)
ar.(StringArray).Strings[i] = string(v.(String))
}
return ar, true
} else if t0 == reflect.TypeOf(Bool(false)) {
ar := BoolArray{}.Make(CopyShape(A))
for i := 0; i < A.Size(); i++ {
v := A.At(i)
ar.(BoolArray).Bools[i] = bool(v.(Bool))
}
return ar, true
} else if t0 == reflect.TypeOf(Int(0)) {
ar := IntArray{}.Make(CopyShape(A))
for i := 0; i < A.Size(); i++ {
v := A.At(i)
ar.(IntArray).Ints[i] = int(v.(Int))
}
return ar, true
}
// Unknown uniform type is returnd as it is.
return A, true
} else if max == noNumber {
// If values are not of the same type, and not identified by
// the current tower, there is no chance to make them equal.
return A, false
}
// Try to uptype all values to the same number type.
values := make([]Value, size)
var err error
for i := 0; i < size; i++ {
v := A.At(i)
values[i], _, err = a.Tower.SameType(v.(Number), maxnumber)
if err != nil {
fmt.Println(err)
return A, false
}
}
if u, ok := a.Tower.Uniform(values); ok {
if rs, ok := u.(Reshaper); ok {
return rs.Reshape(CopyShape(A)).(Array), true
}
}
return A, false
}
// UnifyArray tries to unify the input array without uptyping.
func (a *Apl) UnifyArray(A Array) Array {
u, _ := a.Unify(A, false)
return u
}