Skip to content

Commit

Permalink
Change isnull field of Nullable into hasvalue
Browse files Browse the repository at this point in the history
This is consistent with most other languages and formats.
  • Loading branch information
nalimilan committed Sep 14, 2016
1 parent 155147e commit 8da3c10
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 33 deletions.
6 changes: 3 additions & 3 deletions base/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ gc(full::Bool=true) = ccall(:jl_gc_collect, Void, (Cint,), full)
gc_enable(on::Bool) = ccall(:jl_gc_enable, Cint, (Cint,), on)!=0

immutable Nullable{T}
isnull::Bool
hasvalue::Bool
value::T

Nullable() = new(true)
Nullable(value::T, isnull::Bool=false) = new(isnull, value)
Nullable() = new(false)
Nullable(value::T, isnull::Bool=false) = new(!isnull, value)
end
14 changes: 7 additions & 7 deletions base/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,20 @@ otherwise, returns `y` if provided, or throws a `NullException` if not.
"""
@inline function get{S,T}(x::Nullable{S}, y::T)
if isbits(S)
ifelse(x.isnull, y, x.value)
ifelse(isnull(x), y, x.value)
else
x.isnull ? y : x.value
isnull(x) ? y : x.value
end
end

get(x::Nullable) = x.isnull ? throw(NullException()) : x.value
get(x::Nullable) = isnull(x) ? throw(NullException()) : x.value

isnull(x::Nullable) = x.isnull
isnull(x::Nullable) = !x.hasvalue

function isequal(x::Nullable, y::Nullable)
if x.isnull && y.isnull
if isnull(x) && isnull(y)
return true
elseif x.isnull || y.isnull
elseif isnull(x) || isnull(y)
return false
else
return isequal(x.value, y.value)
Expand All @@ -78,7 +78,7 @@ end
const nullablehash_seed = UInt === UInt64 ? 0x932e0143e51d0171 : 0xe51d0171

function hash(x::Nullable, h::UInt)
if x.isnull
if isnull(x)
return h + nullablehash_seed
else
return hash(x.value, h + nullablehash_seed)
Expand Down
24 changes: 12 additions & 12 deletions src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ JL_DLLEXPORT jl_nullable_float64_t jl_try_substrtod(char *str, size_t offset, si
char *bstr = str+offset;
char *pend = bstr+len;
char *tofree = NULL;
int err = 0;
int hasvalue = 0;

errno = 0;
if (!(*pend == '\0' || isspace((unsigned char)*pend) || *pend == ',')) {
Expand All @@ -842,28 +842,28 @@ JL_DLLEXPORT jl_nullable_float64_t jl_try_substrtod(char *str, size_t offset, si
double out = jl_strtod_c(bstr, &p);

if (errno==ERANGE && (out==0 || out==HUGE_VAL || out==-HUGE_VAL)) {
err = 1;
hasvalue = 0;
}
else if (p == bstr) {
err = 1;
hasvalue = 0;
}
else {
// Deal with case where the substring might be something like "1 ",
// which is OK, and "1 X", which we don't allow.
err = substr_isspace(p, pend) ? 0 : 1;
hasvalue = substr_isspace(p, pend) ? 1 : 0;
}

if (__unlikely(tofree))
free(tofree);

jl_nullable_float64_t ret = {(uint8_t)err, out};
jl_nullable_float64_t ret = {(uint8_t)hasvalue, out};
return ret;
}

JL_DLLEXPORT int jl_substrtod(char *str, size_t offset, size_t len, double *out)
{
jl_nullable_float64_t nd = jl_try_substrtod(str, offset, len);
if (0 == nd.isnull) {
if (0 != nd.hasvalue) {
*out = nd.value;
return 0;
}
Expand All @@ -881,7 +881,7 @@ JL_DLLEXPORT jl_nullable_float32_t jl_try_substrtof(char *str, size_t offset, si
char *bstr = str+offset;
char *pend = bstr+len;
char *tofree = NULL;
int err = 0;
int hasvalue = 0;

errno = 0;
if (!(*pend == '\0' || isspace((unsigned char)*pend) || *pend == ',')) {
Expand All @@ -905,28 +905,28 @@ JL_DLLEXPORT jl_nullable_float32_t jl_try_substrtof(char *str, size_t offset, si
#endif

if (errno==ERANGE && (out==0 || out==HUGE_VALF || out==-HUGE_VALF)) {
err = 1;
hasvalue = 0;
}
else if (p == bstr) {
err = 1;
hasvalue = 0;
}
else {
// Deal with case where the substring might be something like "1 ",
// which is OK, and "1 X", which we don't allow.
err = substr_isspace(p, pend) ? 0 : 1;
hasvalue = substr_isspace(p, pend) ? 1 : 0;
}

if (__unlikely(tofree))
free(tofree);

jl_nullable_float32_t ret = {(uint8_t)err, out};
jl_nullable_float32_t ret = {(uint8_t)hasvalue, out};
return ret;
}

JL_DLLEXPORT int jl_substrtof(char *str, int offset, size_t len, float *out)
{
jl_nullable_float32_t nf = jl_try_substrtof(str, offset, len);
if (0 == nf.isnull) {
if (0 != nf.hasvalue) {
*out = nf.value;
return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -1737,12 +1737,12 @@ JL_DLLEXPORT const char *jl_git_commit(void);

// nullable struct representations
typedef struct {
uint8_t isnull;
uint8_t hasvalue;
double value;
} jl_nullable_float64_t;

typedef struct {
uint8_t isnull;
uint8_t hasvalue;
float value;
} jl_nullable_float32_t;

Expand Down
4 changes: 2 additions & 2 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3118,13 +3118,13 @@ f11858(Any[Type{Foo11858}, Type{Bar11858}, typeof(g11858)])
foo11904(x::Int) = x
@inline function foo11904{S}(x::Nullable{S})
if isbits(S)
Nullable(foo11904(x.value), x.isnull)
Nullable(foo11904(x.value), isnull(x))
else
throw_error()
end
end

@test !foo11904(Nullable(1)).isnull
@test !isnull(foo11904(Nullable(1)))

# issue 11874
immutable Foo11874
Expand Down
14 changes: 7 additions & 7 deletions test/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ types = [
# Nullable{T}() = new(true)
for T in types
x = Nullable{T}()
@test x.isnull === true
@test x.hasvalue === false
@test isa(x.value, T)
@test eltype(Nullable{T}) === T
@test eltype(x) === T
Expand All @@ -28,13 +28,13 @@ end
# Nullable{T}(value::T) = new(false, value)
for T in types
x = Nullable{T}(zero(T))
@test x.isnull === false
@test x.hasvalue === true
@test isa(x.value, T)
@test x.value === zero(T)
@test eltype(x) === T

x = Nullable{T}(one(T))
@test x.isnull === false
@test x.hasvalue === true
@test isa(x.value, T)
@test x.value === one(T)
@test eltype(x) === T
Expand All @@ -43,13 +43,13 @@ end
# Nullable{T}(value::T, isnull::Bool) = new(isnull, value)
for T in types
x = Nullable{T}(zero(T),false)
@test x.isnull === false
@test x.hasvalue === true
@test isa(x.value, T)
@test x.value === zero(T)
@test eltype(x) === T

x = Nullable{T}(zero(T),true)
@test x.isnull === true
@test x.hasvalue === false
@test isa(x.value, T)
@test eltype(Nullable{T}) === T
@test eltype(x) === T
Expand All @@ -64,13 +64,13 @@ end
for T in types
v = zero(T)
x = Nullable(v)
@test x.isnull === false
@test x.hasvalue === true
@test isa(x.value, T)
@test x.value === v

v = one(T)
x = Nullable(v)
@test x.isnull === false
@test x.hasvalue === true
@test isa(x.value, T)
@test x.value === v
end
Expand Down

0 comments on commit 8da3c10

Please sign in to comment.