Skip to content

Commit

Permalink
Add ==(::NullableArray, ::NullableArray)
Browse files Browse the repository at this point in the history
  • Loading branch information
nalimilan committed Oct 12, 2015
1 parent 6cf63d0 commit 9b9e155
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,30 @@ Returns the last entry of `X`.
""" ->
Base.endof(X::NullableArray) = endof(X.values) # -> Int

@doc """
`==(A::NullableArray, B::NullableArray)`
When none of the arrays contains a missing values, returns `Nullable(true)`
if all elements of the two arrays are equal according to `==`, and
`Nullable(false)` otherwise. Returns `Nullable{Bool}()` if one of the arrays
contains a missing value.
""" ->
function Base.(:(==))(A::NullableArray, B::NullableArray)
if size(A) != size(B)
return false
end
# Short-circuit is only possible after finding a missing element
ret = true
for i in eachindex(A,B)
if A.isnull[i] || B.isnull[i]
return Nullable{Bool}()
elseif A.values[i] != B.values[i]
ret = false
end
end
return Nullable(ret)
end

@doc """
""" ->
Expand Down
11 changes: 11 additions & 0 deletions test/primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ module TestPrimitives
@test endof(NullableArray(collect(1:10))) == 10
@test endof(NullableArray([1, 2, nothing, 4, nothing])) == 5

# ----- test Base.== ------------------------------------------------------#
x = NullableArray(collect(1:3))
@test get(x == NullableArray([1.0, 2.0, 3.0]))
@test get(x != NullableArray([1.1, 2.0, 3.0]))
y = NullableArray([1.0, 2.0, 3.0], [false, false, true])
z = NullableArray([1.1, 2.0, 3.0], [false, true, false])
@test isnull(x == y)
@test isnull(x != y)
@test isnull(x == z)
@test isnull(x != z)

# ----- test Base.find -------------------------------------------------------#

z = NullableArray(rand(Bool, 10))
Expand Down

0 comments on commit 9b9e155

Please sign in to comment.