views:

400

answers:

2

I want to have a collection of objects, which will be of a class I created called Server. A Server has a string property which is it's IP address, as well as many other pieces of data and objects.

I will have methods for adding and removing servers to this collection, and there will be a need to find a server by it's IP address occasionally. If I were doing this in C# I would use a Dictionary< where the IP string would be the key and the Server object would be the value. I could easily check to see if an item exists in the Dictionary before attempting to add it.

So my requirements are: 1. Ability to add items to the collection (I don't care where they go, front, back, middle) 2. Ability to remove items from anywhere in the collection. 3. Ability to determine if a particular IP address already exists in the collection. 4. Ability to get a reference to a Server object by it's IP. Edit: Oh yes, I would like it to be strongly typed like the Vector... I guess it's not absolutely necesary, but would be nice.

So it seems like an associative arrays will give me what I need, except I'm not sure about how to do #3 or #4.

public var Servers:Object = new Object( );

public function AddServer(server:Server):void
{
   //TODO:need to check if it exists first and throw an error if so
   //(it's the caller's responsibility to call DoesServerExist first)

   Servers[server.IP] = server;
}


public function RemoveServer(IP:string):void
{
   //is it OK to attempt to delete an item if it doesn't already exist?
   //do I need to check if it exists before doing delete?
   delete Servers[IP];
}

public function DoesServerExist(IP:string):bool
{  
    //Do I loop through all the elements testing it's IP property?
    //Or can I just do something like this?
    if(Servers[IP] == null)
    {
        return false;
    }
    else
    {
        return true;
    }
}

public function GetServer(IP:string):Server
{
    return Servers[IP];//what is returned if this IP doesn't exist?
}
A: 

You can just use an array. Example:

var dict:Array = [];
var ip = "164.157.012.122"
dict[ip] = "Server name"

if (dict[ip] == "Server name"){
    trace("Yay");
}

//membership
if (dict[ip]){
    trace(ip + " is a member of dict");
} else {
    trace (ip + " is not a member");
}

//removal:
dict[ip] = null;

AS3 does not really have a built in Dictionary class, unfortunately.

thedayturns
Thanks for the membership test code.I'm kind of confused why you chose to use an array. Why do I see documentation saying not to use Arrays for string indexes? They say none of the array functions will work, and it is no different than just using an object for an associative array.
AaronLS
You're correct, there is no real difference between arrays and objects... I just used one out of habit. If you want some of the array functions, I would recommend using something like this (http://code.ericfeminella.com/classes/as3/HashMap.as.html).
thedayturns
1. AS3 has a dictionary class. 2. correct removal is done with the delete operator, that way the key is really gone, so you can get all keys in a for-in loop. 3. correct lookup is dict.hasOwnProperty(id)
back2dos
also deletion with delete operator returns success as a boolean value
back2dos
Thanks for the notes on delete. The dictionary class in AS3 is nothing like the Dictionary class in C#, and it seems to provide the same features as an associative array except for the strict equality. I don't really need the strict equality, so thedayturns solution will work fine.
AaronLS
Uh, this is not a good answer. Using Array implies to anyone reading your code that your data structure is an Array (and hence, things like dict.pop() will do something meaningful, which in this case they won't). If you don't need any of the features of the Dictionary class, use a plain object... using any old dynamic class as an associative array is just bad karma.
fenomas
Yes I am using an object. He already clarified the array/object issue in a comment.
AaronLS
Yes, but the point of SO is that the answers remain for posterity. The answer as it stands is to use an Array (bad advice) because there is no Dictionary class (wrong)....
fenomas
+2  A: 

Call me goofy, but why not use the Dictionary class? That gets you everything except strong typing.

If you want strong typing then I'd say you need a custom container, which wraps up a Vector of Servers, and a Dictionary or associative array of IP strings that indexes into the Vector. Then you'd need to expose methods for access, test, insert and remove.

fenomas
you should simply hide the lookup dictionary within a class and have typed access methods as add(ip:String,server:Server):void, remove(ip:String):Server and get(ip:String):Server ... greetz
back2dos