summaryrefslogtreecommitdiff
path: root/2026/games_submissions/liquidcake/queue.mjs
blob: e6c3efe0484c215d73965eb4fd3817f5d0168f4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
export class Queue {
  /* Structure is a (hopefully short) list of (longer) lists.
   * Queue to the last list.
   * Hold index into first list.
   */
  length = 0;
  index = -1;
  content = [[]];
  max_len = 100;
  push(x) {
    let content = this.content;
    if (content[content.length - 1].length > this.max_len) {
      content.push([]);
    }
    content[content.length - 1].push(x);
    this.length += 1;
  }
  pop() {
    this.index += 1;
    if (this.index >= this.content[0].length) {
      if (this.content.length == 1) {
	throw("empty");
      }
      this.content.shift();
      this.index = 0;
    }
    this.length -= 1;
    return this.content[0][this.index];
  }
}