You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flex.apache.org by ah...@apache.org on 2016/08/25 15:32:51 UTC

[43/50] [abbrv] git commit: [flex-asjs] [refs/heads/spark] - implement priority queue without dictionary

implement priority queue without dictionary


Project: http://git-wip-us.apache.org/repos/asf/flex-asjs/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-asjs/commit/d7816567
Tree: http://git-wip-us.apache.org/repos/asf/flex-asjs/tree/d7816567
Diff: http://git-wip-us.apache.org/repos/asf/flex-asjs/diff/d7816567

Branch: refs/heads/spark
Commit: d7816567402c2f332fa49d0b9c668048c94f18f6
Parents: 4e61ce4
Author: Alex Harui <ah...@apache.org>
Authored: Tue Aug 23 22:19:43 2016 -0700
Committer: Alex Harui <ah...@apache.org>
Committed: Tue Aug 23 22:19:43 2016 -0700

----------------------------------------------------------------------
 .../flex/mx/managers/ILayoutManagerClient.as    |  11 ++
 .../mx/managers/layoutClasses/PriorityQueue.as  | 117 ++++++++++++-------
 2 files changed, 83 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/d7816567/frameworks/projects/MX/src/main/flex/mx/managers/ILayoutManagerClient.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/MX/src/main/flex/mx/managers/ILayoutManagerClient.as b/frameworks/projects/MX/src/main/flex/mx/managers/ILayoutManagerClient.as
index 1eb5d42..60c45bc 100644
--- a/frameworks/projects/MX/src/main/flex/mx/managers/ILayoutManagerClient.as
+++ b/frameworks/projects/MX/src/main/flex/mx/managers/ILayoutManagerClient.as
@@ -206,6 +206,17 @@ public interface ILayoutManagerClient extends IEventDispatcher
      *  @productversion Flex 3
      */
     function validateDisplayList():void;
+    
+    /**
+     *  Unique identifier for each instance.
+     *  
+     *  @langversion 3.0
+     *  @playerversion Flash 9
+     *  @playerversion AIR 1.1
+     *  @productversion Flex 3
+     */
+    function get uid():String;
+
 }
 
 }

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/d7816567/frameworks/projects/MX/src/main/flex/mx/managers/layoutClasses/PriorityQueue.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/MX/src/main/flex/mx/managers/layoutClasses/PriorityQueue.as b/frameworks/projects/MX/src/main/flex/mx/managers/layoutClasses/PriorityQueue.as
index 4aca9b9..423d67c 100644
--- a/frameworks/projects/MX/src/main/flex/mx/managers/layoutClasses/PriorityQueue.as
+++ b/frameworks/projects/MX/src/main/flex/mx/managers/layoutClasses/PriorityQueue.as
@@ -119,20 +119,10 @@ public class PriorityQueue
             // If no hash exists for the specified priority, create one.
             bin = new PriorityBin();
             priorityBins[priority] = bin;
-            bin.items[obj] = true;
-            bin.length++;
-        }
-        else
-        {
-            // If we don't already hold the obj in the specified hash, add it
-            // and update our item count.
-            if (bin.items[obj] == null)
-            { 
-                bin.items[obj] = true;
-                bin.length++;
-            }
         }
         
