Re: Linking C Headers to D

2017-01-11 Thread Mike Parker via Digitalmars-d-learn

On Thursday, 12 January 2017 at 04:54:18 UTC, DRex wrote:

Hi,

I am trying to link C and D (using GCC and GDC) and I am 
wondering (I could find no answers on google) if it is possible 
to compile C headers into object files and link them to D?  I 
have a large code base of C headers and am not at a point where 
I can translate them all to D in one go, so I need to be able 
to link to the headers.


Thanks.


You can't compile headers. You compile source files. Do that, and 
yes, you can link object files and libraries with your compiled D 
code. D uses the same file formats and linkers as C.


However, the D compiler needs to know what symbols are available 
for you to use on the C side, so you any C functions you want to 
call and types you want to use have to be translated to D. You 
can do it manually or use a tool like DStep to do the bulk of the 
work for you. See the links in the forum post at [1] for more 
info.


[1] http://forum.dlang.org/post/o55cie$2vb8$1...@digitalmars.com


Linking C Headers to D

2017-01-11 Thread DRex via Digitalmars-d-learn

Hi,

I am trying to link C and D (using GCC and GDC) and I am 
wondering (I could find no answers on google) if it is possible 
to compile C headers into object files and link them to D?  I 
have a large code base of C headers and am not at a point where I 
can translate them all to D in one go, so I need to be able to 
link to the headers.


Thanks.


Re: Auto recursive function

2017-01-11 Thread Ignacious via Digitalmars-d-learn

On Wednesday, 11 January 2017 at 19:23:10 UTC, Razvan Nitu wrote:

Hi,

I am currently trying to create a function 
makeMultidimensionalArray which allocates memory for a 
multidimensional array. It is very similar with [1],

the difference being that it is uninitialized. Here is the code:

auto makeMultidimensionalArray(T, Allocator)(auto ref Allocator 
alloc, size_t[] lengths)

{
if (lengths.length == 1)
{
return makeArray!T(alloc, lengths[0]);
}
else
{
alias E = typeof(makeMultidimensionalArray!T(alloc, 
lengths[1..$]));

auto ret = makeArray!E(alloc, lengths[0]);
foreach (ref e; ret)
e = makeMultidimensionalArray!T(alloc, 
lengths[1..$]);

return ret;
}
}

The lengths[] specifies the lengths for each dimension. The 
problem with this code is that auto is going to be evaluated to 
T[] for the first time and when it
recurs, creating T[][] I get the error "mismatched function 
return type inference of T[][] and T[]". Is there a way to 
surpass that? I saw that in [1]
the recursive call is done by prefixing the function name with 
a '.'; I tried that but it doesn't work. I must be missing 
something, any ideas?


Thanks,
RazvanN

[1] 
https://github.com/dlang/phobos/blob/master/std/experimental/ndslice/slice.d#L834



If you change the return type to a void* your code basically 
works.



void* makeMultidimensionalArray(T, Allocator)(auto ref Allocator 
alloc, size_t[] lengths)

{
if (lengths.length == 1)
{
int x = 0x01FEEF01;
return cast(void*)makeArray!T(alloc, lengths[0], x);
}
else
{
alias E = typeof(makeMultidimensionalArray!T(alloc, 
lengths[1..$]));


auto ret = makeArray!E(alloc, lengths[0]);
foreach (ref e; ret)
e = makeMultidimensionalArray!T(alloc, lengths[1..$]);
return cast(void*)ret;
}
}

The problem is that then you need to cast back and that 
essentially results in the original problem. Can be done but 
probably gonna have to use string mixins.


Re: Android Status

2017-01-11 Thread Ignacious via Digitalmars-d-learn

On Wednesday, 11 January 2017 at 03:49:42 UTC, Joakim wrote:

On Tuesday, 10 January 2017 at 18:48:17 UTC, Ignacious wrote:

[...]


It's probably not easier, and in any case, android-x86 won't be 
supported, largely because I don't have any working x86 devices.


[...]


Ok, well the x86 thing wasn't my idea! It seems I was using the 
wrong build command for trying to compile the examples.


In any case, I'll just wait until things get working a bit 
better. I'd suggest you put, in the title of the page, a bit more 
information. I didn't realize I was looking at an old 
version(which looks too similar to the new one).




Re: Auto recursive function

2017-01-11 Thread Ignacious via Digitalmars-d-learn

On Wednesday, 11 January 2017 at 19:23:10 UTC, Razvan Nitu wrote:

Hi,

I am currently trying to create a function 
makeMultidimensionalArray which allocates memory for a 
multidimensional array. It is very similar with [1],

the difference being that it is uninitialized. Here is the code:

auto makeMultidimensionalArray(T, Allocator)(auto ref Allocator 
alloc, size_t[] lengths)

{
if (lengths.length == 1)
{
return makeArray!T(alloc, lengths[0]);
}
else
{
alias E = typeof(makeMultidimensionalArray!T(alloc, 
lengths[1..$]));

auto ret = makeArray!E(alloc, lengths[0]);
foreach (ref e; ret)
e = makeMultidimensionalArray!T(alloc, 
lengths[1..$]);

return ret;
}
}

