swap

Swap items in doubly-linked list by their indices in the backing array

房东的猫 提交于 2019-11-28 14:30:14
I have an array of objects of the following type: struct Node { Node *_pPrev, *_pNext; double *_pData; }; Some of the nodes participate in a doubly-linked list, with _pData!=nullptr for such nodes. There is also a dummy head node with _pNext pointing to the beginning of the list, and _pPrev pointing to the end. The list starts with containing only this head node, and it should be never removed from the list. The doubly-linked list is backed by an array, with initial size equal to the maximum number of nodes in the list. struct Example { Node _nodes[MAXN]; Node _head; }; Now I want to perform

Why don't people use xor swaps? [closed]

喜欢而已 提交于 2019-11-28 11:48:58
I read on a site that using xor swaps is fast because it doesn't use a temporary variable. Here's an example: #include <stdio.h> int main(void) { int a=234,b=789; b=b^a; a=b^a; b=b^a; printf("a=%d,b=%d",a,b); return 0; } Why don't people use this technique in real life code? Is it just poor style? Is there something not well defined about it? Is it an optimisation that my compiler might produce from more clear code, automatically? Because readability is preferred over performance. Because tmp = a; a = b; b = tmp; is not that slow. Because the compiler will optimize it anyway. Because it works

Stepping through all permutations one swap at a time

不羁岁月 提交于 2019-11-28 10:04:58
Given a list of n distinct items, how can I step through each permutation of the items swapping just one pair of values at a time? (I assume it is possible, it certainly feels like it should be.) What I'm looking for is an iterator that yields the indices of the next pair of items to swap, such that if iterated n!-1 times it will step through the n! permutations of the list in some order. If iterating it once more would restore the list to its starting order that would be a bonus, but it isn't a requirement. If all pairs involve the first (resp. the last) element as one of the pair, so that

Why do these swap functions behave differently?

爷,独闯天下 提交于 2019-11-28 09:24:13
问题 #include <stdio.h> void swap1(int a, int b) { int temp = a; a = b; b = temp; } void swap2(int *a, int *b) { int *temp = a; a = b; b = temp; } void swap3(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } main() { int a = 9, b = 4; printf("%d , %d\n", a, b); swap1(a, b); printf("%d , %d\n", a, b); swap2(&a, &b); printf("%d , %d\n", a, b); swap3(&a, &b); printf("%d , %d\n", a, b); } 回答1: C has value semantics for function parameters. This means the a and b for all your three swap variants

Can I tell Windows not to swap out a particular processes’ memory?

我是研究僧i 提交于 2019-11-28 09:07:18
Is there a way to tell Windows that it shouldn't swap out a particular processes' memory to disk? Its a .Net windows service with fairly large memory usage. I got lot of physical RAM but the OS seems to move part of the process memory to the pagefile anyway. You can use VirtualLock to prevent memory from being paged to disk, but I really think you're better off letting the OS manage the system's memory. It's pretty good at it, and I wouldn't second guess why the OS was swapping things to disk unless I really knew what I was doing. VirtualLock by default will run out of quota in a hurry, even

How to move specific item in array list to the first item

此生再无相见时 提交于 2019-11-28 07:55:21
For example : A list A B C D E Given C , Switch to C A B D E Notice that the array size will change, some items may removed in run times Collections.swap(url, url.indexOf(itemToMove), 0); This statement is not working because it output C B A D E not C A B D E , how to fix it? Thanks. Chris Hayes What you want is a very expensive operation in an ArrayList . It requires shifting every element between the beginning of the list and the location of C down by one. However, if you really want to do it: int index = url.indexOf(itemToMove); url.remove(index); url.add(0, itemToMove); If this is a

Overloading global swap for user-defined type

爱⌒轻易说出口 提交于 2019-11-28 07:40:41
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 just do: namespace std { template <class T> void swap(MyType<T>& t1, MyType<T>& t2) { t1.swap(t2); } } ...and that works out fine. But I'm not entirely sure if my usual practice is standard compliant. Am I doing this correctly? What you have is not a specialization, it is overloading and exactly what the standard prohibits. (However, it will almost

Is it possible to swap columns around in a data frame using R?

牧云@^-^@ 提交于 2019-11-28 07:32:16
I have three variables in a data frame and would like to swap the 4 columns around from "dam" "piglet" "fdate" "ssire" to "piglet" "ssire" "dam" "tdate" Is there any way I can do the swapping using R? Any help would be very much appreciated. Baz 42- dfrm <- dfrm[c("piglet", "ssire", "dam", "tdate")] OR: dfrm <- dfrm[ , c("piglet", "ssire", "dam", "tdate")] kohske d <- data.frame(a=1:3, b=11:13, c=21:23) d # a b c #1 1 11 21 #2 2 12 22 #3 3 13 23 d2 <- d[,c("b", "c", "a")] d2 # b c a #1 11 21 1 #2 12 22 2 #3 13 23 3 or you can do same thing using index: d3 <- d[,c(2, 3, 1)] d3 # b c a #1 11 21

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

会有一股神秘感。 提交于 2019-11-28 07:12:16
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? The specialization of std::swap is now optional, but not deprecated. The rationale is performance. For prototyping code, and perhaps even for much shipping code, std::swap will be plenty

How do I swap tensor's axes in TensorFlow?

孤者浪人 提交于 2019-11-28 06:53:59
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 I found nothing. Do you have any idea? keveman tf.transpose provides the same functionality as np.swapaxes , although in a more generalized form. In your case, you can do tf.transpose(orig_tensor, [1, 0, 2]) which would be equivalent to np.swapaxes(orig_np_array, 0, 1) . 来源: https://stackoverflow.com/questions/38212205/how-do-i-swap-tensors-axes