any which array but loose

57
Any Which Array But Loose Michael Labriola Digital Primates

Upload: michaellabriola

Post on 20-Jan-2015

1.061 views

Category:

Technology


0 download

DESCRIPTION

360|Flex Presentation on Arrays, Vectors, ByteArrays, Lists and Collections

TRANSCRIPT

Page 1: Any Which Array But Loose

Any Which Array But Loose

Michael LabriolaDigital Primates

Page 2: Any Which Array But Loose

Who are you?

Michael LabriolaSenior Consultant at Digital Primates

Flex GeekComponent DeveloperFlex Team Mentor

Page 3: Any Which Array But Loose

Who were you?

Michael LabriolaSoftware Engineer

Embedded Systems DeveloperReverse Engineer

Page 4: Any Which Array But Loose

What is this session about?

This session is part of my continuing quest to teach Flash and Flex from the inside out.

Learn what the Flash Player and the Flex framework are really doing and you are more likely to use them successfully, respect their boundaries and extend them in useful ways

Page 5: Any Which Array But Loose

One more reason

Let’s call it “Game Theory”.

If you know how something works really well, you know which rules you can bend and just how far you can bend them before they break.

Sometimes you can even find really creative ways out of difficult situations

Page 6: Any Which Array But Loose

Standard Disclaimer

I am going to lie to you a lot… a whole lot

Even at this ridiculous level of detail, there is much more

All of this is conditional. So, we are just going to take one route and go with it

Page 7: Any Which Array But Loose

What is an Array?

Arrangement of objects in memory.

Generally used to hold values in a directly index-able form

Page 8: Any Which Array But Loose

An Array

Mostly contiguous block of memory

Holds values

Can grow or shrink in size at runtime

Page 9: Any Which Array But Loose

An Array Can BeHomogeneous Heterogeneous

Page 10: Any Which Array But Loose

An Array Can BeDense Sparse

Page 11: Any Which Array But Loose

Null versus SparseArray with Null Sparse

Page 12: Any Which Array But Loose

What is a Vector?

A Vector is a new type of Array introduced with Flash Player 10.

It’s primary benefits are performance and type safety

Page 13: Any Which Array But Loose

A Vector

Is Dense (no gaps)

Is Homogenous (always the same base class)

Can be fixed length

Page 14: Any Which Array But Loose

SyntaxArray

var a:Array = new Array();

a[0] = “value”;a[1] = 345;a.push( 123 );

trace( v.length );

Vector

var v:Vector.<uint> = new

Vector.<uint>(n);

v[0] = “value”; //errorv[1] = 345;v.push( 123 );

trace( v.length );

Page 15: Any Which Array But Loose

A ByteArray

About the lowest level memory access there is in ActionScript

Let’s you access the raw bytes and order of data.

Up to you to manage what exists inside of the array

Page 16: Any Which Array But Loose

SyntaxByteArray

var byteArray:ByteArray = new ByteArray();

byteArray.writeBoolean(true);byteArray.writeDouble(Math.PI);

byteArray.position = 0;

trace( byteArray.readBoolean() ); //truetrace( byteArray.readDouble() ); //3.1415926535897

trace( byteArray[ 2 ] ); //9… why?

Page 17: Any Which Array But Loose

Breaking Apart Pete

When working with a ByteArray, the indices do not necessarily indicate discrete objects. They indicate discrete bytes

Page 18: Any Which Array But Loose

Position

Byte Array also has a position indicator. This indicates the next place you will read from or write to.

This is the beginning of a very important concept.

Page 19: Any Which Array But Loose

Great Things about Array

Array’s can be extremely fast to access data at a known index.

They also provide random access, meaning you can ask for the value at position 3 without caring that there is a position 2

Page 20: Any Which Array But Loose

Bad Things about Array

Array’s can be pretty slow when you need to insert or delete information from the middle

Moving from element to element (moving from 2 to 3) takes a fixed amount of time, which is good, but you also don’t get a speed advantage because those two are right next to eachother

Page 21: Any Which Array But Loose

Array Before Insert

Page 22: Any Which Array But Loose

Array Move Elements

Page 23: Any Which Array But Loose

Array Insert Element

Page 24: Any Which Array But Loose

Array Before Delete

Page 25: Any Which Array But Loose

Array Remove Element

Page 26: Any Which Array But Loose

Array Collapse

Page 27: Any Which Array But Loose

Array SearchingSearching an Array

generally involves iterating through each element looking for the value you would like.

If the array is sorted, you can make the search a smarter

Page 28: Any Which Array But Loose

Array CollectionsAt some point you may have been told to

use the ArrayCollection class.

So, what is it? ..It’s a wrapper around an array.

It provides the basic access to array by acting as a proxy. This allows ArrayCollection to lie to you

Page 29: Any Which Array But Loose

Key LiesSince ArrayCollection controls all information flow

in and out of its little regime, it can lie both about how big the array is and about the order of the elements.

It does this by creating a duplicate array that points to the original, when and if it is convenient.

Fortunately, you control this by means of the sortand filterFunction

Page 30: Any Which Array But Loose

Sorted Array Collection

One you have provided the ArrayCollection a Sort instance and called its refresh() method, it builds an array which maps the original elements to the new sort

Page 31: Any Which Array But Loose

