forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.j
294 lines (245 loc) · 7.48 KB
/
array.j
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
## array.j: Base Array functionality
## Basic functions ##
size(a::Array) = arraysize(a)
size(a::Array, d) = arraysize(a, d)
numel(a::Array) = arraylen(a)
## Constructors ##
jl_comprehension_zeros{T,n}(oneresult::Tensor{T,n}, dims...) = Array(T, dims...)
jl_comprehension_zeros{T}(oneresult::T, dims...) = Array(T, dims...)
jl_comprehension_zeros(oneresult::(), dims...) = Array(None, dims...)
clone(a::Array, T::Type, dims::Dims) = Array(T, dims)
clone{T}(a::Array{T,1}) = Array(T, size(a,1))
clone{T}(a::Array{T,2}) = Array(T, size(a,1), size(a,2))
clone{T}(a::Array{T,1}, S::Type) = Array(S, size(a,1))
clone{T}(a::Array{T,2}, S::Type) = Array(S, size(a,1), size(a,2))
macro matrix_builder(t, f)
quote
function ($f)(dims::Dims)
A = Array($t, dims)
for i = 1:numel(A)
A[i] = ($f)()
end
return A
end
($f)(dims::Size...) = ($f)(dims)
end # quote
end # macro
@matrix_builder Float64 rand
@matrix_builder Float32 randf
@matrix_builder Float64 randn
@matrix_builder Uint32 randui32
zeros{T}(::Type{T}, dims::Dims) = fill(Array(T, dims), zero(T))
zeros(T::Type, dims::Size...) = zeros(T, dims)
zeros(dims::Dims) = zeros(Float64, dims)
zeros(dims::Size...) = zeros(dims)
ones{T}(::Type{T}, dims::Dims) = fill(Array(T, dims), one(T))
ones(T::Type, dims::Size...) = ones(T, dims)
ones(dims::Dims) = ones(Float64, dims)
ones(dims::Size...) = ones(dims)
trues(dims::Dims) = fill(Array(Bool, dims), true)
trues(dims::Size...) = trues(dims)
falses(dims::Dims) = fill(Array(Bool, dims), false)
falses(dims::Size...) = falses(dims)
## Conversions ##
convert{T,n}(::Type{Array{T,n}}, x::Array{T,n}) = x
convert{T,n,S}(::Type{Array{T,n}}, x::Array{S,n}) = copy_to(clone(x,T), x)
int8 {T,n}(x::Array{T,n}) = convert(Array{Int8 ,n}, x)
uint8 {T,n}(x::Array{T,n}) = convert(Array{Uint8 ,n}, x)
int16 {T,n}(x::Array{T,n}) = convert(Array{Int16 ,n}, x)
uint16 {T,n}(x::Array{T,n}) = convert(Array{Uint16 ,n}, x)
int32 {T,n}(x::Array{T,n}) = convert(Array{Int32 ,n}, x)
uint32 {T,n}(x::Array{T,n}) = convert(Array{Uint32 ,n}, x)
int64 {T,n}(x::Array{T,n}) = convert(Array{Int64 ,n}, x)
uint64 {T,n}(x::Array{T,n}) = convert(Array{Uint64 ,n}, x)
bool {T,n}(x::Array{T,n}) = convert(Array{Bool ,n}, x)
char {T,n}(x::Array{T,n}) = convert(Array{Char ,n}, x)
float32{T,n}(x::Array{T,n}) = convert(Array{Float32,n}, x)
float64{T,n}(x::Array{T,n}) = convert(Array{Float64,n}, x)
## Indexing: ref ##
ref(a::Array, i::Index) = arrayref(a,i)
ref{T}(a::Array{T,1}, i::Index) = arrayref(a,i)
ref(a::Array{Any,1}, i::Index) = arrayref(a,i)
ref{T}(a::Array{T,2}, i::Index, j::Index) = arrayref(a, (j-1)*arraysize(a,1)+i)
## Indexing: assign ##
assign(A::Array{Any}, x::Tensor, i::Index) = arrayset(A,i,x)
assign(A::Array{Any}, x, i::Index) = arrayset(A,i,x)
assign{T}(A::Array{T}, x::Tensor, i::Index) = arrayset(A,i,convert(T, x))
assign{T}(A::Array{T}, x, i::Index) = arrayset(A,i,convert(T, x))
## Dequeue functionality ##
function push{T}(a::Array{T,1}, item)
ccall(:jl_array_grow_end, Void, (Any, Ulong), a, ulong(1))
a[end] = item
return a
end
function grow{T}(a::Array{T,1}, n::Int)
ccall(:jl_array_grow_end, Void, (Any, Ulong), a, ulong(n))
return a
end
function pop{T}(a::Array{T,1})
if isempty(a)
error("pop: array is empty")
end
item = a[end]
ccall(:jl_array_del_end, Void, (Any, Ulong), a, ulong(1))
return item
end
function enq{T}(a::Array{T,1}, item)
ccall(:jl_array_grow_beg, Void, (Any, Ulong), a, ulong(1))
a[1] = item
return a
end
function insert{T}(a::Array{T,1}, i::Int, item)
if i < 1
throw(BoundsError())
end
l = length(a)
if i > l
ccall(:jl_array_grow_end, Void, (Any, Ulong), a, ulong(i-l))
elseif i > div(l,2)
ccall(:jl_array_grow_end, Void, (Any, Ulong), a, ulong(1))
for k=l+1:-1:i+1
a[k] = a[k-1]
end
else
ccall(:jl_array_grow_beg, Void, (Any, Ulong), a, ulong(1))
for k=1:(i-1)
a[k] = a[k+1]
end
end
a[i] = item
end
function del{T}(a::Array{T,1}, i::Int)
l = length(a)
if !(1 <= i <= l)
throw(BoundsError())
end
if i > div(l,2)
for k=i:l-1
a[k] = a[k+1]
end
ccall(:jl_array_del_end, Void, (Any, Ulong), a, ulong(1))
else
for k=i:-1:2
a[k] = a[k-1]
end
ccall(:jl_array_del_beg, Void, (Any, Ulong), a, ulong(1))
end
a
end
function del_all{T}(a::Array{T,1})
ccall(:jl_array_del_end, Void, (Any, Ulong), a, ulong(length(a)))
a
end
## Concatenation ##
cat(catdim::Int) = Array(None,0)
vcat() = Array(None,0)
hcat() = Array(None,0)
## cat: special cases
hcat{T}(X::T...) = [ X[j] | i=1, j=1:length(X) ]
vcat{T}(X::T...) = [ X[i] | i=1:length(X) ]
hcat{T}(V::Array{T,1}...) = [ V[j][i] | i=1:length(V[1]), j=1:length(V) ]
function vcat{T}(V::Array{T,1}...)
a = clone(V[1], sum(map(length, V)))
pos = 1
for k=1:length(V)
Vk = V[k]
for i=1:length(Vk)
a[pos] = Vk[i]
pos += 1
end
end
a
end
function hcat{T}(A::Array{T,2}...)
nargs = length(A)
ncols = sum(a->size(a, 2), A)
nrows = size(A[1], 1)
B = clone(A[1], nrows, ncols)
pos = 1
for k=1:nargs
Ak = A[k]
for i=1:numel(Ak)
B[pos] = Ak[i]
pos += 1
end
end
return B
end
function vcat{T}(A::Array{T,2}...)
nargs = length(A)
nrows = sum(a->size(a, 1), A)
ncols = size(A[1], 2)
B = clone(A[1], nrows, ncols)
pos = 1
for j=1:ncols, k=1:nargs
Ak = A[k]
for i=1:size(Ak, 1)
B[pos] = Ak[i,j]
pos += 1
end
end
return B
end
## cat: general case
function cat(catdim::Int, X...)
typeC = promote_type(map(typeof, X)...)
nargs = length(X)
if catdim == 1
dimsC = nargs
elseif catdim == 2
dimsC = (1, nargs)
end
C = Array(typeC, dimsC)
for i=1:nargs
C[i] = X[i]
end
return C
end
vcat(X...) = cat(1, X...)
hcat(X...) = cat(2, X...)
function cat(catdim::Int, A::Array...)
# ndims of all input arrays should be in [d-1, d]
nargs = length(A)
dimsA = map(size, A)
ndimsA = map(ndims, A)
d_max = max(ndimsA)
d_min = min(ndimsA)
cat_ranges = ntuple(nargs, i->(catdim <= ndimsA[i] ? dimsA[i][catdim] : 1))
function compute_dims(d)
if d == catdim
if catdim <= d_max
return sum(cat_ranges)
else
return nargs
end
else
if d <= d_max
return dimsA[1][d]
else
return 1
end
end
end
ndimsC = max(catdim, d_max)
dimsC = ntuple(ndimsC, compute_dims)
typeC = promote_type(ntuple(nargs, i->typeof(A[i]).parameters[1])...)
C = Array(typeC, dimsC)
cat_ranges = cumsum(1, cat_ranges...)
for k=1:nargs
cat_one = ntuple(ndimsC, i->(i != catdim ?
Range1(1,dimsC[i]) :
Range1(cat_ranges[k],cat_ranges[k+1]-1) ))
C[cat_one...] = A[k]
end
return C
end
vcat(A::Array...) = cat(1, A...)
hcat(A::Array...) = cat(2, A...)
function reinterpret{T,S}(::Type{T}, a::Array{S})
b = Array(T, div(numel(a)*sizeof(S),sizeof(T)))
ccall(dlsym(libc, :memcpy),
Ptr{T}, (Ptr{T}, Ptr{S}, Ulong),
b, a, ulong(length(b)*sizeof(T)))
b
end
reinterpret(t,x) = reinterpret(t,[x])[1]