Do you want add contains and remove item Function in array?

FrankLike via Digitalmars-d digitalmars-d at puremagic.com
Thu Feb 5 15:12:44 PST 2015


On Thursday, 5 February 2015 at 15:21:56 UTC, BBaz wrote:
> On Thursday, 5 February 2015 at 14:09:06 UTC, FrankLike wrote:
>> Now operate array is not very quick,such as contains 
>> function,remove item function
>> can't get from arry module.
>>
>> template contains(T)
>> {
>> 	bool contains(T[] Array,T Element)
>> 	{
>> 		foreach(T ArrayElement; Array)
>> 		{
>> 			if(Element==ArrayElement)
>> 			{
>> 				return true;
>> 			}
>> 		}
>> 		return false;
>> 	}
>> }
>>
>> template remove(T)
>> {
>> 	bool remove(T[] Array,T Element)
>> 	{
>> 		?????
>> 		return true;
>> 	}
>> }
>>
>> or
>>
>> remove!("a == ?")(arr)
>>
>> How to get the easy and quickly way?
>>
>> Thank you.
>
> If you encounter difficulties to memorize the functions then 
> you can wrap the usefull thing in a struct, e.g:
>
> ---
> struct array(T)
> {
>     T[] _arr;
>     alias _arr this ;
>
>     bool opIn_r(T)(T t)
>     {
>         import std.algorithm;
>         return canFind(_arr, t);
>     }
>     alias canFind = opIn_r;
>
>     typeof(_arr) remove(T t)
>     {
>         _arr = std.algorithm.remove(_arr, t);
>         return _arr;
>     }
> }
> ---
>
> If the editor you use has a completion proposal system then 
> it'll work like a charm. I've myself in the process of doing 
> something similar for DList because i've been intoxicated for 
> years with the easiness of the pascal Run Time Library lists 
> (they have exchange, add , remove etc...)

Thank  you very  much.


More information about the Digitalmars-d mailing list