[haxe] HDictionary

When trying to implement a new event system that support custom parameters in Haxe, I come across the need of AS3 Dictionary. After doing a quick search I found that there are no Haxe equivalent (yes, there is TypedDictionary but it’s only available if you are targeting Flash). To me, there is no point to write in Haxe if we can only target one platform, so I bring it up to create a HDictionary that works similar to AS3 Dictionary in pure Haxe hopefully this will be helpful to someone out there.

The basic idea is simple, as we do not has any kind of object signature (a unique string equivalent, usually its memory address) to use as the key, we need to store both key and value and then make a loop through all the items, doing comparison until we come across the correct one. You might think that this will cause much overhead (searching a lot) but remember that whatever type of script we used, this overhead can not be avoided.
Continue reading

[AS3] Input

In the efforts to unify the AS3 libraries i realized that writting too many files (each file is specific for one functionality) though very clear and easy, maintainable is a big issue. I usually need to bring the whole library, which caused conflicts to another project when there might be other versions of utils classes (btw, i used to put package’s function for each utility) happen to be there. This caused too much headaches as the old ones will might not work the way we expected with the new ones, overwrite is not an option, especially when the classes was patched for bugs in different projects. Of course, life should be easier if you track down everything you modified to a class got used in the real project or got simple unit test for each file, but still, the portability is not that great.

One class for one thing should be the way to go, though it’s heavier in term of code base, the maintainable process should be easy enough, just move it somewhere, rename the package once if needed. No missing or conflicted dependencies to worry about …

One cool thing in this version is that you don’t need to cancel out theme configuration : if a state used bold = true, we don’t need to cancel out for other state, aka, doesn’t need to put bold=false for each other states. Another one is that we can do input configuration from timeline.
Continue reading

Batch publish .fla files

JSFL is the way to automate tasks in Flash CS IDE, you can write extensions to do the repeating works like export fonts, open files, create symbols, read graphics datas, build multiple .fla files … The one thing i got astonished about is that there are no built-in way (no jsfl api) to know if there are errors during compilation of an .fla file. Hit ctrl+enter, you see that even if there are errors in code, the Flash IDE still trying to launch the .swf file. Hit build from an .flp project, the compiling process won’t stop when errors occurs for files, it just clear the log everytime a new file need to build, so we end up with a bunch of errors in files which we never where they are to apply the fixes.

I don’t know how hard it is for the Flash CS team to add an api allowing us to catch and stop when there are errors building .fla file, but this surely make enough troublesome for me : Once there are errors, i need to wait for the batch building complete then compile one by one manually… so finally decision, there must be a way to do it in jsfl.
Continue reading

MultiKey Dictionary

While developing some kind of event monitoring Manager class i come across a problem of too many Object used : Each IEventDispatcher source can dispatch multiple eventNames, each eventName can have several function attached to it. The popular approach is using nested Dictionary/Object as the main data structure :

_sourceDict : Dictionary; /* map IEventDispatchers to _eventObjects */
_eventObject : Object; /* map eventName to _handlerDict */
_handlerDict : Dictionary; /* map handler to its params */

For faster check and overwrite the existed _handler for a specific source and eventName we need to use a Dictionary for _handlerDict instead of an Array, and by the way, add support for user parameters.

This data structure of course will still work without any problem even though the code looks busy by nested properties access. We might also got slow down by looping through those nested structure. Let’s have a look through another implementation where we save multiple values as the key instead of only one.
Continue reading

Indexed Linked List

Indexed Linked List is an improved version of Single Linked List that enable random access (can only found on Array) and fast item splicing (can only found on Linked List). We can use it for prioritized queues where items often got inserted / removed at arbitrary positions. Some popular applications are loading queue manager (sort by priority), delayed tween list (sort by delay), or custom Event Dispatcher (addListeners with priority).

I. THE GENERAL IDEA :

We have a single linked list (to cut memory cost 30% compared to double linked list), every item has a {.next} pointer point to the next item in the list. Every item has a {.priority} property to be used as mandatory sort term, a {.time} stamp for secondary sort term, this way, every item is unique and can be compared to each other without equality worries.

Besides the normal linked list implementation we have another Array {_grid} to keep track of pivots items at some rough space {_gridSize}, that means for every roughly {_gridSize} items in the list we save an item into the {_grid}. This kind of indexing will enable binary search for position of any item in the list which means fast random access to items (fixed (and small) time to reach any item in the list). Of course, this won’t beat Array’s random access, but it’s fast enough for practical use ! Continue reading

onDataStructure();

One of the most common data structure in Flash is Array, which provides a fast way to construct a list of data items with compact memory cost. This data structure is working fine and rather fast, except for some operations, for example, insert / remove items randomly (splice) or pushing things to the beginning of the Arrays (unshift). Because of the compact memory implementation, all the existed items after the insert position will need to be migrated. With o(n) complexity, operated time goes up linearly with the number of items. And it’s just the same for Vector, although Vector is way faster than Array as there are no dense checking and converting between Dense Array & Hash Table (there are more reasons ? ). In short : Array is slowing down by items migration

