Swap array contents?

No don’t do that.

Yes, these are the correct solutions. As I said, binding swapping is exactly what corresponds to the c++ swap. The conceptual difference between c++ and julia is why such operations are needed in c++ to begin with.

Ok. If you think that too many dragons lurk in gc/array.c, or that no one else wants this, then I am inclined to believe you.

> As I said, binding swapping is exactly what corresponds to the c++ swap.

No. Having a function void do_something(std::vector<T>& A, std::vector<T>& B) that possibly swaps A and B (visible to the calling scope / everywhere, as an intended side-effect) is not an uncommon idiom in C++, and has nothing to do with conceptual differences, programming languages, or the difference between reference vs pointer vs non-bitstype-julia-object (which are different syntactic sugar for the same thing).

Example: I want to transform A, in a way that cannot be reasonably done in-place (e.g. apply_permutation!). So I take a similar(A) (hopefully stashed somewhere), assemble the permuted A, and then swap the two. Without swap I need to either copy back the memory (spurious memcopy(sizeof(A))) or change all code to add one level of indirection. In this specific case, the memcopy probably wins (I paid for one copy with bad access pattern already, may as well do another fast one, except if I want to keep the pre-permutation array).

Since julia arrays and C++ vectors are almost equivalent when viewed as a library from C, I thought that this operation should be possible.

Only when the only reference to the to-be-swapped arrays is held in the caller, then binding swap does the same (say A,B = do_something(A,B) ).

Upon reconsideration, the answer appears to be no, not supported; I can try to do it, but it is probably a bad idea.

Thanks to all of you, and there is probably no point in wasting any more of your time.

The array_resize_buffer function in array.c handles all these cases, no? If resize can be implemented, it seems like swap should be possible to implement reasonably efficiently too.

(Apparently, in C++11 swap is implemented on top of a lower-level move method.)

Whether it is desirable to have such a function is another matter, of course.

No, resize only replace buffer with new allocation, not with the buffer of another array. Also, resize only work with 1-d array.

And that’s exactly why I said it’s not needed in julia. move is even more clearly an operation that C++ need because of the copying semantics.

Please try to keep the conversation respectful and polite. Presumably not intended, but this comes off as rather rude.


The point is that resize can change the buffer pointer inside the array. If you can change it to point to a new allocation, why can’t you swap the buffer pointers of two Array{T,N} objects (with sufficient care for corner cases)?

I’m not arguing that we necessarily need such functionality, but I don’t really understand why it wouldn’t be possible on top of more-or-less the existing Array implementation.

Because the buffer that belongs to another array is not a new allocation. You can replace the buffer and that’s fine. You just can’t stick the buffer of another array in.

And also as I said, resize won’t work for non-1d array.