@@ -65,17 +65,17 @@ public struct SinglyLinkedList<T>
65
65
private var storageForWritting : IndirectStorage < T > {
66
66
67
67
mutating get {
68
- if !isKnownUniquelyReferenced( & self . storage) {
69
- self . storage = self . copyStorage ( )
68
+ if !isKnownUniquelyReferenced( & storage) {
69
+ storage = copyStorage ( )
70
70
}
71
- return self . storage
71
+ return storage
72
72
}
73
73
}
74
74
75
75
/// Returns the last element in the collection
76
76
public var last : T ? {
77
77
get {
78
- return self . storage. tail? . value
78
+ return storage. tail? . value
79
79
}
80
80
}
81
81
@@ -108,7 +108,7 @@ public struct SinglyLinkedList<T>
108
108
public mutating func append( value: T )
109
109
{
110
110
let node = SinglyLinkedListNode < T > ( value: value)
111
- self . append ( node: node)
111
+ append ( node: node)
112
112
}
113
113
114
114
@@ -118,16 +118,16 @@ public struct SinglyLinkedList<T>
118
118
public mutating func prepend( value: T )
119
119
{
120
120
let node = SinglyLinkedListNode < T > ( value: value)
121
- self . prepend ( node: node)
121
+ prepend ( node: node)
122
122
}
123
123
124
124
125
125
public mutating func deleteItem( at index: Int ) -> T
126
126
{
127
- precondition ( ( index >= 0 ) && ( index < self . count) )
127
+ precondition ( ( index >= 0 ) && ( index < count) )
128
128
129
129
var previous : SinglyLinkedListNode < T > ? = nil
130
- var current = self . storageForWritting. head
130
+ var current = storageForWritting. head
131
131
var i = 0
132
132
var elementToDelete : SinglyLinkedListNode < T >
133
133
@@ -139,12 +139,12 @@ public struct SinglyLinkedList<T>
139
139
140
140
// Current is now the element to delete (at index position.tag)
141
141
elementToDelete = current!
142
- if ( self . storage. head === current) {
143
- self . storageForWritting. head = current? . next
142
+ if ( storage. head === current) {
143
+ storageForWritting. head = current? . next
144
144
}
145
145
146
- if ( self . storage. tail === current) {
147
- self . storageForWritting. tail = previous
146
+ if ( storage. tail === current) {
147
+ storageForWritting. tail = previous
148
148
}
149
149
150
150
previous? . next = current? . next
@@ -184,7 +184,7 @@ public struct SinglyLinkedList<T>
184
184
/// - Parameter kthToLast: Reversed ordinal number of the node to fetch.
185
185
public func find( kthToLast: UInt ) -> SinglyLinkedListNode < T > ?
186
186
{
187
- return self . find ( kthToLast: kthToLast, startingAt: self . storage. head, count: UInt ( self . count) )
187
+ return find ( kthToLast: kthToLast, startingAt: storage. head, count: UInt ( count) )
188
188
}
189
189
190
190
}
@@ -202,11 +202,11 @@ private extension SinglyLinkedList {
202
202
private mutating func prepend( node: SinglyLinkedListNode < T > )
203
203
{
204
204
let ( tailFromNewNode, _) = findTail ( in: node)
205
- tailFromNewNode. next = self . storageForWritting. head
206
- self . storageForWritting. head = node
205
+ tailFromNewNode. next = storageForWritting. head
206
+ storageForWritting. head = node
207
207
208
- if self . storage. tail == nil {
209
- self . storageForWritting. tail = tailFromNewNode
208
+ if storage. tail == nil {
209
+ storageForWritting. tail = tailFromNewNode
210
210
}
211
211
}
212
212
@@ -217,22 +217,22 @@ private extension SinglyLinkedList {
217
217
/// - Parameter node: node to be appended. (It can be a list, even contain loops).
218
218
private mutating func append( node: SinglyLinkedListNode < T > )
219
219
{
220
- if self . storage. tail != nil
220
+ if storage. tail != nil
221
221
{
222
222
// Copy on write: we are about to modify next a property in
223
223
// a potentially shared node. Make sure it's new if necessary.
224
- self . storageForWritting. tail? . next = node
224
+ storageForWritting. tail? . next = node
225
225
let ( tail, _) = findTail ( in: node)
226
- self . storageForWritting. tail = tail // There
226
+ storageForWritting. tail = tail // There
227
227
}
228
228
else
229
229
{
230
230
// This also means that there's no head.
231
231
// Otherwise the state would be inconsistent.
232
232
// This will be checked when adding and deleting nodes.
233
- self . storageForWritting. head = node
233
+ storageForWritting. head = node
234
234
let ( tail, _) = findTail ( in: node)
235
- self . storageForWritting. tail = tail // There
235
+ storageForWritting. tail = tail // There
236
236
}
237
237
}
238
238
@@ -243,18 +243,18 @@ private extension SinglyLinkedList {
243
243
// If the list is empty, next time an item will be created, it won't affect
244
244
// other instances of the list that came from copies derived from value types.
245
245
// like assignments or parameters
246
- guard ( self . storage. head != nil ) && ( self . storage. tail != nil ) else {
246
+ guard ( storage. head != nil ) && ( storage. tail != nil ) else {
247
247
return IndirectStorage ( head: nil , tail: nil )
248
248
}
249
249
250
250
// Create a new position in memory.
251
251
// Note that we are shallow copying the value. If it was reference type
252
252
// we just make a copy of the reference.
253
- let copiedHead = SinglyLinkedListNode < T > ( value: self . storage. head!. value)
253
+ let copiedHead = SinglyLinkedListNode < T > ( value: storage. head!. value)
254
254
var previousCopied : SinglyLinkedListNode < T > = copiedHead
255
255
256
256
// Iterate through current list of nodes and copy them.
257
- var current : SinglyLinkedListNode < T > ? = self . storage. head? . next
257
+ var current : SinglyLinkedListNode < T > ? = storage. head? . next
258
258
259
259
while ( current != nil ) {
260
260
// Create a copy
@@ -283,12 +283,12 @@ extension SinglyLinkedList where T: Comparable
283
283
/// - Parameter v: value of the node to be deleted.
284
284
public mutating func deleteNode( withValue v: T ) {
285
285
286
- guard self . storage. head != nil else {
286
+ guard storage. head != nil else {
287
287
return
288
288
}
289
289
290
290
var previous : SinglyLinkedListNode < T > ? = nil
291
- var current = self . storage. head
291
+ var current = storage. head
292
292
293
293
while ( current != nil ) && ( current? . value != v) {
294
294
previous = current
@@ -297,12 +297,12 @@ extension SinglyLinkedList where T: Comparable
297
297
298
298
if let foundNode = current {
299
299
300
- if ( self . storage. head === foundNode) {
301
- self . storageForWritting. head = foundNode. next
300
+ if ( storage. head === foundNode) {
301
+ storageForWritting. head = foundNode. next
302
302
}
303
303
304
- if ( self . storage. tail === foundNode) {
305
- self . storage. tail = previous
304
+ if ( storage. tail === foundNode) {
305
+ storage. tail = previous
306
306
}
307
307
308
308
previous? . next = foundNode. next
@@ -315,8 +315,8 @@ extension SinglyLinkedList where T: Comparable
315
315
/// - Complexity: O(N^2)
316
316
public mutating func deleteDuplicatesInPlace( )
317
317
{
318
- // Copy on write: this updates self. storage if necessary.
319
- var current = self . storageForWritting. head
318
+ // Copy on write: this updates storage if necessary.
319
+ var current = storageForWritting. head
320
320
321
321
while ( current != nil )
322
322
{
@@ -327,12 +327,12 @@ extension SinglyLinkedList where T: Comparable
327
327
{
328
328
if ( current? . value == next? . value) {
329
329
330
- if ( self . storage. head === next) {
331
- self . storage. head = next? . next
330
+ if ( storage. head === next) {
331
+ storage. head = next? . next
332
332
}
333
333
334
- if ( self . storage. tail === next) {
335
- self . storage. tail = previous
334
+ if ( storage. tail === next) {
335
+ storage. tail = previous
336
336
}
337
337
338
338
// Delete next
@@ -358,8 +358,8 @@ public struct SinglyLinkedListForwardIterator<T> : IteratorProtocol {
358
358
359
359
mutating public func next( ) -> T ?
360
360
{
361
- let result = self . head? . value
362
- self . head = self . head? . next
361
+ let result = head? . value
362
+ head = head? . next
363
363
return result
364
364
}
365
365
}
@@ -372,7 +372,7 @@ extension SinglyLinkedList : Sequence
372
372
{
373
373
public func makeIterator( ) -> SinglyLinkedListForwardIterator < T >
374
374
{
375
- return SinglyLinkedListForwardIterator ( head: self . storage. head)
375
+ return SinglyLinkedListForwardIterator ( head: storage. head)
376
376
}
377
377
}
378
378
@@ -386,17 +386,17 @@ extension SinglyLinkedList : Collection {
386
386
387
387
public var startIndex : Index {
388
388
get {
389
- return SinglyLinkedListIndex < T > ( node: self . storage. head, tag: 0 )
389
+ return SinglyLinkedListIndex < T > ( node: storage. head, tag: 0 )
390
390
}
391
391
}
392
392
393
393
public var endIndex : Index {
394
394
get {
395
- if let h = self . storage. head {
395
+ if let h = storage. head {
396
396
let ( _, numberOfElements) = findTail ( in: h)
397
397
return SinglyLinkedListIndex < T > ( node: h, tag: numberOfElements)
398
398
} else {
399
- return SinglyLinkedListIndex < T > ( node: nil , tag: self . startIndex. tag)
399
+ return SinglyLinkedListIndex < T > ( node: nil , tag: startIndex. tag)
400
400
}
401
401
}
402
402
}
@@ -425,23 +425,23 @@ extension SinglyLinkedList : ExpressibleByArrayLiteral
425
425
var headSet = false
426
426
var current : SinglyLinkedListNode < T > ?
427
427
var numberOfElements = 0
428
- self . storage = IndirectStorage ( )
428
+ storage = IndirectStorage ( )
429
429
430
430
for element in elements {
431
431
432
432
numberOfElements += 1
433
433
434
434
if headSet == false {
435
- self . storage. head = SinglyLinkedListNode < T > ( value: element)
436
- current = self . storage. head
435
+ storage. head = SinglyLinkedListNode < T > ( value: element)
436
+ current = storage. head
437
437
headSet = true
438
438
} else {
439
439
let newNode = SinglyLinkedListNode < T > ( value: element)
440
440
current? . next = newNode
441
441
current = newNode
442
442
}
443
443
}
444
- self . storage. tail = current
444
+ storage. tail = current
445
445
}
446
446
}
447
447
@@ -467,7 +467,7 @@ public struct SinglyLinkedListIndex<T> : Comparable
467
467
extension SinglyLinkedList where T : KeyValuePair {
468
468
469
469
public func find( elementWithKey key: T . K ) -> T ? {
470
- let searchResults = self . filter { ( keyValuePair) -> Bool in
470
+ let searchResults = filter { ( keyValuePair) -> Bool in
471
471
return keyValuePair. key == key
472
472
}
473
473
0 commit comments