Installing x32 packages on x64 debian (or fix a dmd bug)

2012-03-27 Thread simendsjo
This might not be the best forum to ask, but as it relates a dmd bug, I  
hope others here have found a workaround.


I'm using a debian-based x64 distro. Due to  
http://d.puremagic.com/issues/show_bug.cgi?id=5570, I have to compile for  
-m32.
The challenge is installing 32-bit libraries using apt-get. I've found  
some "solutions" on the web, but they are all terrible hacks, and I  
haven't gotten it working properly.

Downloading packages directly (and all dependencies!) is a real drag!

Has someone found an acceptable way of dealing with this?


Printing stacktrace on segfault

2012-03-27 Thread simendsjo

Is there a way to print a stacktrace on segfaults on linux?


Re: Installing x32 packages on x64 debian (or fix a dmd bug)

2012-03-27 Thread Jordi Sayol
Al 27/03/12 11:52, En/na simendsjo ha escrit:
> This might not be the best forum to ask, but as it relates a dmd bug, I hope 
> others here have found a workaround.
> 
> I'm using a debian-based x64 distro. Due to 
> http://d.puremagic.com/issues/show_bug.cgi?id=5570, I have to compile for 
> -m32.
> The challenge is installing 32-bit libraries using apt-get. I've found some 
> "solutions" on the web, but they are all terrible hacks, and I haven't gotten 
> it working properly.
> Downloading packages directly (and all dependencies!) is a real drag!
> 
> Has someone found an acceptable way of dealing with this?
> 

Hello simendsjo,

All deb packages at http://dlang.org/download.html includes both 32-bit and 
64-bit libraries, so, on a debian-based x64 distro, just install the 64-bit 
deb, and You'll be able to compile to 32-bit just adding -m32 as argument.
Same situation for debian-based x32 distros, You can compile to 64-bit adding 
-m64 as argument. Of course, you'll not allowed to run a 64-bit binary on a x32 
platform.

Best regards,
-- 
Jordi Sayol


Re: Installing x32 packages on x64 debian (or fix a dmd bug)

2012-03-27 Thread simendsjo

On Tue, 27 Mar 2012 12:18:45 +0200, Jordi Sayol  wrote:


Al 27/03/12 11:52, En/na simendsjo ha escrit:
This might not be the best forum to ask, but as it relates a dmd bug, I  
hope others here have found a workaround.


I'm using a debian-based x64 distro. Due to  
http://d.puremagic.com/issues/show_bug.cgi?id=5570, I have to compile  
for -m32.
The challenge is installing 32-bit libraries using apt-get. I've found  
some "solutions" on the web, but they are all terrible hacks, and I  
haven't gotten it working properly.

Downloading packages directly (and all dependencies!) is a real drag!

Has someone found an acceptable way of dealing with this?



Hello simendsjo,

All deb packages at http://dlang.org/download.html includes both 32-bit  
and 64-bit libraries, so, on a debian-based x64 distro, just install the  
64-bit deb, and You'll be able to compile to 32-bit just adding -m32 as  
argument.
Same situation for debian-based x32 distros, You can compile to 64-bit  
adding -m64 as argument. Of course, you'll not allowed to run a 64-bit  
binary on a x32 platform.


Best regards,


Thanks, but I was probably not very clear when writing the question.
DMD itself is not a problem (rather than the bug which forces me to only  
use 32-bit), it's all the packages available through the package manager -  
it only downloads 64-bit libraries.
If I want to use a c library, I need to find both the library and all it's  
dependencies as 32-bit libraries rather than just installing through the  
package manager.

It can be a tedious process..


Re: Installing x32 packages on x64 debian (or fix a dmd bug)

2012-03-27 Thread Jordi Sayol
Al 27/03/12 12:40, En/na simendsjo ha escrit:
> On Tue, 27 Mar 2012 12:18:45 +0200, Jordi Sayol  wrote:
> 
>> Al 27/03/12 11:52, En/na simendsjo ha escrit:
>>> This might not be the best forum to ask, but as it relates a dmd bug, I 
>>> hope others here have found a workaround.
>>>
>>> I'm using a debian-based x64 distro. Due to 
>>> http://d.puremagic.com/issues/show_bug.cgi?id=5570, I have to compile for 
>>> -m32.
>>> The challenge is installing 32-bit libraries using apt-get. I've found some 
>>> "solutions" on the web, but they are all terrible hacks, and I haven't 
>>> gotten it working properly.
>>> Downloading packages directly (and all dependencies!) is a real drag!
>>>
>>> Has someone found an acceptable way of dealing with this?
>>>
>>
>> Hello simendsjo,
>>
>> All deb packages at http://dlang.org/download.html includes both 32-bit and 
>> 64-bit libraries, so, on a debian-based x64 distro, just install the 64-bit 
>> deb, and You'll be able to compile to 32-bit just adding -m32 as argument.
>> Same situation for debian-based x32 distros, You can compile to 64-bit 
>> adding -m64 as argument. Of course, you'll not allowed to run a 64-bit 
>> binary on a x32 platform.
>>
>> Best regards,
> 
> Thanks, but I was probably not very clear when writing the question.
> DMD itself is not a problem (rather than the bug which forces me to only use 
> 32-bit), it's all the packages available through the package manager - it 
> only downloads 64-bit libraries.

This is not strictly truth. dmd deb packages installs, among others, 
"gcc-multilib", witch make possible to compile to 32-bit in x64 platform and 
vice versa .

> If I want to use a c library, I need to find both the library and all it's 
> dependencies as 32-bit libraries rather than just installing through the 
> package manager.
> It can be a tedious process..
> 

If you want to run a 32-bit program on a x64, install "ia32-libs" and all its 
dependencies, through the package manager.

Best regards,
-- 
Jordi Sayol


Re: Rewrite of std.range docs (Was: Re: Making sense of ranges)

2012-03-27 Thread Marco Leise
Am Tue, 27 Mar 2012 06:00:58 +0200
schrieb "Jesse Phillips" :

> On Monday, 26 March 2012 at 00:50:32 UTC, H. S. Teoh wrote:
> 
> > This thread has further convinced me that std.range's docs 
> > *need* this
> > rewrite. So here's my first attempt at it:
> >
> > https://github.com/quickfur/phobos/tree/stdrange_docs
> 
> I find that opening to be much better. Look forward to the 
> improvement.

I agree. To the newcomer it is now easy to see the rationale behind ranges and 
why they should take the time to understand the concept. What I missed when I 
started with D, was some explanation that the returned ranges from many 
algorithms are compile time generated structs. I often found myself wondering 
why I don't get an array returned when I put one into the algorithm and had to 
do array(...) all over the place, when actually I could often have passed on 
the ranges directly to a following foreach or similar.

