text:
type Trie{T}
    value::T
    children::Dict{Char,Trie{T}}
    is_key::Bool

    function Trie()
        self = new()
        self.children = (Char=>Trie{T})[]
        self.is_key = false
        self
    end
end

Trie() = Trie{Any}()

function setindex!{T}(t::Trie{T}, val::T, key::String)
    node = t
    for char in key
        if !haskey(node.children, char)
            node.children[char] = Trie{T}()
        end
        node = node.children[char]
    end
    node.is_key = true
    node.value = val
end

function subtrie(t::Trie, prefix::String)
    node = t
    for char in prefix
        if !haskey(node.children, char)
            return nothing
        else
            node = node.children[char]
        end
    end
    node
end

function haskey(t::Trie, key::String)
    node = subtrie(t, key)
    node != nothing && node.is_key
end

get(t::Trie, key::String) = get(t, key, nothing)
function get(t::Trie, key::String, notfound)
    node = subtrie(t, key)
    if node != nothing && node.is_key
        return node.value
    end
    notfound
end

function keys(t::Trie, prefix::String, found)
    if t.is_key
        push(found, prefix)
    end
    for (char,child) in t.children
        keys(child, strcat(prefix,char), found)
    end
end
keys(t::Trie, prefix::String) = (found=String[]; keys(t, prefix, found); found)
keys(t::Trie) = keys(t, "")

function keys_with_prefix(t::Trie, prefix::String)
    st = subtrie(t, prefix)
    st != nothing ? keys(st,prefix) : []
end


text:
using CImGui
using CImGui.CSyntax
using CImGui.CSyntax.CSwitch
using CImGui.CSyntax.CStatic
using Printf

"""
    ShowExampleAppLongText(p_open::Ref{Bool})
Test rendering huge amount of text, and the incidence of clipping.
"""
function ShowExampleAppLongText(p_open::Ref{Bool})
    CImGui.SetNextWindowSize((520,600), CImGui.ImGuiCond_FirstUseEver)
    CImGui.Begin("Example: Long text display", p_open) || (CImGui.End(); return)

@cstatic test_type=Cint(0) lines=0 log=CImGui.TextBuffer() begin
    CImGui.Text("Printing unusually long amount of text.")
    @c CImGui.Combo("Test type", &test_type, "Single call to TextUnformatted()\0Multiple calls to Text(), clipped manually\0Multiple calls to Text(), not clipped (slow)\0")
    CImGui.Text("Buffer contents: $lines lines, $(CImGui.Size(log)) bytes")
    if CImGui.Button("Clear")
        @info "Trigger Clear | find me here: $(@__FILE__) at line $(@__LINE__)"
        CImGui.Clear(log)
        lines = 0
    end
    CImGui.SameLine()
    if CImGui.Button("Add 1000 lines")
        @info "Trigger Add 1000 lines | find me here: $(@__FILE__) at line $(@__LINE__)"
        foreach(i->CImGui.Append(log, "$(lines+i) The quick brown fox jumps over the lazy dog\n"), 1:1000)
        lines += 1000
    end
    CImGui.BeginChild("Log")
        @cswitch test_type begin
            @case 0
                # single call to TextUnformatted() with a big buffer
                CImGui.TextUnformatted(CImGui.Begin(log), CImGui.End(log))
                break
            @case 1
                # multiple calls to Text(), manually coarsely clipped - demonstrate how to use the ImGuiListClipper helper.
                CImGui.PushStyleVar(CImGui.ImGuiStyleVar_ItemSpacing, (0,0))
                clipper = CImGui.Clipper()
                CImGui.Begin(clipper, lines)
                while CImGui.Step(clipper)
                    s = CImGui.Get(clipper, :DisplayStart)
                    e = CImGui.Get(clipper, :DisplayEnd)-1
                    foreach(i->CImGui.Text("$i The quick brown fox jumps over the lazy dog"), s:e)
                end
                CImGui.PopStyleVar()
                CImGui.Destroy(clipper)
                break
            @case 2
                # multiple calls to Text(), not clipped (slow)
                CImGui.PushStyleVar(CImGui.ImGuiStyleVar_ItemSpacing, (0,0))
                foreach(i->CImGui.Text("$i The quick brown fox jumps over the lazy dog"), 1:lines)
                CImGui.PopStyleVar()
                break
        end
        CImGui.EndChild()
    CImGui.End()
