Skip to content

Instantly share code, notes, and snippets.

@stickupkid
Created March 16, 2012 10:36
Show Gist options
  • Save stickupkid/2049496 to your computer and use it in GitHub Desktop.
Save stickupkid/2049496 to your computer and use it in GitHub Desktop.
Comparing dictionaries
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.utils.Dictionary;
import flash.utils.getTimer;
import flash.utils.setTimeout;
public class Main extends Sprite
{
private var textField : TextField;
private var dict1 : Dictionary;
private var dict2 : Dictionary;
private var objects : Vector.<Object> = new Vector.<Object>();
public function Main()
{
setTimeout(boot, 3000);
}
private function boot():void
{
textField = new TextField();
textField.text = "Boot:\n";
textField.width = 500;
textField.height = 500;
addChild(textField);
for (var i : int = 0; i < 100000; i++)
{
objects.push({key:i});
}
dict1 = generateDictionary();
dict2 = generateDictionary();
setTimeout(init, 2000);
}
private function init():void
{
var start : int = getTimer();
start = getTimer();
textField.appendText(compareDictionaries(dict1, dict2) + "\n");
textField.appendText(getTimer() - start + "\n");
start = getTimer();
textField.appendText(areEqualDictionaries(dict1, dict2) + "\n");
textField.appendText(getTimer() - start + "\n");
start = getTimer();
textField.appendText(compareDictionariesAlt(dict1, dict2) + "\n");
textField.appendText(getTimer() - start + "\n");
start = getTimer();
textField.appendText(compareDictionariesAlt2(dict1, dict2) + "\n");
textField.appendText(getTimer() - start + "\n");
}
private function generateDictionary() : Dictionary
{
const dict : Dictionary = new Dictionary();
var keys : Array = [];
for (var i : int = 0; i < 100000; i++)
{
var n : Object = objects[i];
dict[n] = n;
keys.push(n);
}
return dict;
}
public function compareDictionaries(p0 : Dictionary, p1 : Dictionary) : Boolean
{
if (p0 == p1)
{
return true;
}
if(p0 == null || p1 == null)
{
return false;
}
const matched:Dictionary = new Dictionary();
for(var k0:Object in p0) {
if(p0[k0] != p1[k0]) {
return false;
}
matched[k0] = k0;
}
for(var k1:Object in p1) {
if(matched[k1]) {
continue;
} else {
if(p1[k1] != p0[k1]) {
return false;
}
}
}
return true;
}
public function compareDictionariesAlt(d1 : Dictionary, d2 : Dictionary) : Boolean
{
// quick check for the same object
if ( d1 == d2 )
return true;
// check for null
if ( d1 == null || d2 == null )
return false;
// go through the keys in d1 and check if they're in d2 - also keep a count
var count : int = 0;
for ( var key:* in d1 )
{
// check that the values are the same
if ( d1[key] !== d2[key] )
return false;
count++;
}
// now just make sure d2 has the same number of keys
var count2 : int = 0;
for ( key in d2 )
count2++;
// return if they're the same size
return ( count == count2 );
}
public function compareDictionariesAlt2(d1 : Dictionary, d2 : Dictionary) : Boolean
{
// quick check for the same object
if ( d1 == d2 )
return true;
// check for null
if ( d1 == null || d2 == null )
return false;
// go through the keys in d1 and check if they're in d2 - also keep a count
var count : int = 0;
for ( var key:* in d1 )
{
// check that the values are the same
if (!(key in d2))
return false;
count++;
}
// now just make sure d2 has the same number of keys
var count2 : int = 0;
for ( key in d2 )
count2++;
// return if they're the same size
return ( count == count2 );
}
public function areEqualDictionaries(dict1 : Dictionary, dict2 : Dictionary) : Boolean
{
if (dict1 === dict2) return true;
var keys : Array = equalKeysArrayOrNull(dict1, dict2);
if (keys)
return haveEqualValues(keys, dict1, dict2);
else
return false;
}
private function equalKeysArrayOrNull(dict1 : Dictionary, dict2 : Dictionary) : Array
{
var keys1 : Array = enumerateKeys(dict1).sort();
var keys2 : Array = enumerateKeys(dict2).sort();
if ( keys1.length != keys2.length ) return null;
var i : int = -1;
while (++i < keys1.length)
if (keys1[i] !== keys2[i]) return null;
return keys1;
}
private function haveEqualValues(keys : Array, dict1 : Dictionary, dict2 : Dictionary) : Boolean
{
for each (var key:* in keys)
if (dict1[key] != dict2[key]) return false;
return true;
}
private function enumerateKeys(dict : Dictionary) : Array
{
var keys : Array = [];
for (var key:* in dict)
keys.push(key);
return keys;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment