Lite Collections Fx for ActionScript

With this post I am going to release and publish a new professional set of data collection classes for the ActionScript platform. While working on different projects, my needs for handy and reliable data collections grew enormously, so that I finally developed the Lite Collections Fx. And here they are! I hope you enjoy them :-)

Table of contents

Definition and placement

The Lite Collections Fx provide a lightweight framework, ready-for-use collections and base implementations targeted to simplify and standardise data handling in ActionScript applications.

9 basic and 5 bindable collections

The library contains 9 basic collections (each three lists, sets and maps), a set of powerful iterators and a couple of utilities. Using the library is most valuable in the data tier of ActionScript applications. However, since most of the 9 collections come additionally in an event dispatching version, binding user interfaces is a second goal of the Lite Collections Fx.

Easy to learn, open for contribution

The library is meant to be easily utilisable by both beginners and advanced developers in commercial or non-commercial projects. Hence, the software is placed under an open source software licence. There is a SourceForge project account containing the most recent sources and encouraging your contribution.

If you are not familiar with collections yet, please read first this post: Why we need a collection framework in ActionScript.

Example

The example shows the basic handling of data collections. A SortedList is added to a LinkedMap. The composition is then written to the console. The SortedList maintains its items in a sorted order, the LinkedMap in the order they were inserted.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package as3.collections {
    import as3.collections.utils.*;
    import flash.display.Sprite;    
    public class BasicExample extends Sprite {
        public function BasicExample() {
 
            var list : IList = // display break
                new SortedList(new StringComparator());
            list.addItem("list");
            list.addItem("list1");
            list.addItem("list1");
            list.addItem("List");
 
            var map : IMap = new LinkedMap();
            map.addItem(1, "map1");
            map.addItem(1.5, list);
            map.addItem(-1, "map3");
            map.replaceItem(-1, "map4");
 
            trace(CollectionUtils.dumpAsString(map));
 
            // [LinkedMap] items:3
            // .......map1
            // .......[SortedList] items:4
            // ..............List
            // ..............list
            // ..............list1
            // ..............list1
            // .......map4
        }
    }
}

More examples can be found here: More examples

Objectives

Common solution

The framework is built upon the common software design patterns. The architectural approach is simple and plausible.

Small interfaces

The Lite Collections are equipped with only the very basic and the most intuitive operations. More functionality == less easy to adopt. The framework is designed for extensibility.

Extensibility

All collections are interfaced. Relevant internal classes and properties are “protected” visible. The collections mostly do not depend on each other. The code is open source and can be reused in different contexts.

Ready-for-use

All collections can be used out of the box without major preparation and the need to embed the entire package.

Tested software

The library is entirely unit tested and also integration tested in a few example projects.

Documentation

The documentation consists of a comprehensive APIdoc and demonstrative articles in this blog. The level of demand is tried to keep low to bring collections to the people.

Performance

All of the collections and algorithms have been tested and improved for an optimal performance. Performance will be always enhanced by choosing a collection type, that meets the specific problem.

Quick overview

The library contains 9 distinctive ready-for-use collections, 5 of wich additionally in an event dispatching version, different iterators, a few utilities and several framework internally used algorithms and implementations. The different responsibilities are reflected in the package structure (click to enlarge).

as3_collections_package

The collections

The base collections in the root package cover the most common use cases. Special purpose implementations can be easily obtained through combination, subclassing or alternative interface realisation. For example, creating a priority queue, a stack or something unmodfiable hardly requires effort. Simply overriding a parent class method does the trick. The event dispatching collections in the fx package use inheritance. The SortedMap uses composition of a Dictionary and a binary tree. The diagram below shows the constitutional structure of the framework’s collections (click).

collections_fx

The lists, sets and maps