The lengths[] specifies the lengths for each dimension. The 
problem with this code is that auto is going to be evaluated to 
T[] for the first time and when it
recurs, creating T[][] I get the error "mismatched function 
return type inference of T[][] and T[]". Is there a way to 
surpass that? I saw that in [1]
the recursive call is done by prefixing the function name with 
a '.'; I tried that but it doesn't work. I must be missing 
something, any ideas?


Thanks,
RazvanN

[1] 
https://github.com/dlang/phobos/blob/master/std/experimental/ndslice/slice.d#L834



This is probably not possible. You are trying to have multiple 
return types for the same function. You are thinking that each 
recursive call is a new template but that doesn't seem to be the 
case.



Instead, maybe try using string mixins to generate the 
allocations. Should be quite easy and will work.


You could also try to use a helper function that you pass the 
fully declared array(all dimensions) and the helper function then 
allocates each dimension recursively... The difference is that 
you are not passing around/returning sub-arrays so you don't have 
to worry about type mismatches.








Re: Auto recursive function

2017-01-11 Thread Martin Nowak via Digitalmars-d-learn

On Wednesday, 11 January 2017 at 19:39:17 UTC, Ali Çehreli wrote:
return choose(lengths.length == 1, one!T(alloc, 
lengths[0]), two!T(alloc, lengths));


Well, choose is the right tool when the choice can only be made 
at runtime. That would be uncommon for dimensionality.


Anyhow mentioning ndslice for multi-dim seems like the sanest tip 
here.

https://github.com/libmir/mir


Re: Auto recursive function

2017-01-11 Thread Razvan Nitu via Digitalmars-d-learn

On Wednesday, 11 January 2017 at 19:30:05 UTC, Stefan Koch wrote:
On Wednesday, 11 January 2017 at 19:23:10 UTC, Razvan Nitu 
wrote:

Hi,

I am currently trying to create a function 
makeMultidimensionalArray which allocates memory for a 
multidimensional array. It is very similar with [1],
the difference being that it is uninitialized. Here is the 
code:


[...]


I believe you should not do this with recursion.
Because that'll instantiate the template multiple times with 
different types.

What is wrong with using ndslice here ?


Maybe you want to allocate memory for more complex data 
structures;
Using the function I am implementing, you will have the 
multidimensional
array initialized with the default values; you don't have to pass 
a slice

from which the initialization data is taken.


Re: Auto recursive function

2017-01-11 Thread Ali Çehreli via Digitalmars-d-learn

On 01/11/2017 11:30 AM, Stefan Koch wrote:

On Wednesday, 11 January 2017 at 19:23:10 UTC, Razvan Nitu wrote:

Hi,

I am currently trying to create a function makeMultidimensionalArray
which allocates memory for a multidimensional array. It is very
similar with [1],
the difference being that it is uninitialized. Here is the code:

[...]


I believe you should not do this with recursion.
Because that'll instantiate the template multiple times with different
types.
What is wrong with using ndslice here ?



Agreed but the question is still valid. I had similar cases before where 
std.range.choose looked like a solution but did not work. Here's my 
current attempt, which fails:


import std.stdio;
import std.experimental.allocator.gc_allocator;
import std.experimental.allocator;

auto one(T, Allocator)(auto ref Allocator alloc, size_t length) {
return makeArray!T(alloc, length);
}

auto two(T, Allocator)(auto ref Allocator alloc, size_t[] lengths) {


// Error: forward reference to inferred return type of function call 
'makeMultidimensionalArray!int(alloc, lengths[1..__dollar])'



alias E = typeof(makeMultidimensionalArray!T(alloc, lengths[1..$]));
auto ret = makeArray!E(alloc, lengths[0]);
foreach (ref e; ret)
e = makeMultidimensionalArray!T(alloc, lengths[1..$]);
return ret;
}

auto makeMultidimensionalArray(T, Allocator)(auto ref Allocator alloc, 
size_t[] lengths)

{
import std.range: choose;

return choose(lengths.length == 1, one!T(alloc, lengths[0]), 
two!T(alloc, lengths));

}

void main() {
writeln(makeMultidimensionalArray!int(GCAllocator.instance, [3, 4]));
}

Ali



Re: Auto recursive function

2017-01-11 Thread Martin Nowak via Digitalmars-d-learn

On Wednesday, 11 January 2017 at 19:23:10 UTC, Razvan Nitu wrote:
auto makeMultidimensionalArray(T, Allocator)(auto ref Allocator 
alloc, size_t[] lengths)

