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
73
votes
2 answers

Is id = 1 - id atomic?

From page 291 of OCP Java SE 6 Programmer Practice Exams, question 25: public class Stone implements Runnable { static int id = 1; public void run() { id = 1 - id; if (id == 0) pick(); else …
Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
58
votes
2 answers

std::swap vs std::exchange vs swap operator

An implementation of std::swap might look like this: template void swap (T& a, T& b) { T c(std::move(a)); a=std::move(b); b=std::move(c); } template void swap (T (&a)[N], T (&b)[N]) { for (size_t i = 0; i
user1508519
58
votes
6 answers

iter_swap() versus swap() -- what's the difference?

MSDN says: swap should be used in preference to iter_swap, which was included in the C++ Standard for backward compatibility. But comp.std.c++ says: Most STL algorithms operate on iterator ranges. It therefore makes sense to use iter_swap when…
user541686
  • 205,094
  • 128
  • 528
  • 886
57
votes
10 answers

Why is a = (a+b) - (b=a) a bad choice for swapping two integers?

I stumbled into this code for swapping two integers without using a temporary variable or the use of bitwise operators. int main(){ int a=2,b=3; printf("a=%d,b=%d",a,b); a=(a+b)-(b=a); printf("\na=%d,b=%d",a,b); return 0; } But…
ashfaque
  • 522
  • 4
  • 8
56
votes
1 answer

Swapping elements in an NSMutableArray

Are there any special methods to make swapping elements in an NSMutableArray easier or more direct?
node ninja
  • 31,796
  • 59
  • 166
  • 254
56
votes
19 answers

How to write a basic swap function in Java

I am new to java. How to write the java equivalent of the following C code. void Swap(int *p, int *q) { int temp; temp = *p; *p = *q; *q = temp; }
Melinda
  • 577
  • 1
  • 5
  • 3
54
votes
13 answers

Is it possible to write swap method in Java?

Here is the question: write a method that swaps two variables. These two variables should be primitives. It doesn't need to be generic e.g. two int variables. Is there a way?!
Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
50
votes
8 answers

Java method to swap primitives

how do I make my swap function in java if there is no method by which we can pass by reference? Could somebody give me a code? swap(int a, int b) { int temp = a; a = b; b = temp; } But the changes wont be reflected back since java…
higherDefender
  • 1,551
  • 6
  • 23
  • 35
47
votes
7 answers

Can I tell Linux not to swap out a particular processes' memory?

Is there a way to tell Linux that it shouldn't swap out a particular processes' memory to disk? Its a Java app, so ideally I'm hoping for a way to do this from the command line. I'm aware that you can set the global swappiness to 0, but is this…
sanity
  • 35,347
  • 40
  • 135
  • 226
46
votes
1 answer

Swap two rows in a numpy array in python

How to swap xth and yth rows of the 2-D NumPy array? x & y are inputs provided by the user. Lets say x = 0 & y =2 , and the input array is as below: a = [[4 3 1] [5 7 0] [9 9 3] [8 2 4]] Expected Output : [[9 9 3] …
Manish
  • 649
  • 2
  • 7
  • 10
46
votes
3 answers

How do I swap tensor's axes in TensorFlow?

I have a tensor of shape (30, 116, 10), and I want to swap the first two dimensions, so that I have a tensor of shape (116, 30, 10) I saw that numpy as such a function implemented (np.swapaxes) and I searched for something similar in tensorflow but…
Alexis Rosuel
  • 563
  • 1
  • 5
  • 12
44
votes
6 answers

Why are there so many specializations of std::swap?

While looking at the documentation for std::swap, I see a lot of specializations. It looks like every STL container, as well as many other std facilities have a specialized swap. I thought with the aid of templates, we wouldn't need all of these…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
43
votes
3 answers

Linker performance related to swap space?

Sometimes it's handy to mock up something with a little C program that uses a big chunk of static memory. I noticed after changing to Fedora 15 the program took a long time to compile. We're talking 30s vs. 0.1s. Even more weird was that ld…
Rooke
  • 2,013
  • 3
  • 22
  • 34
41
votes
8 answers

Add swap memory with ansible

I'm working on a project where having swap memory on my servers is a needed to avoid some python long running processes to go out of memory and realized for the first time that my ubuntu vagrant boxes and AWS ubuntu instances didn't already have one…
gonz
  • 5,226
  • 5
  • 39
  • 54
41
votes
2 answers

Is std::swap(x, x) guaranteed to leave x unchanged?

This question is based on discussion below a recent blog post by Scott Meyers. It seems "obvious" that std::swap(x, x) should leave x unchanged in both C++98 and C++11, but I can't find any guarantee to that effect in either standard. C++98 defines…
KnowItAllWannabe
  • 12,972
  • 8
  • 50
  • 91