Skip to content

Commit 6714e28

Browse files
authored
Merge pull request #888 from Peeoner174/patch-1
Update Queue-Optimized.swift
2 parents 81bc070 + 0299a1c commit 6714e28

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

Queue/Queue-Optimized.swift

+10-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public struct Queue<T> {
2323
}
2424

2525
public mutating func dequeue() -> T? {
26-
guard head < array.count, let element = array[head] else { return nil }
26+
guard let element = array[guarded: head] else { return nil }
2727

2828
array[head] = nil
2929
head += 1
@@ -45,3 +45,12 @@ public struct Queue<T> {
4545
}
4646
}
4747
}
48+
49+
extension Array {
50+
subscript(guarded idx: Int) -> Element? {
51+
guard (startIndex..<endIndex).contains(idx) else {
52+
return nil
53+
}
54+
return self[idx]
55+
}
56+
}

0 commit comments

Comments
 (0)