Skip to content

Commit 0041be6

Browse files
committed
Remove internal initializer method that takes a node as a parameter to prevent misuse that could break value semantics.
1 parent be11f6b commit 0041be6

File tree

2 files changed

+4
-15
lines changed

2 files changed

+4
-15
lines changed

Singly Linked List/SinglyLinkedList.swift

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,13 @@ public struct SinglyLinkedList<T>
8383

8484
// MARK: INITIALIZERS
8585

86-
/// Creates a list with the given node.
87-
/// NOTE: This method can break value semantics by accepting a node.
88-
///
89-
/// - Parameter head: First node
90-
internal init(head: SinglyLinkedListNode<T>)
91-
{
92-
self.storage = IndirectStorage()
93-
self.append(node: head)
94-
}
95-
96-
9786
/// Creates a list with a single element
9887
///
9988
/// - Parameter value: element to populate the list with
10089
public init(value: T)
10190
{
102-
let node = SinglyLinkedListNode<T>(value: value)
103-
self.init(head: node)
91+
self.init()
92+
self.append(value: value)
10493
}
10594

10695

Singly Linked List/Tests/Tests/SinglyLinkedListTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class SinglyLinkedListTests: XCTestCase {
4747
}
4848

4949
func testDelete() {
50-
var list = SinglyLinkedList<Int>(head: SinglyLinkedListNode<Int>(value: 1))
50+
var list: SinglyLinkedList<Int> = [1]
5151
list.append(value: 2)
5252
list.append(value: 3)
5353
list.append(value: 4)
@@ -87,7 +87,7 @@ class SinglyLinkedListTests: XCTestCase {
8787
}
8888

8989
func testDeleteDuplicatesInPlace() {
90-
var list = SinglyLinkedList<Int>(head: SinglyLinkedListNode<Int>(value: 1))
90+
var list: SinglyLinkedList<Int> = [1]
9191
list.append(value: 2)
9292
list.append(value: 2)
9393
list.append(value: 3)

0 commit comments

Comments
 (0)