Skip to content

Commit c538e27

Browse files
committed
Fix memory issue
1 parent b783c28 commit c538e27

File tree

5 files changed

+40
-28
lines changed

5 files changed

+40
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ FortuneSwift is a swift framework that uses Fortune's Algorithm to generate Voro
1616
The FortuneSwift framework can be installed using the Swift Package Manager. In Xcode: `File > Swift Packages > Add Package Dependency..` with URL: `https://github.com/TateLiang/FortuneSwift.git`
1717

1818
Or alternatively:
19-
- Add `.package(url: "https://github.com/TateLiang/FortuneSwift.git", from: "1.1.5")` to your `Package.swift` file's `dependencies`.
19+
- Add `.package(url: "https://github.com/TateLiang/FortuneSwift.git", from: "1.1.6")` to your `Package.swift` file's `dependencies`.
2020
- Update your packages using `$ swift package update`.
2121

2222
## Usage

Sources/FortuneSwift/BeachLine/BeachNode.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class BeachNode {
8383

8484

8585

86-
//MARK: - Setup Values
86+
//MARK: - Setup Values
8787

8888
init(_ value: Data) {
8989
self.data = value
@@ -107,7 +107,7 @@ class BeachNode {
107107
This also means that the newNode will not be up to date with the tree, use the root directly in this case.
108108
- Parameter newNode: A reference to the node which will replace this one.
109109
*/
110-
func replace(with newNode: inout BeachNode) {
110+
func replace(with newNode: BeachNode) {
111111
if parent != nil { //we are not a root
112112
newNode.parent = parent
113113

Sources/FortuneSwift/Events/Event.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Foundation
1414
enum Event: Comparable, Equatable {
1515

1616
case site(Coordinate)
17-
case circle(Coordinate, center: Coordinate, parabola: BeachNode)
17+
case circle(Coordinate, center: Coordinate, parabola: Weak<BeachNode>)
1818

1919
var coordinate: Coordinate {
2020
switch self {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Tate on 2020-08-14.
6+
//
7+
8+
import Foundation
9+
10+
/**
11+
Weak object wrapper.
12+
- Used to avoid retain cycles in enum associated values.
13+
*/
14+
struct Weak<T: AnyObject> {
15+
weak var value: T?
16+
init(_ value: T?) { self.value = value }
17+
}

Sources/FortuneSwift/Fortune.swift

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct Fortune {
4141
case .site(let coordinate):
4242
handleSiteEvent(at: coordinate)
4343
case .circle(let coordinate, let center, let parabola):
44-
handleCircleEvent(at: coordinate, center: center, parabola: parabola)
44+
handleCircleEvent(at: coordinate, center: center, parabola: parabola.value!)
4545
}
4646
}
4747

@@ -52,6 +52,7 @@ struct Fortune {
5252
vertices = updatedValues.vertices
5353
}
5454

55+
beachLine = nil
5556
return (self.sites, vertices, edges)
5657
}
5758

@@ -86,7 +87,7 @@ struct Fortune {
8687
let newPoint = site
8788
let oldPoint = focus
8889

89-
var leftBreakpoint: BeachNode = BeachNode(.breakpoint(sites: (oldPoint, newPoint), edge: nil))
90+
let leftBreakpoint: BeachNode = BeachNode(.breakpoint(sites: (oldPoint, newPoint), edge: nil))
9091
let rightBreakpoint: BeachNode = BeachNode(.breakpoint(sites: (newPoint, oldPoint), edge: nil))
9192

9293
let leftLeaf: BeachNode = BeachNode(.parabola(site: oldPoint, circleEvent: nil))
@@ -121,7 +122,7 @@ struct Fortune {
121122
*/
122123

123124
//replace node with new node
124-
parabolaAboveSite.replace(with: &leftBreakpoint)
125+
parabolaAboveSite.replace(with: leftBreakpoint)
125126

126127
if let leftCousin = leftLeaf.predecessor() {
127128
checkCircleEvent(leftCousin, leftLeaf, middleLeaf)
@@ -178,21 +179,18 @@ struct Fortune {
178179
C⬆ <- newEdge1
179180
C⬇ <- newEdge2
180181
*/
181-
if let deleted = updates.deleted,
182-
let updated = updates.updated,
182+
if let updated = updates.updated,
183183
let left = updates.left,
184184
let right = updates.right {
185185

186-
guard let deletedBreakpointEdge = deleted.data.breakpointEdge else { return }
187-
guard let updatedBreakpointEdge = updated.data.breakpointEdge else { return }
188186
guard let leftBreakpointEdge = left.data.breakpointEdge else { return }
189187
guard let rightBreakpointEdge = right.data.breakpointEdge else { return }
190188

191189
//finalizing the old edges' origins
192-
deletedBreakpointEdge.breakpoint = nil
193-
deletedBreakpointEdge.origin = vertex
194-
updatedBreakpointEdge.breakpoint = nil
195-
updatedBreakpointEdge.origin = vertex
190+
leftBreakpointEdge.breakpoint = nil
191+
leftBreakpointEdge.origin = vertex
192+
rightBreakpointEdge.breakpoint = nil
193+
rightBreakpointEdge.origin = vertex
196194

197195
//creating a new edge
198196
let leftPoint = updated.data.breakpointSites!.0
@@ -214,8 +212,8 @@ struct Fortune {
214212
newEdge1.setNext(rightBreakpointEdge)
215213

216214
//adding incidentedges
217-
vertex.incidentEdges.append(updatedBreakpointEdge)
218-
vertex.incidentEdges.append(deletedBreakpointEdge)
215+
vertex.incidentEdges.append(leftBreakpointEdge)
216+
vertex.incidentEdges.append(rightBreakpointEdge)
219217
vertex.incidentEdges.append(newEdge2)
220218

221219
//update new breakpoint Edge
@@ -242,9 +240,8 @@ struct Fortune {
242240
- Returns: The two breakpoints classified into deleted, updated, left and right.
243241
*/
244242
private func deleteParabola(_ parabola: BeachNode, pred: BeachNode, succ: BeachNode, sweepLine: Double) ->
245-
(deleted: BeachNode?, updated: BeachNode?, left: BeachNode?, right: BeachNode?)? {
243+
(updated: BeachNode?, left: BeachNode?, right: BeachNode?)? {
246244

247-
var deleted: BeachNode?
248245
var updated: BeachNode?
249246
var left: BeachNode?
250247
var right: BeachNode?
@@ -262,37 +259,35 @@ struct Fortune {
262259
PRED SUCC
263260
*/
264261
if parabola.isLeftChild() { //implies parent = the breakpoint on the right
265-
guard var rightParabola = parabola.parent?.rightChild else { return nil }
266-
parabola.parent?.replace(with: &rightParabola)
262+
guard let rightParabola = parabola.parent?.rightChild else { return nil }
263+
parabola.parent?.replace(with: rightParabola)
267264

268265
//finding the other breakpoint
269266
guard let leftBreakpoint = beachLine?.getBreakpointNode(between: (pred, parabola), sweepLine: sweepLine) else { return nil }
270267
guard let successorSite = succ.data.parabolaSite else { return nil }
271268
leftBreakpoint.data.updateBreakpointSites(right: successorSite)
272269

273270
//assigning to the return variables
274-
deleted = parabola.parent
275271
updated = leftBreakpoint
276272
left = leftBreakpoint
277273
right = parabola.parent
278274

279275
}else if parabola.isRightChild() { //implies parent = breakpoint on the left
280-
guard var leftParabola = parabola.parent?.leftChild else { return nil }
281-
parabola.parent?.replace(with: &leftParabola)
276+
guard let leftParabola = parabola.parent?.leftChild else { return nil }
277+
parabola.parent?.replace(with: leftParabola)
282278

283279
//finding the other breakpoint
284280
guard let rightBreakpoint = beachLine?.getBreakpointNode(between: (parabola, succ), sweepLine: sweepLine) else { return nil }
285281
guard let predecessorSite = pred.data.parabolaSite else { return nil }
286282
rightBreakpoint.data.updateBreakpointSites(left: predecessorSite)
287283

288284
//assigning to the return variables
289-
deleted = parabola.parent
290285
updated = rightBreakpoint
291286
left = parabola.parent
292287
right = rightBreakpoint
293288
}
294-
295-
return (deleted: deleted, updated: updated, left: left, right: right)
289+
290+
return (updated: updated, left: left, right: right)
296291
}
297292

298293

@@ -312,7 +307,7 @@ struct Fortune {
312307
//check counterclockwise since we are using a plane where y increases downward (clockwise if not)
313308
//^^^ therefore this reverses with the angle calculation which uses unit circle
314309
if !CircleGeometry.checkClockwise(a, b, c, center: circle.center) {
315-
let circleEvent: Event = .circle(circle.eventPoint, center: circle.center, parabola: bNode)
310+
let circleEvent: Event = .circle(circle.eventPoint, center: circle.center, parabola: Weak<BeachNode>(bNode))
316311
bNode.data.parabolaCircleEvent = circleEvent
317312
eventQueue.enqueue(circleEvent)
318313
}

0 commit comments

Comments
 (0)