Skip to content

Commit bb21f61

Browse files
committed
[Code refactoring] Renamed atIndex label in method to at.
1 parent 3de5a1b commit bb21f61

File tree

3 files changed

+70
-70
lines changed

3 files changed

+70
-70
lines changed

Linked List/LinkedList.playground/Contents.swift

+26-26
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public final class LinkedList<T> {
7070
///
7171
/// - Parameter index: Integer value of the node's index to be returned
7272
/// - Returns: Optional LinkedListNode
73-
public func node(atIndex index: Int) -> Node {
73+
public func node(at index: Int) -> Node {
7474
assert(head != nil, "List is empty")
7575
assert(index >= 0, "index must be greater than 0")
7676

@@ -94,7 +94,7 @@ public final class LinkedList<T> {
9494
///
9595
/// - Parameter index: Integer value of the requested value's index
9696
public subscript(index: Int) -> T {
97-
let node = self.node(atIndex: index)
97+
let node = self.node(at: index)
9898
return node.value
9999
}
100100

@@ -135,23 +135,23 @@ public final class LinkedList<T> {
135135
/// - Parameters:
136136
/// - value: The data value to be inserted
137137
/// - index: Integer value of the index to be insterted at
138-
public func insert(_ value: T, atIndex index: Int) {
138+
public func insert(_ value: T, at index: Int) {
139139
let newNode = Node(value: value)
140-
self.insert(newNode, atIndex: index)
140+
self.insert(newNode, at: index)
141141
}
142142

143143
/// Insert a copy of a node at a specific index. Crashes if index is out of bounds (0...self.count)
144144
///
145145
/// - Parameters:
146146
/// - node: The node containing the value to be inserted
147147
/// - index: Integer value of the index to be inserted at
148-
public func insert(_ newNode: Node, atIndex index: Int) {
148+
public func insert(_ newNode: Node, at index: Int) {
149149
if index == 0 {
150150
newNode.next = head
151151
head?.previous = newNode
152152
head = newNode
153153
} else {
154-
let prev = node(atIndex: index-1)
154+
let prev = node(at: index-1)
155155
let next = prev.next
156156
newNode.previous = prev
157157
newNode.next = next
@@ -165,14 +165,14 @@ public final class LinkedList<T> {
165165
/// - Parameters:
166166
/// - list: The LinkedList to be copied and inserted
167167
/// - index: Integer value of the index to be inserted at
168-
public func insert(_ list: LinkedList, atIndex index: Int) {
168+
public func insert(_ list: LinkedList, at index: Int) {
169169
if list.isEmpty { return }
170170

171171
if index == 0 {
172172
list.last?.next = head
173173
head = list.head
174174
} else {
175-
let prev = node(atIndex: index-1)
175+
let prev = node(at: index-1)
176176
let next = prev.next
177177

178178
prev.next = list.head
@@ -220,8 +220,8 @@ public final class LinkedList<T> {
220220
///
221221
/// - Parameter index: Integer value of the index of the node to be removed
222222
/// - Returns: The data value contained in the deleted node
223-
@discardableResult public func remove(atIndex index: Int) -> T {
224-
let node = self.node(atIndex: index)
223+
@discardableResult public func remove(at index: Int) -> T {
224+
let node = self.node(at: index)
225225
return remove(node: node)
226226
}
227227
}
@@ -323,9 +323,9 @@ list.first!.next!.value // "World"
323323
list.last!.previous!.value // "Hello"
324324
list.last!.next // nil
325325

326-
list.node(atIndex: 0).value // "Hello"
327-
list.node(atIndex: 1).value // "World"
328-
//list.node(atIndex: 2) // crash!
326+
list.node(at: 0).value // "Hello"
327+
list.node(at: 1).value // "World"
328+
//list.node(at: 2) // crash!
329329

330330
list[0] // "Hello"
331331
list[1] // "World"
@@ -338,18 +338,18 @@ list.append(list2) // [Hello, World, Goodbye, World]
338338
list2.removeAll() // [ ]
339339
list2.isEmpty // true
340340
list.removeLast() // "World"
341-
list.remove(atIndex: 2) // "Goodbye"
341+
list.remove(at: 2) // "Goodbye"
342342

343-
list.insert("Swift", atIndex: 1)
343+
list.insert("Swift", at: 1)
344344
list[0] // "Hello"
345345
list[1] // "Swift"
346346
list[2] // "World"
347347
print(list)
348348

349349
list.reverse() // [World, Swift, Hello]
350350

351-
list.node(atIndex: 0).value = "Universe"
352-
list.node(atIndex: 1).value = "Swifty"
351+
list.node(at: 0).value = "Universe"
352+
list.node(at: 1).value = "Swifty"
353353
let m = list.map { s in s.count }
354354
m // [8, 6, 5]
355355
let f = list.filter { s in s.count > 5 }
@@ -366,28 +366,28 @@ list.head?.value
366366
list.count // 1
367367
list[0] // "Swifty"
368368

369-
list.remove(atIndex: 0) // "Swifty"
369+
list.remove(at: 0) // "Swifty"
370370
list.count // 0
371371

372372
let list3 = LinkedList<String>()
373-
list3.insert("2", atIndex: 0) // [2]
373+
list3.insert("2", at: 0) // [2]
374374
list3.count // 1
375-
list3.insert("4", atIndex: 1) // [2,4]
375+
list3.insert("4", at: 1) // [2,4]
376376
list3.count // 2
377-
list3.insert("5", atIndex: 2) // [2,4,5]
377+
list3.insert("5", at: 2) // [2,4,5]
378378
list3.count // 3
379-
list3.insert("3", atIndex: 1) // [2,3,4,5]
380-
list3.insert("1", atIndex: 0) // [1,2,3,4,5]
379+
list3.insert("3", at: 1) // [2,3,4,5]
380+
list3.insert("1", at: 0) // [1,2,3,4,5]
381381

382382
let list4 = LinkedList<String>()
383-
list4.insert(list3, atIndex: 0) // [1,2,3,4,5]
383+
list4.insert(list3, at: 0) // [1,2,3,4,5]
384384
list4.count // 5
385385

386386
let list5 = LinkedList<String>()
387387
list5.append("0") // [0]
388-
list5.insert("End", atIndex:1) // [0,End]
388+
list5.insert("End", at:1) // [0,End]
389389
list5.count // 2
390-
list5.insert(list4, atIndex: 1) // [0,1,2,3,4,5,End]
390+
list5.insert(list4, at: 1) // [0,1,2,3,4,5,End]
391391
list5.count // 7
392392

393393

Linked List/LinkedList.swift

+10-10
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public final class LinkedList<T> {
4848
return count
4949
}
5050

51-
public func node(atIndex index: Int) -> Node {
51+
public func node(at index: Int) -> Node {
5252
assert(head != nil, "List is empty")
5353
assert(index >= 0, "index must be greater than 0")
5454
if index == 0 {
@@ -68,7 +68,7 @@ public final class LinkedList<T> {
6868
}
6969

7070
public subscript(index: Int) -> T {
71-
let node = self.node(atIndex: index)
71+
let node = self.node(at: index)
7272
return node.value
7373
}
7474

@@ -95,18 +95,18 @@ public final class LinkedList<T> {
9595
}
9696
}
9797

98-
public func insert(_ value: T, atIndex index: Int) {
98+
public func insert(_ value: T, at index: Int) {
9999
let newNode = Node(value: value)
100-
self.insert(newNode, atIndex: index)
100+
self.insert(newNode, at: index)
101101
}
102102

103-
public func insert(_ newNode: Node, atIndex index: Int) {
103+
public func insert(_ newNode: Node, at index: Int) {
104104
if index == 0 {
105105
newNode.next = head
106106
head?.previous = newNode
107107
head = newNode
108108
} else {
109-
let prev = node(atIndex: index-1)
109+
let prev = node(at: index-1)
110110
let next = prev.next
111111
newNode.previous = prev
112112
newNode.next = next
@@ -115,14 +115,14 @@ public final class LinkedList<T> {
115115
}
116116
}
117117

118-
public func insert(_ list: LinkedList, atIndex index: Int) {
118+
public func insert(_ list: LinkedList, at index: Int) {
119119
if list.isEmpty { return }
120120

121121
if index == 0 {
122122
list.last?.next = head
123123
head = list.head
124124
} else {
125-
let prev = self.node(atIndex: index-1)
125+
let prev = self.node(at: index-1)
126126
let next = prev.next
127127

128128
prev.next = list.head
@@ -158,8 +158,8 @@ public final class LinkedList<T> {
158158
return remove(node: last!)
159159
}
160160

161-
@discardableResult public func remove(atIndex index: Int) -> T {
162-
let node = self.node(atIndex: index)
161+
@discardableResult public func remove(at index: Int) -> T {
162+
let node = self.node(at: index)
163163
return remove(node: node)
164164
}
165165
}

Linked List/Tests/LinkedListTests.swift

+34-34
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class LinkedListTest: XCTestCase {
9797
let list = LinkedList<Int>()
9898
list.append(123)
9999

100-
let node = list.node(atIndex: 0)
100+
let node = list.node(at: 0)
101101
XCTAssertNotNil(node)
102102
XCTAssertEqual(node.value, 123)
103103
XCTAssertTrue(node === list.first)
@@ -109,18 +109,18 @@ class LinkedListTest: XCTestCase {
109109
let nodeCount = list.count
110110
XCTAssertEqual(nodeCount, numbers.count)
111111

112-
let first = list.node(atIndex: 0)
112+
let first = list.node(at: 0)
113113
XCTAssertNotNil(first)
114114
XCTAssertTrue(first === list.first)
115115
XCTAssertEqual(first.value, numbers[0])
116116

117-
let last = list.node(atIndex: nodeCount - 1)
117+
let last = list.node(at: nodeCount - 1)
118118
XCTAssertNotNil(last)
119119
XCTAssertTrue(last === list.last)
120120
XCTAssertEqual(last.value, numbers[nodeCount - 1])
121121

122122
for i in 0..<nodeCount {
123-
let node = list.node(atIndex: i)
123+
let node = list.node(at: i)
124124
XCTAssertNotNil(node)
125125
XCTAssertEqual(node.value, numbers[i])
126126
}
@@ -135,25 +135,25 @@ class LinkedListTest: XCTestCase {
135135

136136
func testInsertAtIndexInEmptyList() {
137137
let list = LinkedList<Int>()
138-
list.insert(123, atIndex: 0)
138+
list.insert(123, at: 0)
139139

140140
XCTAssertFalse(list.isEmpty)
141141
XCTAssertEqual(list.count, 1)
142142

143-
let node = list.node(atIndex: 0)
143+
let node = list.node(at: 0)
144144
XCTAssertNotNil(node)
145145
XCTAssertEqual(node.value, 123)
146146
}
147147

148148
func testInsertAtIndex() {
149149
let list = buildList()
150-
let prev = list.node(atIndex: 2)
151-
let next = list.node(atIndex: 3)
150+
let prev = list.node(at: 2)
151+
let next = list.node(at: 3)
152152
let nodeCount = list.count
153153

154-
list.insert(444, atIndex: 3)
154+
list.insert(444, at: 3)
155155

156-
let node = list.node(atIndex: 3)
156+
let node = list.node(at: 3)
157157
XCTAssertNotNil(node)
158158
XCTAssertEqual(node.value, 444)
159159
XCTAssertEqual(nodeCount + 1, list.count)
@@ -169,36 +169,36 @@ class LinkedListTest: XCTestCase {
169169
let list2 = LinkedList<Int>()
170170
list2.append(99)
171171
list2.append(102)
172-
list.insert(list2, atIndex: 2)
172+
list.insert(list2, at: 2)
173173
XCTAssertTrue(list.count == 8)
174-
XCTAssertEqual(list.node(atIndex: 1).value, 2)
175-
XCTAssertEqual(list.node(atIndex: 2).value, 99)
176-
XCTAssertEqual(list.node(atIndex: 3).value, 102)
177-
XCTAssertEqual(list.node(atIndex: 4).value, 10)
174+
XCTAssertEqual(list.node(at: 1).value, 2)
175+
XCTAssertEqual(list.node(at: 2).value, 99)
176+
XCTAssertEqual(list.node(at: 3).value, 102)
177+
XCTAssertEqual(list.node(at: 4).value, 10)
178178
}
179179

180180
func testInsertListAtFirstIndex() {
181181
let list = buildList()
182182
let list2 = LinkedList<Int>()
183183
list2.append(99)
184184
list2.append(102)
185-
list.insert(list2, atIndex: 0)
185+
list.insert(list2, at: 0)
186186
XCTAssertTrue(list.count == 8)
187-
XCTAssertEqual(list.node(atIndex: 0).value, 99)
188-
XCTAssertEqual(list.node(atIndex: 1).value, 102)
189-
XCTAssertEqual(list.node(atIndex: 2).value, 8)
187+
XCTAssertEqual(list.node(at: 0).value, 99)
188+
XCTAssertEqual(list.node(at: 1).value, 102)
189+
XCTAssertEqual(list.node(at: 2).value, 8)
190190
}
191191

192192
func testInsertListAtLastIndex() {
193193
let list = buildList()
194194
let list2 = LinkedList<Int>()
195195
list2.append(99)
196196
list2.append(102)
197-
list.insert(list2, atIndex: list.count)
197+
list.insert(list2, at: list.count)
198198
XCTAssertTrue(list.count == 8)
199-
XCTAssertEqual(list.node(atIndex: 5).value, 5)
200-
XCTAssertEqual(list.node(atIndex: 6).value, 99)
201-
XCTAssertEqual(list.node(atIndex: 7).value, 102)
199+
XCTAssertEqual(list.node(at: 5).value, 5)
200+
XCTAssertEqual(list.node(at: 6).value, 99)
201+
XCTAssertEqual(list.node(at: 7).value, 102)
202202
}
203203

204204
func testAppendList() {
@@ -208,9 +208,9 @@ class LinkedListTest: XCTestCase {
208208
list2.append(102)
209209
list.append(list2)
210210
XCTAssertTrue(list.count == 8)
211-
XCTAssertEqual(list.node(atIndex: 5).value, 5)
212-
XCTAssertEqual(list.node(atIndex: 6).value, 99)
213-
XCTAssertEqual(list.node(atIndex: 7).value, 102)
211+
XCTAssertEqual(list.node(at: 5).value, 5)
212+
XCTAssertEqual(list.node(at: 6).value, 99)
213+
XCTAssertEqual(list.node(at: 7).value, 102)
214214
}
215215

216216
func testAppendListToEmptyList() {
@@ -220,15 +220,15 @@ class LinkedListTest: XCTestCase {
220220
list2.append(10)
221221
list.append(list2)
222222
XCTAssertTrue(list.count == 2)
223-
XCTAssertEqual(list.node(atIndex: 0).value, 5)
224-
XCTAssertEqual(list.node(atIndex: 1).value, 10)
223+
XCTAssertEqual(list.node(at: 0).value, 5)
224+
XCTAssertEqual(list.node(at: 1).value, 10)
225225
}
226226

227227
func testRemoveAtIndexOnListWithOneElement() {
228228
let list = LinkedList<Int>()
229229
list.append(123)
230230

231-
let value = list.remove(atIndex: 0)
231+
let value = list.remove(at: 0)
232232
XCTAssertEqual(value, 123)
233233

234234
XCTAssertTrue(list.isEmpty)
@@ -239,16 +239,16 @@ class LinkedListTest: XCTestCase {
239239

240240
func testRemoveAtIndex() {
241241
let list = buildList()
242-
let prev = list.node(atIndex: 2)
243-
let next = list.node(atIndex: 3)
242+
let prev = list.node(at: 2)
243+
let next = list.node(at: 3)
244244
let nodeCount = list.count
245245

246-
list.insert(444, atIndex: 3)
246+
list.insert(444, at: 3)
247247

248-
let value = list.remove(atIndex: 3)
248+
let value = list.remove(at: 3)
249249
XCTAssertEqual(value, 444)
250250

251-
let node = list.node(atIndex: 3)
251+
let node = list.node(at: 3)
252252
XCTAssertTrue(next === node)
253253
XCTAssertTrue(prev.next === node)
254254
XCTAssertTrue(node.previous === prev)

0 commit comments

Comments
 (0)