Skip to content

Commit 2a22a4e

Browse files
authored
Merge pull request kodecocodes#397 from dsantosp12/master
Added ExpressibleByArrayLiteral to the Linked List and Tests
2 parents 602d1ea + 6863590 commit 2a22a4e

File tree

3 files changed

+92
-30
lines changed

3 files changed

+92
-30
lines changed

Linked List/LinkedList.playground/Contents.swift

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,27 @@ public class LinkedListNode<T> {
44
var value: T
55
var next: LinkedListNode?
66
weak var previous: LinkedListNode?
7-
7+
88
public init(value: T) {
99
self.value = value
1010
}
1111
}
1212

13-
public class LinkedList<T> {
13+
public final class LinkedList<T> {
1414
public typealias Node = LinkedListNode<T>
15-
15+
1616
fileprivate var head: Node?
17-
17+
18+
public init() {}
19+
1820
public var isEmpty: Bool {
1921
return head == nil
2022
}
21-
23+
2224
public var first: Node? {
2325
return head
2426
}
25-
27+
2628
public var last: Node? {
2729
if var node = head {
2830
while case let next? = node.next {
@@ -33,7 +35,7 @@ public class LinkedList<T> {
3335
return nil
3436
}
3537
}
36-
38+
3739
public var count: Int {
3840
if var node = head {
3941
var c = 1
@@ -46,7 +48,7 @@ public class LinkedList<T> {
4648
return 0
4749
}
4850
}
49-
51+
5052
public func node(atIndex index: Int) -> Node? {
5153
if index >= 0 {
5254
var node = head
@@ -59,80 +61,88 @@ public class LinkedList<T> {
5961
}
6062
return nil
6163
}
62-
64+
6365
public subscript(index: Int) -> T {
6466
let node = self.node(atIndex: index)
6567
assert(node != nil)
6668
return node!.value
6769
}
68-
70+
6971
public func append(_ value: T) {
7072
let newNode = Node(value: value)
73+
self.append(newNode)
74+
}
75+
76+
public func append(_ newNode: Node) {
7177
if let lastNode = last {
7278
newNode.previous = lastNode
7379
lastNode.next = newNode
7480
} else {
7581
head = newNode
7682
}
7783
}
78-
84+
7985
private func nodesBeforeAndAfter(index: Int) -> (Node?, Node?) {
8086
assert(index >= 0)
81-
87+
8288
var i = index
8389
var next = head
8490
var prev: Node?
85-
91+
8692
while next != nil && i > 0 {
8793
i -= 1
8894
prev = next
8995
next = next!.next
9096
}
9197
assert(i == 0) // if > 0, then specified index was too large
92-
98+
9399
return (prev, next)
94100
}
95-
101+
96102
public func insert(_ value: T, atIndex index: Int) {
97-
let (prev, next) = nodesBeforeAndAfter(index: index)
98-
99103
let newNode = Node(value: value)
104+
self.insert(newNode, atIndex: index)
105+
}
106+
107+
public func insert(_ newNode: Node, atIndex index: Int) {
108+
let (prev, next) = nodesBeforeAndAfter(index: index)
109+
100110
newNode.previous = prev
101111
newNode.next = next
102112
prev?.next = newNode
103113
next?.previous = newNode
104-
114+
105115
if prev == nil {
106116
head = newNode
107117
}
108118
}
109-
119+
110120
public func removeAll() {
111121
head = nil
112122
}
113-
114-
public func remove(node: Node) -> T {
123+
124+
@discardableResult public func remove(node: Node) -> T {
115125
let prev = node.previous
116126
let next = node.next
117-
127+
118128
if let prev = prev {
119129
prev.next = next
120130
} else {
121131
head = next
122132
}
123133
next?.previous = prev
124-
134+
125135
node.previous = nil
126136
node.next = nil
127137
return node.value
128138
}
129-
130-
public func removeLast() -> T {
139+
140+
@discardableResult public func removeLast() -> T {
131141
assert(!isEmpty)
132142
return remove(node: last!)
133143
}
134-
135-
public func remove(atIndex index: Int) -> T {
144+
145+
@discardableResult public func remove(atIndex index: Int) -> T {
136146
let node = self.node(atIndex: index)
137147
assert(node != nil)
138148
return remove(node: node!)
@@ -173,7 +183,7 @@ extension LinkedList {
173183
}
174184
return result
175185
}
176-
186+
177187
public func filter(predicate: (T) -> Bool) -> LinkedList<T> {
178188
let result = LinkedList<T>()
179189
var node = head
@@ -190,13 +200,23 @@ extension LinkedList {
190200
extension LinkedList {
191201
convenience init(array: Array<T>) {
192202
self.init()
193-
203+
194204
for element in array {
195205
self.append(element)
196206
}
197207
}
198208
}
199209

210+
extension LinkedList: ExpressibleByArrayLiteral {
211+
public convenience init(arrayLiteral elements: T...) {
212+
self.init()
213+
214+
for element in elements {
215+
self.append(element)
216+
}
217+
}
218+
}
219+
200220
let list = LinkedList<String>()
201221
list.isEmpty // true
202222
list.first // nil
@@ -255,3 +275,13 @@ list[0] // "Swift"
255275

256276
list.remove(atIndex: 0) // "Swift"
257277
list.count // 0
278+
279+
let linkedList: LinkedList<Int> = [1, 2, 3, 4] // [1, 2, 3, 4]
280+
linkedList.count // 4
281+
linkedList[0] // 1
282+
283+
// Infer the type from the array
284+
let listArrayLiteral2: LinkedList = ["Swift", "Algorithm", "Club"]
285+
listArrayLiteral2.count // 3
286+
listArrayLiteral2[0] // "Swift"
287+
listArrayLiteral2.removeLast() // "Club"

Linked List/LinkedList.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class LinkedListNode<T> {
88
}
99
}
1010

11-
public class LinkedList<T> {
11+
public final class LinkedList<T> {
1212
public typealias Node = LinkedListNode<T>
1313

1414
fileprivate var head: Node?
@@ -204,3 +204,13 @@ extension LinkedList {
204204
}
205205
}
206206
}
207+
208+
extension LinkedList: ExpressibleByArrayLiteral {
209+
public convenience init(arrayLiteral elements: T...) {
210+
self.init()
211+
212+
for element in elements {
213+
self.append(element)
214+
}
215+
}
216+
}

Linked List/Tests/LinkedListTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,26 @@ class LinkedListTest: XCTestCase {
248248
XCTAssertTrue(last === list.first)
249249
XCTAssertEqual(nodeCount, list.count)
250250
}
251+
252+
func testArrayLiteralInitTypeInfer() {
253+
let arrayLiteralInitInfer: LinkedList = [1.0, 2.0, 3.0]
254+
255+
XCTAssertEqual(arrayLiteralInitInfer.count, 3)
256+
XCTAssertEqual(arrayLiteralInitInfer.first?.value, 1.0)
257+
XCTAssertEqual(arrayLiteralInitInfer.last?.value, 3.0)
258+
XCTAssertEqual(arrayLiteralInitInfer[1], 2.0)
259+
XCTAssertEqual(arrayLiteralInitInfer.removeLast(), 3.0)
260+
XCTAssertEqual(arrayLiteralInitInfer.count, 2)
261+
}
262+
263+
func testArrayLiteralInitExplicit() {
264+
let arrayLiteralInitExplicit: LinkedList<Int> = [1, 2, 3]
265+
266+
XCTAssertEqual(arrayLiteralInitExplicit.count, 3)
267+
XCTAssertEqual(arrayLiteralInitExplicit.first?.value, 1)
268+
XCTAssertEqual(arrayLiteralInitExplicit.last?.value, 3)
269+
XCTAssertEqual(arrayLiteralInitExplicit[1], 2)
270+
XCTAssertEqual(arrayLiteralInitExplicit.removeLast(), 3)
271+
XCTAssertEqual(arrayLiteralInitExplicit.count, 2)
272+
}
251273
}

0 commit comments

Comments
 (0)