SyntaxArrayCollection Sort

var ac:ArrayCollection = new ArrayCollection(data);

var ac:ArrayCollection = new ArrayCollection();var sort:Sort = new Sort();var field:SortField = new SortField( "hireDate" );sort.fields = [ field ];ac.sort = sort;ac.refresh();

Page 32: Any Which Array But Loose

Collection ResortedSorting doesn’t actually need to shift the original data, just the new internal array.

Page 33: Any Which Array But Loose

Searching a CollectionOnce an ArrayCollection has been sorted,

searching it is a very efficient and easy process.

The reduction in time to sort a large ArrayCollection is very significant over an array.

Page 34: Any Which Array But Loose

Filtered Array Collection

One you have provided the ArrayCollection with a filter function and called its refresh() method, it builds an array which maps the original elements to the new array if they pass the test in your function

Page 35: Any Which Array But Loose

SyntaxArrayCollection Filter

var ac:ArrayCollection = new ArrayCollection();ac.filterFunction = ifIFeelLikeIt;ac.refresh();

function ifIFeelLikeIt( item:Object ):Boolean {//do some logic and decidereturn ( item != Andy );

}

Page 36: Any Which Array But Loose

Andy Filtered Out

Page 37: Any Which Array But Loose

Another Key FeatureWhile not busy deceiving you, ArrayCollection

has a handful of really useful additional features.

The reason most people use ArrayCollection, even if they aren’t sure why, is that it is an event dispatcher.

When you use the ArrayCollection with DataBinding, the ArrayCollection informs you of changes

Page 38: Any Which Array But Loose

Collection Broadcaster

Page 39: Any Which Array But Loose

Collection InternalsgetProperty( 3 );trace( collection[ 3 ] );

getItemAt( 3 );

get localIndex[ 3 ];

setProperty( 3, 8 ); collection[ 3 ] = 8;

setItemAt( 8, 3 );

list.setItemAt;

Update Value

Dispatch Event

Page 40: Any Which Array But Loose

Final OneThrough inheritance ArrayCollection also

provides one final important feature, it can work with a cursor.

A bit like the position in a ByteArray, the cursor points to a given object in the collection. You can use the cursor to move forward to the next item, move back to a previous item, seek to well-known locations or search sorted data.

Page 41: Any Which Array But Loose

Cursor

Page 42: Any Which Array But Loose

Cursor.moveNext()

Page 43: Any Which Array But Loose

DataGrid and List BaseIn fact, the List base controls in Flex use

these cursors pretty much exclusively inside of these components.

One of the reasons they can accept many different types of input is they simply wrap the input in one of (n) classes that implement ICollectionView

Page 44: Any Which Array But Loose

ICollectionViewICollectionView mandates that an object can

dispatch events, understands how to sort, filter and refresh along with a handful of other items, and, most importantly, that the object can create a cursor that will move through an child objects.

Page 45: Any Which Array But Loose

ICollectionViewThis is really important as it allows us to use

various types of Data Structures, other than just ArrayCollection to fill this need.

For example, we can trivially make a VectorCollection which uses vectors instead of Arrays. We can create a ByteArrayCollection should we feel so inclined.

Page 46: Any Which Array But Loose

Cursor/IteratorWith this new found knowledge, we could

also explore data structures that don’t exist in Flex today, but could.

One data structure in particular is really fantastic when you need to do frequent inserts/deletes or simply move through the values sequentially, a linked list.

Page 47: Any Which Array But Loose

Linked ListA linked list is a data structure that is just a

sequence of pieces of data, often called nodes.

Linked lists can be implemented in a single fashion, where each node only knows about the next, or, in a double fashion where each node knows who came before it and who came after it

Page 48: Any Which Array But Loose

Linked List

Each node in this linked list knows about who is sequentially before it, and who is sequentially after.

They don’t have any information about the other nodes

Page 49: Any Which Array But Loose

Inserting ListThis makes the process

of inserting into a list extremely efficient.

There is no need to move any of the existing nodes, you simply change who (in this case) Pete and Andy believe are next and previous.

Page 50: Any Which Array But Loose

Deleting ListDeleting is equally as

efficient.

In this case when we chose to remove Andy, we simply change the previous and next of Pete and Thomas

Page 51: Any Which Array But Loose

Andy is GCedEventually in the

future, the garbage collector ruthlessly slaughters Andy.

However, that is not the concern of this session.

Page 52: Any Which Array But Loose

Searching List

Searching an unsorted list takes about the same effort as searching an Array.

Page 53: Any Which Array But Loose

Searching By Index

Where linked lists are much slower is when you need to find an item at a particular index. For an array, that is a very fast operation.

Here it requires a walk through the list.

Page 54: Any Which Array But Loose

Working with Cursors

The paradigm used by cursors, however, works very well with lists where we generally are moving between nodes

Page 55: Any Which Array But Loose

Results

For good measure, let’s take a look at some data

Page 56: Any Which Array But Loose

Q & A

Seriously? You must have some questions by now?

Page 57: Any Which Array But Loose

Resources

Blog Aggregator (All of the Digital Primates)http://blogs.digitalprimates.net/

My Blog Specificallyhttp://blogs.digitalprimates.net/codeSlinger/

Follow Me on Twittermlabriola