Re: Impose structure on array

2019-05-26 Thread 9il via Digitalmars-d-learn

On Monday, 20 May 2019 at 12:09:02 UTC, Alex wrote:
given some array, is there some way to easily impose structure 
on that array at runtime?


void* data;

auto x = cast(byte[A,B,C])data;

X is then an AxBxC matrix.

I'm having to compute the index myself and it just seems 
unnecessary. A and B are not known at compile time though.


Obviously it should be just as efficient as computing the 
offset manually.


I could probably do this with a struct and override opIndex but 
I'm wondering what is already out there. If it's slower or 
consumes more memory than manual it's not worth it(since my 
code already calcs the index).


Slightly updated version of the prev. example.
```
import mir.ndslice;
byte data[];
...
// canonical is `optional`
auto tensor = data.sliced(A, B, C).canonical;
...
byte elem = tensor[i, j, k];
auto matrix = tensor[i, j];
auto otherKindOfMatrix = tensor[0..$, i, j];
```

It is efficient as handwritten code.


Re: Extract code of function

2019-05-26 Thread Andrey via Digitalmars-d-learn

On Sunday, 26 May 2019 at 18:21:23 UTC, Dennis wrote:

On Sunday, 26 May 2019 at 18:14:23 UTC, Jacob Carlborg wrote:

No, that's not possible.


Some hacky solutions are possible by importing a source file as 
a string and parsing it manually. dglsl actually extracts D 
function code to put into glsl shaders. Here's the snippet. 
See: 
https://github.com/icecocoa6/dglsl/blob/master/source/dglsl/translator.d#L140


Interesting solution... Thanks for a hint.


Re: rdmd takes 2-3 seconds on a first-run of a simple .d script

2019-05-26 Thread Jon Degenhardt via Digitalmars-d-learn

On Saturday, 25 May 2019 at 22:18:16 UTC, Andre Pany wrote:

On Saturday, 25 May 2019 at 08:32:08 UTC, BoQsc wrote:
I have a simple standard .d script and I'm getting annoyed 
that it takes 2-3 seconds to run and see the results via rdmd.


Also please keep in mind there could be other factors like slow 
disks, anti virus scanners,... which causes a slow down.


I have seen similar behavior that I attribute to virus scan 
software. After compiling a program, the first run takes several 
seconds to run, after that it runs immediately. I'm assuming the 
first run of an unknown binary triggers a scan, though I cannot 
be completely sure.


Try compiling a new binary in D or C++ and see if a similar 
effect is seen.


--Jon



Re: Extract code of function

2019-05-26 Thread Dennis via Digitalmars-d-learn

On Sunday, 26 May 2019 at 18:14:23 UTC, Jacob Carlborg wrote:

No, that's not possible.


Some hacky solutions are possible by importing a source file as a 
string and parsing it manually. dglsl actually extracts D 
function code to put into glsl shaders. Here's the snippet. See: 
https://github.com/icecocoa6/dglsl/blob/master/source/dglsl/translator.d#L140




Re: Extract code of function

2019-05-26 Thread Aphex via Digitalmars-d-learn

On Sunday, 26 May 2019 at 17:46:35 UTC, Andrey wrote:

Hello,
Is it possible to extract code of some function into string 
variable using CT reflextion?

For example:

int test(bool flag)
{
return flag ? 100 : getRandom();
}

enum string code = GetFunctionCode!test; // "return flag ? 100 
: getRandom();"



You can hack this by using import(filename) and importing the 
file. If you had a good D parser you could potentially do this 
safely. You must use the -J switch though.


Re: Extract code of function

2019-05-26 Thread Jacob Carlborg via Digitalmars-d-learn

On 2019-05-26 19:46, Andrey wrote:

Hello,
Is it possible to extract code of some function into string variable 
using CT reflextion?

For example:

int test(bool flag)
{
    return flag ? 100 : getRandom();
}

enum string code = GetFunctionCode!test; // "return flag ? 100 : 
getRandom();"


No, that's not possible.

--
/Jacob Carlborg


Instantiate templated class at runtime

2019-05-26 Thread Aphex via Digitalmars-d-learn
I have been using Orange SerDes library for an application and 
have run in to a major hurdle. I have the need to SerDes a 
templated class. It seems Orange can only Serialize the class but 
when it attempts to deserialize it at runtime it cannot build the 
class.



It represents the type in the output as `Issue1.Y!string.Y` which 
doesn't even seem right but regardless, it cannot seem to create 
a new instance of Y!T.


I realize that the templates do not probably exist at runtime for 
instantiation and so this is impossible, but Y!string surely does 
exist since I create an object with it(although if separate paths 
exist I could see how it could be an issue).



I need to be able to create such classes because it seems to be 
the only limitation of deseralizing templates:


If, for example, I modify the instance creation code(in 
Reflection.d):


Object newInstance (string name)
{
auto classInfo = ClassInfo.find(name);

if (!classInfo)
{

if (name == "Issue1.Y!string")
{
import Issue1;
return new Y!string;
}
if (name == "Issue1.Y!int")
{
import Issue1;
return new Y!int;
}

		throw new Exception("Cannot find runtime class information for 
`"~name~"` to instantiate!");


return null;
}

return newInstance(classInfo);
}

and manually create the classes then everything seems to work 
great.



I have a few questions:

1. Is it possible to have a runtime class and struct creator 
function that can deal with templates. It doesn't seem like it 
should be that big a deal. One only needs to actually know the 
size of class and that will depend on the template parameter 
sizes, which if they are known then everything is known?


2. Is the output or Orange for templated classes correct? 
"Issue1.Y!string.Y" or should it be "Issue1.Y!string"? Else this 
is a bug that I'll need to track down and fix.


3. I figure that 1 might be a bit off so I will have to hack the 
code to create a factory to build the various templated types 
I'll use. This is not ideal.


In any case, my feeling is that ClassInfo should handle templated 
classes too.




















-- MWE 


import std.stdio;

import orange.serialization.Serializable : nonSerialized;
import std.file;
import orange.serialization._, orange.serialization.archives._;


class C
{
int x = 53;
}

class Y(T) : C
{
T x;

}

int main()
{


auto archive = new XmlArchive!(char); // create an XML archive
	auto serializer = new Serializer(archive); // create the 
serializer


serializer.register!(C);
serializer.register!(Y!int);
serializer.register!(Y!string);

C c = new C();

C[] ya;
auto yi = new Y!int;
yi.x = 22;
auto ys = new Y!string;
ys.x = "sfda";
ya ~= yi;
ya ~= ys;



auto slfp = `test.xml`;
if (!false && exists(slfp))
{   

auto f = std.file.read(slfp);
archive.beginUnarchiving(cast(immutable(void)[])f);
auto ud = archive.untypedData;

ya = serializer.deserialize!(C[])(ud);

} else
{   
// write xml file
archive.beginArchiving();
std.file.write(slfp, cast(byte[])serializer.serialize(ya));
}


return 0;
}





Extract code of function

2019-05-26 Thread Andrey via Digitalmars-d-learn

Hello,
Is it possible to extract code of some function into string 
variable using CT reflextion?

For example:

int test(bool flag)
{
return flag ? 100 : getRandom();
}

enum string code = GetFunctionCode!test; // "return flag ? 100 
: getRandom();"