This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@propertyWrapper | |
struct CopyOnWrite<Object> { | |
var wrappedValue: Object { | |
get { storage.object } | |
set { | |
if !isKnownUniquelyReferenced(&storage) { | |
storage = storage.copy() | |
} | |
storage.object = newValue | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Matrix<Element> { | |
private var storage: [[Element]] | |
init(_ storage: [[Element]]) { | |
self.storage = storage | |
} | |
} | |
extension Matrix: Collection { | |
struct Index: Comparable { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class Node<Value>: Sequence { | |
typealias NextNode = Node<Value>? | |
let value: Value | |
var next: NextNode | |
init(value: Value, nextNode: NextNode = nil) { | |
self.value = value | |
self.next = nextNode | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
final class Threadsafe<Value> { | |
init(_ value: Value) { | |
self._value = value | |
} | |
var value: Value { | |
return queue.sync { return _value } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Sequence where Element: Comparable { | |
func isAlmostIncreasingSequence() -> Bool { | |
var foundMisplacedElement = false | |
for (a, b) in zip(self, self.dropFirst()) where a >= b { | |
guard !foundMisplacedElement else { return false } | |
foundMisplacedElement = true | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import Foundation; | |
@interface ThreadSafe<Value>: NSProxy | |
- (instancetype)initWithValue:(Value)value; | |
@property (nonatomic, strong, readonly) Value value; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Nagle's algorithm is a means of improving the efficiency of TCP/IP networks by reducing the number of packets that need to | |
// be sent over the network. | |
// https://en.wikipedia.org/wiki/Nagle%27s_algorithm | |
#import <netinet/in.h> | |
#import <netinet/tcp.h> | |
@implementation NSStream (DisableNaglesAlgorithm) | |
- (void)disableNaglesAlgorithmWithError:(NSError **)error { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class WeakBox<A: AnyObject> { | |
weak var unbox: A? | |
init(_ value: A) { | |
unbox = value | |
} | |
} | |
typealias Callback<T> = (T) -> () | |
final class Observation<T> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class ExecuteOnce { | |
typealias Work = () -> () | |
private var work: Work? | |
init(_ work: @escaping Work) { | |
self.work = work | |
} | |
func execute() { | |
work?() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
@objc public final class SingleThreadedOperationQueue: Thread { | |
public typealias Operation = () -> () | |
public func addOperation(_ operation: @escaping Operation) { | |
enqueueLock.lock() | |
defer { | |
enqueueLock.unlock() | |
} |
NewerOlder