diff --git a/REQUIRE b/REQUIRE index f406143..891ac1b 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,2 +1,3 @@ julia 0.3.0- StatsBase 0.3 +Iterators 0.1.5+ diff --git a/src/DataArrays.jl b/src/DataArrays.jl index e0e9ea9..1fc4fa3 100644 --- a/src/DataArrays.jl +++ b/src/DataArrays.jl @@ -1,7 +1,7 @@ using StatsBase module DataArrays - using StatsBase + using StatsBase, Iterators const DEFAULT_POOLED_REF_TYPE = Uint32 diff --git a/src/dataarray.jl b/src/dataarray.jl index 4b2db46..c32757c 100644 --- a/src/dataarray.jl +++ b/src/dataarray.jl @@ -299,12 +299,12 @@ Base.endof(da::DataArray) = endof(da.data) # -> Int function Base.find(da::DataArray{Bool}) # -> Array{Int} data = da.data ntrue = 0 - @inbounds @bitenumerate da.na i na begin + @inbounds @itr for (i, na) in enumerate(da.na) ntrue += !na && data[i] end res = Array(Int, ntrue) count = 1 - @inbounds @bitenumerate da.na i na begin + @inbounds @itr for (i, na) in enumerate(da.na) if !na && data[i] res[count] = i count += 1 diff --git a/src/operators.jl b/src/operators.jl index 7cb9726..7e36a13 100644 --- a/src/operators.jl +++ b/src/operators.jl @@ -261,7 +261,7 @@ macro dataarray_unary(f, intype, outtype, N...) function $(f){T<:$(intype)}(d::$(isempty(N) ? :(DataArray{T}) : :(DataArray{T,$(N[1])}))) data = d.data res = similar(data, $(outtype)) - @bitenumerate d.na i na begin + @itr for (i, na) in enumerate(d.na) if !na @inbounds res[i] = $(f)(data[i]) end @@ -295,11 +295,11 @@ macro dataarray_binary_scalar(vectorfunc, scalarfunc, outtype, swappable) :(function $(vectorfunc)(a::DataArray, b::$t) data = a.data res = similar(data, $outtype) - @bitenumerate a.na i na begin + @itr(for (i, na) in enumerate(a.na) if !na @inbounds res[i] = $(scalarfunc)(data[i], b) end - end + end) DataArray(res, copy(a.na)) end), :(function $(vectorfunc)(a::AbstractDataArray, b::$t) @@ -333,11 +333,11 @@ macro dataarray_binary_array(vectorfunc, scalarfunc, outtype) data2 = $(btype == :DataArray || btype == :(DataArray{Bool}) ? :(b.data) : :b) res = Array($outtype, promote_shape(size(a), size(b))) resna = $narule - @bitenumerate resna i na begin + @itr(for (i, na) in enumerate(resna) if !na @inbounds res[i] = $(scalarfunc)(data1[i], data2[i]) end - end + end) DataArray(res, resna) end end @@ -399,7 +399,7 @@ for (f, elf) in ((:(Base.ctranspose), :conj), (:(Base.transpose), :identity)) data = d.data sz = (size(data, 1), size(data, 2)) res = similar(data, size(data, 2), size(data, 1)) - @bitenumerate d.na i na begin + @itr for (i, na) in enumerate(d.na) if !na jnew, inew = ind2sub(sz, i) @inbounds res[inew, jnew] = $(elf)(data[i]) @@ -494,7 +494,7 @@ for f in (:(Base.round), :(Base.ceil), :(Base.floor), :(Base.trunc)) function $(f){T<:Real}(d::DataArray{T}, args::Integer...) data = similar(d.data) - @bitenumerate d.na i na begin + @itr for (i, na) in enumerate(d.na) if !na @inbounds data[i] = $(f)(d[i], args...) end @@ -565,7 +565,7 @@ function Base.isequal(a::DataArray, b::DataArray) if size(a) != size(b) || a.na != b.na return false end - @bitenumerate a.na i na begin + @itr for (i, na) in enumerate(a.na) @inbounds if !na && !isequal(a.data[i], b.data[i]) return false end @@ -589,7 +589,7 @@ function Base.(:(==))(a::DataArray, b::DataArray) bdata = b.data bchunks = b.na.chunks has_na = false - @bitenumerate a.na i na begin + @itr for (i, na) in enumerate(a.na) if na || Base.unsafe_bitgetindex(bchunks, i) has_na = true else @@ -607,7 +607,7 @@ end size(a) == size(b) || return false adata = a.data has_na = false - @bitenumerate a.na i na begin + @itr for (i, na) in enumerate(a.na) if na has_na = true else @@ -828,7 +828,7 @@ for f in cumulative_vector_operators new_data = ($f)(dv.data) new_na = falses(length(dv)) hitna = false - @bitenumerate dv.na i na begin + @itr for (i, na) in enumerate(dv.na) hitna |= na if hitna new_na[i] = true @@ -879,7 +879,7 @@ end function Base.all(dv::DataArray{Bool}) data = dv.data has_na = false - @bitenumerate dv.na i na begin + @itr for (i, na) in enumerate(dv.na) if !na data[i] || return false else @@ -904,7 +904,7 @@ end function Base.any(dv::DataArray{Bool}) has_na = false - @bitenumerate dv.na i na begin + @itr for (i, na) in enumerate(dv.na) if !na if dv.data[i] return true diff --git a/src/utils.jl b/src/utils.jl index 29cfbd8..aff23db 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -8,14 +8,3 @@ function typeloop(vals) end return toptype end - -# Enumerate a BitArray. This is faster than using enumerate() -macro bitenumerate(ba, i, x, code) - esc(quote - $i = 1 - for $x in $ba - $code - $i += 1 - end - end) -end