/
util.jl
161 lines (127 loc) · 3.67 KB
/
util.jl
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
import Base: convert, +, *, /, -, isless
###### Filesize algebra ######
struct Bytes
val::Float64
end
(*)(n::Number, b::Bytes) = Bytes(n*b.val)
(+)(a::Bytes, b::Bytes) = Bytes(a.val+b.val)
(-)(a::Bytes, b::Bytes) = Bytes(a.val-b.val)
isless(a::Bytes, b::Bytes) = a.val < b.val
(/)(a::Bytes, b::Number) = Bytes(ceil(UInt64, a.val/b))
(/)(a::Bytes, b::Bytes) = a.val/b.val
const B = Bytes(1)
const kB = 1024B
const MB = 1024kB
const GB = 1024MB
const TB = 1024GB
convert(::Type{Bytes}, n::Integer) = Bytes(n)
##### Base patches ######
_intersect(x,y) = intersect(x,y)
_intersect(::Colon, ::Colon) = Colon()
_intersect(::Colon, r) = r
_intersect(r, ::Colon) = r
##### Utility functions ######
"""
Utility function to divide the range `range` into `n` chunks
"""
function split_range(range::AbstractRange{T}, n) where T
len = length(range)
starts = len >= n ?
round.(T, Base.range(first(range), stop=last(range)+1, length=n+1)) :
vcat(collect(first(range):(last(range)+1)), zeros(T, n-len))
map((x,y)->x:y, starts[1:end-1], starts[2:end] .- 1)
end
function split_range(r::AbstractRange{Char}, n)
map((x) -> Char(first(x)):Char(last(x)), split_range(Int(first(r)):Int(last(r)), n))
end
"""
split_range_interval(range, n)
split a range into pieces each of length `n` or lesser
"""
function split_range_interval(range, n)
f = first(range)
N = length(range)
npieces = ceil(Int, N / n)
ranges = Array{UnitRange}(npieces)
for i=1:npieces
ranges[i] = f:min(N, f+n-1)
f += n
end
ranges
end
###### interface macro ######
getvarname(x::Expr) = x.args[1]
getvarname(x::Symbol) = x
"""
@unimplemented fname(<args...>)
While it is nice to define generic function ad-hoc, it can sometimes
get confusing to figure out which method is missing.
`@interface` creates a function which errors out pointing
which method is missing.
"""
macro unimplemented(expr)
@assert expr.head == :call
fname = expr.args[1]
args = expr.args[2:end]
sig = string(expr)
vars = map(getvarname, args)
typs = Expr(:vect, map(x -> :(typeof($x)), vars)...,)
:(function $(esc(fname))($(args...,))
error(string("a method of ", $sig, " specialized to ", ($typs...,), " is missing"))
end)
end
"""
Tree reduce
"""
function treereduce(f, xs)
length(xs) == 1 && return xs[1]
l = length(xs)
m = div(l, 2)
f(treereduce(f, xs[1:m]), treereduce(f, xs[m+1:end]))
end
if !isdefined(Base, :reduced_indices)
function reduced_dims(x, dim)
Base.reduced_dims(size(x), dim)
end
else
reduced_dims(x, dim) = Base.reduced_indices(axes(x), dim)
end
function treereducedim(op, xs::Array, dim::Int)
l = size(xs, dim)
colons = Any[Colon() for i=1:length(size(xs))]
if dim > length(size(xs))
return xs
end
ys = treereduce((x,y)->map(op, x,y), Any[(
colons[dim] = [i];
view(xs, colons...)
) for i=1:l])
reshape(ys[:], reduced_dims(xs, dim))
end
function treereducedim(op, xs::Array, dim::Tuple)
reduce((prev, d) -> treereducedim(op, prev, d), dim, init=xs)
end
function allslices(xs, n)
idx = Any[Colon() for i in 1:ndims(xs)]
[(idx[n] = j; view(xs, idx...)) for j in 1:size(xs, n)]
end
function treereduce_nd(fs, xs)
n = ndims(xs)
if n==1
treereduce(fs[n], xs)
else
treereduce(fs[n], map(ys->treereduce_nd(fs, ys), allslices(xs, n)))
end
end
function setindex(x::NTuple{N}, idx, v) where N
map(ifelse, ntuple(x->idx === x, Val(N)), ntuple(x->v, Val(N)), x)
end
function showloc(f, argcount)
args = ntuple(x->Any, argcount)
ms = methods(f)
if length(ms) == 1
string(first(ms))
else
string(f)
end
end