initial
This commit is contained in:
71
Pods/RxSwift/Platform/AtomicInt.swift
generated
Normal file
71
Pods/RxSwift/Platform/AtomicInt.swift
generated
Normal file
@@ -0,0 +1,71 @@
|
||||
//
|
||||
// AtomicInt.swift
|
||||
// Platform
|
||||
//
|
||||
// Created by Krunoslav Zaher on 10/28/18.
|
||||
// Copyright © 2018 Krunoslav Zaher. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
final class AtomicInt: NSLock {
|
||||
fileprivate var value: Int32
|
||||
public init(_ value: Int32 = 0) {
|
||||
self.value = value
|
||||
}
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
@inline(__always)
|
||||
func add(_ this: AtomicInt, _ value: Int32) -> Int32 {
|
||||
this.lock()
|
||||
let oldValue = this.value
|
||||
this.value += value
|
||||
this.unlock()
|
||||
return oldValue
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
@inline(__always)
|
||||
func sub(_ this: AtomicInt, _ value: Int32) -> Int32 {
|
||||
this.lock()
|
||||
let oldValue = this.value
|
||||
this.value -= value
|
||||
this.unlock()
|
||||
return oldValue
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
@inline(__always)
|
||||
func fetchOr(_ this: AtomicInt, _ mask: Int32) -> Int32 {
|
||||
this.lock()
|
||||
let oldValue = this.value
|
||||
this.value |= mask
|
||||
this.unlock()
|
||||
return oldValue
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
func load(_ this: AtomicInt) -> Int32 {
|
||||
this.lock()
|
||||
let oldValue = this.value
|
||||
this.unlock()
|
||||
return oldValue
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
@inline(__always)
|
||||
func increment(_ this: AtomicInt) -> Int32 {
|
||||
add(this, 1)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
@inline(__always)
|
||||
func decrement(_ this: AtomicInt) -> Int32 {
|
||||
sub(this, 1)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
func isFlagSet(_ this: AtomicInt, _ mask: Int32) -> Bool {
|
||||
(load(this) & mask) != 0
|
||||
}
|
||||
181
Pods/RxSwift/Platform/DataStructures/Bag.swift
generated
Normal file
181
Pods/RxSwift/Platform/DataStructures/Bag.swift
generated
Normal file
@@ -0,0 +1,181 @@
|
||||
//
|
||||
// Bag.swift
|
||||
// Platform
|
||||
//
|
||||
// Created by Krunoslav Zaher on 2/28/15.
|
||||
// Copyright © 2015 Krunoslav Zaher. All rights reserved.
|
||||
//
|
||||
|
||||
import Swift
|
||||
|
||||
let arrayDictionaryMaxSize = 30
|
||||
|
||||
struct BagKey {
|
||||
/**
|
||||
Unique identifier for object added to `Bag`.
|
||||
|
||||
It's underlying type is UInt64. If we assume there in an idealized CPU that works at 4GHz,
|
||||
it would take ~150 years of continuous running time for it to overflow.
|
||||
*/
|
||||
fileprivate let rawValue: UInt64
|
||||
}
|
||||
|
||||
/**
|
||||
Data structure that represents a bag of elements typed `T`.
|
||||
|
||||
Single element can be stored multiple times.
|
||||
|
||||
Time and space complexity of insertion and deletion is O(n).
|
||||
|
||||
It is suitable for storing small number of elements.
|
||||
*/
|
||||
struct Bag<T> : CustomDebugStringConvertible {
|
||||
/// Type of identifier for inserted elements.
|
||||
typealias KeyType = BagKey
|
||||
|
||||
typealias Entry = (key: BagKey, value: T)
|
||||
|
||||
private var _nextKey: BagKey = BagKey(rawValue: 0)
|
||||
|
||||
// data
|
||||
|
||||
// first fill inline variables
|
||||
var _key0: BagKey?
|
||||
var _value0: T?
|
||||
|
||||
// then fill "array dictionary"
|
||||
var _pairs = ContiguousArray<Entry>()
|
||||
|
||||
// last is sparse dictionary
|
||||
var _dictionary: [BagKey: T]?
|
||||
|
||||
var _onlyFastPath = true
|
||||
|
||||
/// Creates new empty `Bag`.
|
||||
init() {
|
||||
}
|
||||
|
||||
/**
|
||||
Inserts `value` into bag.
|
||||
|
||||
- parameter element: Element to insert.
|
||||
- returns: Key that can be used to remove element from bag.
|
||||
*/
|
||||
mutating func insert(_ element: T) -> BagKey {
|
||||
let key = _nextKey
|
||||
|
||||
_nextKey = BagKey(rawValue: _nextKey.rawValue &+ 1)
|
||||
|
||||
if _key0 == nil {
|
||||
_key0 = key
|
||||
_value0 = element
|
||||
return key
|
||||
}
|
||||
|
||||
_onlyFastPath = false
|
||||
|
||||
if _dictionary != nil {
|
||||
_dictionary![key] = element
|
||||
return key
|
||||
}
|
||||
|
||||
if _pairs.count < arrayDictionaryMaxSize {
|
||||
_pairs.append((key: key, value: element))
|
||||
return key
|
||||
}
|
||||
|
||||
_dictionary = [key: element]
|
||||
|
||||
return key
|
||||
}
|
||||
|
||||
/// - returns: Number of elements in bag.
|
||||
var count: Int {
|
||||
let dictionaryCount: Int = _dictionary?.count ?? 0
|
||||
return (_value0 != nil ? 1 : 0) + _pairs.count + dictionaryCount
|
||||
}
|
||||
|
||||
/// Removes all elements from bag and clears capacity.
|
||||
mutating func removeAll() {
|
||||
_key0 = nil
|
||||
_value0 = nil
|
||||
|
||||
_pairs.removeAll(keepingCapacity: false)
|
||||
_dictionary?.removeAll(keepingCapacity: false)
|
||||
}
|
||||
|
||||
/**
|
||||
Removes element with a specific `key` from bag.
|
||||
|
||||
- parameter key: Key that identifies element to remove from bag.
|
||||
- returns: Element that bag contained, or nil in case element was already removed.
|
||||
*/
|
||||
mutating func removeKey(_ key: BagKey) -> T? {
|
||||
if _key0 == key {
|
||||
_key0 = nil
|
||||
let value = _value0!
|
||||
_value0 = nil
|
||||
return value
|
||||
}
|
||||
|
||||
if let existingObject = _dictionary?.removeValue(forKey: key) {
|
||||
return existingObject
|
||||
}
|
||||
|
||||
for i in 0 ..< _pairs.count where _pairs[i].key == key {
|
||||
let value = _pairs[i].value
|
||||
_pairs.remove(at: i)
|
||||
return value
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
extension Bag {
|
||||
/// A textual representation of `self`, suitable for debugging.
|
||||
var debugDescription : String {
|
||||
"\(self.count) elements in Bag"
|
||||
}
|
||||
}
|
||||
|
||||
extension Bag {
|
||||
/// Enumerates elements inside the bag.
|
||||
///
|
||||
/// - parameter action: Enumeration closure.
|
||||
func forEach(_ action: (T) -> Void) {
|
||||
if _onlyFastPath {
|
||||
if let value0 = _value0 {
|
||||
action(value0)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
let value0 = _value0
|
||||
let dictionary = _dictionary
|
||||
|
||||
if let value0 = value0 {
|
||||
action(value0)
|
||||
}
|
||||
|
||||
for i in 0 ..< _pairs.count {
|
||||
action(_pairs[i].value)
|
||||
}
|
||||
|
||||
if dictionary?.count ?? 0 > 0 {
|
||||
for element in dictionary!.values {
|
||||
action(element)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension BagKey: Hashable {
|
||||
func hash(into hasher: inout Hasher) {
|
||||
hasher.combine(rawValue)
|
||||
}
|
||||
}
|
||||
|
||||
func ==(lhs: BagKey, rhs: BagKey) -> Bool {
|
||||
lhs.rawValue == rhs.rawValue
|
||||
}
|
||||
23
Pods/RxSwift/Platform/DataStructures/InfiniteSequence.swift
generated
Normal file
23
Pods/RxSwift/Platform/DataStructures/InfiniteSequence.swift
generated
Normal file
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// InfiniteSequence.swift
|
||||
// Platform
|
||||
//
|
||||
// Created by Krunoslav Zaher on 6/13/15.
|
||||
// Copyright © 2015 Krunoslav Zaher. All rights reserved.
|
||||
//
|
||||
|
||||
/// Sequence that repeats `repeatedValue` infinite number of times.
|
||||
struct InfiniteSequence<Element> : Sequence {
|
||||
typealias Iterator = AnyIterator<Element>
|
||||
|
||||
private let repeatedValue: Element
|
||||
|
||||
init(repeatedValue: Element) {
|
||||
self.repeatedValue = repeatedValue
|
||||
}
|
||||
|
||||
func makeIterator() -> Iterator {
|
||||
let repeatedValue = self.repeatedValue
|
||||
return AnyIterator { repeatedValue }
|
||||
}
|
||||
}
|
||||
111
Pods/RxSwift/Platform/DataStructures/PriorityQueue.swift
generated
Normal file
111
Pods/RxSwift/Platform/DataStructures/PriorityQueue.swift
generated
Normal file
@@ -0,0 +1,111 @@
|
||||
//
|
||||
// PriorityQueue.swift
|
||||
// Platform
|
||||
//
|
||||
// Created by Krunoslav Zaher on 12/27/15.
|
||||
// Copyright © 2015 Krunoslav Zaher. All rights reserved.
|
||||
//
|
||||
|
||||
struct PriorityQueue<Element> {
|
||||
private let hasHigherPriority: (Element, Element) -> Bool
|
||||
private let isEqual: (Element, Element) -> Bool
|
||||
|
||||
private var elements = [Element]()
|
||||
|
||||
init(hasHigherPriority: @escaping (Element, Element) -> Bool, isEqual: @escaping (Element, Element) -> Bool) {
|
||||
self.hasHigherPriority = hasHigherPriority
|
||||
self.isEqual = isEqual
|
||||
}
|
||||
|
||||
mutating func enqueue(_ element: Element) {
|
||||
elements.append(element)
|
||||
bubbleToHigherPriority(elements.count - 1)
|
||||
}
|
||||
|
||||
func peek() -> Element? {
|
||||
elements.first
|
||||
}
|
||||
|
||||
var isEmpty: Bool {
|
||||
elements.count == 0
|
||||
}
|
||||
|
||||
mutating func dequeue() -> Element? {
|
||||
guard let front = peek() else {
|
||||
return nil
|
||||
}
|
||||
|
||||
removeAt(0)
|
||||
|
||||
return front
|
||||
}
|
||||
|
||||
mutating func remove(_ element: Element) {
|
||||
for i in 0 ..< elements.count {
|
||||
if self.isEqual(elements[i], element) {
|
||||
removeAt(i)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private mutating func removeAt(_ index: Int) {
|
||||
let removingLast = index == elements.count - 1
|
||||
if !removingLast {
|
||||
elements.swapAt(index, elements.count - 1)
|
||||
}
|
||||
|
||||
_ = elements.popLast()
|
||||
|
||||
if !removingLast {
|
||||
bubbleToHigherPriority(index)
|
||||
bubbleToLowerPriority(index)
|
||||
}
|
||||
}
|
||||
|
||||
private mutating func bubbleToHigherPriority(_ initialUnbalancedIndex: Int) {
|
||||
precondition(initialUnbalancedIndex >= 0)
|
||||
precondition(initialUnbalancedIndex < elements.count)
|
||||
|
||||
var unbalancedIndex = initialUnbalancedIndex
|
||||
|
||||
while unbalancedIndex > 0 {
|
||||
let parentIndex = (unbalancedIndex - 1) / 2
|
||||
guard self.hasHigherPriority(elements[unbalancedIndex], elements[parentIndex]) else { break }
|
||||
elements.swapAt(unbalancedIndex, parentIndex)
|
||||
unbalancedIndex = parentIndex
|
||||
}
|
||||
}
|
||||
|
||||
private mutating func bubbleToLowerPriority(_ initialUnbalancedIndex: Int) {
|
||||
precondition(initialUnbalancedIndex >= 0)
|
||||
precondition(initialUnbalancedIndex < elements.count)
|
||||
|
||||
var unbalancedIndex = initialUnbalancedIndex
|
||||
while true {
|
||||
let leftChildIndex = unbalancedIndex * 2 + 1
|
||||
let rightChildIndex = unbalancedIndex * 2 + 2
|
||||
|
||||
var highestPriorityIndex = unbalancedIndex
|
||||
|
||||
if leftChildIndex < elements.count && self.hasHigherPriority(elements[leftChildIndex], elements[highestPriorityIndex]) {
|
||||
highestPriorityIndex = leftChildIndex
|
||||
}
|
||||
|
||||
if rightChildIndex < elements.count && self.hasHigherPriority(elements[rightChildIndex], elements[highestPriorityIndex]) {
|
||||
highestPriorityIndex = rightChildIndex
|
||||
}
|
||||
|
||||
guard highestPriorityIndex != unbalancedIndex else { break }
|
||||
elements.swapAt(highestPriorityIndex, unbalancedIndex)
|
||||
|
||||
unbalancedIndex = highestPriorityIndex
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension PriorityQueue : CustomDebugStringConvertible {
|
||||
var debugDescription: String {
|
||||
elements.debugDescription
|
||||
}
|
||||
}
|
||||
148
Pods/RxSwift/Platform/DataStructures/Queue.swift
generated
Normal file
148
Pods/RxSwift/Platform/DataStructures/Queue.swift
generated
Normal file
@@ -0,0 +1,148 @@
|
||||
//
|
||||
// Queue.swift
|
||||
// Platform
|
||||
//
|
||||
// Created by Krunoslav Zaher on 3/21/15.
|
||||
// Copyright © 2015 Krunoslav Zaher. All rights reserved.
|
||||
//
|
||||
|
||||
/**
|
||||
Data structure that represents queue.
|
||||
|
||||
Complexity of `enqueue`, `dequeue` is O(1) when number of operations is
|
||||
averaged over N operations.
|
||||
|
||||
Complexity of `peek` is O(1).
|
||||
*/
|
||||
struct Queue<T>: Sequence {
|
||||
/// Type of generator.
|
||||
typealias Generator = AnyIterator<T>
|
||||
|
||||
private let resizeFactor = 2
|
||||
|
||||
private var storage: ContiguousArray<T?>
|
||||
private var innerCount = 0
|
||||
private var pushNextIndex = 0
|
||||
private let initialCapacity: Int
|
||||
|
||||
/**
|
||||
Creates new queue.
|
||||
|
||||
- parameter capacity: Capacity of newly created queue.
|
||||
*/
|
||||
init(capacity: Int) {
|
||||
initialCapacity = capacity
|
||||
|
||||
storage = ContiguousArray<T?>(repeating: nil, count: capacity)
|
||||
}
|
||||
|
||||
private var dequeueIndex: Int {
|
||||
let index = pushNextIndex - count
|
||||
return index < 0 ? index + storage.count : index
|
||||
}
|
||||
|
||||
/// - returns: Is queue empty.
|
||||
var isEmpty: Bool { count == 0 }
|
||||
|
||||
/// - returns: Number of elements inside queue.
|
||||
var count: Int { innerCount }
|
||||
|
||||
/// - returns: Element in front of a list of elements to `dequeue`.
|
||||
func peek() -> T {
|
||||
precondition(count > 0)
|
||||
|
||||
return storage[dequeueIndex]!
|
||||
}
|
||||
|
||||
mutating private func resizeTo(_ size: Int) {
|
||||
var newStorage = ContiguousArray<T?>(repeating: nil, count: size)
|
||||
|
||||
let count = self.count
|
||||
|
||||
let dequeueIndex = self.dequeueIndex
|
||||
let spaceToEndOfQueue = storage.count - dequeueIndex
|
||||
|
||||
// first batch is from dequeue index to end of array
|
||||
let countElementsInFirstBatch = Swift.min(count, spaceToEndOfQueue)
|
||||
// second batch is wrapped from start of array to end of queue
|
||||
let numberOfElementsInSecondBatch = count - countElementsInFirstBatch
|
||||
|
||||
newStorage[0 ..< countElementsInFirstBatch] = storage[dequeueIndex ..< (dequeueIndex + countElementsInFirstBatch)]
|
||||
newStorage[countElementsInFirstBatch ..< (countElementsInFirstBatch + numberOfElementsInSecondBatch)] = storage[0 ..< numberOfElementsInSecondBatch]
|
||||
|
||||
self.innerCount = count
|
||||
pushNextIndex = count
|
||||
storage = newStorage
|
||||
}
|
||||
|
||||
/// Enqueues `element`.
|
||||
///
|
||||
/// - parameter element: Element to enqueue.
|
||||
mutating func enqueue(_ element: T) {
|
||||
if count == storage.count {
|
||||
resizeTo(Swift.max(storage.count, 1) * resizeFactor)
|
||||
}
|
||||
|
||||
storage[pushNextIndex] = element
|
||||
pushNextIndex += 1
|
||||
innerCount += 1
|
||||
|
||||
if pushNextIndex >= storage.count {
|
||||
pushNextIndex -= storage.count
|
||||
}
|
||||
}
|
||||
|
||||
private mutating func dequeueElementOnly() -> T {
|
||||
precondition(count > 0)
|
||||
|
||||
let index = dequeueIndex
|
||||
|
||||
defer {
|
||||
storage[index] = nil
|
||||
innerCount -= 1
|
||||
}
|
||||
|
||||
return storage[index]!
|
||||
}
|
||||
|
||||
/// Dequeues element or throws an exception in case queue is empty.
|
||||
///
|
||||
/// - returns: Dequeued element.
|
||||
mutating func dequeue() -> T? {
|
||||
if self.count == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
defer {
|
||||
let downsizeLimit = storage.count / (resizeFactor * resizeFactor)
|
||||
if count < downsizeLimit && downsizeLimit >= initialCapacity {
|
||||
resizeTo(storage.count / resizeFactor)
|
||||
}
|
||||
}
|
||||
|
||||
return dequeueElementOnly()
|
||||
}
|
||||
|
||||
/// - returns: Generator of contained elements.
|
||||
func makeIterator() -> AnyIterator<T> {
|
||||
var i = dequeueIndex
|
||||
var innerCount = count
|
||||
|
||||
return AnyIterator {
|
||||
if innerCount == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
defer {
|
||||
innerCount -= 1
|
||||
i += 1
|
||||
}
|
||||
|
||||
if i >= self.storage.count {
|
||||
i -= self.storage.count
|
||||
}
|
||||
|
||||
return self.storage[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
21
Pods/RxSwift/Platform/DispatchQueue+Extensions.swift
generated
Normal file
21
Pods/RxSwift/Platform/DispatchQueue+Extensions.swift
generated
Normal file
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// DispatchQueue+Extensions.swift
|
||||
// Platform
|
||||
//
|
||||
// Created by Krunoslav Zaher on 10/22/16.
|
||||
// Copyright © 2016 Krunoslav Zaher. All rights reserved.
|
||||
//
|
||||
|
||||
import Dispatch
|
||||
|
||||
extension DispatchQueue {
|
||||
private static var token: DispatchSpecificKey<()> = {
|
||||
let key = DispatchSpecificKey<()>()
|
||||
DispatchQueue.main.setSpecific(key: key, value: ())
|
||||
return key
|
||||
}()
|
||||
|
||||
static var isMain: Bool {
|
||||
DispatchQueue.getSpecific(key: token) != nil
|
||||
}
|
||||
}
|
||||
35
Pods/RxSwift/Platform/Platform.Darwin.swift
generated
Normal file
35
Pods/RxSwift/Platform/Platform.Darwin.swift
generated
Normal file
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// Platform.Darwin.swift
|
||||
// Platform
|
||||
//
|
||||
// Created by Krunoslav Zaher on 12/29/15.
|
||||
// Copyright © 2015 Krunoslav Zaher. All rights reserved.
|
||||
//
|
||||
|
||||
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
|
||||
|
||||
import Darwin
|
||||
import Foundation
|
||||
|
||||
extension Thread {
|
||||
static func setThreadLocalStorageValue<T: AnyObject>(_ value: T?, forKey key: NSCopying) {
|
||||
let currentThread = Thread.current
|
||||
let threadDictionary = currentThread.threadDictionary
|
||||
|
||||
if let newValue = value {
|
||||
threadDictionary[key] = newValue
|
||||
}
|
||||
else {
|
||||
threadDictionary[key] = nil
|
||||
}
|
||||
}
|
||||
|
||||
static func getThreadLocalStorageValueForKey<T>(_ key: NSCopying) -> T? {
|
||||
let currentThread = Thread.current
|
||||
let threadDictionary = currentThread.threadDictionary
|
||||
|
||||
return threadDictionary[key] as? T
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
32
Pods/RxSwift/Platform/Platform.Linux.swift
generated
Normal file
32
Pods/RxSwift/Platform/Platform.Linux.swift
generated
Normal file
@@ -0,0 +1,32 @@
|
||||
//
|
||||
// Platform.Linux.swift
|
||||
// Platform
|
||||
//
|
||||
// Created by Krunoslav Zaher on 12/29/15.
|
||||
// Copyright © 2015 Krunoslav Zaher. All rights reserved.
|
||||
//
|
||||
|
||||
#if os(Linux)
|
||||
|
||||
import Foundation
|
||||
|
||||
extension Thread {
|
||||
|
||||
static func setThreadLocalStorageValue<T: AnyObject>(_ value: T?, forKey key: String) {
|
||||
if let newValue = value {
|
||||
Thread.current.threadDictionary[key] = newValue
|
||||
}
|
||||
else {
|
||||
Thread.current.threadDictionary[key] = nil
|
||||
}
|
||||
}
|
||||
|
||||
static func getThreadLocalStorageValueForKey<T: AnyObject>(_ key: String) -> T? {
|
||||
let currentThread = Thread.current
|
||||
let threadDictionary = currentThread.threadDictionary
|
||||
|
||||
return threadDictionary[key] as? T
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
34
Pods/RxSwift/Platform/RecursiveLock.swift
generated
Normal file
34
Pods/RxSwift/Platform/RecursiveLock.swift
generated
Normal file
@@ -0,0 +1,34 @@
|
||||
//
|
||||
// RecursiveLock.swift
|
||||
// Platform
|
||||
//
|
||||
// Created by Krunoslav Zaher on 12/18/16.
|
||||
// Copyright © 2016 Krunoslav Zaher. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
#if TRACE_RESOURCES
|
||||
class RecursiveLock: NSRecursiveLock {
|
||||
override init() {
|
||||
_ = Resources.incrementTotal()
|
||||
super.init()
|
||||
}
|
||||
|
||||
override func lock() {
|
||||
super.lock()
|
||||
_ = Resources.incrementTotal()
|
||||
}
|
||||
|
||||
override func unlock() {
|
||||
super.unlock()
|
||||
_ = Resources.decrementTotal()
|
||||
}
|
||||
|
||||
deinit {
|
||||
_ = Resources.decrementTotal()
|
||||
}
|
||||
}
|
||||
#else
|
||||
typealias RecursiveLock = NSRecursiveLock
|
||||
#endif
|
||||
Reference in New Issue
Block a user