差分
ナビゲーションに移動
検索に移動
ページの作成:「容量上限到達でエンキューが無視されるキュー。いわゆるパチンコの「保留」みたいなもの。 <source lang="javascript"> function Lim...」
容量上限到達でエンキューが無視される[[キュー]]。いわゆるパチンコの「保留」みたいなもの。
<source lang="javascript">
function LimitedQueue(){
var queue = [];
var offset = 0;
var limit = 4;
this.getLength = function(){
return (queue.length - offset);
}
this.isEmpty = function(){
return (queue.length == 0);
}
this.isLimit = function(){
return (this.limit <= this.getLength());
}
this.enqueue = function(item){
if (this.isLimit()) return false;
queue.push(item);
return true;
}
this.dequeue = function(){
if (queue.length == 0) return undefined;
var item = queue[offset];
if (++ offset * 2 >= queue.length){
queue = queue.slice(offset);
offset = 0;
}
return item;
}
this.peek = function(){
return (queue.length > 0 ? queue[offset] : undefined);
}
}
</source>
<source lang="javascript">
function LimitedQueue(){
var queue = [];
var offset = 0;
var limit = 4;
this.getLength = function(){
return (queue.length - offset);
}
this.isEmpty = function(){
return (queue.length == 0);
}
this.isLimit = function(){
return (this.limit <= this.getLength());
}
this.enqueue = function(item){
if (this.isLimit()) return false;
queue.push(item);
return true;
}
this.dequeue = function(){
if (queue.length == 0) return undefined;
var item = queue[offset];
if (++ offset * 2 >= queue.length){
queue = queue.slice(offset);
offset = 0;
}
return item;
}
this.peek = function(){
return (queue.length > 0 ? queue[offset] : undefined);
}
}
</source>