Resharper 6.0 - context action discards comments - resharper

When context action such as convert if to switch is invoked, all my comments are lost. Can this be prevented?
e.g.:
int a = 1;
int i;
if (a == 1)
//comment I want to keep 1
i = 1;
else if (a == 2)
//comment I want to keep 2
i = 2;
else
i = 3;

This is a known issue - here it is on YouTrack (and here's a duplicate).

Related

LongAdder Striped64 wasUncontended implementation detail

This is a question not about how LongAdder works, it's about an intriguing implementation detail that I can't figure out.
Here is the code from Striped64 (I've cut out some parts and left the relevant parts for the question):
final void longAccumulate(long x, LongBinaryOperator fn,
boolean wasUncontended) {
int h;
if ((h = getProbe()) == 0) {
ThreadLocalRandom.current(); // force initialization
h = getProbe();
wasUncontended = true;
}
boolean collide = false; // True if last slot nonempty
for (;;) {
Cell[] as; Cell a; int n; long v;
if ((as = cells) != null && (n = as.length) > 0) {
if ((a = as[(n - 1) & h]) == null) {
//logic to insert the Cell in the array
}
// CAS already known to fail
else if (!wasUncontended) {
wasUncontended = true; // Continue after rehash
}
else if (a.cas(v = a.value, ((fn == null) ? v + x : fn.applyAsLong(v, x)))){
break;
}
A lot of things from code are clear to me, except for the :
// CAS already known to fail
else if (!wasUncontended) {
wasUncontended = true; // Continue after rehash
}
Where does this certainty that the following CAS will fail?
This is really confusing for me at least, because this check only makes sense for a single case : when some Thread enters the longAccumulate method for the n-th time (n > 1) and the busy spin is at it's first cycle.
It's like this code is saying : if you (some Thread) have been here before and you have some contention on a particular Cell slot, don't try to CAS your value to the already existing one, but instead rehash the probe.
I honestly hope I will make some sense for someone.
It's not that it will fail, it's more that it has failed. The call to this method is done by the LongAdder add method.
public void add(long x) {
Cell[] as; long b, v; int m; Cell a;
if ((as = cells) != null || !casBase(b = base, b + x)) {
boolean uncontended = true;
if (as == null || (m = as.length - 1) < 0 ||
(a = as[getProbe() & m]) == null ||
!(uncontended = a.cas(v = a.value, v + x)))
longAccumulate(x, null, uncontended);
}
}
The first set of conditionals is related to existence of the long Cells. If the necessary cell doesn't exist, then it will try to accumulate uncontended (as there was no attempt to add) by atomically adding the necessary cell and then adding.
If the cell does exist, try to add (v + x). If the add failed then there was some form of contention, in that case try to do the accumulating optimistically/atomically (spin until successful)
So why does it have
wasUncontended = true; // Continue after rehash
My best guess is that with heavy contention, it will try to give the running thread time to catch up and will force a retry of the existing cells.

interview riddle (string manipulation) - explanation needed

i am studying for an interview and encountered a question + solution.
i am having a problem with one line in the solution and was hoping maybe someone here can explain it.
the question:
Write a method to replace all spaces in a string with ‘%20’.
the solution:
public static void ReplaceFun(char[] str, int length) {
int spaceCount = 0, newLength, i = 0;
for (i = 0; i < length; i++) {
if (str[i] == ‘ ‘) {
spaceCount++;
}
}
newLength = length + spaceCount * 2;
str[newLength] = ‘\0’;
for (i = length - 1; i >= 0; i--) {
if (str[i] == ‘ ‘) {
str[newLength - 1] = ‘0’;
str[newLength - 2] = ‘2’;
str[newLength - 3] = ‘%’;
newLength = newLength - 3;
} else {
str[newLength - 1] = str[i];
newLength = newLength - 1;
}
}
}
my problem is with line number 9. how can he just set str[newLength] to '\0'? or in other words, how can he take over the needed amount of memory without allocating it first or something like that?
isn't he running over a memory?!
Assuming this is actually meant to be in C (private static is not valid C or C++), they can't, as it's written. They're never allocating a new str which will be long enough to hold the old string plus the %20 expansion.
I suspect there's an additional part to the question, which is that str is already long enough to hold the expanded %20 data, and that length is the length of the string in str, not counting the zero terminator.
This is valid code, but it's not good code. You are completely correct in your assessment that we are overwriting the bounds of the initial str[]. This could cause some rather unwanted side-effects depending on what was being overwritten.

Switch Statement: Homework Confusion

Question: What is y after the following switch statement is executed? Rewrite the code using an if statement.
y = 3; x = 3;
switch (x + 3)
{
case 6: y = 1;
default: y += 1;
}
I'm at the beginning of the C++ hike. I don't know what do with this. It's not working in C++ Visual Studio 2013. I've put it in as is and nothing happens.
I use:
y = 3; x = 3;
switch (x + 3)
{
case 6: y = 1;
default: y += 1;
}
return 0;
}
And nothing happens. I have both the answers but I have no clue how to get them...
y is 2
if (x + 3 == 6)
y = 1;
y += 1;
I'm strictly supposed to be using
#include <iostream>
using namespace std;
int main()
{
return 0;
}
switch basics in C/C++:
switch expression is evaluated once, at the beginning. Then the 1st matching case is executed
after the case clause, the execution falls through to the next clause unless there's a break. Since this typically isn't what was indended, fall-throughs are normally documented with a comment or (if multiple cases refer to the same code) by stacking them together (case 2: case 3: <code>).

Renderscript and garbage collector

Recently I modified my code to store everything in the renderscript (before that I copied the data back and forth wasn't effective), but now the garbage collector is collecting garbage like crazy. (Still the app is preforming better this way.)
I can't figure out what needs to be collected, I use everything I don't create new arrays in the functions, which I call frequently. My only idea is that if I do this:
void __attribute__((kernel)) diffuseVelocityY(float in, uint32_t x, uint32_t y) {
velocityY_prev[x] = velocityY[x] + a*(velocityY_prev[x-1] + velocityY_prev[x+1] + velocityY_prev[x-(width)] + velocityY_prev[x+(width)])/(1+(4*a));
}
it creates a temperaly pointer for it because I'm using data from the same pointer that I want to update(I have no idea if this is the way it works). I tried to change it, so it puts the data in an empty pointer and after it finishes I copy the data to the right place. It seemd that it collected less garbage but there was still garbage collection and the preformance went down aswell.
I uploaded my code here if someone wants to look(the _befores are from before I modified the code).
I have no idea how to stop the garbage collection, I hope someone can help.
One of the methods:
void set_bnd_densiy_prev(int b){
for (int i = 1; i <= gridSizeY; i++) {
density_prev[IX(0, i)] = (b == 1 ? -density_prev[IX(1, i)] : density_prev[IX(1, i)]);
density_prev[IX(gridSizeX + 1, i)] = (b == 1 ? -density_prev[IX(gridSizeX, i)] : density_prev[IX(gridSizeX, i)]);
}
for (int i = 1; i <= gridSizeX; i++) {
density_prev[IX(i, 0)] = (b == 2 ? -density_prev[IX(i, 1)] : density_prev[IX(i, 1)]);
density_prev[IX(i, gridSizeY + 1)] = (b == 2 ? -density_prev[IX(i, gridSizeY)] : density_prev[IX(i, gridSizeY)]);
}
density_prev[IX(0 ,0 )] = 0.5f*(density_prev[IX(1,0 )]+density_prev[IX(0 ,1)]);
density_prev[IX(0 ,gridSizeY+1)] = 0.5f*(density_prev[IX(1,gridSizeY+1)]+density_prev[IX(0 ,gridSizeY )]);
density_prev[IX(gridSizeX+1,0 )] = 0.5f*(density_prev[IX(gridSizeX,0 )]+density_prev[IX(gridSizeX+1,1)]);
density_prev[IX(gridSizeX+1,gridSizeY+1)] = 0.5f*(density_prev[IX(gridSizeX,gridSizeY+1)]+density_prev[IX(gridSizeX+1,gridSizeY )]);
}
Code generated from it:
private final static int mExportFuncIdx_set_bnd_densiy_prev = 3;
public void invoke_set_bnd_densiy_prev(int b) {
FieldPacker set_bnd_densiy_prev_fp = new FieldPacker(4);
set_bnd_densiy_prev_fp.addI32(b);
invoke(mExportFuncIdx_set_bnd_densiy_prev, set_bnd_densiy_prev_fp);
}
The problem was with the function args because renderscript needs to create Fieldpackers to handle them. So if you have the same problem remove the function args then copy paste the function and modify the variables and call the different functions not pretty but it works.
(Thanks thebaron for your help)

Brute-force transposition decryption - word segmentation

I'm a 2nd year B. Comp. Sci. student and have a cryptography assignment that's really giving me grief. We've been given a text file of transposition-encrypted English phrases and an English dictionary file, then asked to write a program that deciphers the phrases automatically without any user input.
My first idea was to simply brute-force all possible permutations of the ciphertext, which should be trivial. However, I then have to decide which one is the most-likely to be the actual plaintext, and this is what I'm struggling with.
There's heaps of information on word segmentation here on SO, including this and this amongst other posts. Using this information and what I've already learned at uni, here's what I have so far:
string DecryptTransposition(const string& cipher, const string& dict)
{
vector<string> plain;
int sz = cipher.size();
int maxCols = ceil(sz / 2.0f);
int maxVotes = 0, key = 0;
// Iterate through all possible no.'s of cols.
for (int c = 2; c <= maxCols; c++)
{
int r = sz / c; // No. of complete rows if c is no. of cols.
int e = sz % c; // No. of extra letters if c is no. of cols.
string cipherCpy(cipher);
vector<string> table;
table.assign(r, string(c, ' '));
if (e > 0) table.push_back(string(e, ' '));
for (int y = 0; y < c; y++)
{
for (int x = 0; x <= r; x++)
{
if (x == r && e-- < 1) break;
table[x][y] = cipherCpy[0];
cipherCpy.erase(0, 1);
}
}
plain.push_back(accumulate(table.begin(),
table.end(), string("")));
// plain.back() now points to the plaintext
// generated from cipher with key = c
int votes = 0;
for (int i = 0, j = 2; (i + j) <= sz; )
{
string word = plain.back().substr(i, j);
if (dict.find('\n' + word + '\n') == string::npos) j++;
else
{
votes++;
i += j;
j = 2;
}
}
if (votes > maxVotes)
{
maxVotes = votes;
key = c;
}
}
return plain[key - 2]; // Minus 2 since we started from 2
}
There are two main problems with this algorithm:
It is incredibly slow, taking ~30 sec. to decrypt a 80-char. message.
It isn't completely accurate (I'd elaborate on this if I hadn't already taken up a whole page, but you can try it for yourself with the full VC++ 2012 project).
Any suggestions on how I could improve this algorithm would be greatly appreciated. MTIA :-)

Resources