Skip to content

Instantly share code, notes, and snippets.

struct NilMaxInteger<Base: FixedWidthInteger>: RawRepresentable, ExpressibleByNilLiteral {
var rawValue: Base
init(rawValue: Base) {
self.rawValue = rawValue
}
init(_ value: Base?) {
assert(value != .max)
self.init(rawValue: value ?? .max)
}
struct ResourceManager {
#if DEBUG
let ticket: Int = .random(in: 0..<Int.max)
#endif
private var retainCount: [Int]
init(capacity: Int) {
self.retainCount = .init(repeating: 0, count: capacity)
}
struct Handle: ~Copyable {
func triangularMatrixIndex(row: Int) -> Int {
row * (row + 1) / 2
}
func triangularMatrixCoordinate(_ index: Int) -> (row: Int, column: Int) {
let row = Int(sqrt(2 * Float32(index) + 0.25) - 0.5)
let column = index - triangularMatrixIndex(row: row)
return (row: row, column: column)
}
struct MergeSortedSequence<First: Sequence, Second: Sequence>: Sequence where First.Element == Second.Element {
var first: First
var second: Second
var areInIncreasingOrder: (Element, Element) -> Bool
struct Iterator: IteratorProtocol {
var first: First.Iterator
var second: Second.Iterator
var areInIncreasingOrder: (Element, Element) -> Bool
struct OffsetIndexCollection<Base: RandomAccessCollection>: RandomAccessCollection {
var base: Base
var distance: Int
@inlinable @inline(__always)
var startIndex: Base.Index {
index(base.startIndex, offsetBy: distance)
}
@inlinable @inline(__always)
extension Float32 {
public static func gaussianRandomPair(using generator: inout some RandomNumberGenerator) -> (Float32, Float32) {
let u1 = Float.random(in: 0..<1, using: &generator)
let u2 = Float.random(in: 0..<1, using: &generator)
let r = sqrt(-2 * log(u1))
let theta = 2 * Float.pi * u2
let z0 = r * cos(theta)
let z1 = r * sin(theta)
import Accelerate
public struct Vector512Float32: VectorProtocol, Hashable {
public typealias Scalar = Float32
public var components: [Scalar]
public init(components: [Scalar]) {
self.components = components
}
@JadenGeller
JadenGeller / PinnedCollection.swift
Last active March 11, 2024 05:11
PinnedCollection & ReindexedCollection
struct PinnedCollection<Base: RandomAccessCollection, Index: Strideable>: RandomAccessCollection where Index.Stride == Int {
var reindexedBase: ReindexedCollection<Base, Index>
@inlinable @inline(__always)
var startIndex: Index {
reindexedBase.startIndex
}
@inlinable @inline(__always)
var endIndex: Index {
@JadenGeller
JadenGeller / AdaptiveRangeBuffer.swift
Last active March 11, 2024 05:11
useful for buffering models for a scroll view!
// import https://gist.github.com/JadenGeller/66585051f8de54d9f2a3df1acfd87c32#file-pinnedcollection-swift
import DequeModule
struct AdaptiveRangeBuffer<Index: Strideable, Element>: RandomAccessCollection, MutableCollection where Index.Stride == Int {
var storage: PinnedCollection<Deque<Element>, Index>
var defaultValue: (Index) -> Element
var willRemoveRange: (Slice<Self>) -> Void
init(indices: Range<Index>, defaultValue: @escaping (Index) -> Element, willRemoveRange: @escaping (Slice<Self>) -> Void = { _ in }) {
self.defaultValue = defaultValue
@JadenGeller
JadenGeller / TemporarilyUnhiddenAssets.Swift
Created February 28, 2024 02:05
Helper to temporarily unhide assets (for working around bug in PhotoKit where cloud ID of hidden assets cannot be accessed)
extension PHPhotoLibrary {
// RADAR: https://feedbackassistant.apple.com/feedback/13660931
func withTemporarilyUnhiddenAssets<Success>(_ assets: [PHAsset], _ block: () async throws -> Success) async throws -> Success {
var collectionPlaceholder: PHObjectPlaceholder?
try await performChanges {
let collectionChange = PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: "Temporarily Unhidden Assets")
collectionPlaceholder = collectionChange.placeholderForCreatedAssetCollection
collectionChange.insertAssets(assets as NSArray, at: IndexSet((0...).prefix(assets.count)))
for asset in assets {
precondition(asset.isHidden)