The collections package has some classes in it that provide a very powerful way of storing, retrieving, selecting, sorting and manipulating data. It features well known datastructures like lists (both the double linked list version and the array list version, very useful for storing lists of like object instances, which is useful in almost every application you encounter), queues (very useful for getting things done in a specified order, first in first out or FIFO), stacks , deques ( double ended queue, can function as a stack and a queue), heaps, priorityqueue, set, resultset (very useful when using flash remoting and a database) and trees (for menus, games, searching etc., both binary and n-ary trees). It is a very comprehensive library, influenced by the Java collections package, with a clear interface, easy to use and very consistent throughout the different classes. Know one of them, know ‘em all.
In our projects, whereever we needed datastructures, we’ve been using the great opensource package from Michael Baczynksy at polygonal.de for the use of lists, queues, stacks and trees
.
While it works great, it has some explicit design decisions that we do not want to follow, mainly the direct property access throughout the classes and the lack of reuse throughout the code. This works for him as it is fantastic for pure speed, but in our case we needed some other design decisions, as we wanted to make a better integration in our own package and also needed the flexibility to change the interface and have some stuff in there that better suited our way of programming. Plus the added benefit of throwing in some other powerful stuff, like selection, an easy setup of sorting on the lists, foolproof iterators (no unintended infinite loops) and some robust, high level code.
One of the first design decisions was to use the interface of the Java Collections as a basis for our collections package. This is a very well known interface which is perfectly suited for our goals.
The basics were lists: LinkedList and ArrayList. There were many requirements for the lists, but we were not afraid of making them power packed lists. Therefore the lists themselves were given the capabilities to be functionally very flexible and powerful. We decided to have them implement interfaces for queues, stacks, lists and Deque (double ended queue) as this would cover a lot of our uses cases of lists.
Furthermore we needed the lists to be sortable with quick (custom writable) sorting algorithms and to make them selectable by using a selection criterium that was outside of the list itself (get me all the listitems that satisfy a certain criterium), so we would not have to write code like users.getByFirstName(”henk”) but instead make this more generic (see the specifications post on this blog).
By implementing these interfaces, we were able to design the lists to be used in a multitude of situations just by casting them to a certain interface, for example:
//make it a queue var queue: IQueue = new LinkedList(); queue.enqueue('a'); queue.enqueue('b'); trace(queue.dequeue());//'a' //or make it a stack var stack: IStack = new LinkedList(); stack.push('a'); stack.push('b'); trace(stack.pop());//'b' //or make it have all the power you want from a list var list: LinkedList = new LinkedList(); list.add('c'); list.add('a'); list.add('b'); //sort it, using quicksort, using one of the provided sort methods. list.sort(Comparators.compareStringDescending, SortTypes.QUICK); //now iterate var iterator: IIterator = list.iterator(); while(iterator.hasNext()){ //only one call to access data and go to the next item trace(iterator.next()); } //trace output after sorting: c, b, a //now select some stuff from the list with a one liner, getting a range from our list by using a specification var subsetOfAlphabet: IList = list.selectBy(new getRangeFromAlphabetSpecification('a', 'b')); trace(subsetOfAlphabet.toArray().toString());//b,a
As is obvious from the example above, the lists are usable in a multitude of programming problems where they can fullfill various roles and get to the solution fast. They can be manipulated in very powerful ways, provide good encapsulation and are very heavily unittested, therefore we know they work very well :). Did I also mention that they are fast? The different sorting methods (mergesort, quicksort etc) are very fast, very customizable and very convenient. Adding, removing, iterating, enqueing, dequeing, pushing and popping are also very fast. For concrete examples of using the lists, check out the test code to see what kind of powerful manipulations you can do.
We will delve deeper in the collections package in the near future on the blog. Our nice-to-have /todo list features articles on iterators, trees and how to manipulate / traverse them, sorting and an overview of the api.
Class nl.dpdk.collections.lists.OrderedList missed out ?
How can i get it?
Cheers
Hi Mith, the OrderedList class was removed before the launch. While fully functional, we were not certain about the inheritance chain. I will look back into it, and probably put it back online soon.
Mith, I added the ordered list again, including the unittests. We will probably put another version online with another implementation (it will not inherit from ArrayList, so we can get rid of the IDeque interface) but the interface will be the IList interface for the orderedList.
The OrderedList implements the ISearchable interface and thus is searchable. the implementation is a binary search, which is very fast! Keep in mind the performance advantages and the disadvantages in certain situations for an ordered list.
Again
the OrderedList has been refactored. It now delegates to ArrayList instead of subclassing it. The IDeque interface has been removed, and an IRandomAccess interface has been added for both ArrayList and OrderedList. All unittests passed (go green!).
The latest version is now online.
All things that we wanted to change on the ordered list are now handled.
Check out the binary search and the new map, fold and apply features
Big, big, big, big big big thank you for this.
I tried the poly.de linked list stuff and it was just breaking here and there when I wanted to do some interesting things. This lib never breaks. Its fantastic, and easier to code with. If there is a speed loss, then I will take it gladly because I have regained my sanity.
Thank you!
great that it worked out for you, thanks for the compliments.
Spread the word
Does not work with CS4 because its a pseudo internal class. You have to remove the internal classes for compatability, unless you found another work around. I posted something similar in anohter section that uses this script.
Thanks cyberoptics for pointing this out, I just posted a little update on the site. we decided to get rid of the internal classes as you suggested.