end # @cstatic

end

ext: jl


text:
using HypothesisTests, Test
using HypothesisTests: default_tail
using StableRNGs

@testset "F-tests" begin
    @testset "Basic variance F-test" begin
        rng = StableRNG(12)
        y1_h0 = 4 .+ randn(rng, 500)
        y2_h0 = 4 .+ randn(rng, 400)

        t = VarianceFTest(y1_h0, y2_h0)

        @test t.n_x == 500
        @test t.n_y == 400
        @test t.df_x == 499
        @test t.df_y == 399
        @test t.F                    ≈ 0.859582  rtol=1e-5
        @test pvalue(t)              ≈ 0.109714  rtol=1e-5
        @test pvalue(t, tail=:left)  ≈ 0.0548572 rtol=1e-5
        @test pvalue(t, tail=:right) ≈ 0.945143  rtol=1e-5
        @test default_tail(t) == :both

        t = VarianceFTest(y2_h0, y1_h0)

        @test t.n_x == 400
        @test t.n_y == 500
        @test t.df_x == 399
        @test t.df_y == 499
        @test t.F                    ≈ 1.163355  rtol=1e-5
        @test pvalue(t)              ≈ 0.109714  rtol=1e-5
        @test pvalue(t, tail=:right) ≈ 0.0548572 rtol=1e-5
        @test pvalue(t, tail=:left)  ≈ 0.945143 rtol=1e-5
        @test default_tail(t) == :both

        y1_h1 = 0.7*randn(rng, 200)
        y2_h1 = 1.3*randn(rng, 120)

        t = VarianceFTest(y1_h1, y2_h1)

        @test t.n_x == 200
        @test t.n_y == 120
        @test t.df_x == 199
        @test t.df_y == 119
        @test t.F ≈ 0.264161 rtol=1e-5
        @test pvalue(t) < 1e-8
        @test default_tail(t) == :both
        @test pvalue(t, tail=:left) < 1e-8
        @test pvalue(t, tail=:right) > 1.0 - 1e-8
    end
end


text:
using Random: randperm!
using HDF5

"""
    MODIFIED DataLoader Class
    Modifications:
      - Altered "Base.iterate" implementation to handle hdf5 (.h5) files
      - Files are only read from disk when they're needed

    Original version: https://github.com/boathit/Benchmark-Flux-PyTorch/blob/master/dataloader.jl
    Original documentation:
    DataLoader(dataset::AbstractArray...; batchsize::Int, shuffle::Bool)
DataLoader provides iterators over the dataset.
```julia
X = rand(10, 1000)
Y = rand(1, 1000)
m = Dense(10, 1)
loss(x, y) = Flux.mse(m(x), y)
opt = ADAM(params(m))
trainloader = DataLoader(X, Y, batchsize=256, shuffle=true)
Flux.train!(loss, trainloader, opt)
```
"""
struct DataLoader
  dataset::Tuple
  batchsize::Int
  shuffle::Bool
  indices::Vector{Int}
  n::Int
end

function DataLoader(
  dataset::Tuple{AbstractArray,Vararg{AbstractArray}};
  batchsize::Int,
  shuffle::Bool,
)
  l = last.(size.(dataset))
  n = first(l)
  all(n .== l) || throw(DimensionMismatch("All data should have the same length."))
  indices = collect(1:n)
  shuffle && randperm!(indices)
  DataLoader(dataset, batchsize, shuffle, indices, n)
end

DataLoader(dataset::AbstractArray...; batchsize::Int, shuffle::Bool) =
  DataLoader(dataset, batchsize = batchsize, shuffle = shuffle)

function Base.iterate(it::DataLoader, start = 1)
  if start > it.n
    it.shuffle && randperm!(it.indices)
    return nothing
  end
  nextstart = min(start + it.batchsize, it.n + 1)
  i = it.indices[start:nextstart-1]

  # Select batch data
  raw_batch = Tuple(copy(selectdim(x, ndims(x), i)) for x in it.dataset)

  # Prepare empty batch arrays of size (dim1, dim2 .... dimN, 1)
  # Added dimension: index in batch
  X_batch = Array{Float32}(undef, 40, 40, 4, it.batchsize)
  Y_batch = Array{Float32}(undef, 1, it.batchsize)

  for i in 1:length(raw_batch[1])  # last batch could be smaller than (it.batchsize)
    # raw_batch[1][i] here includes the path to the h5 file to be read

    # img = permutedims(cpu(h5open(raw_batch[1][i], "r"))["data"][1:4,:,1:40], [2, 3, 1])
    f = cpu(h5open(raw_batch[1][i], "r"))
    img = permutedims(f["data"][1:4,:,1:40], [2, 3, 1])
    close(f)

    X_batch[:, :, :, i] = img

    depth = raw_batch[2][i]
    Y_batch[1, i] = depth
  end

  new_element = (X_batch, Y_batch)

  return gpu(new_element), nextstart
end

Base.length(it::DataLoader) = it.n
Base.eltype(it::DataLoader) = typeof(it.dataset)

function Base.show(io::IO, it::DataLoader)
  print(io, "DataLoader(dataset size = $(it.n)")
  print(io, ", batchsize = $(it.batchsize), shuffle = $(it.shuffle)")
  print(io, ")")
end


text:
# ---------------------------------------------------------------------------- #
#
#   master.jl
#
#   Abstract master element type
#   This allows for writing an n-dimensional version of solver methods
#
#   λυτέος
#   Fall 2017
#
#   Max Opgenoord
#
# ---------------------------------------------------------------------------- #

"""
    Master

Master abstract type:
Overarching abstract type for master element types. Currently triangle and
tetrahedron implemented.
"""
abstract type Master

end

include("../integration/quadratureLine.jl")
include("../integration/quadratureTriangle.jl")
include("../integration/quadratureTet.jl")
include("../integration/basisFuncLineLag.jl")
include("../integration/basisFuncTriangleLag.jl")
include("../integration/basisFuncTetLag.jl")

include("master2D.jl")
include("master3D.jl")


text:
function Temperature_dist(T,theta_l,theta_v,q_l,q_v,rho_vs,soil_parameters,
  Ta, Hr_atm, St,u, psi_H, psi_m, soil_numerical_parameters,constants,atm_parameters,dt)

theta_top = theta_l[end]; # top layer water content
rs = 10*exp(35.63*(0.15-theta_top)); # soil surface resistance to vapor flow
rv = 1/(u*constants.k^2)*(log((atm_parameters.z_ref-atm_parameters.d+atm_parameters.z_H)/atm_parameters.z_oH)+psi_H)*
    (log((atm_parameters.z_ref-atm_parameters.d+atm_parameters.z_m)/atm_parameters.z_om)+psi_m);
rH = rv;

e_a = 0.611*exp(17.27*(Ta-273.15)/(Ta-35.85))*Hr_atm; # atm. vapor pressure
rho_sa = 1e-3.*exp(19.84-4975.9./Ta);
rho_va = rho_sa*Hr_atm;
eps_a = 0.7+5.95e-5*e_a*exp(1500/Ta); # atm. emmisivity;
eps_s = min(0.9+0.18*theta_top,1); #soil emmisivity

Cp = constants.Cs.*(soil_parameters.theta_s.-theta_l)+constants.Cw.*theta_l.+constants.Cv.*theta_v;
lambda = soil_parameters.b1.+soil_parameters.b2.*theta_l+soil_parameters.b3.*sqrt.(theta_l);
rho_w = 1000 .-7.3e-3.*(T.-(273+4)).^2 .+ 3.79e-5.*(T.-(273+4)).^3;  #water density [kg m^-3]
Lw = 2.501e6 .- 2369.2.*(T .- 273); # latent heat [J kg^-1]
Lo = Lw.*rho_w; # vol. latent heat [J m^-3]
L = 2.260e6; # latent heat for vaporazation [J kg^-1]

T_old = T;
O=zeros(2)
O[1] = 1;
LE=0
while O[1]>soil_numerical_parameters.eps_T
    temp_T_2 = T;
    temp_T = (T+T_old)./2;
    for i = 2:soil_numerical_parameters.Ns-1
        T[i] = T_old[i]+dt/(soil_numerical_parameters.dz_s^2*Cp[i])*(lambda[i]*(temp_T[i+1]-2*temp_T[i]+temp_T[i-1])+
            (lambda[i+1]-lambda[i])*(temp_T[i+1]-temp_T[i])-(constants.Cw*q_l[i]+constants.Cv*q_v[i])*soil_numerical_parameters.dz_s*(temp_T[i+1]-temp_T[i]));
    end
    #set bottom boundary
    T[1] = T_old[1];
    #set top boundary
    Rn = (1-albedo)*St+eps_s*eps_a*constants.sigma*Ta^4-
        eps_s*constants.sigma*temp_T[end]^4;
    H = constants.Ca*(temp_T[end]-Ta)/rH;
    LE = L.*(rho_vs[end].-rho_va)./(rv.+rs);
    G = Rn.-H.-LE;
    T[end] = (G+T[end-1]*lambda[end]/soil_numerical_parameters.dz_s+Lo[end]*q_v[end])/(lambda[end]/soil_numerical_parameters.dz_s-constants.Cv*q_v[end]-constants.Cw*q_l[end]);

    rho_w = 1000 .- 7.3e-3.*(T.-(273+4)).^2 .+ 3.79e-5.*(T.-(273+4)).^3;  #water density [kg m^-3]
    Lw = 2.501e6.-2369.2.*(T.-273); # latent heat [J kg^-1]
    Lo = Lw.*rho_w; # vol. latent heat [J m^-3]

    O[2] = mean(abs.(temp_T_2.-T)./temp_T);

    if O[2]<O[1]
        O[1] = O[2];
    else
        dt = dt/2;
        T = T_old;
        O[1] = 1;
    end
    if dt==0
        dt = 1;
    end

    #println(dt)
end

E = LE/(L*rho_w[end]);

return T,E,dt

end


text:
"`base(::Type{T}, i)` singleton (`i`,) of collection type `T` "
function base end

"Id of Random Variable in projection"
function randvarid end

"`combine(a, b)` Combine (e.g. concatenate) `a` and `b`"
function combine end

"`append(a, b)` append `b` to the end of `a`, like `push!` but functional"
function append end

"`Increment(id::ID)` the id"
function increment end


text:
import Base: isidentifier, is_id_start_char, is_id_char

const RESERVED_WORDS = Set(["begin", "while", "if", "for", "try",
    "return", "break", "continue", "function", "macro", "quote", "let",
    "local", "global", "const", "abstract", "typealias", "type", "bitstype",
    "immutable", "do", "module", "baremodule", "using", "import",
    "export", "importall", "end", "else", "elseif", "catch", "finally"])

VERSION < v"0.6.0-dev.2194" && push!(RESERVED_WORDS, "ccall")
VERSION >= v"0.6.0-dev.2698" && push!(RESERVED_WORDS, "struct")

function identifier(s::AbstractString)
    s = normalize_string(s)
    if !isidentifier(s)
        s = makeidentifier(s)
    end
    @compat(Symbol(in(s, RESERVED_WORDS) ? "_"*s : s))
end

function makeidentifier(s::AbstractString)
    i = start(s)
    done(s, i) && return "x"

    res = IOBuffer(sizeof(s) + 1)

    (c, i) = next(s, i)
    under = if is_id_start_char(c)
        write(res, c)
        c == '_'
    elseif is_id_char(c)
        write(res, 'x', c)
        false
    else
        write(res, '_')
        true
    end

    while !done(s, i)
        (c, i) = next(s, i)
        if c != '_' && is_id_char(c)
            write(res, c)
            under = false
        elseif !under
            write(res, '_')
            under = true
        end
    end

    return String(take!(res))
end

function make_unique(names::Vector{Symbol}; allow_duplicates=true)
    seen = Set{Symbol}()
    names = copy(names)
    dups = Int[]
    for i in 1:length(names)
        name = names[i]
        in(name, seen) ? push!(dups, i) : push!(seen, name)
    end

    if !allow_duplicates && length(dups) > 0
        d = unique(names[dups])
        msg = """Duplicate variable names: $d.
                 Pass allow_duplicates=true to make them unique using a suffix automatically."""
        throw(ArgumentError(msg))
    end

    for i in dups
        nm = names[i]
        k = 1
        while true
            newnm = Symbol("$(nm)_$k")
            if !in(newnm, seen)
                names[i] = newnm
                push!(seen, newnm)
                break
            end
            k += 1
        end
    end

    return names
end

#' @description
#'
#' Generate standardized names for columns of a DataTable. The
#' first name will be :x1, the second :x2, etc.
#'
#' @field n::Integer The number of names to generate.
#'
#' @returns names::Vector{Symbol} A vector of standardized column names.
#'
#' @examples
#'
#' DataTables.gennames(10)
function gennames(n::Integer)
    res = Array{Symbol}(n)
    for i in 1:n
        res[i] = Symbol(@sprintf "x%d" i)
    end
    return res
end

#' @description
#'
#' Count the number of null values in an array.
#'
#' @field a::AbstractArray The array whose missing values are to be counted.
#'
#' @returns count::Int The number of null values in `a`.
#'
#' @examples
#'
#' DataTables.countnull([1, 2, 3])
function countnull(a::AbstractArray)
    res = 0
    for x in a
        res += _isnull(x)
    end
    return res
end

#' @description
#'
#' Count the number of missing values in a NullableArray.
#'
#' @field a::NullableArray The NullableArray whose missing values are to be counted.
#'
#' @returns count::Int The number of null values in `a`.
#'
#' @examples
#'
#' DataTables.countnull(NullableArray([1, 2, 3]))
countnull(a::NullableArray) = sum(a.isnull)

#' @description
#'
#' Count the number of missing values in a NullableCategoricalArray.
#'
#' @field na::CategoricalArray The CategoricalArray whose missing values
#'        are to be counted.
#'
#' @returns count::Int The number of null values in `a`.
#'
#' @examples
#'
#' DataTables.countnull(CategoricalArray([1, 2, 3]))
function countnull(a::CategoricalArray)
    res = 0
    for x in a.refs
        res += x == 0
    end
    return res
end

# Gets the name of a function. Used in groupedatatable/grouping.jl
function _fnames{T<:Function}(fs::Vector{T})
    λcounter = 0
    names = map(fs) do f
        name = string(f)
        if name == "(anonymous function)" # Anonymous functions with Julia < 0.5
            λcounter += 1
            name = "λ$(λcounter)"
        end
        name
    end
    names
end

_isnull(x::Any) = false
_isnull(x::Nullable) = isnull(x)


text:
using DataDeps

register(DataDep(
  "breast-cancer",
  "http://archive.ics.uci.edu/ml/datasets/Breast+Cancer",
  "http://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer/breast-cancer.data",
  "ca7d3fa97b62ff967b6894ffbb3acaaf4a51062e0149e5950b3ad6f685970b65",
  post_fetch_method=(path -> begin
    UCIData.process_dataset(path,
        target_index=10,
        feature_indices=1:9,
        categoric_indices=[1:6; 8:9],
    )
  end),
))

max_stars_repo_name: JackDunnNZ/uci-data

max_issues_repo_name: JackDunnNZ/uci-data

max_forks_repo_name: JackDunnNZ/uci-data


text:
function f(x,y) ::
  Int64 end

max_stars_repo_name: gabriel-doriath-dohler/pjulia

max_issues_repo_name: gabriel-doriath-dohler/pjulia

max_forks_repo_name: gabriel-doriath-dohler/pjulia


text:
function compute_edge_faces_ring(faces)
    """
        compute_edge_face_ring - compute faces adjacent to each edge

          e2f = compute_edge_face_ring(faces);

          e2f(i,j) and e2f(j,i) are the number of the two faces adjacent to
          edge (i,j).

          Copyright (c) 2007 Gabriel Peyre
    """
    n = maximum(faces)
    m = size(faces,2)

    i = [faces[1,:] faces[2,:] faces[3,:]]
    j = [faces[2,:] faces[3,:] faces[1,:]]
    s = [1:m 1:m 1:m];

    # first without duplicate
    tmp = unique(i + (maximum(i) + 1)*j) # unique
    I = indexin(tmp, i + (maximum(i) + 1)*j)

    # remaining items
    J = setdiff(1:length(s), I);

    # flip the duplicates
    i1 = [i[I]; j[J]]
    j1 = [j[I]; i[J]]
    s  = [s[I]; s[J]]

    # remove doublons
    tmp = unique(i1 + (maximum(i1) + 1)*j1)
    I = indexin(tmp, i1 + (maximum(i1) + 1)*j1)
    i1 = i1[I]
    j1 = j1[I]
    s  = s[I]
    A = sparse(i1,j1,s,n,n)

    # add missing points
    I = findall(A'.!=0)
    I = I[A[I].==0]
    A[I]=-1

    return A
end;