Skip to content

Commit 77b8358

Browse files
committed
Implement swapAt for better sorting performance
1 parent b7e0002 commit 77b8358

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Sources/GateEngine/Resources/Geometry/Raw/RawGeometry.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,11 @@ extension RawGeometry.VertexView: RandomAccessCollection, MutableCollection, Ran
509509
}
510510
}
511511

512+
public mutating func swapAt(_ i: Int, _ j: Int) {
513+
// Swap only the vertexIndicies value for performance
514+
self.vertexIndicies.swapAt(i, j)
515+
}
516+
512517
public subscript (index: Index) -> Element {
513518
nonmutating get {
514519
assert(self.indices.contains(index), "Index \(index) out of range \(self.indices)")
@@ -614,6 +619,17 @@ extension RawGeometry: RandomAccessCollection, MutableCollection, RangeReplaceab
614619
return Triangle(v1: v1, v2: v2, v3: v3, repairIfNeeded: false)
615620
}
616621

622+
@inlinable
623+
public mutating func swapAt(_ i: Index, _ j: Index) {
624+
let baseIndexI = i * 3
625+
let baseIndexJ = j * 3
626+
627+
self.vertices.swapAt(baseIndexI + 0, baseIndexJ + 0)
628+
self.vertices.swapAt(baseIndexI + 1, baseIndexJ + 1)
629+
self.vertices.swapAt(baseIndexI + 2, baseIndexJ + 2)
630+
}
631+
632+
@inlinable
617633
public subscript (index: Index) -> Element {
618634
get {
619635
assert(self.indices.contains(index), "Index \(index) out of range \(self.indices)")

0 commit comments

Comments
 (0)