Created
January 3, 2017 00:44
-
-
Save ChenCodes/634be0d2f0c6237684008d4f9be478c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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