Skip to content

Commit

Permalink
Merge branch 'master' into stabilize-quicksort
Browse files Browse the repository at this point in the history
  • Loading branch information
LilithHafner committed May 12, 2022
2 parents 1a30832 + a74ef57 commit 0010aaf
Show file tree
Hide file tree
Showing 70 changed files with 1,191 additions and 553 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Expand Up @@ -87,6 +87,9 @@ Standard library changes

#### REPL

* `Meta-e` now opens the current input in an editor. The content (if modified) will be
executed upon existing the editor.

#### SparseArrays

#### Dates
Expand Down
4 changes: 1 addition & 3 deletions base/array.jl
Expand Up @@ -142,9 +142,7 @@ julia> a = Base.vect(UInt8(1), 2.5, 1//2)
"""
function vect(X...)
T = promote_typeof(X...)
#T[ X[i] for i=1:length(X) ]
# TODO: this is currently much faster. should figure out why. not clear.
return copyto!(Vector{T}(undef, length(X)), X)
return T[X...]
end

size(a::Array, d::Integer) = arraysize(a, convert(Int, d))
Expand Down
7 changes: 3 additions & 4 deletions base/combinatorics.jl
Expand Up @@ -164,8 +164,7 @@ end
Permute vector `v` in-place, according to permutation `p`. No checking is done
to verify that `p` is a permutation.
To return a new permutation, use `v[p]`. Note that this is generally faster than
`permute!(v,p)` for large vectors.
To return a new permutation, use `v[p]`. Note that this is faster than `permute!(v, p)`.
See also [`invpermute!`](@ref).
Expand All @@ -185,7 +184,7 @@ julia> A
1
```
"""
permute!(a, p::AbstractVector) = permute!!(a, copymutable(p))
permute!(v, p::AbstractVector) = (v .= v[p])

function invpermute!!(a, p::AbstractVector{<:Integer})
require_one_based_indexing(a, p)
Expand Down Expand Up @@ -232,7 +231,7 @@ julia> A
1
```
"""
invpermute!(a, p::AbstractVector) = invpermute!!(a, copymutable(p))
invpermute!(v, p::AbstractVector) = (v[p] = v; v)

"""
invperm(v)
Expand Down
21 changes: 0 additions & 21 deletions base/dict.jl
@@ -1,26 +1,5 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

function _truncate_at_width_or_chars(str, width, chars="", truncmark="")
truncwidth = textwidth(truncmark)
(width <= 0 || width < truncwidth) && return ""

wid = truncidx = lastidx = 0
for (idx, c) in pairs(str)
lastidx = idx
wid += textwidth(c)
wid >= width - truncwidth && truncidx == 0 && (truncidx = lastidx)
(wid >= width || c in chars) && break
end

lastidx != 0 && str[lastidx] in chars && (lastidx = prevind(str, lastidx))
truncidx == 0 && (truncidx = lastidx)
if lastidx < lastindex(str)
return String(SubString(str, 1, truncidx) * truncmark)
else
return String(str)
end
end

function show(io::IO, t::AbstractDict{K,V}) where V where K
recur_io = IOContext(io, :SHOWN_SET => t,
:typeinfo => eltype(t))
Expand Down
56 changes: 53 additions & 3 deletions base/docs/basedocs.jl
Expand Up @@ -279,6 +279,53 @@ julia> z
"""
kw"global"

"""
for outer
Reuse an existing local variable for iteration in a `for` loop.
See the [manual section on variable scoping](@ref scope-of-variables) for more information.
See also [`for`](@ref).
# Examples
```jldoctest
julia> function f()
i = 0
for i = 1:3
# empty
end
return i
end;
julia> f()
0
```
```jldoctest
julia> function f()
i = 0
for outer i = 1:3
# empty
end
return i
end;
julia> f()
3
```
```jldoctest
julia> i = 0 # global variable
for outer i = 1:3
end
ERROR: syntax: no outer local variable declaration exists for "for outer"
[...]
```
"""
kw"outer"

"""
' '
Expand Down Expand Up @@ -834,6 +881,10 @@ kw"?", kw"?:"
`for` loops repeatedly evaluate a block of statements while
iterating over a sequence of values.
The iteration variable is always a new variable, even if a variable of the same name
exists in the enclosing scope.
Use [`outer`](@ref) to reuse an existing local variable for iteration.
# Examples
```jldoctest
julia> for i in [1, 4, 0]
Expand Down Expand Up @@ -1970,9 +2021,8 @@ julia> eval(:x)
`Symbol`s can also be constructed from strings or other values by calling the
constructor `Symbol(x...)`.
`Symbol`s are immutable and should be compared using `===`.
The implementation re-uses the same object for all `Symbol`s with the same name,
so comparison tends to be efficient (it can just compare pointers).
`Symbol`s are immutable and their implementation re-uses the same object for all `Symbol`s
with the same name.
Unlike strings, `Symbol`s are "atomic" or "scalar" entities that do not support
iteration over characters.
Expand Down
3 changes: 1 addition & 2 deletions base/errorshow.jl
Expand Up @@ -272,8 +272,7 @@ function showerror(io::IO, ex::MethodError)
if !isempty(kwargs)
print(io, "; ")
for (i, (k, v)) in enumerate(kwargs)
print(io, k, "=")
show(IOContext(io, :limit => true), v)
print(io, k, "::", typeof(v))
i == length(kwargs)::Int || print(io, ", ")
end
end
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Expand Up @@ -882,6 +882,7 @@ export
basename,
dirname,
expanduser,
contractuser,
homedir,
isabspath,
isdirpath,
Expand Down
2 changes: 1 addition & 1 deletion base/int.jl
Expand Up @@ -387,7 +387,7 @@ julia> string(bswap(1), base = 2)
"100000000000000000000000000000000000000000000000000000000"
```
"""
bswap(x::Union{Int8, UInt8}) = x
bswap(x::Union{Int8, UInt8, Bool}) = x
bswap(x::Union{Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128}) =
bswap_int(x)

Expand Down
3 changes: 3 additions & 0 deletions base/multidimensional.jl
Expand Up @@ -1546,6 +1546,9 @@ end
end
end

isassigned(a::AbstractArray, i::CartesianIndex) = isassigned(a, Tuple(i)...)
isassigned(a::AbstractArray, i::Union{Integer, CartesianIndex}...) = isassigned(a, CartesianIndex(i))

## permutedims

## Permute array dims ##
Expand Down
12 changes: 12 additions & 0 deletions base/multimedia.jl
Expand Up @@ -104,6 +104,18 @@ for that case. If a type benefits from custom human-readable output though,
`show(::IO, ::MIME"text/plain", ::T)` should be defined. For example, the `Day` type uses
`1 day` as the output for the `text/plain` MIME type, and `Day(1)` as the output of 2-argument `show`.
# Examples
```jldoctest
julia> struct Day
n::Int
end
julia> Base.show(io::IO, ::MIME"text/plain", d::Day) = print(io, d.n, " day")
julia> Day(1)
1 day
```
Container types generally implement 3-argument `show` by calling `show(io, MIME"text/plain"(), x)`
for elements `x`, with `:compact => true` set in an [`IOContext`](@ref) passed as the first argument.
"""
Expand Down
4 changes: 4 additions & 0 deletions base/path.jl
Expand Up @@ -516,13 +516,17 @@ end
expanduser(path::AbstractString) -> AbstractString
On Unix systems, replace a tilde character at the start of a path with the current user's home directory.
See also: [`contractuser`](@ref).
"""
expanduser(path::AbstractString)

"""
contractuser(path::AbstractString) -> AbstractString
On Unix systems, if the path starts with `homedir()`, replace it with a tilde character.
See also: [`expanduser`](@ref).
"""
contractuser(path::AbstractString)

Expand Down
10 changes: 5 additions & 5 deletions base/range.jl
Expand Up @@ -762,13 +762,13 @@ let bigints = Union{Int, UInt, Int64, UInt64, Int128, UInt128}
# therefore still be valid (if the result is representable at all)
# n.b. !(s isa T)
if s isa Unsigned || -1 <= s <= 1 || s == -s
a = div(diff, s)
a = div(diff, s) % T
elseif s < 0
a = div(unsigned(-diff), -s) % typeof(diff)
a = div(unsigned(-diff), -s) % T
else
a = div(unsigned(diff), s) % typeof(diff)
a = div(unsigned(diff), s) % T
end
return Integer(a) + oneunit(a)
return a + oneunit(T)
end
function checked_length(r::OrdinalRange{T}) where T<:bigints
s = step(r)
Expand All @@ -786,7 +786,7 @@ let bigints = Union{Int, UInt, Int64, UInt64, Int128, UInt128}
else
a = div(checked_sub(start, stop), -s)
end
return checked_add(a, oneunit(a))
return checked_add(convert(T, a), oneunit(T))
end
end

Expand Down
9 changes: 6 additions & 3 deletions base/regex.jl
Expand Up @@ -11,6 +11,9 @@ const DEFAULT_MATCH_OPTS = PCRE.NO_UTF_CHECK
An abstract type representing any sort of pattern matching expression
(typically a regular expression). `AbstractPattern` objects can be used to
match strings with [`match`](@ref).
!!! compat "Julia 1.6"
This type is available in Julia 1.6 and later.
"""
abstract type AbstractPattern end

Expand Down Expand Up @@ -266,7 +269,7 @@ function occursin(r::Regex, s::AbstractString; offset::Integer=0)
return PCRE.exec_r(r.regex, String(s), offset, r.match_options)
end

function occursin(r::Regex, s::SubString; offset::Integer=0)
function occursin(r::Regex, s::SubString{String}; offset::Integer=0)
compile(r)
return PCRE.exec_r(r.regex, s, offset, r.match_options)
end
Expand Down Expand Up @@ -298,7 +301,7 @@ function startswith(s::AbstractString, r::Regex)
return PCRE.exec_r(r.regex, String(s), 0, r.match_options | PCRE.ANCHORED)
end

function startswith(s::SubString, r::Regex)
function startswith(s::SubString{String}, r::Regex)
compile(r)
return PCRE.exec_r(r.regex, s, 0, r.match_options | PCRE.ANCHORED)
end
Expand Down Expand Up @@ -330,7 +333,7 @@ function endswith(s::AbstractString, r::Regex)
return PCRE.exec_r(r.regex, String(s), 0, r.match_options | PCRE.ENDANCHORED)
end

function endswith(s::SubString, r::Regex)
function endswith(s::SubString{String}, r::Regex)
compile(r)
return PCRE.exec_r(r.regex, s, 0, r.match_options | PCRE.ENDANCHORED)
end
Expand Down
50 changes: 42 additions & 8 deletions base/set.jl
Expand Up @@ -384,20 +384,28 @@ See also: [`unique`](@ref), [`issorted`](@ref), [`allequal`](@ref).
# Examples
```jldoctest
julia> a = [1; 2; 3]
3-element Vector{Int64}:
1
2
3
julia> allunique(a)
julia> allunique([1, 2, 3])
true
julia> allunique([a, a])
julia> allunique([1, 2, 1, 2])
false
julia> allunique(Real[1, 1.0, 2])
false
julia> allunique([NaN, 2.0, NaN, 4.0])
false
```
"""
function allunique(C)
if haslength(C)
length(C) < 2 && return true
length(C) < 32 && return _indexed_allunique(collect(C))
end
return _hashed_allunique(C)
end

function _hashed_allunique(C)
seen = Set{eltype(C)}()
x = iterate(C)
if haslength(C) && length(C) > 1000
Expand All @@ -420,6 +428,32 @@ allunique(::Union{AbstractSet,AbstractDict}) = true

allunique(r::AbstractRange) = !iszero(step(r)) || length(r) <= 1

allunique(A::StridedArray) = length(A) < 32 ? _indexed_allunique(A) : _hashed_allunique(A)

function _indexed_allunique(A)
length(A) < 2 && return true
iter = eachindex(A)
I = iterate(iter)
while I !== nothing
i, s = I
a = A[i]
for j in Iterators.rest(iter, s)
isequal(a, @inbounds A[j]) && return false
end
I = iterate(iter, s)
end
return true
end

function allunique(t::Tuple)
length(t) < 32 || return _hashed_allunique(t)
a = afoldl(true, tail(t)...) do b, x
b & !isequal(first(t), x)
end
return a && allunique(tail(t))
end
allunique(t::Tuple{}) = true

"""
allequal(itr) -> Bool
Expand Down

0 comments on commit 0010aaf

Please sign in to comment.