Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
June 29, 2010 dcollections how to LinkList // port c# code | ||||
---|---|---|---|---|
| ||||
Hi, in C# this is common. private List<Server> _servers; _servers = new List<Server> { new Server{ Name = "ServerI", IP = "120.14.220.18" }, new Server{ Name = "ServerII", IP = "120.14.220.19" }, new Server{ Name = "ServerIII", IP = "120.14.220.20" }, new Server{ Name = "ServerIV", IP = "120.14.220.21" }, new Server{ Name = "ServerV", IP = "120.14.220.22" }, }; D2 so far.. import dcollections.LinkList; class LoadBalancer { alias LinkList!Server ServerList; private ServerList sl; this() { sl = new ServerList; sl.add( new Server() ); ... } Do I really have to create something like this auto x = new Server(); x.Name = "Blah"; x.IP = "120.14.220.22"; s1.add(x) (Name and IP are Server properties.) thanks bjoern |
June 29, 2010 Re: dcollections how to LinkList // port c# code | ||||
---|---|---|---|---|
| ||||
Posted in reply to BLS | BLS:
> D2 so far..
> import dcollections.LinkList;
In D use dynamic arrays unless you really need to remove or add a lot of items from the start or middle of the sequence. On modern CPUs linked lists are usually the wrong data structure to use.
Bye,
bearophile
|
June 29, 2010 Re: dcollections how to LinkList // port c# code | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Tue, 29 Jun 2010 15:27:41 -0400, bearophile wrote: > In D use dynamic arrays unless you really need to remove or add a lot of items from the start or middle of the sequence. On modern CPUs linked lists are usually the wrong data structure to use. > > Bye, > bearophile D's dynamic arrays are great! Also you should create a constructor for a common build operation. -------------------------------------------- import std.stdio; class Server { string name; string ip; this( string _name, string _ip ) { name = _name; ip = _ip; } string toString() { return name ~ " - " ~ ip; } } void main() { Server[] serverList = [ new Server( "a", "164.76.0.1" ), new Server( "b", "164.76.0.2" ) ]; foreach( server; serverList ) { writeln( server ); } } ----------------------------------------- a - 164.76.0.1 b - 164.76.0.2 -B |
June 29, 2010 Re: dcollections how to LinkList // port c# code | ||||
---|---|---|---|---|
| ||||
Posted in reply to BLS | On Tue, 29 Jun 2010 15:22:30 -0400, BLS <windevguy@hotmail.de> wrote:
> Hi,
> in C# this is common.
>
> private List<Server> _servers;
> _servers = new List<Server>
> {
> new Server{ Name = "ServerI", IP = "120.14.220.18" },
> new Server{ Name = "ServerII", IP = "120.14.220.19" },
> new Server{ Name = "ServerIII", IP = "120.14.220.20" },
> new Server{ Name = "ServerIV", IP = "120.14.220.21" },
> new Server{ Name = "ServerV", IP = "120.14.220.22" },
> };
>
> D2 so far..
> import dcollections.LinkList;
> class LoadBalancer {
> alias LinkList!Server ServerList;
> private ServerList sl;
>
> this() {
> sl = new ServerList;
> sl.add( new Server() );
>
> ...
> }
>
> Do I really have to create something like this
> auto x = new Server(); x.Name = "Blah"; x.IP = "120.14.220.22";
> s1.add(x)
> (Name and IP are Server properties.)
I think I need to add some constructors that accept data. std.container has some cool construction methods.
For now, can you do something like this?
sl = new ServerList;
sl.add([
new Server("ServerI", "120.14.220.18"),
new Server(...)
...
]);
The new constructor would probably do something like this:
sl = new ServerList(
new Server(...),
new Server(...),
...
);
Does that work for you? If you need to build servers by naming fields, I'm not sure that's really a dcollections issue, D doesn't support constructing object by specifing individual field names. Alternatively, you could define an external constructor:
Server create(string name, string ip)
{
auto retval = new Server();
retval.Name = name;
retval.IP = ip;
return retval;
}
BTW, I don't think I've ever constructed a list that way in C#, it's cool :)
-Steve
|
June 29, 2010 Re: dcollections how to LinkList // port c# code | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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
|
June 29, 2010 Re: dcollections how to LinkList // port c# code | ||||
---|---|---|---|---|
| ||||
Posted in reply to BLS | On Tue, 29 Jun 2010 17:33:13 -0400, BLS <windevguy@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
|
June 29, 2010 Re: dcollections how to LinkList // port c# code | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On 29/06/2010 23:49, Steven Schveighoffer wrote:
> 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.
Hi Steve
This is why I've switched from LinkList to ArrayList. (ArrayList is simply cool)
also :
Server nextServer() { return sl[uniform(0, sl.length)]; }
is impossible with LinkList. ( sl is LinkList)
yeah opIndex on linked lists is simply slow.
However the snippet in the previous msg. does not compile.. will see later.
bjoern
|
June 30, 2010 Re: dcollections how to LinkList // port c# code | ||||
---|---|---|---|---|
| ||||
Posted in reply to BLS | Just a few things that may cause you some bugs/errors On Tue, 29 Jun 2010 23:33:13 +0200, BLS wrote: > On 29/06/2010 22:12, Steven Schveighoffer wrote: > // Confirm these are the same instance > if (b1 == b2 && b2 == b3 ) { > writeln("Same instance\n"); > } I think you mean to use is here if( b1 is b2 && b2 is b3 ) // == compares value, not pointer/references > // D2 singleton > final class LoadBalancer { > private static LoadBalancer lb; > alias ArrayList!Server ServerList; > private ServerList sl; > > static this() { > synchronized lb = new LoadBalancer; > } Might want the declare the class as synchronized, or make lb shared > > private this() { > sl = new ServerList; > sl.add([ > new Server("ServerI", "120.14.220.18"), > new Server("ServerII", "121.14.220.18") > ]); > } > try sl = new ServerList([ new Server("ServerI", "120.14.220.18"), new Server("ServerII", "121.14.220.18") ]); > this(string name, string id) { > this._name = _name; > this._id = id; > } > this._name = name; // you had _name -B |
June 30, 2010 Re: dcollections how to LinkList // port c# code | ||||
---|---|---|---|---|
| ||||
Posted in reply to Byron Heads | Byron Heads: > > this(string name, string id) { > > this._name = _name; > > this._id = id; > > } > > > > this._name = name; // you had _name I have just filed a bug report on this (it's a lot of time I want to write it): http://d.puremagic.com/issues/show_bug.cgi?id=4407 Bye, bearophile |
Copyright © 1999-2021 by the D Language Foundation