forked from JuliaData/DataFrames.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boolNA.jl
182 lines (133 loc) · 5.12 KB
/
boolNA.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
## definition ##
bitstype 8 BoolNA <: Integer
convert(::Type{BoolNA}, x::NAtype) = NA_Bool
promote_rule(::Type{BoolNA}, ::Type{NAtype} ) = BoolNA
promote_rule(::Type{Bool}, ::Type{NAtype} ) = BoolNA
## boolean conversions ##
convert(::Type{BoolNA}, x::NAtype) = NA_Bool
convert(::Type{BoolNA}, x::BoolNA) = x
convert(::Type{Bool}, x::BoolNA) = box(Bool,unbox(BoolNA,x))
convert(::Type{BoolNA}, x::Bool) = box(BoolNA,unbox(Bool,x))
convert(::Type{BoolNA}, x::Integer) = (x!=0)
convert(::Type{BoolNA}, x::Float) = (x!=0)
convert(::Type{IntNA8}, x::BoolNA) = box(IntNA8,unbox(BoolNA,x))
convert(::Type{IntNA16}, x::BoolNA) = box(IntNA16,zext16(unbox(BoolNA,x)))
convert(::Type{IntNA32}, x::BoolNA) = box(IntNA32,zext32(unbox(BoolNA,x)))
convert(::Type{IntNA64}, x::BoolNA) = box(IntNA64,zext64(unbox(BoolNA,x)))
convert(::Type{IntNA128}, x::BoolNA) = box(IntNA128,zext_int(IntNA128,unbox(BoolNA,x)))
convert(::Type{Int64}, x::BoolNA) = box(Int64,zext64(unbox(BoolNA,x)))
convert(::Type{Float32}, x::BoolNA) = box(Float32,sitofp32(unbox(BoolNA,x)))
convert(::Type{Float64}, x::BoolNA) = box(Float64,sitofp64(unbox(BoolNA,x)))
convert(::Type{Float}, x::BoolNA) = convert(Float32, x)
# promote Bool to any other numeric type
promote_rule{T<:Number}(::Type{BoolNA}, ::Type{T}) = T
promote_rule(::Type{Bool}, ::Type{BoolNA} ) = BoolNA
## boolNA definition ##
boolNA(x) = convert(BoolNA, x)
boolNA(x::BoolNA) = x
boolNA(x::AbstractArray) = copy_to(similar(x,BoolNA), x)
boolNA(x::AbstractArray{Bool}) = reinterpret(BoolNA, x, size(x))
## NA definition / handling ##
NA_Bool = boolNA(-128)
NA_Bool = box(BoolNA, unbox(Int8,int8(-128)))
na(::Type{Bool}) = NA_Bool
na(::Type{BoolNA}) = NA_Bool
na(::Bool) = NA_Bool
na(::BoolNA) = NA_Bool
isna(x::BoolNA) = eq_int(unbox(BoolNA, x), unbox(BoolNA, NA_Bool))
basetype(::Type{BoolNA}) = Bool
natype(::Type{Bool}) = BoolNA
natype(::Type{BoolNA}) = BoolNA
function basetype{T<:BoolNA,N}(x::Array{T,N})
res = Array(basetype(T), size(x))
for i in 1:length(x)
res[i] = basetype(x[i])
end
res
end
natype{T<:BoolNA,N}(x::Array{T,N}) = x
function natype{T<:Bool,N}(x::Array{T,N})
res = Array(natype(T), size(x))
for i in 1:length(x)
res[i] = natype(x[i])
end
res
end
sizeof(::Type{BoolNA}) = 1
typemin(::Type{BoolNA}) = false
typemax(::Type{BoolNA}) = true
## boolean operations ##
!(x::BoolNA) = eq_int(unbox(BoolNA,x),trunc8(0))
isequal(x::Bool, y::Bool) = eq_int(unbox(BoolNA,x),unbox(BoolNA,y))
(~)(x::BoolNA) = isna(x) ? NA_Bool : !x
(&)(x::BoolNA, y::BoolNA) = isna(x) || isna(y) ? NA_Bool : eq_int(and_int(unbox(BoolNA,x),unbox(BoolNA,y)),trunc8(1))
(|)(x::BoolNA, y::BoolNA) = isna(x) || isna(y) ? NA_Bool : eq_int( or_int(unbox(BoolNA,x),unbox(BoolNA,y)),trunc8(1))
($)(x::BoolNA, y::BoolNA) = isna(x) || isna(y) ? NA_Bool : (x!=y)
any(x::BoolNA) = isna(x) ? NA_Bool : x
all(x::BoolNA) = isna(x) ? NA_Bool : x
any(x::BoolNA, y::BoolNA) = x | y
all(x::BoolNA, y::BoolNA) = x & y
## do arithmetic as Int ##
signbit(x::BoolNA) = 0
sign(x::BoolNA) = intNA(x)
abs(x::BoolNA) = intNA(x)
<(x::BoolNA, y::BoolNA) = y&!x
==(x::BoolNA, y::BoolNA) = eq_int(unbox(BoolNA,x),unbox(BoolNA,y))
-(x::BoolNA) = -int(x)
+(x::BoolNA, y::BoolNA) = intNA(x)+intNA(y)
-(x::BoolNA, y::BoolNA) = intNA(x)-intNA(y)
*(x::BoolNA, y::BoolNA) = intNA(x)*intNA(y)
/(x::BoolNA, y::BoolNA) = intNA(x)/intNA(y)
^(x::BoolNA, y::BoolNA) = intNA(x)^intNA(y)
div(x::BoolNA, y::BoolNA) = div(intNA(x),intNA(y))
fld(x::BoolNA, y::BoolNA) = fld(intNA(x),intNA(y))
rem(x::BoolNA, y::BoolNA) = rem(intNA(x),intNA(y))
mod(x::BoolNA, y::BoolNA) = mod(intNA(x),intNA(y))
show(io, b::BoolNA) = print(io, isna(b) ? "NA" : b == true ? "true" : "false")
## isbool(::BoolNA) = true
##
## Array stuff
##
indices(I::AbstractVector{BoolNA}) = find(I)
function check_bounds(sz::Int, I::AbstractVector{BoolNA})
if length(I) > sz
throw(BoundsError())
end
end
function check_bounds(A::Array, I::Array{BoolNA})
if !isequal(size(A), size(I))
throw(BoundsError())
end
end
# logical indexing
ref(A::Array, I::AbstractVector{BoolNA}) = A[findNA(I)]
## TODO more combinations here
ref(A::Matrix, I::AbstractVector{BoolNA}, J::AbstractVector{Bool}) = A[findNA(I),find(J)]
ref(A::Matrix, I::AbstractVector{Bool}, J::AbstractVector{BoolNA}) = A[find(I),findNA(J)]
ref(A::Matrix, I::Integer, J::AbstractVector{BoolNA}) = A[I,findNA(J)]
ref(A::Matrix, I::AbstractVector{BoolNA}, J::Integer) = A[findNA(I),J]
ref(A::Matrix, I::AbstractVector{BoolNA}, J::AbstractVector{BoolNA}) = A[findNA(I),findNA(J)]
ref{T<:Integer}(A::Matrix, I::AbstractVector{T}, J::AbstractVector{BoolNA}) = A[I,findNA(J)]
ref{T<:Integer}(A::Matrix, I::AbstractVector{BoolNA}, J::AbstractVector{T}) = A[findNA(I),J]
function nnzorNA(a::StridedArray)
n = 0
for i = 1:numel(a)
n += isna(a[i]) ? 1 : bool(a[i]) ? 1 : 0
end
return n
end
function findNA(A::StridedArray)
nnzA = nnzorNA(A)
I = Array(IntNA, nnzA)
count = 1
for i=1:length(A)
if isna(A[i])
I[count] = NA_Int
count += 1
elseif A[i] != 0
I[count] = i
count += 1
end
end
return I
end