Skip to content

Commit

Permalink
Merge pull request #7 from JuliaArrays/teh/v1.0
Browse files Browse the repository at this point in the history
Get package working on 0.7 and 1.0
  • Loading branch information
timholy authored Aug 14, 2018
2 parents f2ad24a + ea0ea54 commit 1e8759b
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 99 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ os:
- linux
- osx
julia:
- 0.6
- 0.7
- 1.0
- nightly
notifications:
email: false
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
[![codecov.io](http://codecov.io/github/JuliaArrays/CatIndices.jl/coverage.svg?branch=master)](http://codecov.io/github/JuliaArrays/CatIndices.jl?branch=master)

A Julia package for concatenating, growing, and shrinking arrays in
ways that allow control over the resulting indices.
ways that allow control over the resulting axes.

# Usage

## BidirectionalVector

These vectors can grow or shrink from either end, and the indices
These vectors can grow or shrink from either end, and the axes
update correspondingly. In this demo, pay careful attention to the
indices at each step:
axes at each step:

```julia
julia> using CatIndices
Expand Down Expand Up @@ -46,13 +46,13 @@ CatIndices.BidirectionalVector{Float64} with indices CatIndices.URange(-2,5):
julia> pop!(v)
0.28257294456774673

julia> indices(v)
julia> axes(v)
(CatIndices.URange(-2,4),)

julia> shift!(v)
julia> popfirst!(v)
0.9929020233076613

julia> indices(v)
julia> axes(v)
(CatIndices.URange(-1,4),)
```

Expand Down
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
julia 0.6
julia 0.7
OffsetArrays
CustomUnitRanges
40 changes: 24 additions & 16 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
environment:
matrix:
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"
- julia_version: 0.7
- julia_version: 1
- julia_version: nightly

platform:
- x86 # 32-bit
- x64 # 64-bit

# # Uncomment the following lines to allow failures on nightly julia
# # (tests will run but not make your overall status red)
# matrix:
# allow_failures:
# - julia_version: nightly

branches:
only:
Expand All @@ -17,19 +26,18 @@ notifications:
on_build_status_changed: false

install:
- ps: "[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12"
# Download most recent Julia Windows binary
- ps: (new-object net.webclient).DownloadFile(
$env:JULIA_URL,
"C:\projects\julia-binary.exe")
# Run installer silently, output to C:\projects\julia
- C:\projects\julia-binary.exe /S /D=C:\projects\julia
- ps: iex ((new-object net.webclient).DownloadString("https://raw.githubusercontent.com/JuliaCI/Appveyor.jl/version-1/bin/install.ps1"))

build_script:
# Need to convert from shallow to complete for Pkg.clone to work
- IF EXIST .git\shallow (git fetch --unshallow)
- C:\projects\julia\bin\julia -e "versioninfo();
Pkg.clone(pwd(), \"CatIndices\"); Pkg.build(\"CatIndices\")"
- echo "%JL_BUILD_SCRIPT%"
- C:\julia\bin\julia -e "%JL_BUILD_SCRIPT%"

test_script:
- C:\projects\julia\bin\julia -e "Pkg.test(\"CatIndices\")"
- echo "%JL_TEST_SCRIPT%"
- C:\julia\bin\julia -e "%JL_TEST_SCRIPT%"

# # Uncomment to support code coverage upload. Should only be enabled for packages
# # which would have coverage gaps without running on Windows
# on_success:
# - echo "%JL_CODECOV_SCRIPT%"
# - C:\julia\bin\julia -e "%JL_CODECOV_SCRIPT%"
2 changes: 0 additions & 2 deletions src/CatIndices.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
__precompile__()

module CatIndices

export BidirectionalVector, PinIndices, deletetail!, deletehead!
Expand Down
33 changes: 16 additions & 17 deletions src/bidirectional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,28 @@ mutable struct BidirectionalVector{T} <: AbstractVector{T}
end
BidirectionalVector(v::AbstractVector{T}, inds::AbstractUnitRange) where {T} =
BidirectionalVector(copyelts(v), first(inds)-1)
BidirectionalVector(v::AbstractVector) = BidirectionalVector(v, Base.indices1(v))
BidirectionalVector(v::AbstractVector) = BidirectionalVector(v, Base.axes1(v))

# copies but doesn't preserve the indices
# copies but doesn't preserve the axes
function copyelts(v::AbstractVector{T}) where T
inds = Base.indices1(v)
inds = Base.axes1(v)
n = length(inds)
dest = Array{T}(n)
dest = Array{T}(undef, n)
for (vel, j) in zip(v, 1:n)
dest[j] = vel
end
dest
end

# Don't implement size or length
Base.indices1(v::BidirectionalVector) = URange(1+v.offset, length(v.data)+v.offset)
Base.indices( v::BidirectionalVector) = (Base.indices1(v),)
Base.axes1(v::BidirectionalVector) = URange(1+v.offset, length(v.data)+v.offset)
Base.axes( v::BidirectionalVector) = (Base.axes1(v),)
Base.size( v::BidirectionalVector) = (length(v),)
Base.length(v::BidirectionalVector) = length(v.data)

function Base.similar(v::AbstractArray, T::Type, inds::Tuple{URange})
inds1 = inds[1]
n = length(inds1)
BidirectionalVector(Array{T}(n), first(inds1)-1)
BidirectionalVector(Array{T}(undef, n), first(inds1)-1)
end

function Base.similar(f::Union{Function,Type}, inds::Tuple{URange})
Expand All @@ -52,22 +53,22 @@ Base.push!(v::BidirectionalVector, x) = (push!(v.data, x); v)
Base.pop!(v::BidirectionalVector) = pop!(v.data)
Base.append!(v::BidirectionalVector, collection2) = (append!(v.data, collection2); v)
function Base.prepend!(v::BidirectionalVector, collection2)
v.offset -= _length(collection2)
v.offset -= length(collection2)
prepend!(v.data, collection2)
v
end
function Base.shift!(v::BidirectionalVector)
function Base.popfirst!(v::BidirectionalVector)
v.offset += 1
shift!(v.data)
popfirst!(v.data)
end
function Base.unshift!(v::BidirectionalVector, x)
function Base.pushfirst!(v::BidirectionalVector, x)
v.offset -= 1
unshift!(v.data, x)
pushfirst!(v.data, x)
v
end
@inline function Base.unshift!(v::BidirectionalVector, y...)
@inline function Base.pushfirst!(v::BidirectionalVector, y...)
v.offset -= length(y)
unshift!(v.data, y...)
pushfirst!(v.data, y...)
v
end

Expand All @@ -81,5 +82,3 @@ function deletehead!(v::BidirectionalVector, n::Integer)
deleteat!(v.data, 1:n)
v
end

_length(a::AbstractArray) = length(linearindices(a))
4 changes: 2 additions & 2 deletions src/cat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ function Base.vcat(X::Union{AbstractVector,PinIndices}...)
l = f = 0
for x in X
if is_pinned(x)
f = first(indices(unpin(x),1))
f = first(axes(unpin(x),1))
break
end
l += _length(x)
l += length(x)
end
OffsetArray(c, (f-l-1,))
end
108 changes: 54 additions & 54 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
using CatIndices
using Base.Test
using Test

### BidirectionalVector
@testset "CatIndices" begin
function checkvals(v)
for i in axes(v,1)
@test v[i] == i
end
nothing
end
@testset "BidirectionalVector" begin
v = BidirectionalVector(1:3)
@test axes(v,1) == 1:3
checkvals(v)
@test isa(push!(v, 4), BidirectionalVector)
@test axes(v,1) == 1:4
checkvals(v)
@test isa(append!(v, 5:11), BidirectionalVector)
@test axes(v,1) == 1:11
checkvals(v)
@test pop!(v) == 11
@test axes(v,1) == 1:10
checkvals(v)
@test isa(deletetail!(v, 2), BidirectionalVector)
@test axes(v,1) == 1:8
checkvals(v)
@test popfirst!(v) == 1
@test axes(v,1) == 2:8
checkvals(v)
@test isa(pushfirst!(v, 0, 1), BidirectionalVector)
@test isa(pushfirst!(v, -1), BidirectionalVector)
@test axes(v,1) == -1:8
checkvals(v)
@test isa(prepend!(v, -5:-2), BidirectionalVector)
@test axes(v,1) == -5:8
checkvals(v)
@test isa(deletehead!(v,2), BidirectionalVector)
@test axes(v,1) == -3:8
checkvals(v)

v[0] = 200
@test v[0] == 200

@test axes(similar(v)) === axes(v)
@test axes(similar(v, Float64)) === axes(v)
@test axes(similar(v, Float64, 3)) === (Base.OneTo(3),)
a = similar(Array{Float64}, axes(v))
@test isa(a, BidirectionalVector)
@test axes(a) === axes(v)
end

function checkvals(v)
for i in indices(v,1)
@test v[i] == i
@testset "vcat" begin
@test !CatIndices.is_pinned(1:3)
@test CatIndices.is_pinned(PinIndices(1:3))
v = vcat(1:3, PinIndices(4:5), 6:10)
@test axes(v,1) == -2:7
@test_throws ArgumentError vcat(1:3, PinIndices(4:5), PinIndices(6:10))
end
nothing
end

v = BidirectionalVector(1:3)
@test indices(v,1) == 1:3
checkvals(v)
@test isa(push!(v, 4), BidirectionalVector)
@test indices(v,1) == 1:4
checkvals(v)
@test isa(append!(v, 5:11), BidirectionalVector)
@test indices(v,1) == 1:11
checkvals(v)
@test pop!(v) == 11
@test indices(v,1) == 1:10
checkvals(v)
@test isa(deletetail!(v, 2), BidirectionalVector)
@test indices(v,1) == 1:8
checkvals(v)
@test shift!(v) == 1
@test indices(v,1) == 2:8
checkvals(v)
@test isa(unshift!(v, 0, 1), BidirectionalVector)
@test isa(unshift!(v, -1), BidirectionalVector)
@test indices(v,1) == -1:8
checkvals(v)
@test isa(prepend!(v, -5:-2), BidirectionalVector)
@test indices(v,1) == -5:8
checkvals(v)
@test isa(deletehead!(v,2), BidirectionalVector)
@test indices(v,1) == -3:8
checkvals(v)

v[0] = 200
@test v[0] == 200

@test indices(similar(v)) === indices(v)
@test indices(similar(v, Float64)) === indices(v)
@test indices(similar(v, Float64, 3)) === (Base.OneTo(3),)
a = similar(Array{Float64}, indices(v))
@test isa(a, BidirectionalVector)
@test indices(a) === indices(v)

### vcat

@test !CatIndices.is_pinned(1:3)
@test CatIndices.is_pinned(PinIndices(1:3))
v = vcat(1:3, PinIndices(4:5), 6:10)
@test indices(v,1) == -2:7
@test_throws ArgumentError vcat(1:3, PinIndices(4:5), PinIndices(6:10))

nothing

0 comments on commit 1e8759b

Please sign in to comment.