Skip to content

Instantly share code, notes, and snippets.

@jasonm23
Created April 30, 2010 10:12
Show Gist options
  • Save jasonm23/385032 to your computer and use it in GitHub Desktop.
Save jasonm23/385032 to your computer and use it in GitHub Desktop.
ArrayDiff compares two arrays and provides added, matched & removed arrays.
package
{
/**
* ArrayDiff compares two arrays comparison & master, and
* produces 3 arrays:
*
* matched array is a collection of items that are present
* in both arrays.
*
* removed array is a collection of items that are present
* in master but not comparison.
*
* added array is a collection of items that are present
* in comparison but not master.
*/
public class ArrayDiff
{
public function ArrayDiff(comparison:Array, master:Array)
{
compare(comparison, master);
}
private var _added:Array = new Array();
public function get added():Array
{
return _added;
}
private var _matched:Array = new Array();
public function get matched():Array
{
return _matched;
}
private var _removed:Array = new Array();
public function get removed():Array
{
return _removed;
}
/**
* Compare will flush the current matched, added, removed items
* from the ArrayDiff and create new ones from the supplied
* master and comparison array.
*/
public function compare(comparison:Array, master:Array):void
{
_added = new Array();
_removed = new Array();
_matched = new Array();
for each (var item:* in master)
{
if (comparison.indexOf(item) == -1)
{
removed.push(item);
}
else
{
matched.push(item);
}
}
for each (var item:* in comparison)
{
if (master.indexOf(item) == -1)
{
added.push(item);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment