Skip to content

Instantly share code, notes, and snippets.

@ChenCodes
Created January 3, 2017 00:44
Show Gist options
  • Save ChenCodes/634be0d2f0c6237684008d4f9be478c5 to your computer and use it in GitHub Desktop.
Save ChenCodes/634be0d2f0c6237684008d4f9be478c5 to your computer and use it in GitHub Desktop.
public struct Queue<T> {
fileprivate var array = [T?]()
fileprivate var head = 0
public var isEmpty: Bool {
return count == 0
}
public var count: Int {
return array.count - head
}
public mutating func enqueue(_ element: T) {
array.append(element)
}
public mutating func dequeue() -> T? {
guard head < array.count, let element = array[head] else { return nil }
array[head] = nil
head += 1
let percentage = Double(head)/Double(array.count)
if array.count > 50 && percentage > 0.25 {
array.removeFirst(head)
head = 0
}
return element
}
public var front: T? {
return array.first
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment