What is the way of doing this in the view template in playframework? I have tagged with groovy, because apparently, the play template engine is based on groovy.
%{
for(int i=0, int j = 0; i < userdata.size(), j < user.size();i = i + 4, j++){
}%
<div style="text-align: center;">
<h3>
${foo.get(j)}
</h3>
</div>
If this is not possible, or just for curiosity sake:
I also tried passing foo as a hashmap, the keys of which are already present in userdata. I tried something like this but to no avail:
${foo.each{ k, v -> println "${k}:${v}" }}
Since you are talking about groovy, I presume you are using playframework 1.x. Playframework 2 uses scala templates.
You can loop over two conditions, like you would do in any other language. The syntax is just a little different.
Java:
for (int i = 0; i < 10; i++){
for (int j = 0; j < 10; j++) {
System.out.println(String.format("i: %d, j: %d", i, j));
}
}
playframework template:
#{list items:0..10, as:'i'}
#{list items:0..10, as:'j'}
<p>i: ${i}, j: ${j}</p>
#{/list}
#{/list}
Check out the documentation for the #{list} tag
It seams that you can't do this with play template builtin tags. Furthermore, I also have a compilation failure using for loop with multi-parameter initialization in a play 1.2.4 template. You can make it work with a while loop :
%{
int j = 0, i = 0;
while (i*i <= j) {
}%
${i}^2 <= ${j}
%{
i++;
j = j+2;
}
}%
//prints
//0^2 <= 0
//1^2 <= 2
//2^2 <= 4
Related
please help a newbie like me, I'm lost, I don't get it - I looked at answers, it still doesn't work and I can't spot the error.
I'm working on plurality, I know we can solve it without bubble sort but I want to find out why my solution isn't working.
I bubble sorted it - and I can't get the right names to print.
I feel like I tried to put the printf everywhere in any bracket and it is never right and shows me any solution but the right one.
Currently, it prints the last 2 names regardless of who has the most votes.
I feel like it's something pretty simple and basic but it's been 3 days now and I can't get anywhere - please help me, where am I misunderstanding things?
void print_winner(void)
{
int temp;
for (int i = 0; i < candidate_count; i++) //going through each name/vote struct - -1 because c-count is acvg arguments
{
for(int j = 0; j < (candidate_count - i - 1); j++) // c_c - i - 1 is for ignoring already compared candidates
{
if (candidates[j].votes < candidates[j + 1].votes)
{
temp = candidates[j].votes;
candidates[j].votes = candidates[j + 1].votes;
candidates[j + 1].votes = temp;
}
}
}
int top = candidates[candidate_count - 1].votes;
for (int i = 0; i < candidate_count; i++)
{
if (candidates[i].votes == top)
{
printf("%s\n", candidates[i].name);
}
}
return;
}
I am trying to see if I can improve the performance of the following loop in C++, which uses two dimensional vectors (_external and _Table) and has a carried loop dependency on the previous iteration. Additionally, it has a calculated index accessor in the innermost loop that will make the access of _Table non sequential on the right hand side.
int N = 8000;
int M = 400
int P = 100;
for(int i = 1; i <= N; i++){
for(int j = 0; j < M; j++){
for(int k =0; k < P; k++){
int index = _external.at(j).at(k);
_Table.at(j).at(i) += _Table.at(index).at(i-1);
}
}
}
What can I do to improve the performance of a loop like this?
Well it looks to me like the order in which these statements:
int index = _external.at(j).at(k);
_Table.at(j).at(i) += _Table.at(index).at(i-1);
are executed is critical to correctness. (That is, if the iteration order for i, j, k changes, then the results will be different ... and incorrect.)
So I think you are only left with micro-optimizations, like hoisting the expressions _Table.at(j).at(i) and _external.at(j) out of the innermost loop.
Consider this:
for(int k =0; k < P; k++){
int index = _external.at(j).at(k);
_Table.at(j).at(i) += _Table.at(index).at(i-1);
}
This loop is repeatedly adding numbers to _Table.at(j).at(i). Since (by inspection) _Table.at(index).at(i-1) must be reading from a different cell of the table (because of i-1 versus i), you could do this:
int temp = 0;
for(int k =0; k < P; k++){
int index = _external.at(j).at(k);
temp += _Table.at(index).at(i-1);
}
_Table.at(j).at(i) += temp;
This will reduce the number of calls to at, and may also improve cache performance a bit.
I'm rewriting C code in Rust which heavily relies on u32 variables and wrapping them around. For example, I have a loop defined like this:
#define NWORDS 24
#define ZERO_WORDS 11
int main()
{
unsigned int i, j;
for (i = 0; i < NWORDS; i++) {
for (j = 0; j < i; j++) {
if (j < (i-ZERO_WORDS+1)) {
}
}
}
return 0;
}
Now, the if statement will need to wrap around u32 for a few values as initially i = 0. I came across the wrapping_neg method but it seems to just compute -self. Is there any more flexible way to work with u32 in Rust by also allowing wrapping?
As mentioned in the comments, the literal answer to your question is to use u32::wrapping_sub and u32::wrapping_add:
const NWORDS: u32 = 24;
const ZERO_WORDS: u32 = 11;
fn main() {
for i in 0..NWORDS {
for j in 0..i {
if j < i.wrapping_sub(ZERO_WORDS).wrapping_add(1) {}
}
}
}
However, I'd advocate avoiding relying on wrapping operations unless you are performing hashing / cryptography / compression / something similar. Wrapping operations are non-intuitive. For example, j < i-ZERO_WORDS+1 doesn't have the same results as j+ZERO_WORDS < i+1.
Even better would be to rewrite the logic. I can't even tell in which circumstances that if expression will be true without spending a lot of time thinking about it!
It turns out that the condition will be evaluated for i=9, j=8, but not for i=10, j=0. Perhaps all of this is clearer in the real code, but devoid of context it's very confusing.
This appears to have the same logic, but seems much more understandable to me:
i < ZERO_WORDS - 1 || i - j > ZERO_WORDS - 1;
Compare:
j < i.wrapping_sub(ZERO_WORDS).wrapping_add(1);
I've got a serial version of BML and I'm trying to write a parallel one with OpenMP. Basically my code works with a main witin a loop calling two functions for horizontal and vertical moves. Like that:
for (s = 0; s < nmovss; s++) {
horizontal_movs(grid, N);
copy_sides(grid, N);
cur = 1-cur;
vertical_movs(grid, N);
copy_sides(grid, N);
cur = 1-cur;
}
Where cur is the current grid. Then horizontal and vertical functions are similar and have a nested loop:
for(i = 1; i <= n; i++) {
for(j = 1; j <= n+1; j++) {
if(grid[cur][i][j-1] == LR && grid[cur][i][j] == EMPTY) {
grid[1-cur][i][j-1] = EMPTY;
grid[1-cur][i][j] = LR;
}
else {
grid[1-cur][i][j] = grid[cur][i][j];
}
}
}
The code produces a ppm image at every step, and whit a certain input the serial version produce an output that we can suppose good. But using #pragma omp parallel for inside the two functions H and V, the ppm file results splitted in such zones as the number of threads(i.e. 4):
I suppose the problem is that every thread should be doing both functions in sequence before termitate because movememnts are strictcly connected. I don't know how to do that. If I set pragma at a highter level like before main loop, there is no speed-up. Obviously the ppm file has to be not sliced like the image.
Goin'on I tried this solution that gives me an identical result as the serial code, but I don't excatly understand why
# pragma omp parallel num_threads(thread_count) default(none) \
shared(grid, n, cur) private(i, j)
for(i = 1; i <= n+1; i++) {
# pragma omp for
for(j = 1; j <= n; j++) {
if(grid[cur][i-1][j] == TB && grid[cur][i][j] == EMPTY) {
grid[1-cur][i-1][j] = EMPTY;
grid[1-cur][i][j] = TB;
}
else {
grid[1-cur][i][j] = grid[cur][i][j];
}
}
}
}
Therefore, if i use just one thread more than available cores(4), the execution time "explodes" instead of remain barely the same.
hello what I'm currently dealing with is the ability to get input from a text file and then convert it into a bitmap and save it to a file.
the input looks like this:
########
# #
########
and I want to draw it using allegro and instead of # there would be pixels of specified size. Each # should represent a tile (10x10 pixel). So the final result would look like this
link to an image
I've actually drawn it using this code:
for (int i = 0; i < 80; i++){
for (int j = 0; j < 10; j++){
al_draw_pixel(i, j, al_map_rgb(0, 0, 0));
}
}
for (int i = 0; i < 10; i++){
for (int j = 10; j < 20; j++){
al_draw_pixel(i, j, al_map_rgb(0, 0, 0));
}
}
for (int i = 70; i < 80; i++){
for (int j = 10; j < 20; j++){
al_draw_pixel(i, j, al_map_rgb(0, 0, 0));
}
}
for (int i = 0; i < 80; i++){
for (int j = 20; j < 30; j++){
al_draw_pixel(i, j, al_map_rgb(0, 0, 0));
}
}
yeah that's pretty bad, so how do I achieve something like that but with a common procedure which would be independent on the text file? thanks for any advice.
note: the only allowed headers are allegro5/allegro.h and allegro5/allegro_image.h
To draw to an image with Allegro 5, you need to do something like:
ALLEGRO_BITMAP *bmp = al_create_bitmap(640, 480);
al_set_target_bitmap(bmp);
Now all of your drawing operations will happen on the image. To later save it:
al_save_bitmap("somefile.bmp", bmp);
You can also use png and jpg as extensions if your image library has support for it enabled.
Use these functions to read the text file:
al_fopen
al_fgetc
al_feof
al_fclose
Set int x and y to zero. You'll be looping over until the end of the file. On every iteration increment x by one. If you reach a new line character (\n) increment y by one and set x to zero. (You should ignore \r characters.)
Now, depending on the character read, draw a tile:
ALLEGRO_BITAMP *tile_to_draw = NULL;
if (c == '#')
tile_to_draw = bmp1;
else if (c == ' ')
tile_to_draw = bmp2;
if (tile_to_draw)
al_draw_bitmap(tile_to_draw, x * 10, y * 10, 0);
Of course there's better ways to map characters to tiles than a series of ifs, but the above works and should be enough to help you finish your homework.