From 140f8c86830905fff87d4bf1cefd67387036dd1e Mon Sep 17 00:00:00 2001 From: Shawn Brenneman Date: Wed, 23 Jan 2013 17:40:55 +0100 Subject: [PATCH 1/6] Add goog.structs.LinkedMap from Closure Library --- src/ol/structs/linkedmap.js | 473 ++++++++++++++++++++++++++++++++++++ 1 file changed, 473 insertions(+) create mode 100644 src/ol/structs/linkedmap.js diff --git a/src/ol/structs/linkedmap.js b/src/ol/structs/linkedmap.js new file mode 100644 index 0000000000..1995349a73 --- /dev/null +++ b/src/ol/structs/linkedmap.js @@ -0,0 +1,473 @@ +// Copyright 2007 The Closure Library Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS-IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * @fileoverview A LinkedMap data structure that is accessed using key/value + * pairs like an ordinary Map, but which guarantees a consistent iteration + * order over its entries. The iteration order is either insertion order (the + * default) or ordered from most recent to least recent use. By setting a fixed + * size, the LRU version of the LinkedMap makes an effective object cache. This + * data structure is similar to Java's LinkedHashMap. + * + * @author brenneman@google.com (Shawn Brenneman) + */ + + +goog.provide('goog.structs.LinkedMap'); + +goog.require('goog.structs.Map'); + + + +/** + * Class for a LinkedMap datastructure, which combines O(1) map access for + * key/value pairs with a linked list for a consistent iteration order. Sample + * usage: + * + *
+ * var m = new LinkedMap();
+ * m.set('param1', 'A');
+ * m.set('param2', 'B');
+ * m.set('param3', 'C');
+ * alert(m.getKeys()); // param1, param2, param3
+ *
+ * var c = new LinkedMap(5, true);
+ * for (var i = 0; i < 10; i++) {
+ *   c.set('entry' + i, false);
+ * }
+ * alert(c.getKeys()); // entry9, entry8, entry7, entry6, entry5
+ *
+ * c.set('entry5', true);
+ * c.set('entry1', false);
+ * alert(c.getKeys()); // entry1, entry5, entry9, entry8, entry7
+ * 
+ * + * @param {number=} opt_maxCount The maximum number of objects to store in the + * LinkedMap. If unspecified or 0, there is no maximum. + * @param {boolean=} opt_cache When set, the LinkedMap stores items in order + * from most recently used to least recently used, instead of insertion + * order. + * @constructor + */ +goog.structs.LinkedMap = function(opt_maxCount, opt_cache) { + /** + * The maximum number of entries to allow, or null if there is no limit. + * @type {?number} + * @private + */ + this.maxCount_ = opt_maxCount || null; + + /** + * @type {boolean} + * @private + */ + this.cache_ = !!opt_cache; + + this.map_ = new goog.structs.Map(); + + this.head_ = new goog.structs.LinkedMap.Node_('', undefined); + this.head_.next = this.head_.prev = this.head_; +}; + + +/** + * Finds a node and updates it to be the most recently used. + * @param {string} key The key of the node. + * @return {goog.structs.LinkedMap.Node_} The node or null if not found. + * @private + */ +goog.structs.LinkedMap.prototype.findAndMoveToTop_ = function(key) { + var node = /** @type {goog.structs.LinkedMap.Node_} */ (this.map_.get(key)); + if (node) { + if (this.cache_) { + node.remove(); + this.insert_(node); + } + } + return node; +}; + + +/** + * Retrieves the value for a given key. If this is a caching LinkedMap, the + * entry will become the most recently used. + * @param {string} key The key to retrieve the value for. + * @param {*=} opt_val A default value that will be returned if the key is + * not found, defaults to undefined. + * @return {*} The retrieved value. + */ +goog.structs.LinkedMap.prototype.get = function(key, opt_val) { + var node = this.findAndMoveToTop_(key); + return node ? node.value : opt_val; +}; + + +/** + * Retrieves the value for a given key without updating the entry to be the + * most recently used. + * @param {string} key The key to retrieve the value for. + * @param {*=} opt_val A default value that will be returned if the key is + * not found. + * @return {*} The retrieved value. + */ +goog.structs.LinkedMap.prototype.peekValue = function(key, opt_val) { + var node = this.map_.get(key); + return node ? node.value : opt_val; +}; + + +/** + * Sets a value for a given key. If this is a caching LinkedMap, this entry + * will become the most recently used. + * @param {string} key The key to retrieve the value for. + * @param {*} value A default value that will be returned if the key is + * not found. + */ +goog.structs.LinkedMap.prototype.set = function(key, value) { + var node = this.findAndMoveToTop_(key); + if (node) { + node.value = value; + } else { + node = new goog.structs.LinkedMap.Node_(key, value); + this.map_.set(key, node); + this.insert_(node); + } +}; + + +/** + * Returns the value of the first node without making any modifications. + * @return {*} The value of the first node or undefined if the map is empty. + */ +goog.structs.LinkedMap.prototype.peek = function() { + return this.head_.next.value; +}; + + +/** + * Returns the value of the last node without making any modifications. + * @return {*} The value of the last node or undefined if the map is empty. + */ +goog.structs.LinkedMap.prototype.peekLast = function() { + return this.head_.prev.value; +}; + + +/** + * Removes the first node from the list and returns its value. + * @return {*} The value of the popped node, or undefined if the map was empty. + */ +goog.structs.LinkedMap.prototype.shift = function() { + return this.popNode_(this.head_.next); +}; + + +/** + * Removes the last node from the list and returns its value. + * @return {*} The value of the popped node, or undefined if the map was empty. + */ +goog.structs.LinkedMap.prototype.pop = function() { + return this.popNode_(this.head_.prev); +}; + + +/** + * Removes a value from the LinkedMap based on its key. + * @param {string} key The key to remove. + * @return {boolean} True if the entry was removed, false if the key was not + * found. + */ +goog.structs.LinkedMap.prototype.remove = function(key) { + var node = /** @type {goog.structs.LinkedMap.Node_} */ (this.map_.get(key)); + if (node) { + this.removeNode(node); + return true; + } + return false; +}; + + +/** + * Removes a node from the {@code LinkedMap}. It can be overridden to do + * further cleanup such as disposing of the node value. + * @param {!goog.structs.LinkedMap.Node_} node The node to remove. + * @protected + */ +goog.structs.LinkedMap.prototype.removeNode = function(node) { + node.remove(); + this.map_.remove(node.key); +}; + + +/** + * @return {number} The number of items currently in the LinkedMap. + */ +goog.structs.LinkedMap.prototype.getCount = function() { + return this.map_.getCount(); +}; + + +/** + * @return {boolean} True if the cache is empty, false if it contains any items. + */ +goog.structs.LinkedMap.prototype.isEmpty = function() { + return this.map_.isEmpty(); +}; + + +/** + * Sets the maximum number of entries allowed in this object, truncating any + * excess objects if necessary. + * @param {number} maxCount The new maximum number of entries to allow. + */ +goog.structs.LinkedMap.prototype.setMaxCount = function(maxCount) { + this.maxCount_ = maxCount || null; + if (this.maxCount_ != null) { + this.truncate_(this.maxCount_); + } +}; + + +/** + * @return {!Array.} The list of the keys in the appropriate order for + * this LinkedMap. + */ +goog.structs.LinkedMap.prototype.getKeys = function() { + return this.map(function(val, key) { + return key; + }); +}; + + +/** + * @return {!Array} The list of the values in the appropriate order for + * this LinkedMap. + */ +goog.structs.LinkedMap.prototype.getValues = function() { + return this.map(function(val, key) { + return val; + }); +}; + + +/** + * Tests whether a provided value is currently in the LinkedMap. This does not + * affect item ordering in cache-style LinkedMaps. + * @param {Object} value The value to check for. + * @return {boolean} Whether the value is in the LinkedMap. + */ +goog.structs.LinkedMap.prototype.contains = function(value) { + return this.some(function(el) { + return el == value; + }); +}; + + +/** + * Tests whether a provided key is currently in the LinkedMap. This does not + * affect item ordering in cache-style LinkedMaps. + * @param {string} key The key to check for. + * @return {boolean} Whether the key is in the LinkedMap. + */ +goog.structs.LinkedMap.prototype.containsKey = function(key) { + return this.map_.containsKey(key); +}; + + +/** + * Removes all entries in this object. + */ +goog.structs.LinkedMap.prototype.clear = function() { + this.truncate_(0); +}; + + +/** + * Calls a function on each item in the LinkedMap. + * + * @see goog.structs.forEach + * @param {Function} f The function to call for each item. The function takes + * three arguments: the value, the key, and the LinkedMap. + * @param {Object=} opt_obj The object context to use as "this" for the + * function. + */ +goog.structs.LinkedMap.prototype.forEach = function(f, opt_obj) { + for (var n = this.head_.next; n != this.head_; n = n.next) { + f.call(opt_obj, n.value, n.key, this); + } +}; + + +/** + * Calls a function on each item in the LinkedMap and returns the results of + * those calls in an array. + * + * @see goog.structs.map + * @param {!Function} f The function to call for each item. The function takes + * three arguments: the value, the key, and the LinkedMap. + * @param {Object=} opt_obj The object context to use as "this" for the + * function. + * @return {!Array} The results of the function calls for each item in the + * LinkedMap. + */ +goog.structs.LinkedMap.prototype.map = function(f, opt_obj) { + var rv = []; + for (var n = this.head_.next; n != this.head_; n = n.next) { + rv.push(f.call(opt_obj, n.value, n.key, this)); + } + return rv; +}; + + +/** + * Calls a function on each item in the LinkedMap and returns true if any of + * those function calls returns a true-like value. + * + * @see goog.structs.some + * @param {Function} f The function to call for each item. The function takes + * three arguments: the value, the key, and the LinkedMap, and returns a + * boolean. + * @param {Object=} opt_obj The object context to use as "this" for the + * function. + * @return {boolean} Whether f evaluates to true for at least one item in the + * LinkedMap. + */ +goog.structs.LinkedMap.prototype.some = function(f, opt_obj) { + for (var n = this.head_.next; n != this.head_; n = n.next) { + if (f.call(opt_obj, n.value, n.key, this)) { + return true; + } + } + return false; +}; + + +/** + * Calls a function on each item in the LinkedMap and returns true only if every + * function call returns a true-like value. + * + * @see goog.structs.some + * @param {Function} f The function to call for each item. The function takes + * three arguments: the value, the key, and the Cache, and returns a + * boolean. + * @param {Object=} opt_obj The object context to use as "this" for the + * function. + * @return {boolean} Whether f evaluates to true for every item in the Cache. + */ +goog.structs.LinkedMap.prototype.every = function(f, opt_obj) { + for (var n = this.head_.next; n != this.head_; n = n.next) { + if (!f.call(opt_obj, n.value, n.key, this)) { + return false; + } + } + return true; +}; + + +/** + * Appends a node to the list. LinkedMap in cache mode adds new nodes to + * the head of the list, otherwise they are appended to the tail. If there is a + * maximum size, the list will be truncated if necessary. + * + * @param {goog.structs.LinkedMap.Node_} node The item to insert. + * @private + */ +goog.structs.LinkedMap.prototype.insert_ = function(node) { + if (this.cache_) { + node.next = this.head_.next; + node.prev = this.head_; + + this.head_.next = node; + node.next.prev = node; + } else { + node.prev = this.head_.prev; + node.next = this.head_; + + this.head_.prev = node; + node.prev.next = node; + } + + if (this.maxCount_ != null) { + this.truncate_(this.maxCount_); + } +}; + + +/** + * Removes elements from the LinkedMap if the given count has been exceeded. + * In cache mode removes nodes from the tail of the list. Otherwise removes + * nodes from the head. + * @param {number} count Number of elements to keep. + * @private + */ +goog.structs.LinkedMap.prototype.truncate_ = function(count) { + for (var i = this.map_.getCount(); i > count; i--) { + this.removeNode(this.cache_ ? this.head_.prev : this.head_.next); + } +}; + + +/** + * Removes the node from the LinkedMap if it is not the head, and returns + * the node's value. + * @param {!goog.structs.LinkedMap.Node_} node The item to remove. + * @return {*} The value of the popped node. + * @private + */ +goog.structs.LinkedMap.prototype.popNode_ = function(node) { + if (this.head_ != node) { + this.removeNode(node); + } + return node.value; +}; + + + +/** + * Internal class for a doubly-linked list node containing a key/value pair. + * @param {string} key The key. + * @param {*} value The value. + * @constructor + * @private + */ +goog.structs.LinkedMap.Node_ = function(key, value) { + this.key = key; + this.value = value; +}; + + +/** + * The next node in the list. + * @type {!goog.structs.LinkedMap.Node_} + */ +goog.structs.LinkedMap.Node_.prototype.next; + + +/** + * The previous node in the list. + * @type {!goog.structs.LinkedMap.Node_} + */ +goog.structs.LinkedMap.Node_.prototype.prev; + + +/** + * Causes this node to remove itself from the list. + */ +goog.structs.LinkedMap.Node_.prototype.remove = function() { + this.prev.next = this.next; + this.next.prev = this.prev; + + delete this.prev; + delete this.next; +}; From c7d07124808b461bce71bb74ea209f05f315d4e2 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 23 Jan 2013 17:41:43 +0100 Subject: [PATCH 2/6] Rename goog.structs.LinkedMap to ol.structs.LinkedMap --- src/ol/structs/linkedmap.js | 82 ++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/src/ol/structs/linkedmap.js b/src/ol/structs/linkedmap.js index 1995349a73..3a9810d8a0 100644 --- a/src/ol/structs/linkedmap.js +++ b/src/ol/structs/linkedmap.js @@ -24,7 +24,7 @@ */ -goog.provide('goog.structs.LinkedMap'); +goog.provide('ol.structs.LinkedMap'); goog.require('goog.structs.Map'); @@ -60,7 +60,7 @@ goog.require('goog.structs.Map'); * order. * @constructor */ -goog.structs.LinkedMap = function(opt_maxCount, opt_cache) { +ol.structs.LinkedMap = function(opt_maxCount, opt_cache) { /** * The maximum number of entries to allow, or null if there is no limit. * @type {?number} @@ -76,7 +76,7 @@ goog.structs.LinkedMap = function(opt_maxCount, opt_cache) { this.map_ = new goog.structs.Map(); - this.head_ = new goog.structs.LinkedMap.Node_('', undefined); + this.head_ = new ol.structs.LinkedMap.Node_('', undefined); this.head_.next = this.head_.prev = this.head_; }; @@ -84,11 +84,11 @@ goog.structs.LinkedMap = function(opt_maxCount, opt_cache) { /** * Finds a node and updates it to be the most recently used. * @param {string} key The key of the node. - * @return {goog.structs.LinkedMap.Node_} The node or null if not found. + * @return {ol.structs.LinkedMap.Node_} The node or null if not found. * @private */ -goog.structs.LinkedMap.prototype.findAndMoveToTop_ = function(key) { - var node = /** @type {goog.structs.LinkedMap.Node_} */ (this.map_.get(key)); +ol.structs.LinkedMap.prototype.findAndMoveToTop_ = function(key) { + var node = /** @type {ol.structs.LinkedMap.Node_} */ (this.map_.get(key)); if (node) { if (this.cache_) { node.remove(); @@ -107,7 +107,7 @@ goog.structs.LinkedMap.prototype.findAndMoveToTop_ = function(key) { * not found, defaults to undefined. * @return {*} The retrieved value. */ -goog.structs.LinkedMap.prototype.get = function(key, opt_val) { +ol.structs.LinkedMap.prototype.get = function(key, opt_val) { var node = this.findAndMoveToTop_(key); return node ? node.value : opt_val; }; @@ -121,7 +121,7 @@ goog.structs.LinkedMap.prototype.get = function(key, opt_val) { * not found. * @return {*} The retrieved value. */ -goog.structs.LinkedMap.prototype.peekValue = function(key, opt_val) { +ol.structs.LinkedMap.prototype.peekValue = function(key, opt_val) { var node = this.map_.get(key); return node ? node.value : opt_val; }; @@ -134,12 +134,12 @@ goog.structs.LinkedMap.prototype.peekValue = function(key, opt_val) { * @param {*} value A default value that will be returned if the key is * not found. */ -goog.structs.LinkedMap.prototype.set = function(key, value) { +ol.structs.LinkedMap.prototype.set = function(key, value) { var node = this.findAndMoveToTop_(key); if (node) { node.value = value; } else { - node = new goog.structs.LinkedMap.Node_(key, value); + node = new ol.structs.LinkedMap.Node_(key, value); this.map_.set(key, node); this.insert_(node); } @@ -150,7 +150,7 @@ goog.structs.LinkedMap.prototype.set = function(key, value) { * Returns the value of the first node without making any modifications. * @return {*} The value of the first node or undefined if the map is empty. */ -goog.structs.LinkedMap.prototype.peek = function() { +ol.structs.LinkedMap.prototype.peek = function() { return this.head_.next.value; }; @@ -159,7 +159,7 @@ goog.structs.LinkedMap.prototype.peek = function() { * Returns the value of the last node without making any modifications. * @return {*} The value of the last node or undefined if the map is empty. */ -goog.structs.LinkedMap.prototype.peekLast = function() { +ol.structs.LinkedMap.prototype.peekLast = function() { return this.head_.prev.value; }; @@ -168,7 +168,7 @@ goog.structs.LinkedMap.prototype.peekLast = function() { * Removes the first node from the list and returns its value. * @return {*} The value of the popped node, or undefined if the map was empty. */ -goog.structs.LinkedMap.prototype.shift = function() { +ol.structs.LinkedMap.prototype.shift = function() { return this.popNode_(this.head_.next); }; @@ -177,7 +177,7 @@ goog.structs.LinkedMap.prototype.shift = function() { * Removes the last node from the list and returns its value. * @return {*} The value of the popped node, or undefined if the map was empty. */ -goog.structs.LinkedMap.prototype.pop = function() { +ol.structs.LinkedMap.prototype.pop = function() { return this.popNode_(this.head_.prev); }; @@ -188,8 +188,8 @@ goog.structs.LinkedMap.prototype.pop = function() { * @return {boolean} True if the entry was removed, false if the key was not * found. */ -goog.structs.LinkedMap.prototype.remove = function(key) { - var node = /** @type {goog.structs.LinkedMap.Node_} */ (this.map_.get(key)); +ol.structs.LinkedMap.prototype.remove = function(key) { + var node = /** @type {ol.structs.LinkedMap.Node_} */ (this.map_.get(key)); if (node) { this.removeNode(node); return true; @@ -201,10 +201,10 @@ goog.structs.LinkedMap.prototype.remove = function(key) { /** * Removes a node from the {@code LinkedMap}. It can be overridden to do * further cleanup such as disposing of the node value. - * @param {!goog.structs.LinkedMap.Node_} node The node to remove. + * @param {!ol.structs.LinkedMap.Node_} node The node to remove. * @protected */ -goog.structs.LinkedMap.prototype.removeNode = function(node) { +ol.structs.LinkedMap.prototype.removeNode = function(node) { node.remove(); this.map_.remove(node.key); }; @@ -213,7 +213,7 @@ goog.structs.LinkedMap.prototype.removeNode = function(node) { /** * @return {number} The number of items currently in the LinkedMap. */ -goog.structs.LinkedMap.prototype.getCount = function() { +ol.structs.LinkedMap.prototype.getCount = function() { return this.map_.getCount(); }; @@ -221,7 +221,7 @@ goog.structs.LinkedMap.prototype.getCount = function() { /** * @return {boolean} True if the cache is empty, false if it contains any items. */ -goog.structs.LinkedMap.prototype.isEmpty = function() { +ol.structs.LinkedMap.prototype.isEmpty = function() { return this.map_.isEmpty(); }; @@ -231,7 +231,7 @@ goog.structs.LinkedMap.prototype.isEmpty = function() { * excess objects if necessary. * @param {number} maxCount The new maximum number of entries to allow. */ -goog.structs.LinkedMap.prototype.setMaxCount = function(maxCount) { +ol.structs.LinkedMap.prototype.setMaxCount = function(maxCount) { this.maxCount_ = maxCount || null; if (this.maxCount_ != null) { this.truncate_(this.maxCount_); @@ -243,7 +243,7 @@ goog.structs.LinkedMap.prototype.setMaxCount = function(maxCount) { * @return {!Array.} The list of the keys in the appropriate order for * this LinkedMap. */ -goog.structs.LinkedMap.prototype.getKeys = function() { +ol.structs.LinkedMap.prototype.getKeys = function() { return this.map(function(val, key) { return key; }); @@ -254,7 +254,7 @@ goog.structs.LinkedMap.prototype.getKeys = function() { * @return {!Array} The list of the values in the appropriate order for * this LinkedMap. */ -goog.structs.LinkedMap.prototype.getValues = function() { +ol.structs.LinkedMap.prototype.getValues = function() { return this.map(function(val, key) { return val; }); @@ -267,7 +267,7 @@ goog.structs.LinkedMap.prototype.getValues = function() { * @param {Object} value The value to check for. * @return {boolean} Whether the value is in the LinkedMap. */ -goog.structs.LinkedMap.prototype.contains = function(value) { +ol.structs.LinkedMap.prototype.contains = function(value) { return this.some(function(el) { return el == value; }); @@ -280,7 +280,7 @@ goog.structs.LinkedMap.prototype.contains = function(value) { * @param {string} key The key to check for. * @return {boolean} Whether the key is in the LinkedMap. */ -goog.structs.LinkedMap.prototype.containsKey = function(key) { +ol.structs.LinkedMap.prototype.containsKey = function(key) { return this.map_.containsKey(key); }; @@ -288,7 +288,7 @@ goog.structs.LinkedMap.prototype.containsKey = function(key) { /** * Removes all entries in this object. */ -goog.structs.LinkedMap.prototype.clear = function() { +ol.structs.LinkedMap.prototype.clear = function() { this.truncate_(0); }; @@ -302,7 +302,7 @@ goog.structs.LinkedMap.prototype.clear = function() { * @param {Object=} opt_obj The object context to use as "this" for the * function. */ -goog.structs.LinkedMap.prototype.forEach = function(f, opt_obj) { +ol.structs.LinkedMap.prototype.forEach = function(f, opt_obj) { for (var n = this.head_.next; n != this.head_; n = n.next) { f.call(opt_obj, n.value, n.key, this); } @@ -321,7 +321,7 @@ goog.structs.LinkedMap.prototype.forEach = function(f, opt_obj) { * @return {!Array} The results of the function calls for each item in the * LinkedMap. */ -goog.structs.LinkedMap.prototype.map = function(f, opt_obj) { +ol.structs.LinkedMap.prototype.map = function(f, opt_obj) { var rv = []; for (var n = this.head_.next; n != this.head_; n = n.next) { rv.push(f.call(opt_obj, n.value, n.key, this)); @@ -343,7 +343,7 @@ goog.structs.LinkedMap.prototype.map = function(f, opt_obj) { * @return {boolean} Whether f evaluates to true for at least one item in the * LinkedMap. */ -goog.structs.LinkedMap.prototype.some = function(f, opt_obj) { +ol.structs.LinkedMap.prototype.some = function(f, opt_obj) { for (var n = this.head_.next; n != this.head_; n = n.next) { if (f.call(opt_obj, n.value, n.key, this)) { return true; @@ -365,7 +365,7 @@ goog.structs.LinkedMap.prototype.some = function(f, opt_obj) { * function. * @return {boolean} Whether f evaluates to true for every item in the Cache. */ -goog.structs.LinkedMap.prototype.every = function(f, opt_obj) { +ol.structs.LinkedMap.prototype.every = function(f, opt_obj) { for (var n = this.head_.next; n != this.head_; n = n.next) { if (!f.call(opt_obj, n.value, n.key, this)) { return false; @@ -380,10 +380,10 @@ goog.structs.LinkedMap.prototype.every = function(f, opt_obj) { * the head of the list, otherwise they are appended to the tail. If there is a * maximum size, the list will be truncated if necessary. * - * @param {goog.structs.LinkedMap.Node_} node The item to insert. + * @param {ol.structs.LinkedMap.Node_} node The item to insert. * @private */ -goog.structs.LinkedMap.prototype.insert_ = function(node) { +ol.structs.LinkedMap.prototype.insert_ = function(node) { if (this.cache_) { node.next = this.head_.next; node.prev = this.head_; @@ -411,7 +411,7 @@ goog.structs.LinkedMap.prototype.insert_ = function(node) { * @param {number} count Number of elements to keep. * @private */ -goog.structs.LinkedMap.prototype.truncate_ = function(count) { +ol.structs.LinkedMap.prototype.truncate_ = function(count) { for (var i = this.map_.getCount(); i > count; i--) { this.removeNode(this.cache_ ? this.head_.prev : this.head_.next); } @@ -421,11 +421,11 @@ goog.structs.LinkedMap.prototype.truncate_ = function(count) { /** * Removes the node from the LinkedMap if it is not the head, and returns * the node's value. - * @param {!goog.structs.LinkedMap.Node_} node The item to remove. + * @param {!ol.structs.LinkedMap.Node_} node The item to remove. * @return {*} The value of the popped node. * @private */ -goog.structs.LinkedMap.prototype.popNode_ = function(node) { +ol.structs.LinkedMap.prototype.popNode_ = function(node) { if (this.head_ != node) { this.removeNode(node); } @@ -441,7 +441,7 @@ goog.structs.LinkedMap.prototype.popNode_ = function(node) { * @constructor * @private */ -goog.structs.LinkedMap.Node_ = function(key, value) { +ol.structs.LinkedMap.Node_ = function(key, value) { this.key = key; this.value = value; }; @@ -449,22 +449,22 @@ goog.structs.LinkedMap.Node_ = function(key, value) { /** * The next node in the list. - * @type {!goog.structs.LinkedMap.Node_} + * @type {!ol.structs.LinkedMap.Node_} */ -goog.structs.LinkedMap.Node_.prototype.next; +ol.structs.LinkedMap.Node_.prototype.next; /** * The previous node in the list. - * @type {!goog.structs.LinkedMap.Node_} + * @type {!ol.structs.LinkedMap.Node_} */ -goog.structs.LinkedMap.Node_.prototype.prev; +ol.structs.LinkedMap.Node_.prototype.prev; /** * Causes this node to remove itself from the list. */ -goog.structs.LinkedMap.Node_.prototype.remove = function() { +ol.structs.LinkedMap.Node_.prototype.remove = function() { this.prev.next = this.next; this.next.prev = this.prev; From 3f6ef77a5a72157bb9d5cde5deb2431e7cb5589c Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 23 Jan 2013 17:43:41 +0100 Subject: [PATCH 3/6] Add ol.structs.LinkedMap.prototype.peekLastKey --- src/ol/structs/linkedmap.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/ol/structs/linkedmap.js b/src/ol/structs/linkedmap.js index 3a9810d8a0..63971748c5 100644 --- a/src/ol/structs/linkedmap.js +++ b/src/ol/structs/linkedmap.js @@ -12,6 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +// FIXME replace this with goog.structs.LinkedMap when goog.structs.LinkedMap +// adds peekLastKey() + /** * @fileoverview A LinkedMap data structure that is accessed using key/value * pairs like an ordinary Map, but which guarantees a consistent iteration @@ -164,6 +167,16 @@ ol.structs.LinkedMap.prototype.peekLast = function() { }; +/** + * Returns the key of the last node without making any modifications. + * @return {string|undefined} The key of the last node or undefined if the map + * is empty. + */ +ol.structs.LinkedMap.prototype.peekLastKey = function() { + return this.head_.prev.key; +}; + + /** * Removes the first node from the list and returns its value. * @return {*} The value of the popped node, or undefined if the map was empty. From 0d7196c09848589578d9e044bd6ffe8bc6d05b48 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 23 Jan 2013 19:46:03 +0100 Subject: [PATCH 4/6] Expire old textures from texture cache --- src/ol/renderer/webgl/webglmaprenderer.js | 72 +++++++++++++++++++---- 1 file changed, 60 insertions(+), 12 deletions(-) diff --git a/src/ol/renderer/webgl/webglmaprenderer.js b/src/ol/renderer/webgl/webglmaprenderer.js index dfd7d823b1..926a581c0b 100644 --- a/src/ol/renderer/webgl/webglmaprenderer.js +++ b/src/ol/renderer/webgl/webglmaprenderer.js @@ -1,5 +1,3 @@ -// FIXME clear textureCache -// FIXME generational tile texture garbage collector newFrame/get // FIXME check against gl.getParameter(webgl.MAX_TEXTURE_SIZE) goog.provide('ol.renderer.webgl.Map'); @@ -22,10 +20,17 @@ goog.require('ol.renderer.Map'); goog.require('ol.renderer.webgl.FragmentShader'); goog.require('ol.renderer.webgl.TileLayer'); goog.require('ol.renderer.webgl.VertexShader'); +goog.require('ol.structs.LinkedMap'); goog.require('ol.webgl'); goog.require('ol.webgl.WebGLContextEventType'); +/** + * @define {number} Texture cache high water mark. + */ +ol.WEBGL_TEXTURE_CACHE_HIGH_WATER_MARK = 1024; + + /** * @typedef {{magFilter: number, minFilter: number, texture: WebGLTexture}} */ @@ -179,9 +184,15 @@ ol.renderer.webgl.Map = function(container, map) { /** * @private - * @type {Object.} + * @type {ol.structs.LinkedMap} */ - this.textureCache_ = {}; + this.textureCache_ = new ol.structs.LinkedMap(undefined, true); + + /** + * @private + * @type {number} + */ + this.textureCacheFrameMarkerCount_ = 0; /** * @private @@ -227,8 +238,8 @@ ol.renderer.webgl.Map.prototype.bindTileTexture = function(tile, magFilter, minFilter) { var gl = this.getGL(); var tileKey = tile.getKey(); - var textureCacheEntry = this.textureCache_[tileKey]; - if (goog.isDef(textureCacheEntry)) { + if (this.textureCache_.containsKey(tileKey)) { + var textureCacheEntry = this.textureCache_.get(tileKey); gl.bindTexture(goog.webgl.TEXTURE_2D, textureCacheEntry.texture); if (textureCacheEntry.magFilter != magFilter) { gl.texParameteri( @@ -253,11 +264,11 @@ ol.renderer.webgl.Map.prototype.bindTileTexture = goog.webgl.CLAMP_TO_EDGE); gl.texParameteri(goog.webgl.TEXTURE_2D, goog.webgl.TEXTURE_WRAP_T, goog.webgl.CLAMP_TO_EDGE); - this.textureCache_[tileKey] = { + this.textureCache_.set(tileKey, { texture: texture, magFilter: magFilter, minFilter: minFilter - }; + }); } }; @@ -287,14 +298,42 @@ ol.renderer.webgl.Map.prototype.disposeInternal = function() { goog.object.forEach(this.shaderCache_, function(shader) { gl.deleteShader(shader); }); - goog.object.forEach(this.textureCache_, function(textureCacheEntry) { - gl.deleteTexture(textureCacheEntry.texture); + this.textureCache_.forEach(function(textureCacheEntry) { + if (!goog.isNull(textureCacheEntry)) { + gl.deleteTexture(textureCacheEntry.texture); + } }); } goog.base(this, 'disposeInternal'); }; +/** + * @param {ol.Map} map Map. + * @param {ol.FrameState} frameState Frame state. + * @private + */ +ol.renderer.webgl.Map.prototype.expireCache_ = function(map, frameState) { + var gl = this.getGL(); + var key, textureCacheEntry; + while (this.textureCache_.getCount() - this.textureCacheFrameMarkerCount_ > + ol.WEBGL_TEXTURE_CACHE_HIGH_WATER_MARK) { + textureCacheEntry = /** @type {?ol.renderer.webgl.TextureCacheEntry} */ + (this.textureCache_.peekLast()); + if (goog.isNull(textureCacheEntry)) { + if (Number(this.textureCache_.peekLastKey()) == frameState.time) { + break; + } else { + --this.textureCacheFrameMarkerCount_; + } + } else { + gl.deleteTexture(textureCacheEntry.texture); + } + this.textureCache_.pop(); + } +}; + + /** * @inheritDoc */ @@ -400,7 +439,8 @@ ol.renderer.webgl.Map.prototype.handleWebGLContextLost = function(event) { this.arrayBuffer_ = null; this.shaderCache_ = {}; this.programCache_ = {}; - this.textureCache_ = {}; + this.textureCache_.clear(); + this.textureCacheFrameMarkerCount_ = 0; goog.object.forEach(this.layerRenderers, function(layerRenderer) { layerRenderer.handleWebGLContextLost(); }); @@ -437,7 +477,7 @@ ol.renderer.webgl.Map.prototype.initializeGL_ = function() { * @return {boolean} Is tile texture loaded. */ ol.renderer.webgl.Map.prototype.isTileTextureLoaded = function(tile) { - return tile.getKey() in this.textureCache_; + return this.textureCache_.containsKey(tile.getKey()); }; @@ -481,6 +521,9 @@ ol.renderer.webgl.Map.prototype.renderFrame = function(frameState) { return false; } + this.textureCache_.set(frameState.time.toString(), null); + ++this.textureCacheFrameMarkerCount_; + goog.array.forEach(frameState.layersArray, function(layer) { var layerState = frameState.layerStates[goog.getUid(layer)]; if (!layerState.visible || !layerState.ready) { @@ -563,6 +606,11 @@ ol.renderer.webgl.Map.prototype.renderFrame = function(frameState) { this.calculateMatrices2D(frameState); + if (this.textureCache_.getCount() - this.textureCacheFrameMarkerCount_ > + ol.WEBGL_TEXTURE_CACHE_HIGH_WATER_MARK) { + frameState.postRenderFunctions.push(goog.bind(this.expireCache_, this)); + } + }; From b0eb7a4b9bba31eaa514ecc4de6d9483038dc370 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 23 Jan 2013 19:49:22 +0100 Subject: [PATCH 5/6] Port ol.TileCache to ol.structs.LinkedMap --- src/ol/tilecache.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ol/tilecache.js b/src/ol/tilecache.js index e37dd93d00..9645fb9513 100644 --- a/src/ol/tilecache.js +++ b/src/ol/tilecache.js @@ -1,9 +1,9 @@ goog.provide('ol.TileCache'); goog.require('goog.dispose'); -goog.require('goog.structs.LinkedMap'); goog.require('ol.Tile'); goog.require('ol.TileRange'); +goog.require('ol.structs.LinkedMap'); /** @@ -15,7 +15,7 @@ ol.DEFAULT_TILE_CACHE_HIGH_WATER_MARK = 512; /** * @constructor - * @extends {goog.structs.LinkedMap} + * @extends {ol.structs.LinkedMap} * @param {number=} opt_highWaterMark High water mark. */ ol.TileCache = function(opt_highWaterMark) { @@ -30,7 +30,7 @@ ol.TileCache = function(opt_highWaterMark) { opt_highWaterMark : ol.DEFAULT_TILE_CACHE_HIGH_WATER_MARK; }; -goog.inherits(ol.TileCache, goog.structs.LinkedMap); +goog.inherits(ol.TileCache, ol.structs.LinkedMap); /** From 8cac270234a3d2b014fdd701593beb9c72f61595 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Thu, 24 Jan 2013 16:31:56 +0100 Subject: [PATCH 6/6] Use + rather than Number for string to number conversion --- src/ol/renderer/webgl/webglmaprenderer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ol/renderer/webgl/webglmaprenderer.js b/src/ol/renderer/webgl/webglmaprenderer.js index 926a581c0b..a1eefb7326 100644 --- a/src/ol/renderer/webgl/webglmaprenderer.js +++ b/src/ol/renderer/webgl/webglmaprenderer.js @@ -321,7 +321,7 @@ ol.renderer.webgl.Map.prototype.expireCache_ = function(map, frameState) { textureCacheEntry = /** @type {?ol.renderer.webgl.TextureCacheEntry} */ (this.textureCache_.peekLast()); if (goog.isNull(textureCacheEntry)) { - if (Number(this.textureCache_.peekLastKey()) == frameState.time) { + if (+this.textureCache_.peekLastKey() == frameState.time) { break; } else { --this.textureCacheFrameMarkerCount_;