Skip to content

Commit 8e955cd

Browse files
authored
Merge pull request kodecocodes#371 from micazeve/master
Fixed the trimming of deques
2 parents 93b1026 + 096eca3 commit 8e955cd

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

Deque/Deque-Optimized.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ public struct Deque<T> {
77
private var array: [T?]
88
private var head: Int
99
private var capacity: Int
10+
private let originalCapacity:Int
1011

1112
public init(_ capacity: Int = 10) {
1213
self.capacity = max(capacity, 1)
14+
originalCapacity = self.capacity
1315
array = [T?](repeating: nil, count: capacity)
1416
head = capacity
1517
}
@@ -30,7 +32,7 @@ public struct Deque<T> {
3032
if head == 0 {
3133
capacity *= 2
3234
let emptySpace = [T?](repeating: nil, count: capacity)
33-
array.insertContentsOf(emptySpace, at: 0)
35+
array.insert(contentsOf: emptySpace, at: 0)
3436
head = capacity
3537
}
3638

@@ -44,7 +46,7 @@ public struct Deque<T> {
4446
array[head] = nil
4547
head += 1
4648

47-
if capacity > 10 && head >= capacity*2 {
49+
if capacity >= originalCapacity && head >= capacity*2 {
4850
let amountToRemove = capacity + capacity/2
4951
array.removeFirst(amountToRemove)
5052
head -= amountToRemove

Deque/README.markdown

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,11 @@ public struct Deque<T> {
119119
private var array: [T?]
120120
private var head: Int
121121
private var capacity: Int
122+
private let originalCapacity:Int
122123

123124
public init(_ capacity: Int = 10) {
124125
self.capacity = max(capacity, 1)
126+
originalCapacity = self.capacity
125127
array = [T?](repeating: nil, count: capacity)
126128
head = capacity
127129
}
@@ -267,7 +269,7 @@ Those empty spots at the front only get used when you call `enqueueFront()`. But
267269
array[head] = nil
268270
head += 1
269271

270-
if capacity > 10 && head >= capacity*2 {
272+
if capacity >= originalCapacity && head >= capacity*2 {
271273
let amountToRemove = capacity + capacity/2
272274
array.removeFirst(amountToRemove)
273275
head -= amountToRemove
@@ -279,6 +281,8 @@ Those empty spots at the front only get used when you call `enqueueFront()`. But
279281

280282
Recall that `capacity` is the original number of empty places at the front of the queue. If the `head` has advanced more to the right than twice the capacity, then it's time to trim off a bunch of these empty spots. We reduce it to about 25%.
281283

284+
> **Note:** The deque will keep at least its original capacity by comparing `capacity` to `originalCapacity`.
285+
282286
For example, this:
283287

284288
[ x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, 1, 2, 3 ]
@@ -288,9 +292,9 @@ For example, this:
288292
becomes after trimming:
289293

290294
[ x, x, x, x, x, 1, 2, 3 ]
291-
|
292-
head
293-
capacity
295+
|
296+
head
297+
capacity
294298

295299
This way we can strike a balance between fast enqueuing and dequeuing at the front and keeping the memory requirements reasonable.
296300

0 commit comments

Comments
 (0)