"Ranges whose elements are sorted affords ..." <- insert a comma before affords 
perhaps? It would help non-native speakers.

-- 
Marco



std.stream.File help required (and classes)

2012-03-27 Thread akaz

Hi all,

 I am trying to port some application based on a library called 
mediastreamer2, part of linphone software.


 The basic software component built on top of mediastreamer2 is 
called a "filter".


 Basically, it is a C structure with parameters and some methods 
(pointers to functions). Ammong the latter: a kind of constructor 
(.init), a kind of destructor (.uninit), some setters and a sort 
of "run method" (.process).


 The "run method" is called by an independent thread (a time 
ticker). It reads some queues, extract messages (packaged data) 
from there and process this data.


 Now, I implemented a filter which basically takes packaged data 
(messages) from an input queue and *should* write this data into 
a file.


 This is built around a std.stream.File class.

 Now, the questions:

 1. why there is std.stdio.File, but also std.stream.File? This 
gives a conflict and explicit names must then be used to avoid 
conflict.


 2. is the std.stream.File the correct choice here? Should I use 
std.file instead? Then, why so many file classes (count 
std.stdio.File too).


 3. std.stream.File does not have, apparently, some 
easy-to-interogate properties to know the name (path) and the 
state of the associated file. Are there any?


 4. the "run method" seems to lose knowledge about the file. The 
file is opened in a setter and then it should remain seekable and 
writeable. However, into the "run method" it appears as 
unseekable and unwriteable. More, trying to access .toString() 
property of the std.stream.File variable or to write some data 
into the file triggers a segmentation fault error.


 Here is the file msf_sinker_binaryfile.d, but if required, then 
I will post other files too. I am not sure how to extract a 
simpler test case.


//msf_sinker_binaryfile.d==
module msf_sinker_binaryfile;
import std.stream;
import std.stdio;

import mediastreamer2_layer;
import msf_commons;

const uint MSF_SINKER_BINARYFILE_OPEN = 
MS_FILTER_METHOD!(byte)(cast(MSFilterId)MSFilterIdNew.MSF_SINKER_BINARYFILE_ID,0);
const uint MSF_SINKER_BINARYFILE_CLOSE = 
MS_FILTER_METHOD_NO_ARG(cast(MSFilterId)MSFilterIdNew.MSF_SINKER_BINARYFILE_ID,1);


private struct _MSF_State{
__gshared std.stream.File filedesc; //file descriptor
};
private alias _MSF_State MSF_State;

private void msf_init(MSFilter* f){ //a constructor

printf("msf_binfile_init-start\n\n");

MSF_State* s=ms_new!(MSF_State)(1);
s.filedesc = new std.stream.File;
printf("s.filedesc=%p+\n",s.filedesc);

writef("s.filedesc.seekable=%b+\n",s.filedesc.seekable);

writef("s.filedesc.readable=%b+\n",s.filedesc.readable);

writef("s.filedesc.writeable=%b+\n",s.filedesc.writeable);

writef("s.filedesc.toString=%b+\n",s.filedesc.toString);

f.data=s;
printf("msf_binfile_init-stop\n\n");
}

private int msf_open(MSFilter* f, void* arg){ //a setter

printf("msf_binfile_open-start\n\n");

MSF_State* s=cast(MSF_State*)f.data;
ms_mutex_lock(&(f.lock));
printf("s.filedesc=%p+\n",s.filedesc);

writef("s.filedesc.seekable=%b+\n",s.filedesc.seekable);

writef("s.filedesc.readable=%b+\n",s.filedesc.readable);

writef("s.filedesc.writeable=%b+\n",s.filedesc.writeable);


writef("s.filedesc.toString=%b+\n",s.filedesc.toString);
s.filedesc.open(*(cast(string*)arg),FileMode.OutNew);
printf("s.filedesc=%p+\n",s.filedesc);

writef("s.filedesc.seekable=%b+\n",s.filedesc.seekable);

writef("s.filedesc.readable=%b+\n",s.filedesc.readable);

writef("s.filedesc.writeable=%b+\n",s.filedesc.writeable);


writef("s.filedesc.toString=%b+\n",s.filedesc.toString);
ms_mutex_unlock(&(f.lock));
printf("msf_binfile_open-stop\n\n");
return 0;
}

