//
// IntQueue1.swift
// Queues
//
import Foundation
/**
IntQueue1 is our first implementation of a basic first-in, first-out queue
for Integers.
An IntQueue can be described as [n_0, n_1, ..., n_k], where n_0 is the
least-recently-added item in the queue and is the next item to be
removed. n_k is the most-recently-added and will be the last of the
current elements to be removed.
An IntQueue can also be described constructively, with the append operation,
':', such that [n0, n1, ..., n_k] : n_k+1 is the result of enqueing n_k+1
at the end of the queue.
*/
public class IntQueue1 {
// This class represents a queue as an array where the front of
// the array (ie, index 0) corresponds to the front of the queue.
// TODO: write abstraction function and representation invarant
private var entries : [Int]
/**
**Effects**: Constructs an empty queue
*/
public init() {
entries = []
checkRep()
}
/**
Enqueue an item
**Modifies**: self
**Effects**: places entry at the end of the queue
- Parameter entry: item to be added to the queue
*/
public func enqueue(entry : Int) {
checkRep()
entries.append(entry)
checkRep()
}
/**
Dequeue an item
**Requires**: count > 0
**Modifies**: self
**Effects**: removes the item at the front of the queue
- Returns :the item that was first in the queue
*/
public func dequeue() -> Int {
checkRep()
assert (entries.count > 0)
let front = entries.removeFirst()
checkRep()
return front
}
/// The item currently first in the queue, or nil if it does not exist
var front : Int? {
checkRep()
return entries.first
}
/// Number of elements in the queue
public var count : Int {
checkRep()
return entries.count
}
/// Whether count == 0
public var isEmpty : Bool {
checkRep()
return entries.isEmpty
}
public func checkRep() {
// ...
}
}