123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512 |
- goog.provide('goog.structs.LinkedMap');
- goog.require('goog.structs.Map');
- goog.structs.LinkedMap = function(
- opt_maxCount, opt_cache, opt_evictionCallback) {
-
- this.maxCount_ = opt_maxCount || null;
-
- this.cache_ = !!opt_cache;
-
- this.evictionCallback_ = opt_evictionCallback;
-
- this.map_ = new goog.structs.Map();
- this.head_ = new goog.structs.LinkedMap.Node_('', undefined);
- this.head_.next = this.head_.prev = this.head_;
- };
- goog.structs.LinkedMap.prototype.findAndMoveToTop_ = function(key) {
- var node = this.map_.get(key);
- if (node) {
- if (this.cache_) {
- node.remove();
- this.insert_(node);
- }
- }
- return node;
- };
- goog.structs.LinkedMap.prototype.get = function(key, opt_val) {
- var node = this.findAndMoveToTop_(key);
- return node ? node.value : opt_val;
- };
- goog.structs.LinkedMap.prototype.peekValue = function(key, opt_val) {
- var node = this.map_.get(key);
- return node ? node.value : opt_val;
- };
- 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);
- }
- };
- goog.structs.LinkedMap.prototype.peek = function() {
- return this.head_.next.value;
- };
- goog.structs.LinkedMap.prototype.peekLast = function() {
- return this.head_.prev.value;
- };
- goog.structs.LinkedMap.prototype.shift = function() {
- return this.popNode_(this.head_.next);
- };
- goog.structs.LinkedMap.prototype.pop = function() {
- return this.popNode_(this.head_.prev);
- };
- goog.structs.LinkedMap.prototype.remove = function(key) {
- var node = this.map_.get(key);
- if (node) {
- this.removeNode(node);
- return true;
- }
- return false;
- };
- goog.structs.LinkedMap.prototype.removeNode = function(node) {
- node.remove();
- this.map_.remove(node.key);
- };
- goog.structs.LinkedMap.prototype.getCount = function() {
- return this.map_.getCount();
- };
- goog.structs.LinkedMap.prototype.isEmpty = function() {
- return this.map_.isEmpty();
- };
- goog.structs.LinkedMap.prototype.setEvictionCallback = function(
- evictionCallback) {
- this.evictionCallback_ = evictionCallback;
- };
- goog.structs.LinkedMap.prototype.setMaxCount = function(maxCount) {
- this.maxCount_ = maxCount || null;
- if (this.maxCount_ != null) {
- this.truncate_(this.maxCount_);
- }
- };
- goog.structs.LinkedMap.prototype.getKeys = function() {
- return this.map(function(val, key) { return key; });
- };
- goog.structs.LinkedMap.prototype.getValues = function() {
- return this.map(function(val, key) { return val; });
- };
- goog.structs.LinkedMap.prototype.contains = function(value) {
- return this.some(function(el) { return el == value; });
- };
- goog.structs.LinkedMap.prototype.containsKey = function(key) {
- return this.map_.containsKey(key);
- };
- goog.structs.LinkedMap.prototype.clear = function() {
- this.truncate_(0);
- };
- 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);
- }
- };
- 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;
- };
- 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;
- };
- 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;
- };
- 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_);
- }
- };
- goog.structs.LinkedMap.prototype.truncate_ = function(count) {
- while (this.getCount() > count) {
- var toRemove = this.cache_ ? this.head_.prev : this.head_.next;
- this.removeNode(toRemove);
- if (this.evictionCallback_) {
- this.evictionCallback_(toRemove.key, toRemove.value);
- }
- }
- };
- goog.structs.LinkedMap.prototype.popNode_ = function(node) {
- if (this.head_ != node) {
- this.removeNode(node);
- }
- return node.value;
- };
- goog.structs.LinkedMap.Node_ = function(key, value) {
-
- this.key = key;
-
- this.value = value;
- };
- goog.structs.LinkedMap.Node_.prototype.next;
- goog.structs.LinkedMap.Node_.prototype.prev;
- goog.structs.LinkedMap.Node_.prototype.remove = function() {
- this.prev.next = this.next;
- this.next.prev = this.prev;
- delete this.prev;
- delete this.next;
- };
|