123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- goog.provide('goog.structs.CircularBuffer');
- goog.structs.CircularBuffer = function(opt_maxSize) {
-
- this.nextPtr_ = 0;
-
- this.maxSize_ = opt_maxSize || 100;
-
- this.buff_ = [];
- };
- goog.structs.CircularBuffer.prototype.add = function(item) {
- var previousItem = this.buff_[this.nextPtr_];
- this.buff_[this.nextPtr_] = item;
- this.nextPtr_ = (this.nextPtr_ + 1) % this.maxSize_;
- return previousItem;
- };
- goog.structs.CircularBuffer.prototype.get = function(index) {
- index = this.normalizeIndex_(index);
- return this.buff_[index];
- };
- goog.structs.CircularBuffer.prototype.set = function(index, item) {
- index = this.normalizeIndex_(index);
- this.buff_[index] = item;
- };
- goog.structs.CircularBuffer.prototype.getCount = function() {
- return this.buff_.length;
- };
- goog.structs.CircularBuffer.prototype.isEmpty = function() {
- return this.buff_.length == 0;
- };
- goog.structs.CircularBuffer.prototype.clear = function() {
- this.buff_.length = 0;
- this.nextPtr_ = 0;
- };
- goog.structs.CircularBuffer.prototype.getValues = function() {
-
-
- return this.getNewestValues(this.getCount());
- };
- goog.structs.CircularBuffer.prototype.getNewestValues = function(maxCount) {
- var l = this.getCount();
- var start = this.getCount() - maxCount;
- var rv = [];
- for (var i = start; i < l; i++) {
- rv.push(this.get(i));
- }
- return rv;
- };
- goog.structs.CircularBuffer.prototype.getKeys = function() {
- var rv = [];
- var l = this.getCount();
- for (var i = 0; i < l; i++) {
- rv[i] = i;
- }
- return rv;
- };
- goog.structs.CircularBuffer.prototype.containsKey = function(key) {
- return key < this.getCount();
- };
- goog.structs.CircularBuffer.prototype.containsValue = function(value) {
- var l = this.getCount();
- for (var i = 0; i < l; i++) {
- if (this.get(i) == value) {
- return true;
- }
- }
- return false;
- };
- goog.structs.CircularBuffer.prototype.getLast = function() {
- if (this.getCount() == 0) {
- return null;
- }
- return this.get(this.getCount() - 1);
- };
- goog.structs.CircularBuffer.prototype.normalizeIndex_ = function(index) {
- if (index >= this.buff_.length) {
- throw Error('Out of bounds exception');
- }
- if (this.buff_.length < this.maxSize_) {
- return index;
- }
- return (this.nextPtr_ + Number(index)) % this.maxSize_;
- };
|