# Reversed Arrays

**URL:** <https://discourse.julialang.org/t/reversed-arrays/8860>\
**Category:** General Usage\
**Created:** [February 6, 2018, 6:20am UTC](https://discourse.julialang.org/t/reversed-arrays/8860 "2018-02-06T06:20:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [February 6, 2018, 6:20am UTC](https://discourse.julialang.org/t/reversed-arrays/8860/1 "2018-02-06T06:20:25Z")

</div>

Is there a package that implements an array type where the index is reversed? I have an array that I would normally reverse the order but I don’t really want to incur an extra allocation cost or performance hit. So it would be nice to reference `a[1]` for the `a[end]` element and vice versa.

I thought it would be simple enough to create a custom array type but don’t want to reinvent any wheels…

---

<div class="post-metadata">

**Author:** ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)\
**Post date:** [February 6, 2018, 6:25am UTC](https://discourse.julialang.org/t/reversed-arrays/8860/2 "2018-02-06T06:25:24Z")

</div>

Would this work?

```julia

julia> a = [1,2,3,4,5]
5-element Array{Int64,1}:
 1
 2
 3
 4
 5

julia> b = view(a, length(a):-1:1)
5-element view(::Array{Int64,1}, 5:-1:1) with eltype Int64:
 5
 4
 3
 2
 1

julia> b[end]
1

```

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [February 6, 2018, 6:55am UTC](https://discourse.julialang.org/t/reversed-arrays/8860/3 "2018-02-06T06:55:37Z")

</div>

Thanks @yurivish. Very nice solution! Never thought of that being so simple 😀

BTW, indexing performance seems to suffer though:

```julia
julia> @benchmark getindex($a, 3)
BenchmarkTools.Trial: 
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 1.755 ns (0.00% GC)
  median time: 1.762 ns (0.00% GC)
  mean time: 1.799 ns (0.00% GC)
  maximum time: 15.594 ns (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 1000

julia> @benchmark getindex($b, 3)
BenchmarkTools.Trial: 
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 15.855 ns (0.00% GC)
  median time: 16.240 ns (0.00% GC)
  mean time: 17.283 ns (0.00% GC)
  maximum time: 111.072 ns (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 998

```

So I just did this for fun:

```julia
julia> struct ReverseVector{T} <: AbstractVector{T} 
           ar::Vector{T}
       end

julia> Base.getindex(v::ReverseVector, i::Integer) = getindex(v.ar, length(v.ar)-i+1)

julia> Base.size(v::ReverseVector) = size(v.ar)

julia> v = ReverseVector([1,2,3,4,5])
5-element ReverseVector{Int64}:
 5
 4
 3
 2
 1

julia> @benchmark getindex($v, 3)
BenchmarkTools.Trial: 
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 2.102 ns (0.00% GC)
  median time: 2.451 ns (0.00% GC)
  mean time: 2.725 ns (0.00% GC)
  maximum time: 78.021 ns (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 1000

```

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [February 6, 2018, 8:42am UTC](https://discourse.julialang.org/t/reversed-arrays/8860/4 "2018-02-06T08:42:42Z")

</div>

FWIW, as your `ReverseVector` is immutable, you could store `length(v.ar)` as separate field to avoid a call everytime you index. Don’t know if/how much this really matters, though. Same for `size(v.ar)`.