{
if (lengths.length == 1)


Looks like `static if` would fix your specific problem.



Re: Auto recursive function

2017-01-11 Thread Stefan Koch via Digitalmars-d-learn

On Wednesday, 11 January 2017 at 19:23:10 UTC, Razvan Nitu wrote:

Hi,

I am currently trying to create a function 
makeMultidimensionalArray which allocates memory for a 
multidimensional array. It is very similar with [1],

the difference being that it is uninitialized. Here is the code:

[...]


I believe you should not do this with recursion.
Because that'll instantiate the template multiple times with 
different types.

What is wrong with using ndslice here ?



Auto recursive function

2017-01-11 Thread Razvan Nitu via Digitalmars-d-learn

Hi,

I am currently trying to create a function 
makeMultidimensionalArray which allocates memory for a 
multidimensional array. It is very similar with [1],

the difference being that it is uninitialized. Here is the code:

auto makeMultidimensionalArray(T, Allocator)(auto ref Allocator 
alloc, size_t[] lengths)

{
if (lengths.length == 1)
{
return makeArray!T(alloc, lengths[0]);
}
else
{
alias E = typeof(makeMultidimensionalArray!T(alloc, 
lengths[1..$]));

auto ret = makeArray!E(alloc, lengths[0]);
foreach (ref e; ret)
e = makeMultidimensionalArray!T(alloc, lengths[1..$]);
return ret;
}
}

The lengths[] specifies the lengths for each dimension. The 
problem with this code is that auto is going to be evaluated to 
T[] for the first time and when it
recurs, creating T[][] I get the error "mismatched function 
return type inference of T[][] and T[]". Is there a way to 
surpass that? I saw that in [1]
the recursive call is done by prefixing the function name with a 
'.'; I tried that but it doesn't work. I must be missing 
something, any ideas?


Thanks,
RazvanN

[1] 
https://github.com/dlang/phobos/blob/master/std/experimental/ndslice/slice.d#L834


Re: Mixin in Inline Assembly

2017-01-11 Thread Era Scarecrow via Digitalmars-d-learn
On Wednesday, 11 January 2017 at 15:39:49 UTC, Guillaume Piolat 
wrote:
On Wednesday, 11 January 2017 at 06:14:35 UTC, Era Scarecrow 
wrote:


Suddenly reminds me some of the speedup assembly I was writing 
for wideint, but seems I lost my code. too bad, the 128bit 
multiply had sped up and the division needed some work.


I'm a taker if you have some algorithm to reuse 32-bit divide 
in wideint division instead of scanning bits :)


 I remember the divide was giving me some trouble. The idea was 
to try and use the built in registers and limits of the assembly 
to take advantage of full 128bit division, unfortunately if the 
result is too large to fit in a 64bit result it breaks, rather 
than giving me half the result and letting me work with it.


 Still I think I'll impliment my own version and then if it's 
faster I'll submit it.


Re: Check of point inside/outside polygon

2017-01-11 Thread Basile B. via Digitalmars-d-learn

On Tuesday, 26 July 2016 at 13:32:00 UTC, Suliman wrote:
Ideally I need algorithm that can return bool if one polygon 
overlapped/intersected by another. But I do not know math.


After some googling I found topic on SO[1] about point 
inside/outside polygon. It's not directly what I need, but as 
temporal solution would be enough.


Maybe somebody already wrote this algorithm in D. Could you 
share it plz.


I tried to translate algorithm in D, but I do not understand 
some things. For example:


public static bool PointInPolygon(LatLong p, List 
poly) // Ok we are getting `p` - looking point, and `poly` -- 
our polygon. But what format it should have? WKT? Something 
else?


poly.Add(new LatLong { Lat = poly[0].Lat, Lon = poly[0].Lon }); 
// Why we add Lat and Long to poly? And again what it's format?


All other code look work in D to.


[1] 
http://stackoverflow.com/questions/924171/geo-fencing-point-inside-outside-polygon/6786279#6786279


How could I miss this. Working:

https://github.com/BBasile/kheops/blob/master/src/kheops/helpers/polygons.d#L130

It works fine. I've tested it after translation and rotation: 
Okay.


Re: Mixin in Inline Assembly

2017-01-11 Thread Guillaume Piolat via Digitalmars-d-learn
On Wednesday, 11 January 2017 at 06:14:35 UTC, Era Scarecrow 
wrote:


Suddenly reminds me some of the speedup assembly I was writing 
for wideint, but seems I lost my code. too bad, the 128bit 
multiply had sped up and the division needed some work.


I'm a taker if you have some algorithm to reuse 32-bit divide in 
wideint division instead of scanning bits :)


Re: taggedPointer to char array on heap

2017-01-11 Thread Nordlöw via Digitalmars-d-learn

On Wednesday, 11 January 2017 at 00:26:27 UTC, Ali Çehreli wrote:

@property char* p() {


Should be

@property inout(char)* p() inout

Thanks!



Re: Vibe.d: Implementing file upload with WEB interface

2017-01-11 Thread aberba via Digitalmars-d-learn

On Monday, 9 January 2017 at 19:36:18 UTC, aberba wrote:

Turns out my question was answered somewhere on vibe.d website

For those situations where more control is required, it is 
possible to simply declare parameters of type HTTPServerRequest 
or HTTPServerResponse to give a method full access.