EzDevInfo.com

swap

Exchange rates library for PHP Swap by florianv

How to swap two variables in JavaScript

I have this two variables:

var a = 1,
    b = 2;

My question is how to swap them? Only this variables, not any objects.


Source: (StackOverflow)

Why does `basic_ios::swap` only do a partial swap?

C++11 ยง27.5.4.2/21:

void swap(basic_ios& rhs);

Effects: The states of *this and rhs shall be exchanged, except that rdbuf() shall return the same value as it returned before the function call, and rhs.rdbuf() shall return the same value as it returned before the function call.

What is this partial swapping useful for?

Can it cause trouble?


Source: (StackOverflow)

Advertisements

how to provide a swap function for my class?

What is the proper way to enable my swap in STL algorithms?

1) Member swap. Does std::swap use SFINAE trick to use the member swap.

2) Free standing swap in the same namespace.

3) Partial specialization of std::swap.

4) All of the above.

Thank you.

EDIT: Looks like I didn't word my question clearly. Basically, I have a template class and I need STL algos to use the (efficient) swap method I wrote for that class.


Source: (StackOverflow)

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?!


Source: (StackOverflow)

convert big endian to little endian in C [without using provided func] [closed]

I need to write a function to convert big endian to little endian in C. I can not use any library function.


Source: (StackOverflow)

Why does swapping values with XOR fail when using this compound form?

I found this code to swap two numbers without using a third variable, using the XOR ^ operator.

Code:

int i = 25;
int j = 36;
j ^= i;       
i ^= j;
j ^= i;

Console.WriteLine("i:" + i + " j:" + j);

//numbers Swapped correctly
//Output: i:36 j:25

Now I changed the above code to this equivalent code.

My Code:

int i = 25;
int j = 36;

j ^= i ^= j ^= i;   // I have changed to this equivalent (???).

Console.WriteLine("i:" + i + " j:" + j);

//Not Swapped correctly            
//Output: i:36 j:0

Now, I want to know, Why does my code give incorrect output?


Source: (StackOverflow)

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 
            release();
    }

    private static synchronized void pick() {
        System.out.print("P ");
        System.out.print("Q ");
    }

    private synchronized void release() {
        System.out.print("R ");
        System.out.print("S ");
    }

    public static void main(String[] args) {
        Stone st = new Stone();
        new Thread(st).start();
        new Thread(st).start();
    }
}

One of the answers is:

The output could be P Q P Q

I marked this answer as correct. My reasoning:

  1. We are starting two threads.
  2. First one enters run().
  3. According to JLS 15.26.1, it firstly evaluates 1 - id. Result is 0. It is stored on the thread's stack. We are just about to save that 0 to static id, but...
  4. Boom, scheduler chooses the second thread to run.
  5. So, the second thread enters run(). Static id is still 1, so he executes method pick(). P Q is printed.
  6. Scheduler chooses first thread to run. It takes 0 from its stack and saves to static id. So, the first thread also executes pick() and prints P Q.

However, in the book it's written that this answer is incorrect:

It is incorrect because the line id = 1 - id swaps the value of id between 0 and 1. There is no chance for the same method to be executed twice.

I don't agree. I think there is some chance for the scenario I presented above. Such swap is not atomic. Am I wrong?


Source: (StackOverflow)

What can I do with a moved-from object?

Does the standard define precisely what I can do with an object once it has been moved from? I used to think that all you can do with a moved-from object is do destruct it, but that would not be sufficient.

For example, take the function template swap as defined in the standard library:

template <typename T>
void swap(T& a, T& b)
{
    T c = std::move(a); // line 1
    a = std::move(b);   // line 2: assignment to moved-from object!
    b = std::move(c);   // line 3: assignment to moved-from object!
}

Obviously, it must be possible to assign to moved-from objects, otherwise lines 2 and 3 would fail. So what else can I do with moved-from objects? Where exactly can I find these details in the standard?

(By the way, why is it T c = std::move(a); instead of T c(std::move(a)); in line 1?)


Source: (StackOverflow)

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

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 I think this code has undefined behavior in the swap statement a = (a+b) - (b=a); as it does not contain any sequence points to determine the order of evaluation.

My question is: Is this an acceptable solution to swap two integers?


Source: (StackOverflow)

Is specializing std::swap deprecated now that we have move semantics? [duplicate]

Possible Duplicate:
Move semantics == custom swap function obsolete?

This is how std::swap looks like in C++11:

template<typename T>
void swap(T& x, T& y)
{
  T z = std::move(x);
    x = std::move(y);
    y = std::move(z);
}

Do I still have to specialize std::swap for my own types, or will std::swap be as efficient as it gets, provided that my class defines a move constructor and a move assignment operator, of course?


Source: (StackOverflow)

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 swapping elements within those ranges, since that is its intended purpose --- swapping the elements pointed to by two iterators. This allows optimizations for node-based sequences such as std::list, whereby the nodes are just relinked, rather than the data actually being swapped.

So which one is correct? Should I use iter_swap, or should I use swap? (Is iter_swap only for backwards compatibility?) Why?


Source: (StackOverflow)

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

An implementation of std::swap might look like this:

template <class T> void swap (T& a, T& b)
{
  T c(std::move(a)); a=std::move(b); b=std::move(c);
}
template <class T, size_t N> void swap (T (&a)[N], T (&b)[N])
{
  for (size_t i = 0; i<N; ++i) swap (a[i],b[i]);
}

An implementation std::exchange n3668 might look like this:

 template< typename T, typename U = T >
   T exchange( T & obj, U && new_val )
   {
     T old_val = std::move(obj);
     obj = std::forward<U>(new_val);
     return old_val;
   }

It says:

For primitive types, this is equivalent to the obvious implementation, while for more complex types, this definition

  • Avoids copying the old value when that type defines a move constructor
  • Accepts any type as the new value, taking advantage of any converting assignment operator
  • Avoids copying the new value if it's a temporary or moved.

I chose the name for symmetry with atomic_exchange, since they behave the same except for this function not being atomic.

n3746 also proposes a built-in swap operator that looks like this:

inline C& C::operator :=: (C&& y) &  { see below; return *this; } 
inline C& C::operator :=: (C& y)  &  { return *this :=: std::move(y); }

From what I gather, the proposals would like all three of these options to live side by side, rather than replacing each other. Why is it necessary to have three different ways to swap objects?


Source: (StackOverflow)

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 wise?


Source: (StackOverflow)

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.

public void Swap(int &p, int &q)
{
   int temp;
   temp = *p;
   *p = *q;
   *q = temp;
} 

Source: (StackOverflow)

Swap two variables without using a temp variable

I'd like to be able to swap out two variables without the use of a temp variable in C#. Can this be done?

decimal startAngle = Convert.ToDecimal(159.9);
decimal stopAngle = Convert.ToDecimal(355.87);

//swap each:
//startAngle becomes: 355.87
//stopAngle becomes: 159.9

Source: (StackOverflow)