A rather popular solution is using Linked List – either double or single. Linked list has ability to add and remove items very fast at an arbitrary position as no item is being migrated. Double linked list cost more memory (~ 1.5x ) and a bit slower than single Linked List (which, consume 2x more memory than Array) but it allows traversing in both direction. For some operations, the doubles run faster (~ 2x faster) than single ones. But as we won’t have random access to items by numeric indexes like Arrays, it’s really slow if we need to skip forward / backward to get to some random position. In short : Linked List is slowed down by skipping to specific position  Continue reading

A better AS3 Singleton implementation

As a developer we usually need to create libraries to reuse in various projects, most of them are of the manager style : one class (or instance) to handle all in coming tasks from various places, popular examples are : Tween engines, Loading services, 3D libraries, Sound processing library … The simplest implementation is the static-class-methods way.

I. THE STATIC CLASS METHODS IMPLEMENTATION :

Let’s have a look through a simple library class :

package {
	public class MyUtility {
		public static function methodA(): void {
			//do A things here
		}
		public static function methodB(): void {
			//do B things here
		}
	}
}

And its simple usage syntax

MyUtility.methodA();//call method A
MyUtility.methodB();//call method B

Continue reading

onGaiaFramework();

After using Gaia for a long time with numerous websites built, i can tell you that completely like it as the workflow is simplified so much, i really love the scaffolding, assets managements, contextMenu, event hijacking, routing, assetPath, tracking, binding expression, the complete integration of SWFAddress, SEO … everything ! But that’s not to say there are no more room to get improved. Let’s dig in for more details !

    Continue reading

First time on a Mac

Get used to Windows for many years and several times on Ubuntu but this is my first time to a Mac, i feel awkward getting the way through… there are some cool things and there are some bad things compared to windows OS, see it for yourself !

  1. There are no shift-delete and i can’t find a way to delete a file without move it to trash first, it takes time and not efficient.
  2. You can not show desktop (windows+D) without installing a utility (use F11 for quick windows hide)
  3. You can not use function keys for functions, just you it for the brightness / volume adjust … you will need to install a utility then (try function flip).
  4. You can force move files around by cmd+drag just like shift+drag/drop on windows
  5. You can force copy files by alt+drag just like ctrl+drag/drop on windows
  6. There are no end/home/pageUp/pageDown keys, use cmd+arrows instead
  7. The delete button actually is backspace, there are no pc-like delete button
  8. The mouse has only 1 button, want right click ? tap with two fingers
  9. Replace cmd for pc’s ctrl : cmd+c for copy, cmd+v for paste, cmd+x for cut (editor only, won’t work on Finder), cmd+z for undo
  10. Delete a file / folder : cmd+delete, just press delete won’t have any effect (why?)
  11. Need to scroll down / up a file ? hold 2 fingers on the mouse pad and move at the same time – just works exactly like your mouse wheel.
  12. Want to unzip a file ? double click onto it, the file will be extract to a folder. Want to view the content of the zip without extract ? you can’t do it without install a small utility. Want to zip a folder or a file ? right click, choose archive, a new archive.zip file will be created, you can change the name after that.
  13. You can read an NTFS drive from Mac but can not write to it, use some utility to enable write (with slow speed to be cared about)
  14. Disable creation of .DS_STORE and other .CLEAN_FILES files (only for network volumes): open terminal and type the following defaults write com.apple.desktopservices DSDontWriteNetworkStores true or use Tinker tool, any suggestion for completely remove .FILES for shared volumes between Mac + PC ? BlueHarvest ?

to be continued …

Anh còn nhớ

Trước mặt ba mẹ em, anh đã xiết tay em thật chặt, và luôn khẳng định sẽ ở bên, che chở cho em đến trọn đời.

Lần em buồn khi công việc không như ý, anh đến công ty em chỉ để gửi lá thư, có một dòng: “Can đảm lên, người yêu! Anh luôn bên em!”.

Anh còn nhớ, với gương mặt hỉ hả anh xách về cho em một túi cà chua và không ngừng kể công: “Cà chua sạch đấy!”. Quên sao được cảnh thi thoảng anh thức dậy kê lại cái gối chân cho vợ đang có bầu khỏi mỏi. Anh còn đảm đang dậy sớm nấu ăn sáng, để vợ ngủ thêm chút nữa, vì thương vợ khó ngủ.

Anh còn nhớ lần em buột miệng than khi trời đã tối khuya, rằng xe máy phanh hơi sâu và vào số hơi nặng? Anh không quản ngại, lôi đồ nghề ra chỉnh lại luôn, bởi sáng hôm sau phải đi làm sớm nhỡ không kịp.

Từng hành động quan tâm nhỏ cùng những tháng ngày hạnh phúc ấy, chẳng khi nào em quên. Còn anh? Continue reading