Data structures: Hash table
A hash table is a short of collection of linked lists. It allows us to store pairs data-object, and later retrieve the data using its key
You can view a detailled explanation of what a hash map is here
And you can download my AS2 implementation here
To test it you could try something like this:
import net.designnation.structs.*
var a: HashMap= new HashMap( );
// Setitem
//
key= "4";
trace( "Adding "+ key );
a.setItem( key, new Object( key ) );
trace( "HashMap contents: "+ a );
key= "21";
trace( "Adding "+ key );
a.setItem( key, [ "a1", "a2" ] );
trace( "HashMap content: "+ a );
key= "5";
trace( "Adding "+ key );
a.setItem( key, new Object( key ) );
trace( "HashMap content: "+ a );
key= "8";
trace( "Adding "+ key );
a.setItem( key, new Object( key ) );
trace( "HashMap content: "+ a );
key= "81";
trace( "Adding "+ key );
a.setItem( key, new Object( key ) );
trace( "HashMap content: "+ a );
key= "8";
trace( "Adding "+ key );
a.setItem( key, new Object( key ) );
key= "8";
trace( "Retrieving "+ key );
trace( a.getItem( key ).toString( ) );
// Count
//
trace( "Count: "+ a.count( ) );
// Remove
//
key= "4";
trace( "Removing "+ key );
a.remove( key );
trace( "HashMap content: "+ a.toString( ) );
trace( "Count: "+ a.count( ) );
Good night!
Comentarios
is there also a method to check the existence of a key (e.g. hasItem())?
Would it be possible for you to send me the as source if your HashMap class. I don't know where flash installs these mxp files, therefore I couldn't take a look into the classe files and find out the provided functions by myself.
regards,
paul
Publicado por: Paul | March 7, 2004 09:08 PM
Hi, Paul.
I haven't implemented any method to check for a key yet, but I'll try to do it as soon as possible.
The as files are installed in the following folder ( windows, I don't know about macs ):
C:\documents and settings\{user}\Local configuration\Program data\Macromedia\FlashMX2004\{language}\Configuration\Classes
There is a folder called "net", containing my as files.
Publicado por: Cesar Tardaguila | March 7, 2004 10:31 PM
thanks for your help, I found the as files.
did you perform some test comparing the access time of your HashMap and a normal assoiative Array?
Is there a size limit you would suggest for the HashMap? I am planing on using it as a cache object to store product-data in but it could become quite large (200-300 items or more), do you think this would still be ok?
Thanks a lot for your help,
Regards,
Paul
Publicado por: Paul | March 8, 2004 10:11 AM
Hi, Paul, sorry for the delay.
I think this structure should be ok to store much more than 300 items. I've tested it with 500 objects ( movieclips ) and there was no problem.
Anyway, I'm planing to update it this weekend, to try to improve the performance, so if you can wait untill sunday...
Publicado por: Cesar Tardaguila | March 9, 2004 08:25 PM