Add ol.structs.LRUCache
This commit is contained in:
271
src/ol/structs/lrucache.js
Normal file
271
src/ol/structs/lrucache.js
Normal file
@@ -0,0 +1,271 @@
|
||||
goog.provide('ol.structs.LRUCache');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('goog.object');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* Implements a Least-Recently-Used cache where the keys do not conflict with
|
||||
* Object's properties (e.g. 'hasOwnProperty' is not allowed as a key). Expiring
|
||||
* items from the cache is the responsibility of the user.
|
||||
*/
|
||||
ol.structs.LRUCache = function() {
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {number}
|
||||
*/
|
||||
this.count_ = 0;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {Object.<string, ol.structs.LRUCacheEntry_>}
|
||||
*/
|
||||
this.entries_ = {};
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {?ol.structs.LRUCacheEntry_}
|
||||
*/
|
||||
this.oldest_ = null;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {?ol.structs.LRUCacheEntry_}
|
||||
*/
|
||||
this.newest_ = null;
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* FIXME empty description for jsdoc
|
||||
*/
|
||||
ol.structs.LRUCache.prototype.assertValid = function() {
|
||||
if (this.count_ === 0) {
|
||||
goog.asserts.assert(goog.object.isEmpty(this.entries_));
|
||||
goog.asserts.assert(goog.isNull(this.oldest_));
|
||||
goog.asserts.assert(goog.isNull(this.newest_));
|
||||
} else {
|
||||
goog.asserts.assert(goog.object.getCount(this.entries_) == this.count_);
|
||||
goog.asserts.assert(!goog.isNull(this.oldest_));
|
||||
goog.asserts.assert(goog.isNull(this.oldest_.older));
|
||||
goog.asserts.assert(!goog.isNull(this.newest_));
|
||||
goog.asserts.assert(goog.isNull(this.newest_.newer));
|
||||
var i, entry;
|
||||
var older = null;
|
||||
i = 0;
|
||||
for (entry = this.oldest_; !goog.isNull(entry); entry = entry.newer) {
|
||||
goog.asserts.assert(entry.older === older);
|
||||
older = entry;
|
||||
++i;
|
||||
}
|
||||
goog.asserts.assert(i == this.count_);
|
||||
var newer = null;
|
||||
i = 0;
|
||||
for (entry = this.newest_; !goog.isNull(entry); entry = entry.older) {
|
||||
goog.asserts.assert(entry.newer === newer);
|
||||
newer = entry;
|
||||
++i;
|
||||
}
|
||||
goog.asserts.assert(i == this.count_);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* FIXME empty description for jsdoc
|
||||
*/
|
||||
ol.structs.LRUCache.prototype.clear = function() {
|
||||
this.count_ = 0;
|
||||
this.entries_ = {};
|
||||
this.oldest_ = null;
|
||||
this.newest_ = null;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {string} key Key.
|
||||
* @return {boolean} Contains key.
|
||||
*/
|
||||
ol.structs.LRUCache.prototype.containsKey = function(key) {
|
||||
return key in this.entries_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {Function} f Function.
|
||||
* @param {Object=} opt_obj Object.
|
||||
*/
|
||||
ol.structs.LRUCache.prototype.forEach = function(f, opt_obj) {
|
||||
var entry = this.oldest_;
|
||||
while (!goog.isNull(entry)) {
|
||||
f.call(opt_obj, entry.value, entry.key, this);
|
||||
entry = entry.newer;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {string} key Key.
|
||||
* @return {*} Value.
|
||||
*/
|
||||
ol.structs.LRUCache.prototype.get = function(key) {
|
||||
var entry = this.entries_[key];
|
||||
goog.asserts.assert(goog.isDef(entry));
|
||||
if (entry === this.newest_) {
|
||||
return entry.value;
|
||||
} else if (entry === this.oldest_) {
|
||||
this.oldest_ = this.oldest_.newer;
|
||||
this.oldest_.older = null;
|
||||
} else {
|
||||
entry.newer.older = entry.older;
|
||||
entry.older.newer = entry.newer;
|
||||
}
|
||||
entry.newer = null;
|
||||
entry.older = this.newest_;
|
||||
this.newest_.newer = entry;
|
||||
this.newest_ = entry;
|
||||
return entry.value;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {number} Count.
|
||||
*/
|
||||
ol.structs.LRUCache.prototype.getCount = function() {
|
||||
return this.count_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {Array.<string>} Keys.
|
||||
*/
|
||||
ol.structs.LRUCache.prototype.getKeys = function() {
|
||||
var keys = new Array(this.count_);
|
||||
var i = 0;
|
||||
var entry;
|
||||
for (entry = this.newest_; !goog.isNull(entry); entry = entry.older) {
|
||||
keys[i++] = entry.key;
|
||||
}
|
||||
goog.asserts.assert(i == this.count_);
|
||||
return keys;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {string} Last key.
|
||||
*/
|
||||
ol.structs.LRUCache.prototype.getLastKey = function() {
|
||||
goog.asserts.assert(!goog.isNull(this.newest_));
|
||||
return this.newest_.key;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {Array} Values.
|
||||
*/
|
||||
ol.structs.LRUCache.prototype.getValues = function() {
|
||||
var values = new Array(this.count_);
|
||||
var i = 0;
|
||||
var entry;
|
||||
for (entry = this.newest_; !goog.isNull(entry); entry = entry.older) {
|
||||
values[i++] = entry.value;
|
||||
}
|
||||
goog.asserts.assert(i == this.count_);
|
||||
return values;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {*} Last value.
|
||||
*/
|
||||
ol.structs.LRUCache.prototype.peekLast = function() {
|
||||
goog.asserts.assert(!goog.isNull(this.oldest_));
|
||||
return this.oldest_.value;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {string} Last key.
|
||||
*/
|
||||
ol.structs.LRUCache.prototype.peekLastKey = function() {
|
||||
goog.asserts.assert(!goog.isNull(this.oldest_));
|
||||
return this.oldest_.key;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {*} value Value.
|
||||
*/
|
||||
ol.structs.LRUCache.prototype.pop = function() {
|
||||
goog.asserts.assert(!goog.isNull(this.oldest_));
|
||||
goog.asserts.assert(!goog.isNull(this.newest_));
|
||||
var entry = this.oldest_;
|
||||
goog.asserts.assert(entry.key in this.entries_);
|
||||
delete this.entries_[entry.key];
|
||||
if (!goog.isNull(entry.newer)) {
|
||||
entry.newer.older = null;
|
||||
}
|
||||
this.oldest_ = entry.newer;
|
||||
if (goog.isNull(this.oldest_)) {
|
||||
this.newest_ = null;
|
||||
}
|
||||
--this.count_;
|
||||
return entry.value;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {string} key Key.
|
||||
* @param {*} value Value.
|
||||
*/
|
||||
ol.structs.LRUCache.prototype.set = function(key, value) {
|
||||
goog.asserts.assert(!(key in {}));
|
||||
goog.asserts.assert(!(key in this.entries_));
|
||||
var entry = new ol.structs.LRUCacheEntry_(key, value, null, this.newest_);
|
||||
if (goog.isNull(this.newest_)) {
|
||||
this.oldest_ = entry;
|
||||
} else {
|
||||
this.newest_.newer = entry;
|
||||
}
|
||||
this.newest_ = entry;
|
||||
this.entries_[key] = entry;
|
||||
++this.count_;
|
||||
};
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @private
|
||||
* @param {string} key Key.
|
||||
* @param {*} value Value.
|
||||
* @param {ol.structs.LRUCacheEntry_} newer Newer.
|
||||
* @param {ol.structs.LRUCacheEntry_} older Older.
|
||||
*/
|
||||
ol.structs.LRUCacheEntry_ = function(key, value, newer, older) {
|
||||
|
||||
/**
|
||||
* @type {string}
|
||||
*/
|
||||
this.key = key;
|
||||
|
||||
/**
|
||||
* @type {*}
|
||||
*/
|
||||
this.value = value;
|
||||
|
||||
/**
|
||||
* @type {ol.structs.LRUCacheEntry_}
|
||||
*/
|
||||
this.newer = newer;
|
||||
|
||||
/**
|
||||
* @type {ol.structs.LRUCacheEntry_}
|
||||
*/
|
||||
this.older = older;
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user