The choice for the particular list, set and map collections reflects the different requirements to data handling. Although it might be a good idea to have a sole collection doing everything, such a thing would be very unhandy, difficult to maintain and unperformant. Each particular Lite Collection has its distinctive purpose, and neither of them can fully replace one of the others. It’s up to the developer to decide wich collection suits best to the taks. Lists are very intuitive, since they are not limited in adding elements, but finding is generally slow. We use lists often for sequential data. Sets enable high speed access to elements but cannot contain duplicates. Sets are optimal for storing objects, which get passed around the application. Maps are practical when working with IDs or to link wrapped objects with their originals. The table below shows the features of the different Lite Collections.

Collection Characteristics Realisation
Multiplicity Order Access
The lists
ArrayList
ArrayListFx
multiple ordered random
sequential
Array
LinkedList multiple ordered sequential Composition
SortedList multiple sorted random
sequential
Array
The sets
Set unique unordered random Dictionary
LinkedSet
LinkedSetFx
unique ordered random
sequential
Dictionary +
Composition
SortedSet
SortedSetFx
unique sorted random
sequential
Binary tree
The maps
Map unique keys
multiple items
unordered random Dictionary
LinkedMap
LinkedMapFx
unique keys
multiple items
ordered random
sequential
Dictionary +
Composition
SortedMap
SortedMapFx
unique keys
unique items
sorted random
sequential
Dictionary +
Binary tree
Iterators

The Lite Collections can be only enumerated using their iterators, but it should be no issue to set up some alternative realisation that supports for-in-loops. Iterators provide a uniform way to retrieve the collection’s content while hiding the specific implementation. Iterators can be combined, filtered or recursively traversed. It’s possible to preinitialise iterators starting enumeration at a specific position.

Each collection is accompanied with at least one iterator. Two additional iterators are available for the map collections (key iterator, map iterator). Iterators don’t have the ability to modify a collection in any way. Each linked collection is provided with a pointer, which is elsewhere known as IViewCursor (Flex) or ListIterator (Java). The pointer is capable to add or remove items.

The special purpose RecursiveIterator and FilterIterator finalise the set of iterators. The RecursiveIterator enumerates a composite collection recursively. Filter iterators enable different views to the same collection. Both can be combined without limitation and make up this way a very powerful feature. Here an example: Iterator Example

Utilities

Two fundamental ready-made comparators (string, number) and the CollectionUtils, which converts any collection in a list or serialises collections recursively for debugging purposes.

Internal

All framework internal functionality lives in the core package. There are abstract implementations, shared types or data structures utilised by the particular collections. The library defines a custom namespace, which is occasionally applied to achieve framework internal visibility rather than confusing with masses of public methods.

Getting started

There is not much to get the collections running.

  1. Download and unzip the sources or checkout the SVN trunk.
  2. Link or copy the sources into your project.
  3. Import the collection classes and write code.
  4. Run the ready-made Examples or Examples Fx.

Resources

5 Responses to “Lite Collections Fx for ActionScript”


  1. 1 Visitor

    The iterator for your linked list class needs to support remove otherwise there is no way to delete an item from the list without performing an O(n) walk.

  2. 2 Jens Struwe

    Thanks for your reply. Even if the iterator would have a remove operation, you would need to iterate the entire list to have the cursor at the item to remove.

    I did not equip iterators with write access at all. To remove an item from a LinkedList you might use the LinkedList pointer, which is a special iterator only available to linked lists where random access is not possible. With this pointer you may add or remove items during a traversal.

    An example can be found here:

    http://sibirjak.com/blog/index.php/collections/lite-collections-examples/#pointer

  3. 3 Visitor

    Hi Jens, thanks for your reply. The standard Iterator interface in java has remove built in, which is what I was expecting.

  4. 4 Jens Struwe

    No worries. A removal is not as easy to implement, and I have considered removals during an iteration to be a rather rare situation.

    As an advanced developer you may access an extended set of operations using the internal namespace as3_collections. This way you pull the first node of a linked structure and modify the collection as you like.

    I have added an example here:

    http://sibirjak.com/blog/index.php/collections/lite-collections-examples/#linkedlist_traversal

  1. 1 New: Lite Collections for ActionScript

Leave a Reply