Skip to content

Commit 9df032e

Browse files
author
Daniel Dahan
committed
development: refactored Deque
1 parent 25e7cce commit 9df032e

File tree

7 files changed

+126
-153
lines changed

7 files changed

+126
-153
lines changed

Sources/Algorithm+Array.swift

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,39 @@
2929
*/
3030

3131
extension Array where Element: Equatable {
32-
@discardableResult
32+
/**
33+
Removes a given Element from an Array if it exists.
34+
- Parameter object: An Element.
35+
- Returns: An optional Element if the removed
36+
element exists.
37+
*/
38+
@discardableResult
3339
mutating func remove(object: Element) -> Element? {
34-
if let v: Int = index(of: object) {
35-
return remove(at: v)
36-
}
37-
return nil
40+
guard let v = index(of: object) else {
41+
return nil
42+
}
43+
return remove(at: v)
3844
}
3945

46+
/**
47+
Removes a list of given Elements from an Array if
48+
they exists.
49+
- Parameter objects: A list of Elements.
50+
*/
4051
@discardableResult
4152
mutating func remove(objects: Element...) {
4253
remove(objects: objects)
4354
}
4455

56+
/**
57+
Removes an Array of given Elements from an Array if
58+
they exists.
59+
- Parameter objects: An Array of Elements.
60+
*/
4561
@discardableResult
4662
mutating func remove(objects: [Element]) {
47-
for x in objects {
48-
remove(object: x)
63+
objects.forEach {
64+
self.remove(object: $0)
4965
}
5066
}
5167
}
@@ -67,13 +83,13 @@ extension Array: ProbableType {
6783
*/
6884
public func count<Element: Equatable>(of elements: [Element]) -> Int {
6985
var c: Int = 0
70-
for v in elements {
71-
for x in self {
72-
if v == x as! Element {
73-
c += 1
74-
}
75-
}
76-
}
86+
for e in elements {
87+
for x in self {
88+
if e == x as? Element {
89+
c += 1
90+
}
91+
}
92+
}
7793
return c
7894
}
7995

@@ -92,29 +108,25 @@ extension Array: ProbableType {
92108
- Returns: A Double.
93109
*/
94110
public func probability<Element: Equatable>(of elements: [Element]) -> Double {
95-
guard 0 < count else {
96-
return 0
97-
}
98-
99-
return Double(count(of: elements)) / Double(count)
111+
return 0 < count ? Double(count(of: elements)) / Double(count) : 0
100112
}
101113

102114
/**
103115
A probability method that uses a block to determine the member state of a condition.
104116
- Parameter of elements: A list of Elements.
105117
- Returns: A Double.
106118
*/
107-
public func probability(of block: (Element) -> Bool) -> Double {
119+
public func probability(of block: @escaping (Element) -> Bool) -> Double {
108120
guard 0 < count else {
109121
return 0
110122
}
111123

112-
var c: Int = 0
113-
for x in self {
114-
if block(x) {
115-
c += 1
116-
}
117-
}
124+
var c: Int = 0
125+
for e in self {
126+
if block(e) {
127+
c += 1
128+
}
129+
}
118130

119131
return Double(c) / Double(count)
120132
}

Sources/Algorithm+Set.swift

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ extension Set: ProbableType {
4545
*/
4646
public func count<Element: Equatable>(of elements: [Element]) -> Int {
4747
var c: Int = 0
48-
for v in elements {
48+
for e in elements {
4949
for x in self {
50-
if v == x as! Element {
50+
if e == x as? Element {
5151
c += 1
5252
}
5353
}
@@ -70,26 +70,22 @@ extension Set: ProbableType {
7070
- Returns: A Double.
7171
*/
7272
public func probability<Element: Equatable>(of elements: [Element]) -> Double {
73-
guard 0 < count else {
74-
return 0
75-
}
76-
77-
return Double(count(of: elements)) / Double(count)
73+
return 0 < count ? Double(count(of: elements)) / Double(count) : 0
7874
}
7975

8076
/**
8177
A probability method that uses a block to determine the member state of a condition.
8278
- Parameter of elements: A list of Elements.
8379
- Returns: A Double.
8480
*/
85-
public func probability(of block: (Element) -> Bool) -> Double {
81+
public func probability(of block: @escaping (Element) -> Bool) -> Double {
8682
guard 0 < count else {
8783
return 0
8884
}
8985

9086
var c: Int = 0
91-
for x in self {
92-
if block(x) {
87+
for e in self {
88+
if block(e) {
9389
c += 1
9490
}
9591
}

Sources/Deque.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929
*/
3030

31-
public class Deque<Element>: CustomStringConvertible, Sequence {
32-
public typealias Iterator = AnyIterator<Element?>
31+
public struct Deque<Element>: CustomStringConvertible, Sequence {
32+
public typealias Iterator = AnyIterator<Element>
3333

3434
/**
3535
:name: list
@@ -107,7 +107,7 @@ public class Deque<Element>: CustomStringConvertible, Sequence {
107107
:name: insertAtFront
108108
:description: Insert a new element at the front of the Deque.
109109
*/
110-
public func insertAtFront(_ element: Element) {
110+
mutating public func insert(atFront element: Element) {
111111
list.insertAtFront(element)
112112
}
113113

@@ -117,15 +117,15 @@ public class Deque<Element>: CustomStringConvertible, Sequence {
117117
and remove it.
118118
- returns: Element?
119119
*/
120-
public func removeAtFront() -> Element? {
120+
mutating public func removeAtFront() -> Element? {
121121
return list.removeAtFront()
122122
}
123123

124124
/**
125125
:name: insertAtBack
126126
:description: Insert a new element at the back of the Deque.
127127
*/
128-
public func insertAtBack(_ element: Element) {
128+
mutating public func insert(atBack element: Element) {
129129
list.insertAtBack(element)
130130
}
131131

@@ -135,7 +135,7 @@ public class Deque<Element>: CustomStringConvertible, Sequence {
135135
and remove it.
136136
- returns: Element?
137137
*/
138-
public func removeAtBack() -> Element? {
138+
mutating public func removeAtBack() -> Element? {
139139
return list.removeAtBack()
140140
}
141141

@@ -149,18 +149,18 @@ public class Deque<Element>: CustomStringConvertible, Sequence {
149149
}
150150

151151
public func +<Element>(lhs: Deque<Element>, rhs: Deque<Element>) -> Deque<Element> {
152-
let d: Deque<Element> = Deque<Element>()
152+
var d = Deque<Element>()
153153
for x in lhs {
154-
d.insertAtBack(x!)
154+
d.insert(atBack: x)
155155
}
156156
for x in rhs {
157-
d.insertAtBack(x!)
157+
d.insert(atBack: x)
158158
}
159159
return d
160160
}
161161

162-
public func +=<Element>(lhs: Deque<Element>, rhs: Deque<Element>) {
162+
public func +=<Element>(lhs: inout Deque<Element>, rhs: Deque<Element>) {
163163
for x in rhs {
164-
lhs.insertAtBack(x!)
164+
lhs.insert(atBack: x)
165165
}
166166
}

Sources/DoublyLinkedList.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*/
3030

3131
public class DoublyLinkedList<Element>: CustomStringConvertible, Sequence {
32-
public typealias Iterator = AnyIterator<Element?>
32+
public typealias Iterator = AnyIterator<Element>
3333

3434
/**
3535
:name: head
@@ -68,7 +68,7 @@ public class DoublyLinkedList<Element>: CustomStringConvertible, Sequence {
6868
internal var internalDescription: String {
6969
var output: String = "("
7070
var c: Int = 0
71-
var x: DoublyLinkedListNode<Element>? = head
71+
var x = head
7272
while nil !== x {
7373
output += "\(x)"
7474
c += 1
@@ -286,12 +286,12 @@ public class DoublyLinkedList<Element>: CustomStringConvertible, Sequence {
286286
if 0 == count {
287287
return nil
288288
}
289-
let element: Element? = tail!.element
289+
let element = tail?.element
290290
count -= 1
291291
if 0 == count {
292292
reset()
293293
} else {
294-
tail = tail!.previous
294+
tail = tail?.previous
295295
}
296296
return element
297297
}
@@ -320,7 +320,7 @@ public class DoublyLinkedList<Element>: CustomStringConvertible, Sequence {
320320
if nil === current || head === current {
321321
insertAtFront(element)
322322
} else {
323-
let z: DoublyLinkedListNode<Element> = DoublyLinkedListNode<Element>(next: current, previous: current!.previous, element: element)
323+
let z = DoublyLinkedListNode<Element>(next: current, previous: current!.previous, element: element)
324324
current!.previous?.next = z
325325
current!.previous = z
326326
count += 1
@@ -335,7 +335,7 @@ public class DoublyLinkedList<Element>: CustomStringConvertible, Sequence {
335335
if nil === current || tail === current {
336336
insertAtBack(element)
337337
} else {
338-
let z: DoublyLinkedListNode<Element> = DoublyLinkedListNode<Element>(next: current!.next, previous: current, element: element)
338+
let z = DoublyLinkedListNode<Element>(next: current!.next, previous: current, element: element)
339339
current!.next?.previous = z
340340
current!.next = z
341341
count += 1
@@ -352,17 +352,17 @@ public class DoublyLinkedList<Element>: CustomStringConvertible, Sequence {
352352
if 1 >= count {
353353
return removeAtFront()
354354
} else {
355-
let element: Element? = current!.element
356-
current!.previous?.next = current!.next
357-
current!.next?.previous = current!.previous
355+
let element = current?.element
356+
current?.previous?.next = current?.next
357+
current?.next?.previous = current?.previous
358358
if tail === current {
359-
current = tail!.previous
359+
current = tail?.previous
360360
tail = current
361361
} else if head === current {
362-
current = head!.next
362+
current = head?.next
363363
head = current
364364
} else {
365-
current = current!.next
365+
current = current?.next
366366
}
367367
count -= 1
368368
return element
@@ -381,18 +381,18 @@ public class DoublyLinkedList<Element>: CustomStringConvertible, Sequence {
381381
}
382382

383383
public func +<Element>(lhs: DoublyLinkedList<Element>, rhs: DoublyLinkedList<Element>) -> DoublyLinkedList<Element> {
384-
let l: DoublyLinkedList<Element> = DoublyLinkedList<Element>()
384+
let l = DoublyLinkedList<Element>()
385385
for x in lhs {
386-
l.insertAtBack(x!)
386+
l.insertAtBack(x)
387387
}
388388
for x in rhs {
389-
l.insertAtBack(x!)
389+
l.insertAtBack(x)
390390
}
391391
return l
392392
}
393393

394394
public func +=<Element>(lhs: DoublyLinkedList<Element>, rhs: DoublyLinkedList<Element>) {
395395
for x in rhs {
396-
lhs.insertAtBack(x!)
396+
lhs.insertAtBack(x)
397397
}
398398
}

Sources/Queue.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*/
3030

3131
public class Queue<Element>: CustomStringConvertible, Sequence {
32-
public typealias Iterator = AnyIterator<Element?>
32+
public typealias Iterator = AnyIterator<Element>
3333

3434
/**
3535
:name: list
@@ -121,18 +121,18 @@ public class Queue<Element>: CustomStringConvertible, Sequence {
121121
}
122122

123123
public func +<Element>(lhs: Queue<Element>, rhs: Queue<Element>) -> Queue<Element> {
124-
let q: Queue<Element> = Queue<Element>()
124+
let q = Queue<Element>()
125125
for x in lhs {
126-
q.enqueue(x!)
126+
q.enqueue(x)
127127
}
128128
for x in rhs {
129-
q.enqueue(x!)
129+
q.enqueue(x)
130130
}
131131
return q
132132
}
133133

134134
public func +=<Element>(lhs: Queue<Element>, rhs: Queue<Element>) {
135135
for x in rhs {
136-
lhs.enqueue(x!)
136+
lhs.enqueue(x)
137137
}
138138
}

0 commit comments

Comments
 (0)