美文网首页
zip8运算

zip8运算

作者: 幸运的小强本人 | 来源:发表于2016-03-12 20:03 被阅读27次

extension Observable {
public static func zip<O1: ObservableType, O2: ObservableType, O3: ObservableType, O4: ObservableType, O5: ObservableType, O6: ObservableType, O7: ObservableType, O8: ObservableType>
(source1: O1, _ source2: O2, _ source3: O3, _ source4: O4, _ source5: O5, _ source6: O6, _ source7: O7, _ source8: O8, resultSelector: (O1.E, O2.E, O3.E, O4.E, O5.E, O6.E, O7.E, O8.E) throws -> E)
-> Observable<E> {
return Zip8(
source1: source1.asObservable(), source2: source2.asObservable(), source3: source3.asObservable(), source4: source4.asObservable(), source5: source5.asObservable(), source6: source6.asObservable(), source7: source7.asObservable(), source8: source8.asObservable(),
resultSelector: resultSelector
)
}
}

class ZipSink8_<E1, E2, E3, E4, E5, E6, E7, E8, O: ObserverType> : ZipSink<O> {
    typealias R = O.E
    typealias Parent = Zip8<E1, E2, E3, E4, E5, E6, E7, E8, R>

    let _parent: Parent

    var _values1: Queue<E1> = Queue(capacity: 2)
    var _values2: Queue<E2> = Queue(capacity: 2)
    var _values3: Queue<E3> = Queue(capacity: 2)
    var _values4: Queue<E4> = Queue(capacity: 2)
    var _values5: Queue<E5> = Queue(capacity: 2)
    var _values6: Queue<E6> = Queue(capacity: 2)
    var _values7: Queue<E7> = Queue(capacity: 2)
    var _values8: Queue<E8> = Queue(capacity: 2)

    init(parent: Parent, observer: O) {
        _parent = parent
        super.init(arity: 8, observer: observer)
    }

    override func hasElements(index: Int) -> Bool {
        switch (index) {
        case 0: return _values1.count > 0
        case 1: return _values2.count > 0
        case 2: return _values3.count > 0
        case 3: return _values4.count > 0
        case 4: return _values5.count > 0
        case 5: return _values6.count > 0
        case 6: return _values7.count > 0
        case 7: return _values8.count > 0

        default:
            rxFatalError("Unhandled case (Function)")
        }

        return false
    }

    func run() -> Disposable {
        let subscription1 = SingleAssignmentDisposable()
        let subscription2 = SingleAssignmentDisposable()
        let subscription3 = SingleAssignmentDisposable()
        let subscription4 = SingleAssignmentDisposable()
        let subscription5 = SingleAssignmentDisposable()
        let subscription6 = SingleAssignmentDisposable()
        let subscription7 = SingleAssignmentDisposable()
        let subscription8 = SingleAssignmentDisposable()

        let observer1 = ZipObserver(lock: _lock, parent: self, index: 0, setNextValue: { self._values1.enqueue($0) }, this: subscription1)
        let observer2 = ZipObserver(lock: _lock, parent: self, index: 1, setNextValue: { self._values2.enqueue($0) }, this: subscription2)
        let observer3 = ZipObserver(lock: _lock, parent: self, index: 2, setNextValue: { self._values3.enqueue($0) }, this: subscription3)
        let observer4 = ZipObserver(lock: _lock, parent: self, index: 3, setNextValue: { self._values4.enqueue($0) }, this: subscription4)
        let observer5 = ZipObserver(lock: _lock, parent: self, index: 4, setNextValue: { self._values5.enqueue($0) }, this: subscription5)
        let observer6 = ZipObserver(lock: _lock, parent: self, index: 5, setNextValue: { self._values6.enqueue($0) }, this: subscription6)
        let observer7 = ZipObserver(lock: _lock, parent: self, index: 6, setNextValue: { self._values7.enqueue($0) }, this: subscription7)
        let observer8 = ZipObserver(lock: _lock, parent: self, index: 7, setNextValue: { self._values8.enqueue($0) }, this: subscription8)

        subscription1.disposable = _parent.source1.subscribe(observer1)
        subscription2.disposable = _parent.source2.subscribe(observer2)
        subscription3.disposable = _p    arent.source3.subscribe(observer3)
        subscription4.disposable = _parent.source4.subscribe(observer4)
        subscription5.disposable = _parent.source5.subscribe(observer5)
        subscription6.disposable = _parent.source6.subscribe(observer6)
        subscription7.disposable = _parent.source7.subscribe(observer7)
        subscription8.disposable = _parent.source8.subscribe(observer8)

        return CompositeDisposable(disposables: [
           subscription1,
           subscription2,
           subscription3,
           subscription4,
           subscription5,
           subscription6,
           subscription7,
           subscription8
        ])
    }

