<commit_before>//
//  Pipeline.swift
//  Pipeline
//
//  Created by <NAME> on 2015-07-16.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Foundation

public class PipelineQueue {
    public enum Queue {
        case Main
        case QOS(NSQualityOfService)
    }

    private let q: NSOperationQueue

    public let queue: Queue
    public var suspended = true {
        didSet {
            q.suspended = suspended
        }
    }
    public var operations: [NSOperation] {
        return q.operations
    }

    public init(_ queue: Queue = .QOS(.Default)) {
        self.queue = queue
        switch queue {
        case .Main:
            q = NSOperationQueue.mainQueue()
        case .QOS(let QOS):
            q = NSOperationQueue()
            q.qualityOfService = QOS
        }
    }

    public func cancelAllOperations() {
        q.cancelAllOperations()
    }

    public func addOperation<O where O: NSOperation, O: Pipelinable>(op: O, _ queue: Queue? = nil) {
        if let queue = queue {
            switch queue {
            case .Main:
                PipelineQueue(.Main).addOperation(op)
            case .QOS(let QOS):
                op.qualityOfService = QOS
                q.addOperation(op)
            }
        }
        else {
            q.addOperation(op)
        }
    }
}

public class Pipeline {
    private let q: PipelineQueue

    public init<U>(handler: () -> U) {
        q = PipelineQueue(.QOS(.Default))
        let operation = PipelineOperation<U> { fulfill, reject in
            fulfill(handler())
        }
        q.addOperation(operation)
    }

    public init<U>(_ QOS: NSQualityOfService = .Default, handler: () -> U) {
        q = PipelineQueue(.QOS(QOS))
        let operation = PipelineOperation<U> { fulfill, reject in
            fulfill(handler())
        }
        q.addOperation(operation)
    }

    public init<U>(_ QOS: NSQualityOfService = .Default, operationHandler: () -> PipelineOperation<U>) {
        q = PipelineQueue(.QOS(QOS))
        let operation = operationHandler()
        q.addOperation(operation)
    }

    public func start() {
        q.suspended = false
    }

    // Values
    public func success<T, U>(successHandler handler: T -> U) -> Pipeline {
        return self.success(q, q.queue, successHandler: handler)
    }

    public func success<T, U>(QOS: PipelineQueue.Queue, successHandler handler: T -> U) -> Pipeline {
        return self.success(q, QOS, successHandler: handler)
    }

    public func success<T, U>(QOS: NSQualityOfService, successHandler handler: T -> U) -> Pipeline {
        return self.success(q, .QOS(QOS), successHandler: handler)
    }

    public func success<T, U>(queue: PipelineQueue, _ QOS: PipelineQueue.Queue, successHandler handler: T -> U) -> Pipeline {
        if let lastOperation = queue.operations.last as? PipelineOperation<T> {
            let operation = PipelineOperation<U> { fulfill, reject in
                if let output = lastOperation.output {
                    switch output {
                    case .Failure(let error): reject(error)
                    case .Success(let output): fulfill(handler(output))
                    }
                }
            }
            operation.addDependency(lastOperation)
            queue.addOperation(operation, QOS)
        }

        return self
    }

    // Operations
    public func success<T, U>(successHandler handler: T -> PipelineOperation<U>) -> Pipeline {
        return self.success(q, q.queue, successHandler: handler)
    }

    public func success<T, U>(QOS: PipelineQueue.Queue, successHandler handler: T -> PipelineOperation<U>) -> Pipeline {
        return self.success(q, QOS, successHandler: handler)
    }

    public func success<T, U>(QOS: NSQualityOfService, successHandler handler: T -> PipelineOperation<U>) -> Pipeline {
        return self.success(q, .QOS(QOS), successHandler: handler)
    }

