Questions tagged [swap]

Changing position of two items.

Act of rearranging the locations of two items such that the first item now occupies the place of the second and the second item now occupies the place of the first.

2503 questions
16
votes
1 answer

Swapping with rvalues

Suppose I want swap that works on rvalues, and don't want to write 4 versions for all combinations of rvalue/lvalue references (rvalue/rvalue version is kinda pointless but it doesn't hurt). I came up with this: template
yuri kilochek
  • 12,709
  • 2
  • 32
  • 59
15
votes
3 answers

Swap trick: a=b+(b=a)*0;

a=b+(b=a)*0; This sentence can swap the value between a and b. I've tried it with C# and it works. But I just don't konw how it works. e.g. a = 1, b = 2 I list the steps of it as below: b = a -> a = 1, b = 1 b * 0 -> a = 1, b = 1 b + 0 -> a = 1, b…
cindy
  • 213
  • 3
  • 10
15
votes
8 answers

Can I swap colors in image using GD library in PHP?

I got the image like this (it's a graph): (source: kitconet.com) I want to change the colours, so the white is black, the graph line is light blue, etc.. is it possible to achieve with GD and PHP?
Skuta
  • 5,830
  • 27
  • 60
  • 68
15
votes
2 answers

Swapping elements in a Common Lisp list

Is there a Common Lisp function that will swap two elements in a list given their indices and return the modified list?
Matthew Piziak
  • 3,430
  • 4
  • 35
  • 49
15
votes
2 answers

Pandas swap columns based on condition

I have a pandas dataframe like the following: Col1 Col2 Col3 0 A 7 NaN 1 B 16 NaN 1 B 16 15 What I want to do is to swap Col2 with Col3 where the value of Col3 is NaN. Based on other posts and answers on SO, I have…
dagg3r
  • 341
  • 3
  • 5
  • 12
15
votes
2 answers

Swapping values between two columns using data.table

I have been breaking my head over translating this question to a data.table solution. (to keep it simple I'll use the same data set) When V2 == "b I want to swap the columns between V1 <-> V3. dt <- data.table(V1=c(1,2,4), V2=c("a","a","b"),…
Bas
  • 1,066
  • 1
  • 10
  • 28
15
votes
7 answers

Swap arrays by using pointers in C++

I have two arrays of pointers to doubles that I need to swap. Rather than just copy the data within the arrays, it would be more efficient just to swap the pointers to the arrays. I was always under the impression that array names were essentially…
thornate
  • 4,902
  • 9
  • 39
  • 43
15
votes
7 answers

Overloading global swap for user-defined type

The C++ standard prohibits declaring types or defining anything in namespace std, but it does allow you to specialize standard STL templates for user-defined types. Usually, when I want to specialize std::swap for my own custom templated type, I…
Channel72
  • 183
  • 1
  • 5
15
votes
5 answers

Pythonic Swap of 2 lists elements

I found that I have to perform a swap in python and I write something like this: arr[first], arr[second] = arr[second], arr[first] I suppose this is not so pythonic. Does somebody know how to do a swap in python more elegant? EDIT: I think another…
jagttt
  • 1,020
  • 1
  • 12
  • 25
14
votes
9 answers

C - fastest method to swap two memory blocks of equal size?

What is the fastest way to swap two non-overlapping memory areas of equal size? Say, I need to swap (t_Some *a) with (t_Some *b). Considering space-time trade-off, will increased temporary space improve the speed? For example, (char *tmp) vs (int…
psihodelia
  • 29,566
  • 35
  • 108
  • 157
14
votes
1 answer

C++: efficient swap() when using a custom allocator

It seems to be the month of C++ templates for me... I have a SecureString. SecureString looks just like a std::string, except it uses a custom allocator which zeroizes on destruction: class SecureString { public: typedef std::basic_string< char,…
jww
  • 97,681
  • 90
  • 411
  • 885
14
votes
6 answers

How to swap keys with values in array?

I have array like: array( 0 => 'a', 1 => 'b', 2 => 'c' ); I need to convert it to: array( 'a', 'b', 'c' ); What's the fastest way to swap keys with values?
daGrevis
  • 21,014
  • 37
  • 100
  • 139
14
votes
5 answers

Making swap faster, easier to use and exception-safe

I could not sleep last night and started thinking about std::swap. Here is the familiar C++98 version: template void swap(T& a, T& b) { T c(a); a = b; b = c; } If a user-defined class Foo uses external ressources, this is…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
14
votes
2 answers

Why is this statement not working in java x ^= y ^= x ^= y;

int x=1; int y=2; x ^= y ^= x ^= y; I am expecting the values to be swapped.But it gives x=0 and y=1. when i tried in C language it gives the correct result.
sadananda salam
  • 845
  • 1
  • 7
  • 12
14
votes
5 answers

What's a good and functional way to swap collection elements in Scala?

In a project of mine one common use case keeps coming up. At some point I've got a sorted collection of some kind (List, Seq, etc... doesn't matter) and one element of this collection. What I want to do is to swap the given element with it's…
Andreas Eisele
  • 743
  • 1
  • 9
  • 19