dcollections how to LinkList // port c# code

Steven Schveighoffer schveiguy at yahoo.com
Tue Jun 29 14:49:18 PDT 2010


On Tue, 29 Jun 2010 17:33:13 -0400, BLS <windevguy at hotmail.de> wrote:

> On 29/06/2010 22:12, Steven Schveighoffer wrote:
>> For now, can you do something like this?
>>
>> sl = new ServerList;
>> sl.add([
>>     new Server("ServerI", "120.14.220.18"),
>>     new Server(...)
>>     ...
>> ]);
>
> Hi Steve, I think this should work, however I got very strange err. msg.  
> in file ArrayList.d  Have to stop now..need some sleep.
> BTW the new constructor stuff would be nice to have.
>
> //current code.
>
> import std.stdio;
> import std.random;
>
> import dcollections.ArrayList;
> import dcollections.LinkList;
>
> void main() {
>
> 	auto b1 = LoadBalancer();
> 	auto b2 = LoadBalancer();
> 	auto b3 = LoadBalancer();
>
> 	// Confirm these are the same instance
>      if (b1 == b2 && b2 == b3 )  {
>      	writeln("Same instance\n");
>     	}
>
> 	// Next, load 15 requests for a server
>    	for (int i = 0; i < 15; i++) {
> 		string serverName = b1.nextServer.servername;
> 		writeln("Dispatch request to: " ~ serverName);
>        }
> }
>
> // D2 singleton
> final class LoadBalancer {
> 	private static LoadBalancer lb;
> 	alias ArrayList!Server ServerList;
> 	private ServerList sl;	
> 	
> 	static this() {
> 		synchronized lb = new LoadBalancer;
> 	}
>
> 	static LoadBalancer opCall() {
> 		return lb;
> 	}
> 	
> 	private this() {
> 		sl = new ServerList;
> 		sl.add([
> 	 	  	new Server("ServerI", "120.14.220.18"),
>    	 		new Server("ServerII", "121.14.220.18")
> 		]); 	
> 	}
>
> 	@property
> 	{	
> 		Server nextServer() { return sl[uniform(0, sl.length)]; }
> 	}
> 		
> 	private class Server {
> 		private string _name, _id;
>
> 		this(string name, string id) {
> 			this._name = _name;
> 			this._id  = id;	
> 		}
> 		
> 		string servername() {
> 			return _name;
> 		}
> 		
> 		/* OLD PROPERTY STUFF
> 		@property
> 		{
> 			string servername(string sn) { return _name = sn; }
> 			string servername() { return _name;	}
>
> 			string id(string id) { return _id = id; }
> 			string id() { return _id; }
> 		}
> 		*/
> 		
> 	}
> }
>
> cheers,bjoern

One thing to note, ArrayList *does* accept an array as a constructor, and  
it will actually use that array as its storage.  This is so you can "wrap"  
an array as a ArrayList and get the full dcollections functionality from  
it.

The other containers do not accept an array for construction... yet :)

-Steve


More information about the Digitalmars-d-learn mailing list