    public func success<T, U>(queue: PipelineQueue, _ QOS: PipelineQueue.Queue, successHandler handler: T -> PipelineOperation<U>) -> Pipeline {
        if let lastOperation = queue.operations.last as? PipelineOperation<T> {
            var operation: PipelineOperation<U>!
            operation = PipelineOperation<U> { fulfill, reject in
                if let output = lastOperation.output {
                    switch output {
                    case .Failure(let error):
                        reject(error)
                    case .Success(let output):
                        let innerOp = handler(output)
                        innerOp.success { output in fulfill(output) }
                        operation.internalQueue.addOperation(innerOp, QOS)
                    }<commit_msg>Improve internal variable names<commit_after>//
//  Pipeline.swift
//  Pipeline
//
//  Created by <NAME> on 2015-07-16.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Foundation

public class PipelineQueue {
    public enum QueueLevel {
        case Main
        case QOS(NSQualityOfService)
    }

    private let internalQueue: NSOperationQueue

    public let queueLevel: QueueLevel
    public var suspended = true {
        didSet {
            internalQueue.suspended = suspended
        }
    }
    public var operations: [NSOperation] {
        return q.operations
    }

    public init(_ queueLevel: QueueLevel = .QOS(.Default)) {
        self.queueLevel = queueLevel
        switch queueLevel {
        case .Main:
            internalQueue = NSOperationQueue.mainQueue()
        case .QOS(let QOS):
            internalQueue = NSOperationQueue()
            internalQueue.qualityOfService = QOS
        }
    }

    public func cancelAllOperations() {
        q.cancelAllOperations()
    }

    public func addOperation<O where O: NSOperation, O: Pipelinable>(op: O, _ queue: QueueLevel? = nil) {
        if let queue = queue {
            switch queue {
            case .Main:
                PipelineQueue(.Main).addOperation(op)
            case .QOS(let QOS):
                op.qualityOfService = QOS
                internalQueue.addOperation(op)
            }
        }
        else {
            internalQueue.addOperation(op)
        }
    }
}

public class Pipeline {
    private let queue: PipelineQueue

    public init<U>(handler: () -> U) {
        queue = PipelineQueue(.QOS(.Default))
        let operation = PipelineOperation<U> { fulfill, reject in
            fulfill(handler())
        }
        queue.addOperation(operation)
    }

    public convenience init<U>(_ QOS: NSQualityOfService = .Default, handler: () -> U) {
        self.init(.QOS(QOS), handler: handler)
    }

    public init<U>(_ queueLevel: PipelineQueue.QueueLevel = .QOS(.Default), handler: () -> U) {
        queue = PipelineQueue(queueLevel)
        let operation = PipelineOperation<U> { fulfill, reject in
            fulfill(handler())
        }
        queue.addOperation(operation)
    }

    public convenience init<U>(_ QOS: NSQualityOfService = .Default, operationHandler: () -> PipelineOperation<U>) {
        self.init(.QOS(QOS), operationHandler: operationHandler)
    }

    public init<U>(_ queueLevel: PipelineQueue.QueueLevel = .QOS(.Default), operationHandler: () -> PipelineOperation<U>) {
        queue = PipelineQueue(queueLevel)
        let operation = operationHandler()
        queue.addOperation(operation)
    }

    public func start() {
        queue.suspended = false
    }

    // Values
    public func success<T, U>(successHandler handler: T -> U) -> Pipeline {
        return self.success(queue, queue.queueLevel, successHandler: handler)
    }

    public func success<T, U>(QOS: PipelineQueue.QueueLevel, successHandler handler: T -> U) -> Pipeline {
        return self.success(queue, QOS, successHandler: handler)
    }

    // Shortcut for above when using .QOS level instead of .Main
    public func success<T, U>(QOS: NSQualityOfService, successHandler handler: T -> U) -> Pipeline {
        return self.success(queue, .QOS(QOS), successHandler: handler)
    }

    public func success<T, U>(queue: PipelineQueue, _ QOS: PipelineQueue.QueueLevel, successHandler handler: T -> U) -> Pipeline {
        if let lastOperation = queue.operations.last as? PipelineOperation<T> {
            let operation = PipelineOperation<U> { fulfill, reject in
                if let output = lastOperation.output {
                    switch output {
                    case .Failure(let error): reject(error)
                    case .Success(let output): fulfill(handler(output))
                    }
                }
            }
            operation.addDependency(lastOperation)
            queue.addOperation(operation, QOS)
        }

        return self
    }

    // Operations
    public func success<T, U>(successHandler handler: T -> PipelineOperation<U>) -> Pipeline {
        return self.success(queue, queue.queueLevel, successHandler: handler)
    }

    public func success<T, U>(QOS: PipelineQueue.QueueLevel, successHandler handler: T -> PipelineOperation<U>) -> Pipeline {
        return self.success(queue, QOS, successHandler: handler)
    }

    public func success<T, U>(QOS: NSQualityOfService, successHandler handler: T -> PipelineOperation<U>) -> Pipeline {
        return self.success(queue, .QOS(QOS), successHandler: handler)
    }

    public func success<T, U>(queue: PipelineQueue, _ QOS: PipelineQueue.QueueLevel, successHandler handler: T -> PipelineOperation<U>) -> Pipeline {
        if let lastOperation = queue.operations.last as? PipelineOperation<T> {
            var operation: PipelineOperation<U>!
            operation = PipelineOperation<U> { fulfill, reject in
                if let output = lastOperation.output {
                    switch output {
                    case .Failure(let error):
                        reject(error)
                    case .Success(let output):
                        let innerOp = handler(output)
                        innerOp.success { output in fulfill(output) }
                        operation.internalQueue.addOperation(innerOp, QOS)
                    }
{'fraction_non_alphanumeric': 0.10110803324099724, 'fraction_numerical': 0.002374356944994064, 'mean_word_length': 2.0717107262230323, 'pattern_counts': {'":': 0, '<': 58, '<?xml version=': 0, '>': 98, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1966561', 'n_tokens_mistral': 2806, 'n_tokens_neox': 2683, 'n_words': 705}