    override func getResult() throws -> R {
        return try _parent._resultSelector(_values1.dequeue()!, _values2.dequeue()!, _values3.dequeue()!, _values4.dequeue()!, _values5.dequeue()!, _values6.dequeue()!, _values7.dequeue()!, _values8.dequeue()!)
    }
}

class Zip8<E1, E2, E3, E4, E5, E6, E7, E8, R> : Producer<R> {
    typealias ResultSelector = (E1, E2, E3, E4, E5, E6, E7, E8) throws -> R

    let source1: Observable<E1>
    let source2: Observable<E2>
    let source3: Observable<E3>
    let source4: Observable<E4>
    let source5: Observable<E5>
    let source6: Observable<E6>
    let source7: Observable<E7>
    let source8: Observable<E8>

    let _resultSelector: ResultSelector

    init(source1: Observable<E1>, source2: Observable<E2>, source3: Observable<E3>, source4: Observable<E4>, source5: Observable<E5>, source6: Observable<E6>, source7: Observable<E7>, source8: Observable<E8>, resultSelector: ResultSelector) {
        self.source1 = source1
        self.source2 = source2
        self.source3 = source3
        self.source4 = source4
        self.source5 = source5
        self.source6 = source6
        self.source7 = source7
        self.source8 = source8

        _resultSelector = resultSelector
    }

    override func run<O: ObserverType where O.E == R>(observer: O) -> Disposable {
        let sink = ZipSink8_(parent: self, observer: observer)
        sink.disposable = sink.run()
        return sink
    }
}

相关文章

  • zip8运算

    extension Observable {public static func zip

  • RxSwift Combination Operators of

    使用示例 实现原理 Zip 有着一系列类簇,从Zip2 - Zip8 ,实现原理都是一样的区别在于Observab...

  • 2、Swift 基础运算

    赋值运算 算数运算 余数运算 一元加减法运算 混合赋值运算 比较运算 三元运算 空合运算符 范围运算 逻辑运算

  • Python 入门之常用运算符

    Python中的运算按种类可分为算数运算、比较运算、逻辑运算、赋值运算、成员运算、身份运算、位运算 1、常用运算符...

  • 黑猴子的家:Python 数据运算

    1、算术运算 2、比较运算 3、赋值运算 4、逻辑运算 5、成员运算 6、身份运算 7、位运算 code 8、运算...

  • 运算 & 运算符

    运算 & 运算符 算术运算 比较运算 比较运算的结果为bool值 赋值运算 逻辑运算 位运算 二进制的运算 身份检...

  • 2019-07-23

    R中的基本运算包括:算术运算、关系运算、逻辑运算、赋值运算以及其他运算。 算术运算:四则运算(+,-,*, /),...

  • JS 加、减、乘、除运算避免浮点数

    加法运算 减法运算 乘法运算 除法运算

  • 算术运算符

    算术运算符对数字(文字或变量)执行算术运算。 加法运算 减法运算 乘法运算 除法运算 余数运算 自增自减运算 指数...

  • JavaScript快速入门03-运算符

    JS的运算符 算数运算符 算数运算符用于对数值进行算数运算 运算符描述说明+加法运算-减法运算*乘法运算/除法运算...

网友评论

      本文标题:zip8运算

      本文链接:https://www.haomeiwen.com/subject/ptbxlttx.html