Skip to content

Commit

Permalink
Tweaks; compile on all platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
droodman committed Apr 13, 2024
1 parent b57d888 commit 2f508ab
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 21 deletions.
2 changes: 1 addition & 1 deletion benchmarks.do
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
* Derived from https://github.com/FixedEffects/FixedEffectModels.jl/blob/master/benchmark/benchmark.md

scalar N = 100000000
scalar N = 10000000
scalar K = 100
set obs `=N'
gen id1 = runiformint(1, N/K)
Expand Down
2 changes: 1 addition & 1 deletion jl.ado
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*! jl 1.0.0 8 April 2024
*! jl 1.0.0 12 April 2024
*! Copyright (C) 2023-24 David Roodman

* This program is free software: you can redistribute it and/or modify
Expand Down
Binary file modified jl.pluginLINUX64
Binary file not shown.
Binary file modified jl.pluginMACARM64
Binary file not shown.
Binary file modified jl.pluginMACINTEL64
Binary file not shown.
14 changes: 8 additions & 6 deletions jl.sthlp
Original file line number Diff line number Diff line change
Expand Up @@ -297,17 +297,16 @@ and written with Julia commands.
{synoptline}
{synopt:{bf:SF_nobs()}}Number of observations in Stata data set{p_end}
{synopt:{bf:SF_nvar()}}Number of variables{p_end}
{synopt:{bf:SF_varindex(s::String)}}Index in data set of variable named s{p_end}
{synopt:{bf:SF_var_is_string(i::Int)}}Whether variable i is string{p_end}
{synopt:{bf:SF_var_is_strl(i::Int)}}Whether variable i is a strL{p_end}
{synopt:{bf:SF_var_is_binary(i::Int, j::Int)}}Whether observation i of variable j is a binary strL{p_end}
{synopt:{bf:SF_sdatalen(i::Int, j::Int)}}String length of variable i, observation j{p_end}
{synopt:{bf:SF_sdatalen(j::Int, i::Int)}}String length of variable i, observation j{p_end}
{synopt:{bf:SF_is_missing()}}Whether a Float64 value is Stata missing{p_end}
{synopt:{bf:SV_missval()}}Stata floating-point value for missing{p_end}
{synopt:{bf:SF_vstore(i::Int, j::Int, val::Real)}}Set observation j of variable i to val (numeric){p_end}
{synopt:{bf:SF_sstore(i::Int, j::Int, s::String)}}Set observation j of variable i to s (string) {p_end}
{synopt:{bf:SF_vdata(i::Int, j::Int)}}Get observation j of variable i (numeric){p_end}
{synopt:{bf:SF_sdata(i::Int, j::Int)}}Get observation j of variable i (string){p_end}
{synopt:{bf:SF_vstore(j::Int, i::Int, val::Real)}}Set observation j of variable i to val (numeric){p_end}
{synopt:{bf:SF_sstore(j::Int, i::Int, s::String)}}Set observation j of variable i to s (string) {p_end}
{synopt:{bf:SF_vdata(j::Int, i::Int)}}Get observation j of variable i (numeric){p_end}
{synopt:{bf:SF_sdata(j::Int, i::Int)}}Get observation j of variable i (string){p_end}
{synopt:{bf:SF_macro_save(mac::String, tosave::String)}}Set macro value{p_end}
{synopt:{bf:SF_macro_use(mac::String)}}Get macro mac{p_end}
{synopt:{bf:SF_scal_save(scal::String, val::Real)}}Set scalar value{p_end}
Expand All @@ -319,6 +318,9 @@ and written with Julia commands.
{synopt:{bf:SF_display(s::String)}}Print to Stata results window{p_end}
{synopt:{bf:SF_error(s::String)}}Print error to Stata results window{p_end}

{synopt:{bf:st_nobs()}}Number of observations in Stata data set; same as SF_nobs(){p_end}
{synopt:{bf:st_nvar()}}Number of Stata variables; same as SF_nvar(){p_end}
{synopt:{bf:st_varindex(s::String)}}Index in data set of variable named s{p_end}
{synopt:{bf:st_global(mac::String)}}Get global macro mac{p_end}
{synopt:{bf:st_global(mac::String, tosave::String)}}Set global macro mac{p_end}
{synopt:{bf:st_local(mac::String, tosave::String)}}Set local macro mac{p_end}
Expand Down
48 changes: 35 additions & 13 deletions stataplugininterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
module stataplugininterface
export SF_sdatalen, SF_var_is_string, SF_var_is_strl, SF_var_is_binary, SF_nobs, SF_nvars, SF_nvar, SF_ifobs, SF_in1, SF_in2, SF_col,
SF_row, SF_is_missing, SF_missval, SF_vstore, SF_sstore, SF_mat_store, SF_macro_save, SF_scal_save, SF_display, SF_error,
SF_vdata, SF_sdata, SF_mat_el, SF_macro_use, SF_scal_use, st_varindex, st_matrix, st_numscalar, st_data, st_view, st_local, st_global
SF_vdata, SF_sdata, SF_mat_el, SF_macro_use, SF_scal_use, st_varindex, st_matrix, st_numscalar, st_data, st_view, st_local, st_global,
st_nobs, st_nvar

using DataFrames, CategoricalArrays

Expand Down Expand Up @@ -57,14 +58,28 @@ Returns the number of observations in the Stata data set.
"""
SF_nobs() = @ccall dllpath[].jlSF_nobs()::Cint

"""
st_nobs()
Returns the number of observations in the Stata data set.
"""
st_nobs = SF_nobs

"""
SF_nvar()
Returns the number of variables in the Stata data set.
"""
SF_nvar() = @ccall dllpath[].jlSF_nvar()::Cint

SF_nvars() = @ccall dllpath[].jlSF_nvars()::Cint

"""
st_nvar()
Returns the number of variables in the Stata data set.
"""
st_nvar = SF_nvar

SF_ifobs(j::Integer) = @ccall dllpath[].jlSF_ifobs(j::Cint)::Cchar
SF_in1()= @ccall dllpath[].jlSF_in1()::Cint
SF_in2() = @ccall dllpath[].jlSF_in2()::Cint
Expand Down Expand Up @@ -284,7 +299,7 @@ st_numscalar(scalarname, val) = begin SF_scal_save(scalarname, val); nothing end
st_data(varnames::AbstractVector{<:AbstractString}, sample::Vector{Bool}=Bool[])::Matrix{Float64}
st_data(varnames::AbstractString, sample::Vector{Bool}=Bool[])::Matrix{Float64}
Return one or more variables in a matrix. `scalarname` is a vector or space-delimited string of variable names.
Return one or more variables in a matrix. `varnames` is a vector of strings or space-delimited string of variable names.
"""
function st_data(varnames::AbstractVector{<:AbstractString}, sample::Vector{Bool}=Bool[])
ind = st_varindex.(varnames)
Expand Down Expand Up @@ -328,8 +343,13 @@ function NaN2missing(df::DataFrame)
end
end

# view onto Stata column(s). S is Val(true) for subviews.
struct st_view{S} <: AbstractMatrix{Float64}
"""
st_view(varnames::AbstractVector{<:AbstractString}, sample::Vector{Bool}=Bool[])::Matrix{Float64}
st_view(varnames::AbstractString, sample::Vector{Bool}=Bool[])::Matrix{Float64}
Return a matrix-like view onto one or more Stata variables. `varnames` is a vector of strings or space-delimited string of variable names.
"""
struct st_view{S} <: AbstractMatrix{Float64} # S is Val(true) for subviews.
nrows::Int64
ncols::Int32
varindex::Vector{Int32}
Expand All @@ -350,20 +370,22 @@ st_view(varnames::AbstractString, sample::Vector{Bool}=Bool[]) = st_view(split(v

import Base.size, Base.length, Base.getindex, Base.setindex!
size(v::st_view) = v.nrows, v.ncols
getindex(v::st_view{Val(false)}, i::Integer, j::Integer) where T = SF_vdata(i, v.varindex[j])
getindex(v::st_view{Val(true)}, i::Integer, j::Integer) where T = SF_vdata(v.sample[i], v.varindex[j])
getindex(v::st_view, i::Integer) = getindex(v, i, 1)
getindex(v::st_view, I::CartesianIndex{1}) = getindex(v, I[1], 1)
getindex(v::st_view, I::CartesianIndex{2}) = getindex(v, I[1], I[2])
function setindex!(v::st_view{Val(false)}, val::Number, i, j)
@inline getindex(v::st_view{Val(false)}, i::Integer, j::Integer) where T = SF_vdata(i, v.varindex[j])
@inline getindex(v::st_view{Val(true)}, i::Integer, j::Integer) where T = SF_vdata(v.sample[i], v.varindex[j])
@inline getindex(v::st_view, i::Integer) = getindex(v, i, 1)
@inline getindex(v::st_view, I::CartesianIndex{1}) = getindex(v, I[1], 1)
@inline getindex(v::st_view, I::CartesianIndex{2}) = getindex(v, I[1], I[2])
@inline function setindex!(v::st_view{Val(false)}, val::Number, i::Integer, j::Integer)
SF_vstore(i, v.varindex[j], val)
nothing
end
function setindex!(v::st_view{Val(true)}, val::Number, i, j)
@inline function setindex!(v::st_view{Val(true)}, val::Number, i, j)
SF_vstore(v.sample[i], v.varindex[j], val)
nothing
end
setindex!(v::st_view, val::Number, i) = setindex!(v, val, i, 1)
@inline setindex!(v::st_view, val::Number, i::Integer) = setindex!(v, val, i, 1)
@inline setindex!(v::st_view, val::Number, I::CartesianIndex{1}) = setindex!(v, val, I[1], 1)
@inline setindex!(v::st_view, val::Number, I::CartesianIndex{2}) = setindex!(v, val, I[1], I[2])
end


0 comments on commit 2f508ab

Please sign in to comment.