+        bin.add(obj as ILayoutManagerClient);
+        
     }
 
     /**
@@ -153,15 +143,7 @@ public class PriorityQueue
                 bin = priorityBins[maxPriority];
             }
         
-            // Remove the item with largest priority from our priority queue.
-            // Must use a for loop here since we're removing a specific item
-            // from a 'Dictionary' (no means of directly indexing).
-            for (var key:Object in bin.items )
-            {
-                obj = key;
-                removeChild(ILayoutManagerClient(key), maxPriority);
-                break;
-            }
+            obj = bin.next();
 
             // Update maxPriority if applicable.
             while (!bin || bin.length == 0)
@@ -196,7 +178,7 @@ public class PriorityQueue
                     // client, no need to search the entire list, just check to see
                     // if the client exists in the queue (it would be the only item
                     // at that nestLevel).
-                    if (bin.items[client])
+                    if (bin.hasItem(client))
                     {
                         removeChild(ILayoutManagerClient(client), max);
                         return client;
@@ -204,12 +186,12 @@ public class PriorityQueue
                 }
                 else
                 {
-                    for (var key:Object in bin.items )
+                    for each (var obj:Object in bin.items )
                     {
-                        if ((key is DisplayObject) && contains(DisplayObject(client), DisplayObject(key)))
+                        if ((obj is DisplayObject) && contains(DisplayObject(client), DisplayObject(obj)))
                         {
-                            removeChild(ILayoutManagerClient(key), max);
-                            return key;
+                            removeChild(ILayoutManagerClient(obj), max);
+                            return obj;
                         }
                     }
                 }
@@ -247,15 +229,7 @@ public class PriorityQueue
                 bin = priorityBins[minPriority];
             }           
 
-            // Remove the item with smallest priority from our priority queue.
-            // Must use a for loop here since we're removing a specific item
-            // from a 'Dictionary' (no means of directly indexing).
-            for (var key:Object in bin.items )
-            {
-                obj = key;
-                removeChild(ILayoutManagerClient(key), minPriority);
-                break;
-            }
+            obj = bin.next();
 
             // Update minPriority if applicable.
             while (!bin || bin.length == 0)
@@ -288,7 +262,7 @@ public class PriorityQueue
                     // client, no need to search the entire list, just check to see
                     // if the client exists in the queue (it would be the only item
                     // at that nestLevel).
-                    if (bin.items[client])
+                    if (bin.hasItem(client))
                     {
                         removeChild(ILayoutManagerClient(client), min);
                         return client;
@@ -296,12 +270,12 @@ public class PriorityQueue
                 }
                 else
                 {
-                    for (var key:Object in bin.items)
+                    for each (var obj:Object in bin.items)
                     {
-                        if ((key is DisplayObject) && contains(DisplayObject(client), DisplayObject(key)))
+                        if ((obj is DisplayObject) && contains(DisplayObject(client), DisplayObject(obj)))
                         {
-                            removeChild(ILayoutManagerClient(key), min);
-                            return key;
+                            removeChild(ILayoutManagerClient(obj), min);
+                            return obj;
                         }
                     }
                 }
@@ -328,10 +302,9 @@ public class PriorityQueue
     {
         var priority:int = (level >= 0) ? level : client.nestLevel;
         var bin:PriorityBin = priorityBins[priority];
-        if (bin && bin.items[client] != null)
+        if (bin && bin.hasItem(client) != null)
         {
-            delete bin.items[client];
-            bin.length--;
+            bin.remove(client);
             return client;
         }
         return null;
@@ -381,6 +354,7 @@ COMPILE::LATER
 {
 import flash.utils.Dictionary;
 }
+import mx.managers.ILayoutManagerClient;
 
 /**
  *  Represents one priority bucket of entries.
@@ -388,11 +362,64 @@ import flash.utils.Dictionary;
  */
 class PriorityBin 
 {
-    public var length:int;
 	COMPILE::LATER
 	{
     public var items:Dictionary = new Dictionary();
 	}
-	public var items:Object = new Object();
+    
+    public function PriorityBin()
+    {
+        arr = [];
+        map = {};
+    }
+    
+    private var arr:Array;
+    private var map:Object;
+    
+    public function get length():int
+    {
+        return arr.length;
+    }
+    
+    public function add(obj:ILayoutManagerClient):void
+    {
+        if (map[obj.uid] == null)
+        {
+            arr.push(obj);
+            map[obj.uid] = obj;
+        }
+    }
+    
+    public function remove(obj:ILayoutManagerClient):void
+    {
+        var i:int = arr.indexOf(obj);
+        arr.splice(i, 1);
+        delete map[obj.uid];
+    }
+    
+    public function next():ILayoutManagerClient
+    {
+        if (arr.length == 0) return null;
+        
+        var obj:ILayoutManagerClient = arr.shift() as ILayoutManagerClient;
+        delete map[obj.uid];
+        return obj;
+    }
+    
+    public function hasItem(obj:ILayoutManagerClient):Boolean
+    {
+        return map[obj.uid] != null;    
+    }
+    
+    public function empty():void
+    {
+        arr.length == 0;
+        map = {};
+    }
+    
+	public function get items():Object
+    {
+        return arr;
+    }
     
 }