JuliaLang/julia — pull request #8809 by gummif (2014-10-25 02:48:56+00:00)
Title: RFC: Add native Array slices/views with shared data

Add method `arrayslice(A, pos, dim)` which returns a slice of type `Array` and is memory safe. The slices use contiguous parts of memory from A and can be any dimensional. The advantage for this special case compared to ArrayViews/SubArrays is performance and compatibility with any code for Arrays.

The C function `jl_slice_owned_array` is actually a general case of `jl_reshape_array`, so there is a question whether the code duplication is ok, or whether to make the reshape function call the slice function.

Memory check (same as with `reshape`):
```julia
n=int(1e8)
x=rand(n);
gc() # nothing happens
y=arrayslice(x,101,20)
gc() # nothing happens
x=1
gc() # nothing happens
y=1
gc() # memory usage reduces
```
Code example:
```julia
julia> x=rand(3,5)
3x5 Array{Float64,2}:
0.839011 0.954 0.185297 0.236349 0.0152856
0.0864201 0.561014 0.215385 0.219099 0.948724
0.708425 0.489022 0.20037 0.907779 0.399409

julia> y=arrayslice(x,7,(3,3))
3x3 Array{Float64,2}:
0.185297 0.236349 0.0152856
0.215385 0.219099 0.948724
0.20037 0.907779 0.399409

julia> I=eye(3);

julia> copy!(y,I);

julia> x
3x5 Array{Float64,2}:
0.839011 0.954 1.0 0.0 0.0
0.0864201 0.561014 0.0 1.0 0.0
0.708425 0.489022 0.0 0.0 1.0

julia> y=arrayslice(x,1,3)
3-element Array{Float64,1}:
0.839011
0.0864201
0.708425

julia> scale!(100,y);

julia> x
3x5 Array{Float64,2}:
83.9011 0.954 1.0 0.0 0.0
8.64201 0.561014 0.0 1.0 0.0
70.8425 0.489022 0.0 0.0 1.0
```