Re: Get and add to function body

2017-10-12 Thread Psychological Cleanup via Digitalmars-d-learn
I'd also like to be able to save and store delegates, functions, 
and lambdas. One can't store the pointer to the function because 
it will be invalid, so another means is required, any ideas?


Save("f", (int){ ... }); // Saves to disk
auto f = Load("f"); // Loads from disk
f(3);








Get and add to function body

2017-10-12 Thread Psychological Cleanup via Digitalmars-d-learn
Is there any way to get the function body of a function, 
delegate, and lambda? I'd also like to extend functions by 
"wrapping" them at compile time generically. For example, I'd 
like to get all the properties of a class and add some code to 
them(sort of like adding a scope, prolog, and/or epilog).


Is any of this possible in D?


Best way to display images with GTKD

2017-09-30 Thread Psychological Cleanup via Digitalmars-d-learn
I have to display images with a few controls: alpha channel, 
rotation, scaling, cropping. Generally it should be png but I 
want to be able to handle many images. The images will be memory 
based(I'll load the file in to memory then) and will be displayed 
in a drawing area.


Any ideas how to go about this correctly?


Type conversion

2017-09-09 Thread Psychological Cleanup via Digitalmars-d-learn
I've been trying to get this code to work properly over the last 
days. I simply want to convert one sample format to another. This 
involves simply scaling the values down or up so that they map 
the entire range.


There are now two problems. One is that after I added some more 
pad's to make the formatting easier to read, I get out of memory 
errors.


Second, and the real issue, is that the scaling is not done 
properly.


One would think that it would be simple matter of a linear map 
from [minF,maxF] to [minT,maxT], which is just the equation (maxT 
- minT)/(maxF - minF)*(value - minF) + minT but for some reason 
it does not map perfectly, I suspect, due to rounding... but it 
would be nice for a proper solution that is relatively simple and 
fast as this(using ctfe/templates) to solve the problem.


For example, if value = (maxF + minF)/2 we would expect the 
result to be (maxT + minT)/2 and this is true from the formula, 
but the SampleConvert function does not produce this value. One 
can see this because the test computes this value at compile time 
directly and this is what the SampleConvert should be doing.




string SampleConverterTest()()
{
string pad(string s, int L)
{
import std.array;
if (L - s.length <= 0) return s;
return replicate(" ", L - s.length)~s;
}

import std.string;
string str;
	static foreach(f; AliasSeq!(ubyte, byte, ushort, short, uint, 
int, float, double))

{
{
static if (is(f == float) || is (f == double))
{
enum minF = -1;
enum maxF = 1;
enum midF = 0;
}
else
{
enum minF = f.min;
enum maxF = f.max;
enum midF = (maxF + minF)/2;
}

		static foreach(t; AliasSeq!(ubyte, byte, ushort, short, uint, 
int, float, double))

{
{
static if (is(t == float) || is (t == double))
{
enum minT = -1;
enum maxT = 1;
enum midT = 0;
}
else
{
enum minT = t.min;
enum maxT = t.max;
enum midT = (maxT + minT)/2;
}
import mTemplateHelpers;
enum resmF1 = (SampleConvert!(t,f)(minF));
enum resMF1 = (SampleConvert!(t,f)(maxF));
enum resMidF1 = (SampleConvert!(t,f)(midF));
enum resmF = tRemoveCast!(resmF1);
enum resMF = tRemoveCast!(resMF1);
enum resMidF = tRemoveCast!(resMidF1);
enum s1 = ((maxT - minT)/(cast(double)(maxF - 
minF)));
enum s = tRemoveCast!(s1);

pragma(msg, "F/T(", pad(f.stringof, 6), ", ", pad(t.stringof, 
6), ") -- m/C/M F(", pad(to!string(minF),11), ", ", 
pad(to!string(midF),11), ", ", pad(to!string(maxF),11), ") -- 
m/C/M T(", pad(to!string(minT),11), ", ", 
pad(to!string(midT),11), ", ", pad(to!string(maxT),11), ") --  
m/C/M C(",  pad(resmF,6), ", ",  pad(resMidF,6), ", ",  
pad(resMF,6), ") -- s = ", s);


}
}
}
}
return str;
}

T SampleConvert(T, F)(F from)
{   
import std.algorithm;
static if (is(T == F)) return from;

static foreach(t; ["F", "T"])
mixin("
static if (is("~t~" == float) || is ("~t~" == double))
{
enum min"~t~" = -1;
enum max"~t~" = 1;
} else
{
enum min"~t~" = "~t~".min;
enum max"~t~" = "~t~".max;
}");

enum s = (maxT - minT)/(cast(double)(maxF - minF));
return cast(T)(s*(from - minF) + minT);
}


Re: performance cost of sample conversion

2017-09-07 Thread Psychological Cleanup via Digitalmars-d-learn

On Thursday, 7 September 2017 at 05:45:58 UTC, Ali Çehreli wrote:

On 09/06/2017 07:06 PM, Psychological Cleanup wrote:
if I have a non-double buffer and temporarily convert to 
double then
convert back, do I save many cycles rather than just using a 
double
buffer? I know it will bea lot more memory, but I'm 
specifically talking

about the cycles in converting to and from vs no conversion.

Using a double for everything gives the highest precision and 
makes
things much easier but is that the way to go or does it costs 
quite a

bit in performance?


You have to measure. Here's a start:

import std.conv;
import std.range;
import std.datetime;
import std.stdio;

double workWithDouble(double d) {
return d * d / 7;
}

void workWithFloats(float[] floats) {
foreach (ref f; floats) {
f = workWithDouble(f).to!float;
}
}

void workWithDoubles(double[] doubles) {
foreach (ref d; doubles) {
d = workWithDouble(d);
}
}

void main() {
foreach (n; [ 1_000, 1_000_000, 10_000_000 ]) {
const beg = -1f;
const end = 1f;
const step = (end - beg) / n;
auto floats = iota(beg, end, step).array;
auto doubles = iota(double(beg), end, step).array;
{
auto sw = StopWatch(AutoStart.yes);
workWithDoubles(doubles);
writefln("%10s no   conversion: %10s usecs", n, 
sw.peek().usecs);

}
{
auto sw = StopWatch(AutoStart.yes);
workWithFloats(floats);
writefln("%10s with conversion: %10s usecs", n, 
sw.peek().usecs);

}
}
}

Conversion seems to be more costly:

  1000 no   conversion: 27 usecs
  1000 with conversion: 40 usecs
   100 no   conversion:   1715 usecs
   100 with conversion:   5412 usecs
  1000 no   conversion:  16280 usecs
  1000 with conversion:  47190 usecs

Ali


Thanks. my results

dmd x86 debug
asserts on the line `auto floats = iota(beg, end, 
step).array;`



dmd x64 debug
  1000 no   conversion: 15 usecs
  1000 with conversion:  5 usecs
   100 no   conversion:   2824 usecs
   100 with conversion:   5689 usecs
  1000 no   conversion:  24148 usecs
  1000 with conversion:  56335 usecs

dmd release x86
  1000 no   conversion:  1 usecs
  1000 with conversion:  1 usecs
   100 no   conversion:   1903 usecs
   100 with conversion:   1262 usecs
  1000 no   conversion:  19156 usecs
  1000 with conversion:  12831 usecs

dmd release x64
  1000 no   conversion:  4 usecs
  1000 with conversion: 17 usecs
   100 no   conversion:   4531 usecs
   100 with conversion:   4516 usecs
  1000 no   conversion:  45928 usecs
  1000 with conversion:  46080 usecs

ldc x86 debug
  1000 no   conversion:  3 usecs
  1000 with conversion: 32 usecs
   100 no   conversion:   3563 usecs
   100 with conversion:  19240 usecs
  1000 no   conversion:  35986 usecs
  1000 with conversion: 192025 usecs

ldc x64 debug
  1000 no   conversion:  2 usecs
  1000 with conversion: 10 usecs
   100 no   conversion:   2855 usecs
   100 with conversion:  10309 usecs
  1000 no   conversion:  28254 usecs
  1000 with conversion: 101380 usecs

ldc x86 release
  1000 no   conversion:  0 usecs
  1000 with conversion:  0 usecs
   100 no   conversion:   1280 usecs
   100 with conversion:532 usecs
  1000 no   conversion:  10403 usecs
  1000 with conversion:   5752 usecs

ldc x64 release
  1000 no   conversion:  0 usecs
  1000 with conversion:  1 usecs
   100 no   conversion:887 usecs
   100 with conversion:550 usecs
  1000 no   conversion:  10730 usecs
  1000 with conversion:   5482 usecs

The results are strange, sometimes the conversion wins.



performance cost of sample conversion

2017-09-06 Thread Psychological Cleanup via Digitalmars-d-learn
if I have a non-double buffer and temporarily convert to double 
then convert back, do I save many cycles rather than just using a 
double buffer? I know it will bea lot more memory, but I'm 
specifically talking about the cycles in converting to and from 
vs no conversion.


Using a double for everything gives the highest precision and 
makes things much easier but is that the way to go or does it 
costs quite a bit in performance?


Re: dispatcher

2017-09-06 Thread Psychological Cleanup via Digitalmars-d-learn

On Wednesday, 6 September 2017 at 09:47:34 UTC, user1234 wrote:
On Wednesday, 6 September 2017 at 05:57:18 UTC, Psychological 
Cleanup wrote:
I have a C callback that must call some functions declared in 
D. I can't call them off the C thread because it will result 
in a violation. What is a good way to dispatch the call to the 
main D program?


I'm thinking that I might have to create an extra thread that 
monitors for when a call needs to occur and does so. Doesn't 
seem very effective though? Does anyone know how C# does it?


1/ mark the D function as "extern(C)".
2/ take care to how things are passed to the D functions, 
especially arrays.
3/ take care not to mutate memory created from the D side...i.e 
copy after the call.


This wont work. The callback must be relatively performant and 
must access the D code on the D side normally.





pure void* memset(return void* s, int c, size_t n)?

2017-09-06 Thread Psychological Cleanup via Digitalmars-d-learn

What is the return doing there?


dispatcher

2017-09-06 Thread Psychological Cleanup via Digitalmars-d-learn
I have a C callback that must call some functions declared in D. 
I can't call them off the C thread because it will result in a 
violation. What is a good way to dispatch the call to the main D 
program?


I'm thinking that I might have to create an extra thread that 
monitors for when a call needs to occur and does so. Doesn't seem 
very effective though? Does anyone know how C# does it?


Can attributes trigger functionality?

2017-09-05 Thread Psychological Cleanup via Digitalmars-d-learn


I'm having to create a lot of boiler plate code that creates 
"events" and corresponding properties(getter and setter).


I'm curious if I can simplify this without a string mixin.

If I create my own attribute like

@Event double foo();

and I write any code that will trigger when the event is used and 
add more code(such as the setter property and events that I need?


Obviously I could write some master template that scans 
everything, but that seems to be far too much over kill. A string 
mixin is probably my only option but is a bit ulgy for me.


Since attributes can be defined by structures it seems natural 
that we could put functionality in them that are triggered when 
used but I'm unsure if D has such capabilities.


Thanks.







run unittest inside program

2017-09-05 Thread Psychological Cleanup via Digitalmars-d-learn
Is it possible to run a unit test without adding -unittest to the 
command line?


unittest X
{
   pragma(msg, "Boo!");
}

X;

void main() { }