Skip to content

Commit

Permalink
Backport keepat! (#770) and README update (#772) to 3.44.0 (#773)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins authored May 24, 2022
1 parent 3ae185f commit 95ea27f
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Compat"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "3.43.0"
version = "3.44.0"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
30 changes: 26 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,26 @@ To use Compat in your Julia package, add it as a dependency of your package usin
```julia
pkg> add Compat
```
and add a [version specifier line](https://julialang.github.io/Pkg.jl/v1/compatibility/#Version-specifier-format-1) such as `Compat = "2.2, 3"` in the `[compat]`section of the `Project.toml` file
and add a [version specifier line](https://julialang.github.io/Pkg.jl/v1/compatibility/#Version-specifier-format-1)
such as `Compat = "3.22, 4"` in the `[compat]`section of the `Project.toml` file
in your package directory. The version in the latter should be the minimum
version that supports all needed fatures (see list below), and (if applicable)
any newer major versions verified to be compatible. Then, in your package,
shortly after the `module` statement a line like this:
version that supports all needed features (see list below). Note that Compat v4
requires Julia v1.6, but some features may have been backported to Compat v3
(see the
[feature list of the release-3 branch](https://github.com/JuliaLang/Compat.jl/tree/release-3#supported-features)).
If you require any of those backported features, be sure to specify the correct
compatibility in your `Project.toml`. E.g. if the feature from Compat v4.x has
been backported to v3.y, use `Compat = 3.y, 4.x`. If you use a feature that had
originally been added in Compat v3 (e.g. in 3.x), don't forget to also declare
compatibility with v4 with `Compat = 3.x, 4` (unless you use one the very few
things that got removed between Compat v3 and v4, which you most probably
don't).

To minimize dependency conflicts between packages it is recommended that packages
allow for both appropriate v4 and v3 versions of Compat.jl in their Project.toml
(except for rare cases of packages that support only v4 or v3 version of Compat.jl).

Then, in your package, shortly after the `module` statement a line like this:

```julia
using Compat
Expand Down Expand Up @@ -54,6 +69,9 @@ changes in `julia`.

## Supported features

* `keepat!` removes the items at all the indices which are not given and returns
the modified source ([#36229], [#42351]). (since Compat 3.44.0, 4.1.0)

* `@compat (; a, b) = (; c=1, b=2, a=3)` supports property descturing assignment syntax ([#39285]).

* `allequal`, the opposite of `allunique` ([#43354]). (since Compat 3.42.0)
Expand Down Expand Up @@ -296,3 +314,7 @@ Note that you should specify the correct minimum version for `Compat` in the
[#41328]: https://github.com/JuliaLang/julia/pull/41328
[#43354]: https://github.com/JuliaLang/julia/pull/43354
[#39285]: https://github.com/JuliaLang/julia/pull/39285
[#29901]: https://github.com/JuliaLang/julia/issues/29901
[#36229]: https://github.com/JuliaLang/julia/issues/36229
[#39245]: https://github.com/JuliaLang/julia/issues/39245
[#42351]: https://github.com/JuliaLang/julia/issues/42351
59 changes: 50 additions & 9 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1238,9 +1238,9 @@ if VERSION < v"1.8.0-DEV.300"
end

# https://github.com/JuliaLang/julia/pull/39245
if VERSION < v"1.8.0-DEV.487"
if VERSION < v"1.8.0-DEV.487"
export eachsplit

"""
eachsplit(str::AbstractString, dlm; limit::Integer=0)
eachsplit(str::AbstractString; limit::Integer=0)
Expand All @@ -1249,14 +1249,14 @@ if VERSION < v"1.8.0-DEV.487"
substrings. `dlm` can be any of the formats allowed by [`findnext`](@ref)'s first argument
(i.e. as a string, regular expression or a function), or as a single character or collection
of characters.
If `dlm` is omitted, it defaults to [`isspace`](@ref).
The iterator will return a maximum of `limit` results if the keyword argument is supplied.
The default of `limit=0` implies no maximum.
See also [`split`](@ref).
# Examples
```julia
julia> a = "Ma.rch"
Expand All @@ -1268,7 +1268,7 @@ if VERSION < v"1.8.0-DEV.487"
```
"""
function eachsplit end

struct SplitIterator{S<:AbstractString,F}
str::S
splitter::F
Expand All @@ -1278,13 +1278,13 @@ if VERSION < v"1.8.0-DEV.487"

Base.eltype(::Type{<:SplitIterator}) = SubString
Base.IteratorSize(::Type{<:SplitIterator}) = Base.SizeUnknown()

function Base.iterate(iter::SplitIterator, (i, k, n)=(firstindex(iter.str), firstindex(iter.str), 0))
i - 1 > ncodeunits(iter.str)::Int && return nothing
r = findnext(iter.splitter, iter.str, k)::Union{Nothing,Int,UnitRange{Int}}
while r !== nothing && n != iter.limit - 1 && first(r) <= ncodeunits(iter.str)
r = r::Union{Int,UnitRange{Int}} #commit dcc2182db228935fe97d03a44ae3b6889e40c542
#follow #39245, improve inferrability of iterate(::SplitIterator)
#follow #39245, improve inferrability of iterate(::SplitIterator)
#Somehow type constraints from the complex `while` condition don't
#propagate to the `while` body.
j, k = first(r), nextind(iter.str, last(r))::Int
Expand Down Expand Up @@ -1323,6 +1323,47 @@ if VERSION < v"1.8.0-DEV.1494" # 98e60ffb11ee431e462b092b48a31a1204bd263d
allequal(r::AbstractRange) = iszero(step(r)) || length(r) <= 1
end

# This function is available as of Julia 1.7.
@static if !isdefined(Base, :keepat!)
export keepat!

keepat!(B::BitVector, inds) = _keepat!(B, inds)
keepat!(B::BitVector, inds::AbstractVector{Bool}) = _keepat!(B, inds)
keepat!(a::Vector, inds) = _keepat!(a, inds)
keepat!(a::Vector, m::AbstractVector{Bool}) = _keepat!(a, m)

function _keepat!(a::AbstractVector, inds)
local prev
i = firstindex(a)
for k in inds
if @isdefined(prev)
prev < k || throw(ArgumentError("indices must be unique and sorted"))
end
ak = a[k] # must happen even when i==k for bounds checking
if i != k
@inbounds a[i] = ak # k > i, so a[i] is inbounds
end
prev = k
i = nextind(a, i)
end
deleteat!(a, i:lastindex(a))
return a
end

function _keepat!(a::AbstractVector, m::AbstractVector{Bool})
length(m) == length(a) || throw(BoundsError(a, m))
j = firstindex(a)
for i in eachindex(a, m)
@inbounds begin
if m[i]
i == j || (a[j] = a[i])
j = nextind(a, j)
end
end
end
deleteat!(a, j:lastindex(a))
end
end

include("iterators.jl")
include("deprecated.jl")
Expand Down
42 changes: 40 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1316,8 +1316,8 @@ end
#=
cmcaine commented on Sep 8, 2021
This PR implements split with eachsplit and uses eachsplit in a few other places in Base,
so it's kind of already covered by the existing tests.
This PR implements split with eachsplit and uses eachsplit in a few other places in Base,
so it's kind of already covered by the existing tests.
Not sure it needs any more?
so, these are the Base.split tests, but replacing split with eachsplit |> collect
Expand Down Expand Up @@ -1413,3 +1413,41 @@ end
@test !allequal(LinRange(1, 2, 2))
end

@testset "keepat!" begin
a = [1:6;]
@test a === keepat!(a, 1:5)
@test a == 1:5
@test keepat!(a, [2, 4]) == [2, 4]
@test isempty(keepat!(a, []))

a = [1:6;]
@test_throws BoundsError keepat!(a, 1:10) # make sure this is not a no-op
@test_throws BoundsError keepat!(a, 2:10)
@test_throws ArgumentError keepat!(a, [2, 4, 3])

b = BitVector([1, 1, 1, 0, 0])
@test b === keepat!(b, 1:5)
@test b == [1, 1, 1, 0, 0]
@test keepat!(b, 2:4) == [1, 1, 0]
@test_throws BoundsError keepat!(a, -1:10)
@test_throws ArgumentError keepat!(a, [2, 1])
@test isempty(keepat!(a, []))

# Vector
a = Vector(1:10)
keepat!(a, [falses(5); trues(5)])
@test a == 6:10
@test_throws BoundsError keepat!(a, trues(1))
@test_throws BoundsError keepat!(a, trues(11))

# BitVector
ba = rand(10) .> 0.5
@test isa(ba, BitArray)
keepat!(ba, ba)
@test all(ba)

# empty array
ea = []
keepat!(ea, Bool[])
@test isempty(ea)
end

2 comments on commit 95ea27f

@martinholters
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/60936

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v3.44.0 -m "<description of version>" 95ea27f031e699aac702dcb4943bac9160cd5cc4
git push origin v3.44.0

Please sign in to comment.