private void msf_process(MSFilter* f){ //a run() method

printf("msf_binfile_process-start\n\n");

MSF_State* s=cast(MSF_State*)f.data;
mblk_t* m;
	while((m=ms_queue_get(f.inputs[0]))!=null){ //reading samples 
from input

ms_mutex_lock(&(f.lock));
		int len=cast(int)((m.b_wptr-m.b_rptr)); //length of message 
data block

byte* p=cast(byte*)m.b_rptr;
/*printf("\n");
for(int idx=0; idx
writef("s.filedesc.seekable=%b+\n",s.filedesc.seekable);

writef("s.filedesc.readable=%b+\n",s.filedesc.readable);

writef("s.filedesc.writeable=%b+

Re: std.stream.File help required (and classes)

2012-03-27 Thread akaz
I should at that the "__gshared" attribute was added in distress, 
but changed nothing. With or without it, the program still 
crashes.


Re: Rewrite of std.range docs (Was: Re: Making sense of ranges)

2012-03-27 Thread Mike Parker

On 3/27/2012 7:26 PM, Marco Leise wrote:

Am Tue, 27 Mar 2012 06:00:58 +0200
schrieb "Jesse Phillips":


On Monday, 26 March 2012 at 00:50:32 UTC, H. S. Teoh wrote:


This thread has further convinced me that std.range's docs
*need* this
rewrite. So here's my first attempt at it:

https://github.com/quickfur/phobos/tree/stdrange_docs


I find that opening to be much better. Look forward to the
improvement.


I agree. To the newcomer it is now easy to see the rationale behind ranges and 
why they should take the time to understand the concept. What I missed when I 
started with D, was some explanation that the returned ranges from many 
algorithms are compile time generated structs. I often found myself wondering 
why I don't get an array returned when I put one into the algorithm and had to 
do array(...) all over the place, when actually I could often have passed on 
the ranges directly to a following foreach or similar.



Yes. When I saw that the implementation of ranges required more than a 
few minutes of digging around to comprehend, I just avoided them 
completely for quite a while. When I finally did decide to roll up my 
sleeves and get dirty, the lack of documentation in Phobos or on the web 
site was particularly frustrating. I was going to complain about it here 
in the newsgroups (not sure if I did or not), but then Ali announced the 
translation of his book chapter on ranges and I was enlightened.



"Ranges whose elements are sorted affords ..."<- insert a comma before affords 
perhaps? It would help non-native speakers.



Actually, a comma there would be incorrect. But because 'Ranges' is 
plural, 'affords' should lose the 's' (Ranges whose elements are sorted 
afford...). If the wording is confusing, perhaps it could be rewritten 
as "Sorted ranges afford..."




Re: Installing x32 packages on x64 debian (or fix a dmd bug)

2012-03-27 Thread simendsjo

On Tue, 27 Mar 2012 13:24:06 +0200, Jordi Sayol  wrote:


Al 27/03/12 12:40, En/na simendsjo ha escrit:
On Tue, 27 Mar 2012 12:18:45 +0200, Jordi Sayol   
wrote:



Al 27/03/12 11:52, En/na simendsjo ha escrit:
This might not be the best forum to ask, but as it relates a dmd bug,  
I hope others here have found a workaround.


I'm using a debian-based x64 distro. Due to  
http://d.puremagic.com/issues/show_bug.cgi?id=5570, I have to compile  
for -m32.
The challenge is installing 32-bit libraries using apt-get. I've  
found some "solutions" on the web, but they are all terrible hacks,  
and I haven't gotten it working properly.

Downloading packages directly (and all dependencies!) is a real drag!

Has someone found an acceptable way of dealing with this?



Hello simendsjo,

All deb packages at http://dlang.org/download.html includes both  
32-bit and 64-bit libraries, so, on a debian-based x64 distro, just  
install the 64-bit deb, and You'll be able to compile to 32-bit just  
adding -m32 as argument.
Same situation for debian-based x32 distros, You can compile to 64-bit  
adding -m64 as argument. Of course, you'll not allowed to run a 64-bit  
binary on a x32 platform.


Best regards,


Thanks, but I was probably not very clear when writing the question.
DMD itself is not a problem (rather than the bug which forces me to  
only use 32-bit), it's all the packages available through the package  
manager - it only downloads 64-bit libraries.


This is not strictly truth. dmd deb packages installs, among others,  
"gcc-multilib", witch make possible to compile to 32-bit in x64 platform  
and vice versa .


If I want to use a c library, I need to find both the library and all  
it's dependencies as 32-bit libraries rather than just installing  
through the package manager.

It can be a tedious process..



If you want to run a 32-bit program on a x64, install "ia32-libs" and  
all its dependencies, through the package manager.


Best regards,


I have ia32-libs, but say I want to install mysql? I can only get x64  
versions thourgh the package manager. Trying to download and install  
32-bit .deps says it's conflicting with the installed x64 library.


Re: GUI library

2012-03-27 Thread Tyro[17]

On Sunday, 25 March 2012 at 15:59:21 UTC, Jacob Carlborg wrote:

On 2012-03-25 17:22, Kevin Cox wrote:
I would reccomend Qt as well.  You will get native 
cross-platform
widgets with great performance.  I am not sure how far QtD is 
but I know

it once had a lot of development on it.


I don't think Qt is uses the native drawing operations on Mac 
OS X.


Thanks to you both for your assistance.


Re: Installing x32 packages on x64 debian (or fix a dmd bug)

2012-03-27 Thread Jordi Sayol
Al 27/03/12 15:03, En/na simendsjo ha escrit:
> On Tue, 27 Mar 2012 13:24:06 +0200, Jordi Sayol  wrote:
> 
>> Al 27/03/12 12:40, En/na simendsjo ha escrit:
>>> On Tue, 27 Mar 2012 12:18:45 +0200, Jordi Sayol  wrote:
>>>
 Al 27/03/12 11:52, En/na simendsjo ha escrit:
> This might not be the best forum to ask, but as it relates a dmd bug, I 
> hope others here have found a workaround.
>
> I'm using a debian-based x64 distro. Due to 
> http://d.puremagic.com/issues/show_bug.cgi?id=5570, I have to compile for 
> -m32.
> The challenge is installing 32-bit libraries using apt-get. I've found 
> some "solutions" on the web, but they are all terrible hacks, and I 
> haven't gotten it working properly.
> Downloading packages directly (and all dependencies!) is a real drag!
>
> Has someone found an acceptable way of dealing with this?
>

 Hello simendsjo,

 All deb packages at http://dlang.org/download.html includes both 32-bit 
 and 64-bit libraries, so, on a debian-based x64 distro, just install the 
 64-bit deb, and You'll be able to compile to 32-bit just adding -m32 as 
 argument.
 Same situation for debian-based x32 distros, You can compile to 64-bit 
 adding -m64 as argument. Of course, you'll not allowed to run a 64-bit 
 binary on a x32 platform.

 Best regards,
>>>
>>> Thanks, but I was probably not very clear when writing the question.
>>> DMD itself is not a problem (rather than the bug which forces me to only 
>>> use 32-bit), it's all the packages available through the package manager - 
>>> it only downloads 64-bit libraries.
>>
>> This is not strictly truth. dmd deb packages installs, among others, 
>> "gcc-multilib", witch make possible to compile to 32-bit in x64 platform and 
>> vice versa .
>>
>>> If I want to use a c library, I need to find both the library and all it's 
>>> dependencies as 32-bit libraries rather than just installing through the 
>>> package manager.
>>> It can be a tedious process..
>>>
>>
>> If you want to run a 32-bit program on a x64, install "ia32-libs" and all 
>> its dependencies, through the package manager.
>>
>> Best regards,
> 
> I have ia32-libs, but say I want to install mysql? I can only get x64 
> versions thourgh the package manager. Trying to download and install 32-bit 
> .deps says it's conflicting with the installed x64 library.
> 

Ok, then you can try chroot command into a directory created with debootstrap. 
Also you can install a debian-based x32 distro onto a virtualbox machine.

Best regards,
-- 
Jordi Sayol


Re: Printing stacktrace on segfault

2012-03-27 Thread David

Am 27.03.2012 12:04, schrieb simendsjo:

Is there a way to print a stacktrace on segfaults on linux?


I haven't found one, but you can use gdb, the only thing you've to do is 
to compile with -g and -gc (or you use gdc)


Problem about lambda expressions

2012-03-27 Thread Tongzhou Li

Hello again! I'm learning D, and I encountered a problem.
I tried this code:
http://ideone.com/hkpT6
It works well. (Have no idea why codepad.org failed to compile it)
I tried to write a lambda instead of function f, but I got 
nothing printed.

Did I make something wrong?
Compiler used: DMD32 D Compiler v2.058 (Win7 SP1 x64)
Sorry for my poor English :)


Re: Installing x32 packages on x64 debian (or fix a dmd bug)

2012-03-27 Thread simendsjo

On Tue, 27 Mar 2012 15:11:38 +0200, Jordi Sayol  wrote:


Al 27/03/12 15:03, En/na simendsjo ha escrit:


I have ia32-libs, but say I want to install mysql? I can only get x64  
versions thourgh the package manager. Trying to download and install  
32-bit .deps says it's conflicting with the installed x64 library.




Ok, then you can try chroot command into a directory created with  
debootstrap. Also you can install a debian-based x32 distro onto a  
virtualbox machine.


Best regards,


Thanks. The virtualbox solution seems quite simple - I'll go for that one.


Re: Problem about lambda expressions

2012-03-27 Thread Tongzhou Li

Oh, I also tried:
void seq_apply(Params..., Args...)(void delegate(Params) 
func, Args args)

But I got a error:
variadic template parameter must be last
Does it mean that there can only be one variadic template 
parameter? How to fix it?

Thanks


Re: Problem with receiveOnly and classes

2012-03-27 Thread kraybourne

On 3/25/12 19:33 , Ghislain wrote:

Hello,

[...]
I do not understand why an object of type A is fetched as a Variant, while
a object of type B is received correctly.

[...]
Any idea?


Hi!

I get the same on Mac DMD 2.058. I have no idea. Looks like a bug to me, 
although I can't say which part is wrong/right.


The chapter on concurrency in TDPL ( 
http://www.informit.com/articles/article.aspx?p=1609144&seqNum=6 ) only 
ever uses values and arrays as examples, and ddocs sort of strategically 
says nothing, so I guess it's not really clear if passing objects should 
be possible at all?


Either way, I once tried to pass over "ownership" of trees of objects to 
other threads but gave up on that. I don't think std.concurrency and D 
really can work like that. (?) Concurrency seems to work much better 
when you pass messages and data, structs are fine, and then build object 
trees from that on the other side etc. Sorry I can't help more.


Re: Problem about lambda expressions

2012-03-27 Thread dennis luehring

Am 27.03.2012 15:52, schrieb Tongzhou Li:

Oh, I also tried:
  void seq_apply(Params..., Args...)(void delegate(Params)
func, Args args)
But I got a error:
  variadic template parameter must be last
Does it mean that there can only be one variadic template
parameter? How to fix it?
Thanks


just a question:

how on earth should the compiler seperate your 2 variadic parameters???

t( a,b,c,x,y,z ) -> where Params Start/End, where Args magic?


Re: Rewrite of std.range docs (Was: Re: Making sense of ranges)

2012-03-27 Thread H. S. Teoh
On Tue, Mar 27, 2012 at 09:55:43PM +0900, Mike Parker wrote:
> On 3/27/2012 7:26 PM, Marco Leise wrote:
[...]
> >"Ranges whose elements are sorted affords ..."<- insert a comma
> >before affords perhaps? It would help non-native speakers.
> >
> 
> Actually, a comma there would be incorrect. But because 'Ranges' is
> plural, 'affords' should lose the 's' (Ranges whose elements are
> sorted afford...). If the wording is confusing, perhaps it could be
> rewritten as "Sorted ranges afford..."

Fixed, thanks for catching that grammatical glitch.


T

-- 
Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it. -- Brian W. Kernighan


Re: std.stream.File help required (and classes)

2012-03-27 Thread H. S. Teoh
On Tue, Mar 27, 2012 at 01:58:03PM +0200, akaz wrote:
[...]
>  Now, the questions:
> 
>  1. why there is std.stdio.File, but also std.stream.File? This
> gives a conflict and explicit names must then be used to avoid
> conflict.
[...]

This is a design flaw that will be fixed eventually. There's a std.io
module in the works that will eventually replace std.stream and
std.stdio. Unfortunately it's at a roadblock right now.

For the conflicts, you could try using 'alias' to disambiguate between
them, or any of the various import aliasing tools.


T

-- 
The early bird gets the worm. Moral: ewww...


Re: Problem about lambda expressions

2012-03-27 Thread Artur Skawina
On 03/27/12 15:52, Tongzhou Li wrote:
> Oh, I also tried:
> void seq_apply(Params..., Args...)(void delegate(Params) func, Args args)
> But I got a error:
> variadic template parameter must be last
> Does it mean that there can only be one variadic template parameter? How to 
> fix it?

I'm not sure what exactly you're trying to do, but maybe this will help:

void seq_apply(Func, Args...)(Func func, Args args) {
import std.traits;
alias ParameterTypeTuple!Func Params;
enum ArgNum = Params.length-1;
func(args[0], args[1 .. ArgNum + 1]);
static if (args.length > ArgNum + 1) {
seq_apply(func, args[ArgNum + 1 .. args.length]);
}
}

artur


Re: std.stream.File help required (and classes)

2012-03-27 Thread Steven Schveighoffer
On Tue, 27 Mar 2012 10:46:08 -0400, H. S. Teoh   
wrote:



On Tue, Mar 27, 2012 at 01:58:03PM +0200, akaz wrote:
[...]

 Now, the questions:

 1. why there is std.stdio.File, but also std.stream.File? This
gives a conflict and explicit names must then be used to avoid
conflict.

[...]

This is a design flaw that will be fixed eventually. There's a std.io
module in the works that will eventually replace std.stream and
std.stdio. Unfortunately it's at a roadblock right now.


I just want to correct some inaccuracies:

1. std.io will *not* replace std.stdio, it will replace std.stream.   
std.stdio.File will be able to use std.io's objects as implementation, but  
this will be transparent to existing code.

2. There is not a roadblock, I'm working on it very slowly is all :)

I'm hoping to have something more concrete soon.  I just got RefCounted to  
work with classes, and that was a major issue, since std.io is  
interface/class based.


-Steve


Re: Problem about lambda expressions

2012-03-27 Thread Kenji Hara

On Tuesday, 27 March 2012 at 13:42:30 UTC, Tongzhou Li wrote:

Hello again! I'm learning D, and I encountered a problem.
I tried this code:
http://ideone.com/hkpT6
It works well. (Have no idea why codepad.org failed to compile 
it)
I tried to write a lambda instead of function f, but I got 
nothing printed.

Did I make something wrong?
Compiler used: DMD32 D Compiler v2.058 (Win7 SP1 x64)
Sorry for my poor English :)


(obj x, int a0, int a1) => { x.setxxx(a0); x.setyyy(a1); }

This lambda expression returns *a delegate has no parameter*.
Instead:

(obj x, int a0, int a1) => (x.setxxx(a0), x.setyyy(a1))

or:

(obj x, int a0, int a1){ x.setxxx(a0); x.setyyy(a1); }



Re: std.stream.File help required (and classes)

2012-03-27 Thread akaz
Thank you. But why do I lose access to my std.stream.File file? 
Somehow, the variable gets unallocated, thus the file is closed 
back?


With pointers of C it used to be so simple... variable remained 
allocated untel the corresponding free().


I do not quite grasp this (a bit) awkward mix between pointers 
and classes (references?). Sometimes is like C/C++, sometimes is 
like Java...




Re: Printing stacktrace on segfault

2012-03-27 Thread Jonathan M Davis
On Tuesday, March 27, 2012 12:04:59 simendsjo wrote:
> Is there a way to print a stacktrace on segfaults on linux?

You can do it if you install a signal handler for sigsegv and use 
backtrace_symbols to construct a stacktrace. Or you can just run the program 
in gdb or turn core dumps on and use gdb to inspect the core dump. But the 
first solution won't require you to rerun the program or always run with core 
dumps enabled. It _is_ more work though, if you've never done it.

- Jonathan M Davis


Re: Problem about lambda expressions

2012-03-27 Thread Ali Çehreli

On 03/27/2012 06:42 AM, Tongzhou Li wrote:
> Hello again! I'm learning D, and I encountered a problem.
> I tried this code:
> http://ideone.com/hkpT6
> It works well. (Have no idea why codepad.org failed to compile it)
> I tried to write a lambda instead of function f, but I got nothing 
printed.


The lambda syntax (=>) is only for when there is a single return 
statement in the function literal:


  http://dlang.org/expression.html#Lambda

As Kenji Hara has shown, you can have more than one expression in the 
return statement by taking advantage of the comma operator but 
personally I would use something else instead: the longer syntax, a 
local function, etc.


Ali


Re: Printing stacktrace on segfault

2012-03-27 Thread Jérôme M. Berger
simendsjo wrote:
> Is there a way to print a stacktrace on segfaults on linux?

catchsegv (part of glibc, so should be available on just about all
Linux distros...)

Jerome
-- 
mailto:jeber...@free.fr
http://jeberger.free.fr
Jabber: jeber...@jabber.fr



signature.asc
Description: OpenPGP digital signature


D Dll injection problem

2012-03-27 Thread Gyron
Hey there, I want to inject a dll which was created in D into a c 
Program.


Informations:
DMD vs. 2.058
IDE: MonoDevelop with Mono-D
System: Windows 7 64bit

Program Informations:
32-bit
written in c

The Injector is working for sure, so thats not the Problem.

the Source of the DLL:
import std.c.windows.windows;
import core.sys.windows.dll;

__gshared HINSTANCE g_hInst;

extern (Windows)
BOOL DllMain(HINSTANCE hInstance, ULONG ulReason, LPVOID 
pvReserved)

{
final switch (ulReason)
{
case DLL_PROCESS_ATTACH:
g_hInst = hInstance;
dll_process_attach( hInstance, true );
break;

case DLL_PROCESS_DETACH:
dll_process_detach( hInstance, true );
break;

case DLL_THREAD_ATTACH:
dll_thread_attach( true, true );
break;

case DLL_THREAD_DETACH:
dll_thread_detach( true, true );
break;
}
return true;
}

It builds fine, but If I inject it, the program (where the dll is 
injected) says that the dll is not a valid image.

screenshot: http://imagr.eu/up/4f72240329a846_Unbenannt.png

Maybe it's because I have no def file.
But i dont know how to link it with Mono-D.



Re: std.stream.File help required (and classes)

2012-03-27 Thread Ali Çehreli

On 03/27/2012 04:58 AM, akaz wrote:

> 2. is the std.stream.File the correct choice here? Should I use std.file
> instead? Then, why so many file classes (count std.stdio.File too).

std.file is more about files and directories, not file contents. I've 
abandoned std.stream.File some time ago. I just use std.stdio.File 
partly because stdio, stdout, and stderr are of that type anyway. It 
works with ranges as well.


> 3. std.stream.File does not have, apparently, some easy-to-interogate
> properties to know the name (path) and the state of the associated file.

std.stdio.File provides name() for the name and getFP() if you want to 
access the underlying FILE*.



> private void msf_init(MSFilter* f){ //a constructor
> printf("msf_binfile_init-start\n\n");
> MSF_State* s=ms_new!(MSF_State)(1);
> s.filedesc = new std.stream.File;
[...]
> f.data=s;

We are assuming that f never goes away and that f.data is not assigned 
anything else, right? Otherwise the garbage collector will destroy that 
MSF_State and reclaim its memory at an indeterminate time. Is it 
possible that you use the MSF_State object longer than you should, after 
it's lifetime ended? If you did, the object would look usable for a 
while until the GC collects it.


Ali



Re: D Dll injection problem

2012-03-27 Thread maarten van damme
when I tried the previous dmd compiler (have yet to try the curent one on
this problem) I got the same problems while trying to compile a dll and use
it. I have no clue as to why this is happening. worked in 2.54 I thought


Re: D Dll injection problem

2012-03-27 Thread Gyron
On Tuesday, 27 March 2012 at 20:45:52 UTC, maarten van damme 
wrote:
when I tried the previous dmd compiler (have yet to try the 
curent one on
this problem) I got the same problems while trying to compile a 
dll and use
it. I have no clue as to why this is happening. worked in 2.54 
I thought


I thought D would be a good alternative for c++, but as it seems 
I need to stay with c++ :/

Thats really bad, a minus point on my "Why choose D" list.


Re: D Dll injection problem

2012-03-27 Thread Trass3r

Maybe it's because I have no def file.


Very possible.
Just pass it to dmd like the other files.
Or try the new -shared flag.


Re: std.stream.File help required (and classes)

2012-03-27 Thread akaz
std.file is more about files and directories, not file 
contents. I've abandoned std.stream.File some time ago. I just 
use std.stdio.File partly because stdio, stdout, and stderr are 
of that type anyway. It works with ranges as well.


should be re-named std.folder, then, or std.filesystem. having 
another "file" there is quite confusing. Oh, no! there is also 
a... std.cstream!



std.stdio.File provides name() for the name and getFP() if you 
want to access the underlying FILE*.


I understand that std.stdio.File is the newer approach. OK, I 
will try to use that instead. BUT: what is the equivalent of 
std.stream.File.writeBlock(const void* buffer, size_t size)? I 
see there is a std.stdio.rawWrite(T)(in T[] buffer);


But, my data is: a (byte*) pointer and a length. How do I write 
something like
std.stream.File.writeblock(cast(byte*)p,cast(int)len) using 
std.stdio.File.rawWrite?


And why std.stream.File is not marked as deprecated if a better 
(newer) alternative exists?





> private void msf_init(MSFilter* f){ //a constructor
> 
printf("msf_binfile_init-start\n\n");

> MSF_State* s=ms_new!(MSF_State)(1);
> s.filedesc = new std.stream.File;
[...]
> f.data=s;

We are assuming that f never goes away and that f.data is not 
assigned anything else, right? Otherwise the garbage collector 
will destroy that MSF_State and reclaim its memory at an 
indeterminate time. Is it possible that you use the MSF_State 
object longer than you should, after it's lifetime ended? If 
you did, the object would look usable for a while until the GC 
collects it.


Yes, that should be the case. f is the mother structure (a 
pointer towards a MSFilter structure) that should never disappear 
and f.data should not be assigned somewhere else. I have several 
other "filters" implemented using the same schema and they seem 
to work quite well, except that they do not use files or... 
classes (I still cannot grasp that fundamental thing when a class 
instance is a variable and when it is a pointer; when is T x; and 
when is T* x = new T(); or, when is T* x=(T*)calloc(1,sizeof(T));)


For the record, here is the "main.d" filter:

=main.d===
import std.stdio;
import core.thread;
import std.math;
import mediastreamer2_layer;
import msf_sourcer_sinusoid;
import msf_converter_double2int16_t;
import msf_sinker_binaryfile;
import msf_processor_split;
import msf_sinker_void;

int main(char[][] args)
{
MSTicker* ticker;
MSFilter* src_sinus;
MSFilter* cnv_dbl2int16_t;
MSFilter* snk_sndcrd;
MSFilter* snk_binfile;
MSFilter* prc_split1;
MSFilter* prc_split2;
MSFilter* snk_void;

printf("---START PROGRAM---\n");
ms_init();
src_sinus=ms_filter_new_from_desc(&msf_sourcer_sinusoid_desc);

cnv_dbl2int16_t=ms_filter_new_from_desc(&msf_converter_double2int16_t_desc);

snk_sndcrd=ms_snd_card_create_writer(ms_snd_card_manager_get_default_card(ms_snd_card_manager_get()));

snk_binfile=ms_filter_new_from_desc(&msf_sinker_binaryfile_desc);

prc_split1=ms_filter_new_from_desc(&msf_processor_split_desc);
prc_split2=ms_filter_new_from_desc(&msf_processor_split_desc);
snk_void=ms_filter_new_from_desc(&msf_sinker_void_desc);
double f0=1000.0;
ms_filter_call_method(src_sinus,MSF_SOURCER_SINUSOID_SET_FREQ,&f0);
double fe=4000.0;
ms_filter_call_method(src_sinus,MSF_SOURCER_SINUSOID_SET_RATE,&fe);
int fe_int=cast(int)round(fe);
ms_filter_call_method(snk_sndcrd,MS_FILTER_SET_SAMPLE_RATE,&fe_int);
string numefisier="file_givenname.bin";

ms_filter_call_method(snk_binfile,MSF_SINKER_BINARYFILE_OPEN,cast(void*)&numefisier);
ms_filter_link(src_sinus,0,prc_split1,0);
ms_filter_link(prc_split1,0,cnv_dbl2int16_t,0);
ms_filter_link(prc_split1,1,snk_binfile,0);
ms_filter_link(cnv_dbl2int16_t,0,snk_sndcrd,0);
ticker=ms_ticker_new();
ms_ticker_attach(ticker,src_sinus); //START
Thread.sleep(dur!("seconds")(3));
ms_ticker_detach(ticker,src_sinus); //STOP
ms_ticker_destroy(ticker);
ms_filter_call_method_noarg(snk_binfile,MSF_SINKER_BINARYFILE_CLOSE);
ms_exit();
printf("---STOP  PROGRAM---\n");
return 0;
}
==

Finally: thank you very much for your help! I expected D to be a 
sort of "better C with *facultative* classes" but I find it to be 
quite... C#. Is my vision correct?




Re: D Dll injection problem

2012-03-27 Thread Gyron

On Tuesday, 27 March 2012 at 21:12:59 UTC, Trass3r wrote:

Maybe it's because I have no def file.


Very possible.
Just pass it to dmd like the other files.
Or try the new -shared flag.


I have tried both now (shared and def file linking), but know 
it's crashing my App, lol.


I inject it but it returns nothing and the App(where the dll is 
injected) is hanging( not responding).


Could you try it maybe?
I would like to know whether it's a Problem with D or with me.


Re: D Dll injection problem

2012-03-27 Thread Trass3r
I inject it but it returns nothing and the App(where the dll is  
injected) is hanging( not responding).


Could you try it maybe?
I would like to know whether it's a Problem with D or with me.


Are dlls without injection working?


Re: D Dll injection problem

2012-03-27 Thread Gyron

On Tuesday, 27 March 2012 at 21:46:23 UTC, Trass3r wrote:
I inject it but it returns nothing and the App(where the dll 
is injected) is hanging( not responding).


Could you try it maybe?
I would like to know whether it's a Problem with D or with me.


Are dlls without injection working?


I don't know, haven't tested it.
Will test it when I'm back home, but I think they work( not sure).


Re: std.stream.File help required (and classes)

2012-03-27 Thread Ali Çehreli

On 03/27/2012 02:20 PM, akaz wrote:

> what is the equivalent of
> std.stream.File.writeBlock(const void* buffer, size_t size)? I see there
> is a std.stdio.rawWrite(T)(in T[] buffer);
>
> But, my data is: a (byte*) pointer and a length. How do I write
> something like
> std.stream.File.writeblock(cast(byte*)p,cast(int)len) using
> std.stdio.File.rawWrite?

D has this helpful syntax of treating a raw pointer as a slice. Note 
p[0..length] below:


import std.stdio;

void main()
{
byte[] bytes = [ 81, 82, 83, 84, 85 ];

byte * p = &(bytes[1]);
size_t length = 3;

auto file = File("raw_written_bytes", "w");
file.rawWrite(p[0..length]);
}

> Yes, that should be the case. f is the mother structure (a pointer
> towards a MSFilter structure) that should never disappear and f.data
> should not be assigned somewhere else.

How is all that memory allocated?

> I still cannot grasp
> that fundamental thing when a class instance is a variable and when it
> is a pointer

If you don't mind, your code has way too many pointers. :) As a person 
coming from C++, here is how I made sense of it:


  auto var = new MyClass();

The line above has two entities:

1) An anonymous MyClass object on the right-hand side which belongs to 
the runtime. It will be garbage collected later.


2) The 'class variable' on the left-hand side. This is the handle to 
that object. You can think of this as being implemented as a pointer 
behind the scene, but there is no pointer in D with classes.


To complicate matters, you can spell out the type of 'var':

  MyClass var = new MyClass();

Very confusing, but the actual types of the left-hand and right-hand 
sides are still different: "class variable" vs. "class object".


> ; when is T x;

Prefer that one until it doesn't compile. :)

> and when is T* x = new T();

That is needed when T is a value type (e.g. a struct) and that you want 
to handle its lifetime manually.


> or, when is T* x=(T*)calloc(1,sizeof(T));)

Since calloc() is a C library function, you must use a T* there.

Speaking of which, is f.data in such a memory that GC is not aware of? 
Then your class reference in there will not keep the File object alive. 
You must call GC.addRange() to let GC know that you have pointers into 
the GC's memory.


A better thing to do may be to call GC.calloc() from the core.memory module.

(Hmmm. Maybe I will translate this chapter next: 
http://ddili.org/ders/d/bellek_yonetimi.html )


> I expected D to be a sort of
> "better C with *facultative* classes" but I find it to be quite... C#.
> Is my vision correct?

I've never used C# but I know that it has brought many good features 
from many languages. :)


Ali



Re: std.stream.File help required (and classes)

2012-03-27 Thread akaz
OK, I converted into using the std.stdio.File. Without success, 
the programs till crashes.


However, in the meantime:

A) why there is no parameter-less constructor for std.stdio.File? 
I would like to have into my "init" function: s.filedesc=new 
File() and, then, in my setter "open" method 
s.filedesc.open(name,mode). Because I have no such constructor, I 
am forced to have in the "init": s.filedesc=null; and in the 
"open": s.filedesc=new File(name,mode). However, in that case, 
what use for the std.stdio.File.open method? If the name and the 
mode *must* be known at the construction time, then why ask those 
once more for the std.stdio.File.open() method? I am forced to 
open, first, a dummy file (in the constructor), only to be able 
to call, later, the std.stdio.File.open() method with the correct 
file name?


B) In my function:
private int msf_open(MSFilter* f, void* arg){ //a setter

printf("msf_binfile_open-start\n\n");

MSF_State* s=cast(MSF_State*)f.data;
ms_mutex_lock(&(f.lock));
s.filedesc = new File(*(cast(string*)arg),"w+b");
printf("s.filedesc=%p+\n",s.filedesc);
writef("s.filedesc.isOpen=%b+\n",s.filedesc.isOpen);

writef("s.filedesc.name=%s+\n",s.filedesc.name);

writef("s.filedesc.size=%d+\n",s.filedesc.size);

ms_mutex_unlock(&(f.lock));
printf("msf_binfile_open-stop\n\n");
return 0;
}

can you tell me if the line s.filedesc=new 
File(*(cast(string*)arg),"w+b"); PROPAGATES the change into 
f.data? (recall that MSF_State* s=cast(MSF_State*)f.data;). I 
should also force, at the end of the function (just before 
return), something like: free(f.data); f.data=s; to propagate 
this?


As dumb as it may seem: is my "s" variable in MSF_State* 
s=cast(MSF_State*)f.data; a POINTER or a VALUE? I should write 
s->filedesc? Or, maybe, (*s).filedesc? I am a bit lost between 
pointers (s->x or (*s).x) and values (s.x). For structures there 
are pointers, for classes there are no pointers?


Finally, the program crash on the same line, when trying to write 
something in the file. Irony is that the s.filedesc.isOpen 
shows... 1.


Here is my new "msf_sinker_binaryfile.d file:

===msf_sinker_binaryfile.d==
module msf_sinker_binaryfile;
import std.stdio;

import mediastreamer2_layer;
import msf_commons;

const uint MSF_SINKER_BINARYFILE_OPEN = 
MS_FILTER_METHOD!(byte)(cast(MSFilterId)MSFilterIdNew.MSF_SINKER_BINARYFILE_ID,0);
const uint MSF_SINKER_BINARYFILE_CLOSE = 
MS_FILTER_METHOD_NO_ARG(cast(MSFilterId)MSFilterIdNew.MSF_SINKER_BINARYFILE_ID,1);


private struct _MSF_State{
File* filedesc; //file descriptor
};
private alias _MSF_State MSF_State;

private void msf_init(MSFilter* f){ //a constructor

printf("msf_binfile_init-start\n\n");

MSF_State* s=ms_new!(MSF_State)(1);
s.filedesc = null;
f.data=s;
printf("msf_binfile_init-stop\n\n");
}

private int msf_open(MSFilter* f, void* arg){ //a setter

printf("msf_binfile_open-start\n\n");

MSF_State* s=cast(MSF_State*)f.data;
ms_mutex_lock(&(f.lock));
s.filedesc = new File(*(cast(string*)arg),"w+b");
printf("s.filedesc=%p+\n",s.filedesc);
writef("s.filedesc.isOpen=%b+\n",s.filedesc.isOpen);

writef("s.filedesc.name=%s+\n",s.filedesc.name);

writef("s.filedesc.size=%d+\n",s.filedesc.size);

ms_mutex_unlock(&(f.lock));
printf("msf_binfile_open-stop\n\n");
return 0;
}

private void msf_process(MSFilter* f){ //a run() method

printf("msf_binfile_process-start\n\n");

MSF_State* s=cast(MSF_State*)f.data;
mblk_t* m;
	while((m=ms_queue_get(f.inputs[0]))!=null){ //reading samples 
from input

ms_mutex_lock(&(f.lock));
		int len=cast(int)((m.b_wptr-m.b_rptr)); //length of message 
data block

byte* p=cast(byte*)m.b_rptr;
/*printf("\n");
for(int idx=0; idx
writef("s.filedesc.isOpen=%b+\n",s.filedesc.isOpen);

//writef("s.filedesc.name=%s+\n",s.filedesc.name); 
//SEGFAULT

//writef("s.filedesc.size=%d+\n",s.filedesc.size); 
//SEGFAULT
		s.filedesc.writefln("This writes a string to the file."); 
//SEGFAULT

//s.filedesc.writeBlock(p,len);
//filedesc.close();
ms_mutex_unlock(&(f.lock));
freemsg(m);
}

printf("msf_binfile_process-start\n\n");

}

private int msf_close(MSFilter* f, void* arg){ //an unsetter
 

Re: std.stream.File help required (and classes)

2012-03-27 Thread Ali Çehreli

On 03/27/2012 02:57 PM, akaz wrote:

> B) In my function:
> private int msf_open(MSFilter* f, void* arg){ //a setter
> printf("msf_binfile_open-start\n\n");
> MSF_State* s=cast(MSF_State*)f.data;
> ms_mutex_lock(&(f.lock));
> s.filedesc = new File(*(cast(string*)arg),"w+b");

Judging from your free(f.data) proposal below, I think the problem is on 
the previous line: the object is allocated in GC's memory but s.filedesc 
is in a memory that GC is not scanning. You must call GC.addRange() when 
you allocate the memory that s.filedesc is living on.


> can you tell me if the line s.filedesc=new
> File(*(cast(string*)arg),"w+b"); PROPAGATES the change into f.data?

Yes. It is the same as in C.

> (recall that MSF_State* s=cast(MSF_State*)f.data;). I should also force,
> at the end of the function (just before return), something like:
> free(f.data); f.data=s; to propagate this?

Not to propagate, but you may have to call free() to avoid a leak.

> As dumb as it may seem: is my "s" variable in MSF_State*
> s=cast(MSF_State*)f.data; a POINTER or a VALUE?

A pointer just like in C.

>I should write
> s->filedesc? Or, maybe, (*s).filedesc?

That syntax has been useless even in C. (Although it communicates 
something to the reader.) The compiler could use the dot operator and do 
the right thing depending on whether the left-hand side was a pointer or 
an object.


D does not have the -> operator.

> I am a bit lost between pointers
> (s->x or (*s).x) and values (s.x). For structures there are pointers,
> for classes there are no pointers?

Yes.

Ali



How to remove element from an SList?

2012-03-27 Thread Chris Pons
Right now i'm struggling with trying to understand how to remove 
an element from a n SList. I only want to remove one element, not 
a range of elements. I also don't want to use an Array because I 
will have to reshuffle elements to take care of the empty spot 
when I remove it.


The only thing I've seen so far is find from std. algorithm and 
linearRemove. However I can't get find to work and I don't 
exactly believe linearRemove will work either because afaik that 
removes up to the index specified? I'm not to clear on this.


Here's a simplified example of what I was trying:

SList!int intList;
intList.insert( 1 );
auto a = find( intList, 1 );
intList.linearRemove( a );


Re: How to remove element from an SList?

2012-03-27 Thread James Miller
On 28 March 2012 13:02, Chris Pons  wrote:
> Right now i'm struggling with trying to understand how to remove an element
> from a n SList. I only want to remove one element, not a range of elements.
> I also don't want to use an Array because I will have to reshuffle elements
> to take care of the empty spot when I remove it.
>
> The only thing I've seen so far is find from std. algorithm and
> linearRemove. However I can't get find to work and I don't exactly believe
> linearRemove will work either because afaik that removes up to the index
> specified? I'm not to clear on this.
>
> Here's a simplified example of what I was trying:
>
> SList!int intList;
> intList.insert( 1 );
> auto a = find( intList, 1 );
> intList.linearRemove( a );

The documentation is very unclear, I had to figure out what
linearRemove did from the unittest for it.

This example (comments are mine) should help:

auto s = SList!int(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
auto r = s[]; // Slice of the original list
popFrontN(r, 3); // Removes the first 3 elements
auto r1 = take(r, 4); // returns the first 4 elements
assert(equal(r1, [4, 5, 6, 7]));
auto r2 = s.linearRemove(r1); //removes the range [4,5,6,7]
assert(s == SList!int(1, 2, 3, 8, 9, 10));
assert(equal(r2, [8, 9, 10]));

The docs mention that the range you use to remove, should be sourced
from the original list. Also, you actually need to use take(), since
the only to remove from the middle is with a Take!Range, its not very
clear, or understandable, but that's the way you have to do it.

Also, you are right the the other linearRemove does remove upto the
element, however, it works by again, passing a range.

std.containers is an odd beast, and I might do some more
investigation. However, all you need to know is that in order to
remove, say the 5th element from a SList, you need to do this:

SList!int s = [1,2,3,4,5,6,7,8,9,0];
auto r = s[0..4];
auto r1 = take(1, r);
s.linearRemove(r1)

Not the most intuitive way in the world...

--
James Miller


Re: How to remove element from an SList?

2012-03-27 Thread Ali Çehreli

On 03/27/2012 05:02 PM, Chris Pons wrote:
> Right now i'm struggling with trying to understand how to remove an
> element from a n SList. I only want to remove one element, not a range
> of elements.

I don't have experience with std.container but I think you need to call 
take(a, 1).


> The only thing I've seen so far is find from std. algorithm and
> linearRemove. However I can't get find to work and I don't exactly
> believe linearRemove will work either because afaik that removes up to
> the index specified? I'm not to clear on this.
>
> Here's a simplified example of what I was trying:
>
> SList!int intList;
> intList.insert( 1 );
> auto a = find( intList, 1 );
> intList.linearRemove( a );

The following worked for me. Note treating the SList as a range by []:

import std.container;
import std.stdio;
import std.algorithm;
import std.range;

void main()
{
auto l = SList!int(1, 2, 3, 4, 5, 6, 7);

auto a = find(l[], 2);  // Search for 2 ...
l.linearRemove(take(a, 1)); // ... and remove just 2

auto b = find(l[], 6);  // Search for 6 ...
l.linearRemove(b);  // ... and remove from there

assert(l == SList!int(1, 3, 4, 5));
}

Ali



Re: How to remove element from an SList?

2012-03-27 Thread bearophile

Ali Çehreli:

The following worked for me. Note treating the SList as a range 
by []:


import std.container;
import std.stdio;
import std.algorithm;
import std.range;

void main()
{
auto l = SList!int(1, 2, 3, 4, 5, 6, 7);

auto a = find(l[], 2);  // Search for 2 ...
l.linearRemove(take(a, 1)); // ... and remove just 2

auto b = find(l[], 6);  // Search for 6 ...
l.linearRemove(b);  // ... and remove from there

assert(l == SList!int(1, 3, 4, 5));
}


That seems an example for the dlang site docs :-)
(It just needs to show the usage of "cursors" to on the list?)

Bye,
bearophile


Get indexes of character in array

2012-03-27 Thread Andrej Mitrovic
I'd like to get a list of indexes into an array that matches a character. E.g.:

"a foo a bar a".indexes("a") == [0, 6, 12]

Anything like that in Phobos?


Re: Get indexes of character in array

2012-03-27 Thread Andrej Mitrovic
> 3/28/12, Andrej Mitrovic  wrote:
> I'd like to get a list of indexes into an array that matches a character.
> E.g.:
>
> "a foo a bar a".indexes("a") == [0, 6, 12]

Hah I even managed to screw up that "bar" has an 'a' there. Anywho
this works just fine:

size_t[] indexes(string input, dchar target)
{
size_t[] result;
foreach (index, dchar ch; input)
{
if (ch == target)
result ~= index;
}
return result;
}

The cool thing about the foreach loop is that 'index' will point to
the exact code point of the target (it won't just be incremented by +1
on each loop).


Re: Get indexes of character in array

2012-03-27 Thread Andrej Mitrovic
On 3/28/12, Andrej Mitrovic  wrote:
> snip

Also a better name might be 'indices'.


Re: Get indexes of character in array

2012-03-27 Thread James Miller
On 28 March 2012 19:35, Andrej Mitrovic  wrote:
> I'd like to get a list of indexes into an array that matches a character. 
> E.g.:
>
> "a foo a bar a".indexes("a") == [0, 6, 12]
>
> Anything like that in Phobos?

std.regex might be able to produce something like it.

--
James Miller