<commit_before>//
//  FetchedResultsController.swift
//  GRDB
//
//  Created by <NAME> on 09/12/2015.
//  Copyright © 2015 <NAME>. All rights reserved.
//
import UIKit

public typealias FetchedResult = protocol<RowConvertible, TableMapping, Equatable>

private enum Source<T: FetchedResult> {
    case SQL(String, StatementArguments?)
    case FetchRequest(GRDB.FetchRequest<T>)

    func fetchAll(db: DatabaseReader) -> [T] {
        switch self {
        case .SQL(let sql, let arguments):
            return T.fetchAll(db, sql, arguments: arguments)
        case .FetchRequest(let fetchRequest):
            return fetchRequest.fetchAll(db)
        }
    }
}

public class FetchedResultsController<T: FetchedResult> {
    
    // MARK: - Initialization
    public convenience init(database: DatabaseWriter, sql: String, arguments: StatementArguments? = nil) {
        let source: Source<T> = .SQL(sql, arguments)
        self.init(database: database, source: source)
    }
    
    public convenience init(database: DatabaseWriter, fetchRequest: FetchRequest<T>) {
        let source: Source<T> = .FetchRequest(fetchRequest)
        self.init(database: database, source: source)
    }
    
    private init(database: DatabaseWriter, source: Source<T>) {
        self.source = source
        self.database = database
        database.addTransactionObserver(self)
    }
    
    public func performFetch() {
        fetchedResults = self.fetch()
    }
    
    
    // MARK: - Configuration
    
    /// The source
    private let source: Source<T>

    /// The databaseWriter
    public let database: DatabaseWriter
    
    /// Delegate that is notified when the resultss set changes.
    weak public var delegate: FetchedResultsControllerDelegate?
    
    
    // MARK: - Accessing results

    /// Returns the results of the query.
    /// Returns nil if the performQuery: hasn't been called.
    public private(set) var fetchedResults: [T]?

    
    /// Returns the fetched object at a given indexPath.
    public func resultAtIndexPath(indexPath: NSIndexPath) -> T? {
        if let result = fetchedResults?[indexPath.indexAtPosition(1)] {
            return result
        } else {
            return nil
        }
    }
    
    /// Returns the indexPath of a given object.
    public func indexPathForResult(result: T) -> NSIndexPath? {
        return nil
    }
<commit_msg>FetchedResultsController.indexPathForResult(_) is not implemented<commit_after>//
//  FetchedResultsController.swift
//  GRDB
//
//  Created by <NAME> on 09/12/2015.
//  Copyright © 2015 <NAME>. All rights reserved.
//
import UIKit

public typealias FetchedResult = protocol<RowConvertible, TableMapping, Equatable>

private enum Source<T: FetchedResult> {
    case SQL(String, StatementArguments?)
    case FetchRequest(GRDB.FetchRequest<T>)

    func fetchAll(db: DatabaseReader) -> [T] {
        switch self {
        case .SQL(let sql, let arguments):
            return T.fetchAll(db, sql, arguments: arguments)
        case .FetchRequest(let fetchRequest):
            return fetchRequest.fetchAll(db)
        }
    }
}

public class FetchedResultsController<T: FetchedResult> {
    
    // MARK: - Initialization
    public convenience init(database: DatabaseWriter, sql: String, arguments: StatementArguments? = nil) {
        let source: Source<T> = .SQL(sql, arguments)
        self.init(database: database, source: source)
    }
    
    public convenience init(database: DatabaseWriter, fetchRequest: FetchRequest<T>) {
        let source: Source<T> = .FetchRequest(fetchRequest)
        self.init(database: database, source: source)
    }
    
    private init(database: DatabaseWriter, source: Source<T>) {
        self.source = source
        self.database = database
        database.addTransactionObserver(self)
    }
    
    public func performFetch() {
        fetchedResults = self.fetch()
    }
    
    
    // MARK: - Configuration
    
    /// The source
    private let source: Source<T>

    /// The databaseWriter
    public let database: DatabaseWriter
    
    /// Delegate that is notified when the resultss set changes.
    weak public var delegate: FetchedResultsControllerDelegate?
    
    
    // MARK: - Accessing results

    /// Returns the results of the query.
    /// Returns nil if the performQuery: hasn't been called.
    public private(set) var fetchedResults: [T]?

    
    /// Returns the fetched object at a given indexPath.
    public func resultAtIndexPath(indexPath: NSIndexPath) -> T? {
        if let result = fetchedResults?[indexPath.indexAtPosition(1)] {
            return result
        } else {
            return nil
        }
    }
    
    /// Returns the indexPath of a given object.
    public func indexPathForResult(result: T) -> NSIndexPath? {
        // TODO
        fatalError("Not implemented")
    }

{'fraction_non_alphanumeric': 0.096521918090142, 'fraction_numerical': 0.005350895245935378, 'mean_word_length': 2.509025270758123, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 31, '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': '1999614', 'n_tokens_mistral': 1377, 'n_tokens_neox': 1332, 'n_words': 388}