I'm java programmer, but I used to use C++ long time ago. Now I have to translate form C++ to Java one program and I found one line, wchich I don't know what does it mean:
if (wr[m-2] == 0) wr[m-1] == 0;
Program works correctly in C++, but of course there's an AssignmentOperator error in Eclipse.
Looks like you're checking
Based on wr being an array of ints and m being an int, if the array wr is equal to 0 at index m-2, then check if the wr at index m-1 is equal to 0.
That line looks right, but you might want to check that your minus signs are actually minus signs and not a different-but-similar type of hyphen. Otherwise it looks fine to me.
The lines you quoted result in a no-op where there likely should be an assignment.
if (wr[m-2] == 0)
wr[m-1] == 0;
should probably be
if (wr[m-2] == 0)
wr[m-1] = 0;
Related
I have the following cython function.
01:
+02: cdef int count_char_in_x(unicode x,Py_UCS4 c):
03: cdef:
+04: int count = 0
05: Py_UCS4 x_k
06:
+07: for x_k in x: ## Yellow
+08: if x_k == c:
+09: count+=1
10:
+11: return count
Line 07 is not properly optimized.
The annotated HTML code is expanded as:
+07: for x_k in x: ## Yellow
if (unlikely(__pyx_v_x == Py_None)) {
PyErr_SetString(PyExc_TypeError, "'NoneType' is not iterable");
__PYX_ERR(0, 8, __pyx_L1_error)
}
__Pyx_INCREF(__pyx_v_x);
__pyx_t_1 = __pyx_v_x;
__pyx_t_6 = __Pyx_init_unicode_iteration(__pyx_t_1, (&__pyx_t_3), (&__pyx_t_4), (&__pyx_t_5)); if (unlikely(__pyx_t_6 == ((int)-1))) __PYX_ERR(0, 8, __pyx_L1_error)
for (__pyx_t_7 = 0; __pyx_t_7 < __pyx_t_3; __pyx_t_7++) {
__pyx_t_2 = __pyx_t_7;
__pyx_v_x_k = __Pyx_PyUnicode_READ(__pyx_t_5, __pyx_t_4, __pyx_t_2);
Any tips on how could this be improved?
I think it is possible to write a cdef/cpdef function that at runtime completly avoids Python None type checks. Any idea on how this could be done?
The generated C code looks pretty good to me. The loop overall is a int-iterated for loop (i.e. it's not relying on calling the Python methods __iter__ and __next__).
__Pyx_PyUnicode_READ is translated pretty directly to PyUnicode_READ (depending slightly on the Python version you're using). PyUnicode_READ is a C macro which is as close to a direct array access as you can get.
This is probably as good as it's getting. You might get a small improvement by using bytes rather than unicode (provided you're dealing with ASCII characters). You might just consider whether it's really worth reimplementing unicode.count.
If it were a regular def function you could declare x as unicode not None to remove the None check before the loop. That might make a small difference. However, as #ead points out that isn't supported for cdef functions. It's likely the cost of a def function call will be slightly larger than the cost of a None-check, but you should time it if you care.
I have been struggling to write the code to read in .txt data. I am in a directory where all of the file names are ‘img0001.txt’ through ‘img4200.txt’. Each file is a 2-D array of the same size (480x640), eventually I want to fill a 3-D data cube, but first I need to be able to read in all the data.
for i = 1:4200
i
if i<10
A = csvread(['img000',num2str(i),'.txt']);
elseif 10<=i<100
A = csvread(['img00',num2str(i),'.txt']); ***
elseif 100<=i<1000
A = csvread(['img0',num2str(i),'.txt']);
else i>=1000
A = csvread(['img',num2str(i),'.txt']);
end
end
The code prints i=100, and then gives me an error message for a file not found in the line where I added ***. The code is looking for file img00100.txt which does not exist, but I’m not sure why it is doing this.
I have been playing with different versions of writing the if, elseif, statements and the greater than and less than operators. I have also tried using eval and load commands.
Thank you.
m7913d's answer explains your if statement's logic, but a nice way to avoid the confusion would be to remove the if statements entirely using sprintf.
for i = 1:4200
filenum = sprintf('%04d', i); % Zero pads the number e.g. 59 => 0059
disp(['i =', filenum]) % Display current i
A = csvread(['img', filenum, '.txt']); % Load CSV
end
Your condition to check the range is wrong. You should write it as follows:
elseif 10 <= i && i < 100
What you calculated is the following (explained for i == 100):
10 <= i < 100 <=> (10 <= i) < 100 <=> (1) < 100 <=> 1
Note that this is the case for a lot of programming languages (C++, java, js, ...).
I want to create a CoffeeScript range (like [4...496]) but using a length instead of an end range. This can be done with a loop like
myNum = getBigNumber()
newArray = ( n + myNum for n in [0...50] )
but I'm wondering if there is range-related shortcut that I'm missing. Is there something like
[getBigNumber()...].length(50) available in CoffeeScript?
You can just do
range = [myNum...myNum + 50]
Edit: As mu points out in the comments, CoffeeScript will add some complexity whether you use the snippet above or the original code. If performance is an issue, it might be better to drop down to plain JS for the loop (using backticks in the CoffeeScript code).
Assuming you want an ascending (i.e. low to high) range, you can do:
myNum = getBigNumber()
length = 50
range = new Array length
i = 0
`for(; i < length ; i++) { range[i] = i + myNum }` # raw, escaped JS
It's a lot faster than CoffeeScript's way of doing things, but note that CoffeeScript's range syntax also supports creating descending ranges by just flipping the boundary values. So CoffeeScript is (as always) easier on the eyes and simpler to work with, but raw JS is 3.5x faster in my test.
I am looking at the code on page 11 here http://www.cs.usfca.edu/~parrt/doc/java/JavaIO-notes.pdf
I have trouble with one statement. I thought the result of an assignment was an lvalue. So ((byteRead = inFile.read()) != -1) should be the same as (inFile.read()) != -1). This doesn't seem to be the case though looking at the output. So my question is how is the statement ((byteRead = inFile.read()) != -1) parsed?
EDIT: It seems from the responses that I had the current interpretation of the result of an assignment. I was wondering what goes wrong by replacing the code fragment
int byteRead;
while((byteRead = inFile.read()) != -1)
outFile.write(byteRead);
with
while( inFile.read() != -1)
outFile.write( inFile.read());
So, now that you posted both versions of code, the answer is clear:
In your first version, each byte read is assigned to byteRead and then written to the output stream.
In the second version, you consume a byte with the read() but don't assign it to a variable. Then, you read another byte (the next one in the stream) which you write to the output stream.
So, if the input file is:
abcdefghijklmnopqrstuvwxyz
The output of the first version will be :
abcdefghijklmnopqrstuvwxyz
The output of the second will be :
bdfhjlnqrtuxz
((byteRead = inFile.read()) != -1) and (inFile.read() != -1) are, in one sense, equivalent boolean expressions. However, the first one has a side effect: It stores the result of inFile.read() in the variable byteRead.
The code example you referenced uses this for a compact while loop that reads one byte from input, writes it to output and keeps doing that until inFile.read() returns -1 (meaning end of file has been reached).
I have hack I need to employ under these conditions:
It's the last page of data.
It's not the first page, either.
There's not a page-size-even number of data items.
So I tried this code:
my $use_hack =
$last_page_number == $current_page_number and
$page_number != 1 and
$total_items % $items_per_page != 0;
And I keep getting this warning Useless use of numeric ne (!=) in void context about the last condition and it's evaluating true when $total_items % $items_per_page = 0.
say 'NOT EVEN' if $total_items % $items_per_page != 0; #works properly, though...
I've tried various combinations of parentheses to get it right, but nothing seems to work.
Okay, operator precedence. and has almost the lowest precedence of any operator in Perl, so Perl was evaluating the expression in a weird order. Switching to && instead got me correct results. Blarg.
The More You Know.
EDIT:
As Philip Potter pointed out below, Perl Best Practices (p.70) recommends always using &&,||, ! for boolean conditions - limiting and or and not for control flow because of their low precedence. (And it even goes so far as to say to never use and and not, only or for fallback logic.)
Thanks, everybody!
Enclose the RHS in parenthesis:
my $use_hack = (
$last_page_number == $current_page_number and
$page_number != 1 and
$total_items % $items_per_page != 0);
Assignment (=) is having higher precedence when compared to and operator. You can take a look at the Perl Operator Precedence.