nogc Array

Olivier Pisano via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jan 26 00:55:58 PST 2016


On Tuesday, 26 January 2016 at 05:53:29 UTC, Igor wrote:
> On Tuesday, 26 January 2016 at 04:38:13 UTC, Adam D. Ruppe 
> wrote:
>> On Tuesday, 26 January 2016 at 04:31:07 UTC, Igor wrote:
>>> then std.algorithm.find!("a.myInt == b")(classes, 3)
>>
>> Try
>>
>> std.algorithm.find!("a.myInt == b")(classes[], 3)
>>
>> notice the [] after classes
>>
>>
>>> I guess std.container.array isn't a range? Or am I using it 
>>> wrong?
>>
>> Containers aren't really ranges, they instead *offer* ranges 
>> that iterate over them. Built in arrays are a bit special in 
>> that they do this implicitly so the line is more blurred 
>> there, but it is a general rule that you need to get a range 
>> out of a container.
>>
>> Otherwise, consider that iterating over it with popFront would 
>> result in the container being automatically emptied and not 
>> reusable!
>
> Ok, does the [] do any conversion or any thing I don't want or 
> does it just make the template know we are working over an 
> array?
>
> Are there any performance issues? I am already using a for loop 
> to find the type, it's 6 lines of code. I was hoping to get 
> that down to one or 2 and make it a bit easier to understand.
>
> 	App app = null;
> 	for(int i = 0; i < Apps.length(); i++)
> 		if ((Apps[i] !is null) && (Apps[i].hWnd == hWnd))
> 		{
> 			app = Apps[i];
> 			break;
> 		}
>
> versus
>
> find!("a.hWnd == b")(Apps[], hWnd);
>
> Does [] take time to convert to a built in a array or range or 
> whatever or will it be just as fast as the above code?

The [] operator returns a Range object iterating over the Array 
elements, similarly to what the begin()/end() cbegin()/cend() 
function pairs do in C++. The range object does not copy the 
array element, only contains a slice to them.
So your question ends up in comparing hand-written loops over 
std::find_if().


More information about the Digitalmars-d-learn mailing list