« January 2004 | Inicio | March 2004 »

February 17, 2004

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!

February 11, 2004

Accessing a class properties dinamically

Maybe this is commonplace, but working on a class that serializes other classes, I've found this:

I'm working on the following class:

class net.designnation.tests.Serializable { private var prop1: String; private var prop2: Number; private var prop3: Object; private var unseen: String; public static var CONSTANT: String = "This is a constant"; function Serializable( ) { this.unseen = "hello, world!"; } public function set val1( param: String ) { this.prop1 = param + " <:::::::: setter "; } public function get val1( ): String { return this.prop1 + " <:::::::: getter ";; } public function set val2( param: Number ) { this.prop2 = param + 100; } public function get val2( ): Number { return this.prop2 + 10000; } public function set val3( param: Object ) { this.prop3 = param; } public function get val3( ): Object { return this.prop3; } }

So, to read and write its properties, we can do the following:

import net.designnation.tests.* import net.designnation.utils.* var myClass: Serializable = new Serializable( ); myClass.val1 = "Hi!"; myClass.val2 = 32; myClass.val3 = { prop: "value" }; trace(" ------------------------\n "); for ( var k in myClass ) { trace( k + " == " + myClass[ k ] ); } //setting for ( var k in myClass ) { if ( k == "prop1" ) myClass[ k ] = "modified"; if ( k == "unseen" ) myClass[ k ] = "unseen, but modified"; } //checking trace(" \n------------------------\n "); for ( var k in myClass ) { trace( k + " == " + myClass[ k ] ); }

But, what if we want to access to those properties through the get/set functions?. We can't do it, because they are not showed in the for .. in loop.

So, using ASSetPropFlags, we can show all the elements of myClass.__proto__, which is something extremely dangerous, because it's an undocumented method, and we don't know if it will be removed or not in the future. Now, all the getters, setters, and even the class constructor will be available.

class net.designnation.utils.showProps { static function showAll( obj ) { _global.ASSetPropFlags( obj.__proto__, null, 0, 1 ); return obj; } }

And, now, back to our script:

// Showing all the properties myClass = showProps.showAll( myClass ); trace(" \n------------------------\n "); for (var k in myClass) { trace( k ); } //Getting values through the getter methods trace( " \n------------------------\n " ); trace( " Calling getter methods \n" ); for (var k in myClass) { var prefix: String = k.substring( 0, 7 ); var posfix: String = k.substring( 7, k.length); if ( prefix == "__get__" ) { trace( myClass[ k ]( ) ); } } trace( " \n------------------------\n " ); trace( " setting values using setters \n" ); for (var k in myClass) { var prefix: String = k.substring( 0, 7 ); var posfix: String = k.substring( 7, k.length); if ( prefix == "__set__" ) { if ( posfix == "val1" ) myClass[ k ]( " the end " ); } } trace( myClass.val1 );

This is not a very good practice, but can do the trick. At least it has made it for me.

February 09, 2004

Linked list V2

The FIVE of you that follow this blog, will remember my last post, about how to use my linked list class.

As promised, here's a better and cleaner implementation. It works exactly as the first version, except the method "insert", that now is called "insertOrdered".

So download it here

February 01, 2004

A linked list implementation

I hope this will be adult enough ;)

This is an implementation of a simple linked list. It needs some optimization (the method deleteElement relies on deleteElementAt, maybe I sholudn't use indexes to iterate trough the list,...) but it's a start. I will post an optimized version as soon as I finish it

Anyway, here's how it it works. To create an instance of the list, we have to pass it as a parameter an instance of a "comparator" class. That "comparator" class defines a lessThan() method that compares two objects. Depending of the kind of object we want to store in our list, we can define this method. For example, if we want to order the list depending on a property called "index", the "comparator class should look like this:

class net.designnation.structs.Comparador implements IOrder { function Comparador() { } public function lessThan( firstComp: Object, secondComp: Object): Boolean { if (firstComp.index< secondComp.index) { return true; } else { return false; } } }

So, we'll create an instance of the list like this:

import net.designnation.structs.*; var myList:List = new List(new Comparador());

And then, to populate it...

myList.push({index:6, prop:"propvalue6"}); myList.insert({index:3, prop:"propvalue3"}); trace("Lista " + myList); myList.insert({index:0, prop:"propvalue0"}); trace("Lista " + myList); myList.insert({index:2, prop:"propvalue2"}); trace("Lista " + myList); myList.insert({index:1, prop:"propvalue1"}); trace("Lista " + myList); myList.insert({index:4, prop:"propvalue4"}); trace("Lista " + myList); myList.insert({index:5, prop:"propvalue5"}); trace("Lista " + myList); myList.insert({index:7, prop:"propvalue7"}); trace("Lista " + myList); myList.insert({index:6, uno:"propvalue6"}); trace("Lista " + myList); Download it here .