class Queue

class Queue

lib/collections/queue.tya:2

Queue provides the collections/Queue standard library API.

Source
# Queue provides the collections/Queue standard library API.
class Queue
  # Queue.head stores the index of the next item to pop.
  # @type Int
  head: 0

  # Queue.items stores queued values.
  # @type Array
  items: []

  # Queue.initialize provides the collections/Queue standard library operation.
  # @param values Array values value.
  # @return Self the initialized object.
  initialize: values = nil ->
    if values == nil
      self.items = []
    else
      self.items = values.slice(0, values.len())
    self.head = 0

  # Queue.clear provides the collections/Queue standard library operation.
  # @return Nil the resulting value.
  clear: ->
    self.items = []
    self.head = 0
    self

  # Queue.compact provides the collections/Queue standard library operation.
  # @return Any the resulting value.
  compact: ->
    if self.head > 32 and self.head * 2 >= self.items.len()
      self.items = self.to_array()
      self.head = 0

  # Queue.empty? provides the collections/Queue standard library operation.
  # @return Boolean whether the condition is true.
  empty?: ->
    self.len() == 0

  # Queue.from_array provides the collections/Queue standard library operation.
  # @param values Array values value.
  # @return Array the resulting value.
  from_array: values ->
    Queue(values)

  # Queue.len provides the collections/Queue standard library operation.
  # @return Int the resulting value.
  len: ->
    self.items.len() - self.head

  # Queue.peek provides the collections/Queue standard library operation.
  # @return Any the resulting value.
  peek: ->
    if self.empty?()
      raise error("collections.Queue.peek: empty queue")
    self.items[self.head]

  # Queue.pop provides the collections/Queue standard library operation.
  # @return Any the resulting value.
  pop: ->
    if self.empty?()
      raise error("collections.Queue.pop: empty queue")
    value = self.items[self.head]
    self.head = self.head + 1
    self.compact()
    value

  # Queue.push provides the collections/Queue standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  push: value ->
    self.items.push(value)
    self

  # Queue.to_array provides the collections/Queue standard library operation.
  # @return Array the resulting value.
  to_array: ->
    out = []
    i = self.head
    while i < self.items.len()
      out.push(self.items[i])
      i = i + 1
    out

Instance Variables

head

Queue.head

lib/collections/queue.tya:5

Queue.head stores the index of the next item to pop.

Source
  # Queue.head stores the index of the next item to pop.
  # @type Int
  head: 0

items

Queue.items

lib/collections/queue.tya:9

Queue.items stores queued values.

Source
  # Queue.items stores queued values.
  # @type Array
  items: []

Methods

clear

Queue.clear()

lib/collections/queue.tya:23

Queue.clear provides the collections/Queue standard library operation.

Source
  # Queue.clear provides the collections/Queue standard library operation.
  # @return Nil the resulting value.
  clear: ->
    self.items = []
    self.head = 0
    self

compact

Queue.compact()

lib/collections/queue.tya:30

Queue.compact provides the collections/Queue standard library operation.

Source
  # Queue.compact provides the collections/Queue standard library operation.
  # @return Any the resulting value.
  compact: ->
    if self.head > 32 and self.head * 2 >= self.items.len()
      self.items = self.to_array()
      self.head = 0

empty?

Queue.empty?()

lib/collections/queue.tya:37

Queue.empty? provides the collections/Queue standard library operation.

Source
  # Queue.empty? provides the collections/Queue standard library operation.
  # @return Boolean whether the condition is true.
  empty?: ->
    self.len() == 0

from_array

Queue.from_array(values)

lib/collections/queue.tya:43

Queue.from_array provides the collections/Queue standard library operation.

Source
  # Queue.from_array provides the collections/Queue standard library operation.
  # @param values Array values value.
  # @return Array the resulting value.
  from_array: values ->
    Queue(values)

initialize

Queue.initialize(values = nil)

lib/collections/queue.tya:14

Queue.initialize provides the collections/Queue standard library operation.

Source
  # Queue.initialize provides the collections/Queue standard library operation.
  # @param values Array values value.
  # @return Self the initialized object.
  initialize: values = nil ->
    if values == nil
      self.items = []
    else
      self.items = values.slice(0, values.len())
    self.head = 0

len

Queue.len()

lib/collections/queue.tya:48

Queue.len provides the collections/Queue standard library operation.

Source
  # Queue.len provides the collections/Queue standard library operation.
  # @return Int the resulting value.
  len: ->
    self.items.len() - self.head

peek

Queue.peek()

lib/collections/queue.tya:53

Queue.peek provides the collections/Queue standard library operation.

Source
  # Queue.peek provides the collections/Queue standard library operation.
  # @return Any the resulting value.
  peek: ->
    if self.empty?()
      raise error("collections.Queue.peek: empty queue")
    self.items[self.head]

pop

Queue.pop()

lib/collections/queue.tya:60

Queue.pop provides the collections/Queue standard library operation.

Source
  # Queue.pop provides the collections/Queue standard library operation.
  # @return Any the resulting value.
  pop: ->
    if self.empty?()
      raise error("collections.Queue.pop: empty queue")
    value = self.items[self.head]
    self.head = self.head + 1
    self.compact()
    value

push

Queue.push(value)

lib/collections/queue.tya:71

Queue.push provides the collections/Queue standard library operation.

Source
  # Queue.push provides the collections/Queue standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  push: value ->
    self.items.push(value)
    self

to_array

Queue.to_array()

lib/collections/queue.tya:77

Queue.to_array provides the collections/Queue standard library operation.

Source
  # Queue.to_array provides the collections/Queue standard library operation.
  # @return Array the resulting value.
  to_array: ->
    out = []
    i = self.head
    while i < self.items.len()
      out.push(self.items[i])
      i = i + 1
    out