开发者

ActionScript - Comparing & Removing Duplicates of Complex Arrays?

when comparing simple arrays, i use something like the f开发者_Go百科ollowing function to concatenate and remove duplicates:

//Merge
public function merge(a1:Array, a2:Array):Array
{
    var result:Array = a1.concat(a2);

    var dictionary:Dictionary = new Dictionary();

    for each    (var item:Object in result)
                dictionary[item] = true;

    result = new Array();

    for (var key:Object in dictionary)
        result.push(key);

    dictionary = null;

    return result;
}

however, this approach doesn't work on complex arrays.

is there a well known algorithm, or is it even possible to write a function of recursion that can compare a Vector.<Object> with another? one that will always work even if the some objects being compared have additional key/value pairs?


[EDIT]


to be more clear, using a dictionary to determine if items in a simple array only works on primitive data types (int, number, string, etc.) or object references, so the above example works if it's passed 2 arrays resembling something like this:

var arr1:Array = new Array(1, 2, 3, 4, 5);
var arr2:Array = new Array(8, 7, 6, 5, 4);

resulting in a merged array with the following values:

1, 2, 3, 8, 7, 6, 5, 4

in contrast, i'm asking if it's possible to pass a function 2 complex arrays or Vector.<Object> all containing unique objects that may have identical key/value pairs and remove redundencies in the resulting Vector.<Object>. for example:

var vec1:Vector.<Object> = new Vector.<Object>();
vec1.push({city:"Montreal", country:"Canada"});
vec1.push({city:"Halifax", country:"Canada"});

var vec2:Vector.<Object> = new Vector.<Object>();
vec2.push({city:"Halifax", country:"Canada"});
vec2.push({city:"Toronto", country:"Canada"});

merging the above 2 vector objects would result in the following vector by determining and removing objects with identical key/value pairs:

{city:"Montreal", country:"Canada"}
{city:"Halifax", country:"Canada"}
{city:"Toronto", country:"Canada"}

i'm searching for an algorithm which could handle the removal of these similar objects without having to know about their specific key/value names or how many key/value pairs there are within the object.


Sure you can, you can build a similar example with any type of Vector:

     public function mergeObjectVectors(v1:Vector.<Object>,                      
                                        v2:Vector.<Object>):Vector.<Object>
     {
        var dictionary:Dictionary = new Dictionary();
        var concat:Vector.<Object> = v1.concat(v2);
        var result:Vector.<Object> = new Vector.<Object>();

        for each(var i:Object in concat)
        {
            if (!dictionary[i])
            {
                dictionary[i] = true;
                result.push(i);
            }
        }           

        return result;
    } 

However if you plan on accepting vectors of any type, it's different:

        public function testing():void
        {
             var v1:Vector.<Object> = new Vector.<Object>();
             v1.push({name:"Object 1"});
             v1.push({name:"Object 2"});

             // Vector w duplicates
             var v2:Vector.<Object> = new Vector.<Object>();
             var o:Object = {name:"Object 3"};                 
             v2.push(o);
             v2.push(o);
             v2.push(o);

             var resultVector:Vector.<Object> = mergeAnything(v1, v2, Class(Vector.<Object>));
             var resultArray:Array = mergeAnything(v1, v2, Array);
             var resultObject:Object = mergeAnything(v1, v2, Object);
        }


        public function mergeAnything(o1:Object, o2:Object, resultClass:Class):*
        {

            var dictionary:Dictionary = new Dictionary();
            var result:Object = new resultClass();

            var i:int;
            for each(var o:Object in o1)
            {
               if (!dictionary[o])
               {
                  dictionary[o] = true;
                  result[i++] = o;
               }
            }   

            for each(o in o2)
            {
               if (!dictionary[o])
               {
                  dictionary[o] = true;
                  result[i++] = o;
               }
            }

            return result;
        }

The first example will be more resource-efficient.


EDIT: This should do it, try it with your example:

     public function mergeObjectVectors(v1:Vector.<Object>, v2:Vector.<Object>):Vector.<Object>
    {
        var concat:Vector.<Object> = v1.concat(v2);
        var result:Vector.<Object> = new Vector.<Object>();

        var n:int = concat.length;
        loop:for (var i:int = 0; i < n; i++)
        {   
            var objectToAdd:Object = concat[i];

            var m:int = result.length;
            for (var j:int = 0; j < m; j++)
            {
                var addedObject:Object = result[j];
                if (this.areObjectsIdentical(objectToAdd, addedObject))
                {
                    continue loop;
                }
            }
            result.push(objectToAdd);
        }           

        return result;
    } 

    private function areObjectsIdentical(o1:Object, o2:Object):Boolean
    {
        var numComparisons:int = 0;

        for (var s:String in o1)
        {
            numComparisons++;
            if (o1[s] != o2[s])
            {
                return false;
            }
        }                   
        for (s in o2)
        {
            numComparisons--;
        }

        return !numComparisons;     
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