Move elements between vectors

what is an efficient way to move an element from one position in one vector to another position in another vector?
And in particular from the first position of the transferring vector to the last position of the receiving vector?

dest[end] = src[begin]

I better specify the request.

If the two vectors are S (recipient) and cc (donor) the solution must be something equivalent to

      push!(S,cc[1])
      deleteat!(cc,1)

push!(S, popfirst!(cc))

4 Likes

I was thinking of something even more low-level.
I also used the popfirst function. But it’s no better than deleteat !.

julia> function isolapop(c, cidx, CI)
           S=CartesianIndex{2}[]
           ci=[c]
           while !isempty(ci)
               cc=copy(ci)
               for i in ci
                   if !∈(i, S)
                       push!(S,i)
                       popfirst!(cc)
                       #deleteat!(cc,1)
                   end
                   for n in CI
                       x=i+n
                       if (x ∈ cidx) &&  !∈(x,cc) &&  !∈(x,ci) &&  !∈(x,S)  

                           push!(cc,x)
                       end
                   end
               end
               ci=cc
           end
          S
       end
isolapop (generic function with 1 method)

julia> function isoladelat(c, cidx, CI)
           S=CartesianIndex{2}[]
           ci=[c]
           while !isempty(ci)
               cc=copy(ci)
               for i in ci
                   if !∈(i, S)
                       push!(S,i)
                       #popfirst!(cc)
                       deleteat!(cc,1)
                   end
                   for n in CI
                       x=i+n
                       if (x ∈ cidx) &&  !∈(x,cc) &&  !∈(x,ci) &&  !∈(x,S)  

                           push!(cc,x)
                       end
                   end
               end
               ci=cc
           end
          S
       end
isoladelat (generic function with 1 method)


julia> @btime isolapop(cidx[1],cidx, CI)
  758.929 ns (14 allocations: 1.84 KiB)
10-element Vector{CartesianIndex{2}}:
 CartesianIndex(1, 1)
...
 CartesianIndex(6, 2)

julia> @btime isoladelat(cidx[1],cidx, CI)
  762.617 ns (14 allocations: 1.84 KiB)
10-element Vector{CartesianIndex{2}}:
 CartesianIndex(1, 1)
 CartesianIndex(1, 2)
 ...
 CartesianIndex(6, 2)

The problem stems from this discussion … where I was trying to modify various aspects of the functions to make them more efficient.