Re: signbit question
On Thursday, 15 March 2018 at 17:31:38 UTC, rumbu wrote: On Thursday, 15 March 2018 at 17:18:08 UTC, Miguel L wrote: On Thursday, 15 March 2018 at 16:31:56 UTC, Stefan Koch wrote: On Thursday, 15 March 2018 at 15:28:16 UTC, Miguel L wrote: [...] integers don't have a sign-bit. since they are not necessarily singed. However if an integer is signed and using 1-complement you can either do sign = var < 0 or sign = (var & (1 << (sizeof(var)*8 - 1)); though I cannot tell which one is faster you have to experiment. Thanks. Just one more question: Is this code correct? I don't care about +0 or -0 as the calculations on f guarantee it cannot be 0 at all. int a; float f; if((a<0)==signbit(f)) {} else {...} If you are comparing with an integer, please avoid signbit. It will return 1 also for -0.0, -inf and -nan. Don't bother also with signbit for integer types. The compiler usually outsmarts the programmer in finding the best way to compare an integer with 0. You can simply write: if (a < 0 && f < 0) {...} This will cover the [-inf..0) but not the NaN case. You can test it in a separate branch if (isNaN(f)) {...} There are various in my code there are more than two variables, and i'd like to check when their signs differ.I am trying to avoid this, maybe there is no point in it: if(!((a>=0 && f>=0) || (a<0 && f<0))){ //signs are different } Thanks, anyway
Re: signbit question
On Thursday, 15 March 2018 at 16:31:56 UTC, Stefan Koch wrote: On Thursday, 15 March 2018 at 15:28:16 UTC, Miguel L wrote: Why does std.math.signbit only work for floating point types? Is there an analogue function for integer types? what is the best way to compare the sign of a float with the sign of an integer? Thanks in advance integers don't have a sign-bit. since they are not necessarily singed. However if an integer is signed and using 1-complement you can either do sign = var < 0 or sign = (var & (1 << (sizeof(var)*8 - 1)); though I cannot tell which one is faster you have to experiment. Thanks. Just one more question: Is this code correct? I don't care about +0 or -0 as the calculations on f guarantee it cannot be 0 at all. int a; float f; if((a<0)==signbit(f)) {} else {...}
signbit question
Why does std.math.signbit only work for floating point types? Is there an analogue function for integer types? what is the best way to compare the sign of a float with the sign of an integer? Thanks in advance
Re: How to init immutable static array?
On Tuesday, 18 July 2017 at 07:31:22 UTC, Era Scarecrow wrote: On Tuesday, 18 July 2017 at 07:30:30 UTC, Era Scarecrow wrote: my_array[i]=some calculations(based on constants and n) i meant: tmp[i]=some calculations(based on constants and n) That does work, thanks.
How to init immutable static array?
Hi, I need help again. I have an immutable static array and i need to initialize its contents inside a for loop. Something like this: void f(int n)() { immutable float[n] my_array; for(int i=0;i
Re: Function with static array as parameter
Thanks for your help.
Re: Static array with parameter based size?
On Wednesday, 12 July 2017 at 18:49:23 UTC, Jack Applegame wrote: On Wednesday, 12 July 2017 at 05:45:13 UTC, Miguel L wrote: Also what is it possible in D to write a function that accepts an static array of any size? void foo(size_t N)(ref int[N] arr) { ... } int[10] arr; foo(arr); Thank you very much for your answers.
Function with static array as parameter
What is the best way in D to create a function that receives a static array of any length?
Re: Static array with parameter based size?
On Wednesday, 12 July 2017 at 05:45:13 UTC, Miguel L wrote: Hi I need to create a non-dynamic array like this void f(int x) { int[x] my_array; ... this does not compile as x value needs to be known at compile time. The closest to this I can get is: void f(int x) { int[] my_array; my_array.length=x; but I don't really need a dynamic array as length is not going to change inside f. What is the best way to do this? Also what is it possible in D to write a function that accepts an static array of any size? Thanks in advance Sorry, this post is duplicated. Yesterday forum was not working and i received an error when i was trying to post it. Please ignore it.
Static array with parameter based size?
Hi I need to create a non-dynamic array like this void f(int x) { int[x] my_array; ... this does not compile as x value needs to be known at compile time. The closest to this I can get is: void f(int x) { int[] my_array; my_array.length=x; but I don't really need a dynamic array as length is not going to change inside f. What is the best way to do this? Also what is it possible in D to write a function that accepts an static array of any size? Thanks in advance
Static array with parameter based size?
I need to create a non-dynamic array like this void f(int x) { int[x] my_array; ... this does not compile as x value needs to be known at compile time. The closest to this I can get is: void f(int x) { int[] my_array; my_array.length=x; but I don't really need a dynamic array as length is not going to change inside f. What is the best way to do this? Thanks in advance
Re: Different array rotation algorithms benchmark
On Thursday, 1 September 2016 at 13:16:19 UTC, Johan Engelen wrote: On Thursday, 1 September 2016 at 10:37:18 UTC, Miguel L wrote: Also, forgot to specify I am using LDC with -05. And the version of LDC too please ;-) LDC version is: ldc2-1.1.0-beta2-win64-msvc
Re: Different array rotation algorithms benchmark
On Thursday, 1 September 2016 at 09:53:59 UTC, Miguel L wrote: On Thursday, 1 September 2016 at 09:36:16 UTC, Miguel L wrote: Hi I recently needed a very optimized array rotation algorithm, so I did this benchmark, hope you find it interesting. I am a bit surprised by the poor results of std.algorithm.bringToFront: [...] Sorry Rotate4 had a bug, there was an extra for that was not neccesary, this is the correct implementation: void Rotate4(T)(T[] input, long n) pure { /* 1,2,3,4,5,6,7,8 - 2 - * 7,2,3,4,5,6,1,8 - a=0 b=8-2=6 * 7,8,3,4,5,6,1,2 - a=1 b=7 * 7,8,1,4,5,6,3,2 - a=2 b=6 * 7,8,1,2,5,6,3,4 - a=3 b=7 * 7,8,1,2,3,6,5,4 - a=4 b=6 * 7,8,1,2,3,4,5,6 - a=5 b=7 1,2,3,4,5,6,7,8,9 - 3 - * 7,2,3,4,5,6,1,8,9 - a=0 b=9-3=6 * 7,8,3,4,5,6,1,2,9 - a=1 b=7 * 7,8,9,4,5,6,1,2,3 - a=2 b=8 * 7,8,9,1,5,6,4,2,3 - a=3 b=6 * 7,8,9,1,2,6,4,5,3 - a=4 b=7 * 7,8,9,1,2,3,4,5,6 - a=5 b=8 */ if(n<0) n=input.length+n; long a=0,b=input.length-n; T tmp; while(a=input.length) { b=input.length-n; } } } Very sorry again: Rotate4 was not correct with negative offsets. This implementation works correctly: void Rotar4(T)(T[] input, long n) pure { /* 1,2,3,4,5,6,7,8 - 2 - * 7,2,3,4,5,6,1,8 - a=0 b=8-2=6 * 7,8,3,4,5,6,1,2 - a=1 b=7 * 7,8,1,4,5,6,3,2 - a=2 b=6 * 7,8,1,2,5,6,3,4 - a=3 b=7 * 7,8,1,2,3,6,5,4 - a=4 b=6 * 7,8,1,2,3,4,5,6 - a=5 b=7 * * 1,2,3,4,5,6,7,8 - -2 - * 1,8,3,4,5,6,7,2 - a=7 b=1 * 7,8,3,4,5,6,1,2 - a=6 b=0 * 7,6,3,4,5,8,1,2 - a=5 b=1 * 5,6,3,4,7,8,3,4 - a=4 b=0 * 3,6,5,4,7,8,1,2 - a=3 b=1 * 3,4,5,6,7,8,1,2 - a=2 b=0 1,2,3,4,5,6,7,8,9 - 2 - * 7,2,3,4,5,6,1,8,9 - a=0 b=9-3=6 * 7,8,3,4,5,6,1,2,9 - a=1 b=7 * 7,8,9,4,5,6,1,2,3 - a=2 b=8 * 7,8,9,1,5,6,4,2,3 - a=3 b=6 * 7,8,9,1,2,6,4,5,3 - a=4 b=7 * 7,8,9,1,2,3,4,5,6 - a=5 b=8 */ long a,b; T tmp; if(n>0) { a=0; b=input.length-n; while(a =input.length) b=input.length-n; } } else { a=input.length-1; long b0=-n-1; b=b0; while(a>=-n) { tmp=input[b]; input[b]=input[a]; input[a]=tmp; --a; --b; if(b<0) b=b0; } } } Also, forgot to specify I am using LDC with -05. Updated benchmark results: Rotate0: 0.344186s Rotate1: 1.76369s Rotate2: 0.169968s Rotate3: 0.354091s Rotate4: 0.156231s
Re: Different array rotation algorithms benchmark
On Thursday, 1 September 2016 at 09:36:16 UTC, Miguel L wrote: Hi I recently needed a very optimized array rotation algorithm, so I did this benchmark, hope you find it interesting. I am a bit surprised by the poor results of std.algorithm.bringToFront: [...] Sorry Rotate4 had a bug, there was an extra for that was not neccesary, this is the correct implementation: void Rotate4(T)(T[] input, long n) pure { /* 1,2,3,4,5,6,7,8 - 2 - * 7,2,3,4,5,6,1,8 - a=0 b=8-2=6 * 7,8,3,4,5,6,1,2 - a=1 b=7 * 7,8,1,4,5,6,3,2 - a=2 b=6 * 7,8,1,2,5,6,3,4 - a=3 b=7 * 7,8,1,2,3,6,5,4 - a=4 b=6 * 7,8,1,2,3,4,5,6 - a=5 b=7 1,2,3,4,5,6,7,8,9 - 3 - * 7,2,3,4,5,6,1,8,9 - a=0 b=9-3=6 * 7,8,3,4,5,6,1,2,9 - a=1 b=7 * 7,8,9,4,5,6,1,2,3 - a=2 b=8 * 7,8,9,1,5,6,4,2,3 - a=3 b=6 * 7,8,9,1,2,6,4,5,3 - a=4 b=7 * 7,8,9,1,2,3,4,5,6 - a=5 b=8 */ if(n<0) n=input.length+n; long a=0,b=input.length-n; T tmp; while(a=input.length) { b=input.length-n; } } }
Different array rotation algorithms benchmark
Hi I recently needed a very optimized array rotation algorithm, so I did this benchmark, hope you find it interesting. I am a bit surprised by the poor results of std.algorithm.bringToFront: These are the different algorithms: void Rotate0(T)(T[] input, int n) pure { if(n>0)input=input[($-n)..$]~input[0..($-n)]; if(n<0)input=input[(-n)..$]~input[0..(-n)]; } void Rotate(T)(T[] input, int n) pure { if(n>0) std.algorithm.bringToFront(input[0..($-n)],input[($-n)..$]); else if(n<0) std.algorithm.bringToFront(input[0..(-n)],input[(-n)..$]); } void reverse(T)(T[] a, long sz) pure { long i, j; for (i = 0, j = sz; i < j; ++i, --j) { T tmp = a[i]; a[i] = a[j]; a[j] = tmp; } } void Rotate2(T)(T[] array, long amt) pure { /* the algorithm from Jon Bentley's book, "Programming Pearls 2nd Edition" O(n) time and no extra memory usage (since array was specified), */ if (amt < 0) amt = array.length + amt; reverse(array, array.length-amt-1); reverse(array[array.length-amt..$], amt-1); reverse(array, array.length-1); } void Rotate3(T)(T[] input, long n) pure { if(n<0) n=input.length+n; auto tmp=input[($-n)..$].dup; for(auto j=input.length-1;j>=n;--j) input[j]=input[j-n]; input[0..n]=tmp; } void Rotate4(T)(T[] input, long n) pure //No extra memory, just swapping of elements { /* 1,2,3,4,5,6,7,8 - 2 - * 7,2,3,4,5,6,1,8 - a=0 b=8-2=6 * 7,8,3,4,5,6,1,2 - a=1 b=7 * 7,8,1,4,5,6,3,2 - a=2 b=6 * 7,8,1,2,5,6,3,4 - a=3 b=7 * 7,8,1,2,3,6,5,4 - a=4 b=6 * 7,8,1,2,3,4,5,6 - a=5 b=7 1,2,3,4,5,6,7,8,9 - 2 - * 7,2,3,4,5,6,1,8,9 - a=0 b=9-3=6 * 7,8,3,4,5,6,1,2,9 - a=1 b=7 * 7,8,9,4,5,6,1,2,3 - a=2 b=8 * 7,8,9,1,5,6,4,2,3 - a=3 b=6 * 7,8,9,1,2,6,4,5,3 - a=4 b=7 * 7,8,9,1,2,3,4,5,6 - a=5 b=8 */ if(n<0) n=input.length+n; long a=0,b=input.length-n; T tmp; while(a=input.length) { b=input.length-n; } } } This is the times I got for 400 iterations of each rotating an array of 29 elements 2 positions(200 iterations to the left, and 200 iterations to the right). Rotate0: 0.300493s Rotate1: 1.60528s Rotate2: 0.145162s Rotate3: 0.337595s Rotate4: 0.0853269s This is the test/benchmark function code, sorry for the long asserts. void RotateBenchmark() { int[] a=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]; Rotate0(a,2); assert(a==[29,30,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28]); Rotate0(a,-2); assert(a==[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]); Rotate1(a,2); assert(a==[29,30,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28]); Rotate1(a,-2); assert(a==[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]); Rotate2(a,2); assert(a==[29,30,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28]); Rotate2(a,-2); assert(a==[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]); Rotate3(a,2); assert(a==[29,30,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28]); Rotate3(a,-2); assert(a==[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]); Rotate4(a,2); assert(a==[29,30,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28]); Rotate4(a,-2); assert(a==[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]); auto init1 = TickDuration.currSystemTick(); for(auto i=0;i<200;++i) Rotate0(a,2); for(auto i=0;i<200;++i) Rotate0(a,-2); assert(a==[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]); writeln("Rotate0: ",(TickDuration.currSystemTick() - init1).to!("seconds",float),"s"); init1 = TickDuration.currSystemTick(); for(auto i=0;i<200;++i) Rotate1(a,2); for(auto i=0;i<200;++i) Rotate1(a,-2); assert(a==[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]); writeln("Rotate1: ",(TickDuration.currSystemTick() - init1).to!("seconds",float),"s"); init1 =
Re: Best way to clear dynamic array for reuse
On Thursday, 14 July 2016 at 09:12:50 UTC, Jonathan M Davis wrote: So, whether you should be using Appender or assumeSafeAppend or neither depends entirely on what you're doing. However, in general, simply appending to dynamic arrays does not result in many reallocations (just like it doesn't result in a lot of realloctions for std::vector or ArrayList). When reallocations become a problem is when you start slicing a dynamic array so that you have other dynamic arrays which refer to the same memory, and you append to those dynamic arrays, or when you reduce the length of an array and then append to it, because in both of those cases, you're appending to dynamic arrays which do not refer to the last element in their underlying memory block. Hopefully, that makes things at least somewhat clearer. - Jonathan M Davis Thank you Jonathan, that really cleared up a lot of things, I read the article. But I still have this doubt: is assumeSafeAppend() changing a property of the array as "this array is never going to be referenced by any other slice, you can append or change its length any time and it is never going to be reallocated unless it's out of free space"? or it is more like "adjust capacity after last operation" so I should be calling it whenever I am adjusting length or before appending?
Re: Best way to clear dynamic array for reuse
On Wednesday, 13 July 2016 at 17:19:09 UTC, Steven Schveighoffer wrote: On 7/13/16 8:41 AM, Lodovico Giaretta wrote: On Wednesday, 13 July 2016 at 12:37:26 UTC, Miguel L wrote: I tried Appender, but for some reason garbage collector still seems to be running every few iterations. I will try to expand a little on my code because maybe there is something i am missing: Appender!(A[]) a; void foo( out Appender!(A[]) bar) { ... bar~= lot of elements } for() { //a=[]; //discard array contents a.clear(); foo(a) appends thousand of elements to a ... use a for some calculations } Well, I think foo's parameter should be `ref Appender!(A[]) bar` instead of `out Appender!(A[]) bar`. Yes, this is why you still have issues. An out parameter is set to its init value upon function entry, so you have lost all your allocation at that point. Also, if you know you will append lots of elements, doing a.reserve(s), with s being an estimate of the number of appends you expect, might be a good idea. This is true for builtin arrays as well. -Steve Ok, i have read about Appender and assumeSafeAppend(), but i am still a bit confused. What i have understood is: dynamic arrays are (almost) always reallocating when appending to them except assumeSafeAppend() is used, or when wrapping them with Appender. So, if i'm sure there are no slices referencing my array i can use assumeSafeAppend(). But is this permanent? I mean if I declare: array A[] x; x.assumeSafeAppend(); Does that last forever so I can append without reallocating after emptying array x, or should I call assumeSafeAppend() every time I adjust x.length or append to x? Maybe I should give up trying to use dynamic arrays and use fixed length arrays instead.
Re: Best way to clear dynamic array for reuse
On Wednesday, 13 July 2016 at 12:05:18 UTC, Lodovico Giaretta wrote: On Wednesday, 13 July 2016 at 11:59:18 UTC, Miguel L wrote: I am using a temporary dynamic array inside a loop this way: A[] a; for() { a=[]; //discard array contents ... appends thousand of elements to a ... use a for some calculations } I would like to know which would be the best way to clear a contents avoiding reallocations, as there seems to be lots of garbage collection cycles taking place. The options would be: a=[]; a.length=0; a=null; ... any other? Can you help me please? Use std.array.Appender. It allows faster appends, and has a handy .clear method that zeroes the length of the managed array, without de-allocating it, so the same buffer is reused. I tried Appender, but for some reason garbage collector still seems to be running every few iterations. I will try to expand a little on my code because maybe there is something i am missing: Appender!(A[]) a; void foo( out Appender!(A[]) bar) { ... bar~= lot of elements } for() { //a=[]; //discard array contents a.clear(); foo(a) appends thousand of elements to a ... use a for some calculations }
Best way to clear dynamic array for reuse
I am using a temporary dynamic array inside a loop this way: A[] a; for() { a=[]; //discard array contents ... appends thousand of elements to a ... use a for some calculations } I would like to know which would be the best way to clear a contents avoiding reallocations, as there seems to be lots of garbage collection cycles taking place. The options would be: a=[]; a.length=0; a=null; ... any other? Can you help me please?
Re: Most elegant way for split array of struct into components
On Tuesday, 5 July 2016 at 07:33:40 UTC, Ali Çehreli wrote: On 07/04/2016 11:07 PM, Miguel L wrote: > [...] > [...] my_array? The simplest is to pick the element by std.algorithm.map: [...] Thank you Ali, that works. On the same subject, could it be possible to implement some class or struct method that works on an array of objects of that class or method? class A { void Y(A[] a){...} } A[] array; array.Y(); Sorry for my ignorance, i am just starting to work in D.
Most elegant way for split array of struct into components
Hello I would like advice in the most elegant way for doing this in D: I have something like this: struct A { int x; int y; } A[] my_array; And I would need something like this: assert( my_array[0..n].x == [ my_array[0].x, my_array[1].x, ... my_array[n-1].x ]); assert( my_array[0..n].y == [ my_array[0].y, my_array[1].y, ... my_array[n-1].y ]); assert( my_array.x == [ my_array[0].x, my_array[1].x, ... my_array[$-1].x ]); assert( my_array.y == [ my_array[0].y, my_array[1].y, ... my_array[$-1].y ]); Is it possible to implement something like this in D? If not, which would be the best way to get an array of x or y component of my_array? Thank you