Haxe – Data Link

The Goal

Ok, let’s remember the problem of managing a bunch of instances.

Our goal would be a system, that can:

  • register our instances with categories.
  • be able to return instances based on a category.

Of course do we need a name for out product. DataLink will do it.

The Core

We found out that the fastest loop is the for loop with a counter. In this project I will show you how to setup up a doubly linked list and literate over it with the while loop. So let’s setup the DataLink class.

class DataLink {
	
	public function new():Void{
		
	}
}

What informations do we need to store with each instance?

  • The categories
  • Data (The storage itself)

The Storage Class

We will setup a class for this structure to store what we ant.

class DataLinkUnit {
	
	private var _prev:DataLinkUnit;
	public var prev(get, set):DataLinkUnit;
	
	private var _next:DataLinkUnit;
	public var next(get, set):DataLinkUnit;
	
	private var _categories:Array<String>;
	public var categories(get, set):Array<String>;
	
	private var _data:Dynamic;
	public var data(get, set):Dynamic;
	
	public function new(categories:Array<String>, data:Dynamic):Void{
		_categories = categories;
		_data = data;
	}
	
	// Getter Setter
	private function get_prev():DataLinkUnit{
		return _prev;
	}
	
	private function set_prev(unit:DataLinkUnit):DataLinkUnit{
		return _prev = unit;
	}
	
	private function get_next():DataLinkUnit{
		return _next;
	}
	
	private function set_next(unit:DataLinkUnit):DataLinkUnit{
		return _next = unit;
	}
	
	private function get_categories():Array<String>{
		return _categories;
	}
	
	private function set_categories(categories:Array<String>):Array<String>{
		return _categories = categories;
	}
	
	private function get_data():Dynamic{
		return _data;
	}
	
	private function set_data(data:Dynamic):Dynamic{
		return _data = data;
	}
}

The constructor of the class DataLinkUnit needs 2 parameters:

  • An Array within the categories (Strings)
  • Anything you want to store. That’s why the type is Dynamic.

These 2 parameters will be stored in the private properties _categories and _data.
Additionally we need 2 more private properties for our doubly linked list. Each class has to know his previous neighbor and his next neighbor.

Our DataLink class need also an update to be ready for our doubly linked list.

class DataLink {
	
	private var _first:DataLinkUnit;
	private var _last:DataLinkUnit;
	
	public function new():Void{
		
	}
	
}

For the while loop we need to know wich DataLinkUnit is the first and wich is the last.

How do we store?

Next step would be to define a public method to register out instance’s.

public function registerUnit(categories:Array<String>, data:Dynamic):Void{
		
	var tempUnit: DataLinkUnit = new DataLinkUnit(categories, data);
	
	if(_first == null){
		_first = _last = tempUnit;
	}else{
		_last.next = tempUnit;
		tempUnit.prev = _last;
	}
}

Let me explain you this method. The registerUnit method needs 2 parameters:

  • An Array within the categories.
  • Anything you want to store.

We will make an new instance of our DataLinkUnit class and forward the parameters in this instance.

Linking till the end

Then we will ask for the property _first. The first time this condition will return null and we can store our tempUnit in _first.

Whe also store this tempUnit in _last because it’s now the only one. So it’s the _first and the _last instance of DataLinkUnit our core knows.

But if _first returns not null we will be sure that our tempUnit is not the first that we want to store. No we need the properties _prev and next of our DataLinkUnit class.

Our new tempUnit will now be the next neigbor of out last instance. So now we have a singly linked list of DataLinkUnit’s. But we want to make it a doubly linked list. So our last known instance will be the previous of our tempUnit.

That’s it. This way every instance of DataLinkUnit know his previous and next neighbor. You can now easily iterate with a while loop over this linked list.

Now we be able to store our instances in our DataLink system. But how we will get them back to work with them?

I need my units back

Our system needs a method to return units by category. Let’s be creative and call our next method “getUnitsByCategory”.

public function getUnitsByCategory(category:String):Array<DataLinkUnit>{
	var result:Array<SpeedLinkUnit> = [];
	var tempUnit:DataLinkUnit = _first;
	while(tempUnit != null){
		for(i in 0...tempUnit.categories.length){
			if(tempUnit.categories[i] == category){
				result.push(tempUnit);
			}
		}	
		tempUnit = tempUnit.next;
	};
	return result;
}

This method requires only one parameter, the category you want search for. The return of this method will be an Array with elements of the type DataLinkUnit.

To start the search wen need an Array to return our result. Also we need a tempUnit for the while loop we will be using. Initially this tempUnit will get the insance of _first.

While not null

the condition of your while loop is do something till the tempUnit is null. Inside of this loop we loop over the categories with a for loop. If we will found a category match we will push this instance in our result Array. Then the tempUnit will get a new value with the next property. After the loop we will return our result Array.

How to use it?

Var dl:DataLink = new DataLink();

//…

var youtubePlayer:AnyInstance = new AnyInstance();
dl.registerUnit([“media”, “video”, “youtube”], youtubePlayer);

var vimeoPlayer:AnyInstance = new AnyInstance();
dl.registerUnit([“media”, “video”, “vimeo”], vimeoPlayer);

var soundcloudPlayer:AnyInstance = new AnyInstance();
dl.registerUnit([“media”, “audio”, “soundcloud”], soundcloudPlayer);

//...

// Get all media players
var mediaPlayers:Array<DataLinkUnit> = [];
mediaplayers = dl.getUnitsByCategory(“media”);
for(j in 0...mediaplayers.length){
	mediaplayers[j].data.mute();
}

// Get all youtube players
var youtubePlayers:Array<DataLinkUnit> = [];
youtubePlayers = dl.getUnitsByCategory(“youtube”);
for(i in 0...youtubePlayers.length){
	youtubePlayers[i].data.playVideo();
}

What’s next?

You can now add features by yourself. How about:

  • A public method named “exist” that returns true or false, depending if an specific category is found.
  • A public method named “size” that will return the quantity if the linked list.

That’s it. We builded a system to register and organize all kind of instances from scratch.

And the best thing is: We coded only in Haxe and needed no Framework!

My advice

If you start to think about your task, project or whatever, break it down in modules. Start with the base/core first, and use inheritance. Ask yourself, “How will I build this to use it again in the next project?”. I promise that you will start to use fewer and fewer frameworks. You don’t need them anymore. You will end up by picking just a few for tweening/animating or the canvas.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>