www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Do you want add contains and remove item Function in array?

reply "FrankLike" <1150015857 qq.com> writes:
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.
Feb 05 2015
next sibling parent reply "Daniel Murphy" <yebbliesnospam gmail.com> writes:
Contains: array.canFind(element)
Feb 05 2015
next sibling parent "FrankLike" <1150015857 qq.com> writes:
On Thursday, 5 February 2015 at 14:47:40 UTC, Daniel Murphy wrote:
 Contains: array.canFind(element)
Oh, canFind is better than Contains. Thank you.
Feb 05 2015
prev sibling parent reply "FrankLike" <1150015857 qq.com> writes:
On Thursday, 5 February 2015 at 14:47:40 UTC, Daniel Murphy wrote:

But do you have some better way for remove item ?
such as c#: array.remove(item) ,array.removeAt(index)?

Thank you.
Feb 05 2015
parent "Daniel Murphy" <yebbliesnospam gmail.com> writes:
"FrankLike"  wrote in message news:yuinuamnmiqjxqatrvmv forum.dlang.org...

 But do you have some better way for remove item ?
 such as c#: array.remove(item) ,array.removeAt(index)?
There is http://dlang.org/phobos/std_algorithm.html#.remove which can be used like removeAt(index). array = array.remove(3); And it looks like there's an overload that can remove matching elements like this: array = array.remove!(e => e == value);
Feb 05 2015
prev sibling parent reply "BBaz" <bb.temp gmx.com> writes:
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...)
Feb 05 2015
parent "FrankLike" <1150015857 qq.com> writes:
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.
Feb 05 2015