123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- goog.provide('goog.structs.Queue');
- goog.require('goog.array');
- goog.structs.Queue = function() {
-
- this.front_ = [];
-
- this.back_ = [];
- };
- goog.structs.Queue.prototype.maybeFlip_ = function() {
- if (goog.array.isEmpty(this.front_)) {
- this.front_ = this.back_;
- this.front_.reverse();
- this.back_ = [];
- }
- };
- goog.structs.Queue.prototype.enqueue = function(element) {
- this.back_.push(element);
- };
- goog.structs.Queue.prototype.dequeue = function() {
- this.maybeFlip_();
- return this.front_.pop();
- };
- goog.structs.Queue.prototype.peek = function() {
- this.maybeFlip_();
- return goog.array.peek(this.front_);
- };
- goog.structs.Queue.prototype.getCount = function() {
- return this.front_.length + this.back_.length;
- };
- goog.structs.Queue.prototype.isEmpty = function() {
- return goog.array.isEmpty(this.front_) && goog.array.isEmpty(this.back_);
- };
- goog.structs.Queue.prototype.clear = function() {
- this.front_ = [];
- this.back_ = [];
- };
- goog.structs.Queue.prototype.contains = function(obj) {
- return goog.array.contains(this.front_, obj) ||
- goog.array.contains(this.back_, obj);
- };
- goog.structs.Queue.prototype.remove = function(obj) {
- return goog.array.removeLast(this.front_, obj) ||
- goog.array.remove(this.back_, obj);
- };
- goog.structs.Queue.prototype.getValues = function() {
- var res = [];
-
- for (var i = this.front_.length - 1; i >= 0; --i) {
- res.push(this.front_[i]);
- }
- var len = this.back_.length;
- for (var i = 0; i < len; ++i) {
- res.push(this.back_[i]);
- }
- return res;
- };
|