Parcourir la source

improved Collection to SmartCollection

Vousk-prod il y a 10 ans
Parent
commit
789c360ae5

+ 0 - 30
Babylon/Tools/babylon.collection.js

@@ -1,30 +0,0 @@
-var BABYLON;
-(function (BABYLON) {
-    var Collection = (function () {
-        function Collection() {
-            this.count = 0;
-            this.items = {};
-        }
-        Collection.prototype.add = function (key, item) {
-            if (this.items[key] != undefined) {
-                return undefined;
-            }
-            this.items[key] = item;
-            return ++this.count;
-        };
-        Collection.prototype.remove = function (key) {
-            if (this.items[key] == undefined) {
-                return undefined;
-            }
-            delete this.items[key];
-            return --this.count;
-        };
-        Collection.prototype.item = function (key) {
-            return this.items[key];
-        };
-        return Collection;
-    })();
-    BABYLON.Collection = Collection;
-})(BABYLON || (BABYLON = {}));
-
-//# sourceMappingURL=babylon.collection.js.map

+ 0 - 31
Babylon/Tools/babylon.collection.ts

@@ -1,31 +0,0 @@
-module BABYLON {
-    export class Collection {
-        public count: number = 0;
-        public items: any;
-
-        constructor() {
-            this.items = {};
-        }
-
-        public add(key: string, item: any): number {        
-            if (this.items[key] != undefined) {
-                return undefined;
-            }
-            this.items[key] = item;
-            return ++this.count;
-        }
-     
-        public remove(key: string): number {
-            if (this.items[key] == undefined) {
-                return undefined;
-            }
-            delete this.items[key];
-            return --this.count;
-        }
- 
-        public item(key: string): any {
-            return this.items[key];
-        }
-
-    }
-} 

+ 106 - 0
Babylon/Tools/babylon.smartCollection.ts

@@ -0,0 +1,106 @@
+module BABYLON {
+    export class SmartCollection {
+        public count: number = 0;
+        public items: any;
+        
+        private _keys: string[];
+        private _initialCapacity: number;
+        
+        constructor(capacity: number = 10) {
+            this._initialCapacity = capacity;    
+            this.items = {};
+            this._keys = new Array(this._initialCapacity);
+        }
+
+        public add(key: any, item: any): number {        
+            if (this.items[key] != undefined) {
+                return -1;
+            }
+            this.items[key] = item;
+            
+            //literal keys are always strings, but we keep source type of key in _keys array
+            this._keys[this.count++] = key;
+            if (this.count > this._keys.length) {
+                this._keys.length *= 2;
+            }
+            
+            return this.count;
+        }
+     
+        public remove(key: any): number {
+            if (this.items[key] == undefined) {
+                return -1;
+            }
+            
+            return this.removeItemOfIndex(this.indexOf(key));
+        }
+ 
+        public removeItemOfIndex(index: number): number {
+            if (index < this.count && index > -1) {
+                delete this.items[this._keys[index]];
+                    
+                //here, shifting by hand is better optimised than .splice
+                while (index < this.count) { 
+                   this._keys[index] = this._keys[index+1]; index++;
+                }
+            }
+            else { 
+                return -1; 
+            }
+            
+            return --this.count;
+        }
+        
+        public indexOf(key: any): number {
+            for (var i = 0 ; i != this.count ; i++) {
+                if (this._keys[i] === key) { 
+                    return i; 
+                }
+            }
+            return -1;
+        }
+        
+        public item(key: any): any {
+            return this.items[key];
+        }
+
+        public getAllKeys(): any[] {
+            if (this.count > 0) {
+                var keys = new Array(this.count);
+                for (var i = 0; i < this.count; i++) {
+                    keys[i] = this._keys[i];
+                }
+                return keys;
+            } 
+            else {
+                return undefined;
+            }
+        }
+        
+        public getKeyByIndex(index: number): any {
+            if (index < this.count && index > -1) {
+                return this._keys[index];
+            }
+            else {
+                return undefined;
+            }
+        }
+    
+        public getItemByIndex(index: number): any {
+            if (index < this.count && index > -1) {
+                return this.items[this._keys[index]];
+            }
+            else {
+                return undefined;
+            }
+        }
+    
+        public empty(): void {
+            if (this.count > 0) {
+                this.count = 0;
+                this.items = {};
+                this._keys = new Array(this._initialCapacity);
+            }
+        }
+    }
+}