<commit_before>//
//  CollectionEvent.swift
//  SignalKit
//
//  Created by <NAME> on 3/6/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation

public struct CollectionEvent {
    
    public enum Element {
        case Section(Int)
        case Item(index: Int, section: Int)
    }
    
    public enum Operation {
        case Reset
        case Insert(Element)
        case Remove(Element)
        case Update(Element)
    }
    
    public var operations = Set<Operation>()
    
    public init() {}
}

extension CollectionEvent {
    
    /// Register that the data in the collection has been reset
    /// This will translate to table/collection view reload
    
    public mutating func sectionsReset() {
        
        operations.insert(.Reset)
    }
    
    /// Register that a new section has been inserted
    
    public mutating func sectionInsertedAt(index: Int) {
        
        operations.insert(.Insert(.Section(index)))
    }
    
    /// Register that a section has been removed
    
    public mutating func sectionRemovedAt(index: Int) {
        
        operations.insert(.Remove(.Section(index)))
    }
    
    /// Register that a section has been updated with new items
    
    public mutating func sectionUpdatedAt(index: Int) {
        
        operations.insert(.Update(.Section(index)))
    }
    
    /// Register that a new item has been inserted in the given section
    
    public mutating func itemInsertedAt(index: Int, inSection: Int) {
        
        operations.insert(.Insert(.Item(index: index, section: inSection)))
    }
    
    /// Register that an item has been removed from the given section
    
    public mutating func itemRemovedAt(index: Int, inSection: Int) {
        
        operations.insert(.Remove(.Item(index: index, section: inSection)))
    }
    
    /// Register that an item has been updated in the given section
    
    public mutating func itemUpdatedAt(index: Int, inSection: Int) {
        
        operations.insert(.Update(.Item(index: index, section: inSection)))
    }
}

extension CollectionEvent {
    
    public var containsResetOperation: Bool {
        
        return operations.contains(.Reset)
    }
}

extension CollectionEvent.Element {
    
    var isSection: Bool {
        
        guard case .Section = self else { return false }
        
        return true
    }
}

extension CollectionEvent.Operation {
    
    var isSectionOperation: Bool {
        
        switch self {
        case .Reset:
            return false
            
        case let .Insert(element):
            return element.isSection
            
        case let .Remove(element):
            return element.isSection
            
        case let .Update(element):
            return element.isSection
        }
    }
}

// MARK: - Element Equatable

extension CollectionEvent.Element: Equatable {}

public func ==(lhs: CollectionEvent.Element, rhs: CollectionEvent.Element) -> Bool {
    
    switch (lhs, rhs) {
        
    case (let .Section(a), let .Section(b)):
        return a == b
    
    case (let .Item(indexA, sectionA), let .Item(indexB, sectionB)):
        return sectionA == sectionB && indexA == indexB
    
    default:
        return false
    }
}

// MARK: - Operation Equatable

extension CollectionEvent.Operation: Equatable {}

public func ==(lhs: CollectionEvent.Operation, rhs: CollectionEvent.Operation) -> Bool {
    
    switch (lhs, rhs) {
        
    case (.Reset, .Reset):
        return true
        <commit_msg>Remove isSectionOperation<commit_after>//
//  CollectionEvent.swift
//  SignalKit
//
//  Created by <NAME> on 3/6/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation

public struct CollectionEvent {
    
    public enum Element {
        case Section(Int)
        case Item(index: Int, section: Int)
    }
    
    public enum Operation {
        case Reset
        case Insert(Element)
        case Remove(Element)
        case Update(Element)
    }
    
    public var operations = Set<Operation>()
    
    public init() {}
}

extension CollectionEvent {
    
    /// Register that the data in the collection has been reset
    /// This will translate to table/collection view reload
    
    public mutating func sectionsReset() {
        
        operations.insert(.Reset)
    }
    
    /// Register that a new section has been inserted
    
    public mutating func sectionInsertedAt(index: Int) {
        
        operations.insert(.Insert(.Section(index)))
    }
    
    /// Register that a section has been removed
    
    public mutating func sectionRemovedAt(index: Int) {
        
        operations.insert(.Remove(.Section(index)))
    }
    
    /// Register that a section has been updated with new items
    
    public mutating func sectionUpdatedAt(index: Int) {
        
        operations.insert(.Update(.Section(index)))
    }
    
    /// Register that a new item has been inserted in the given section
    
    public mutating func itemInsertedAt(index: Int, inSection: Int) {
        
        operations.insert(.Insert(.Item(index: index, section: inSection)))
    }
    
    /// Register that an item has been removed from the given section
    
    public mutating func itemRemovedAt(index: Int, inSection: Int) {
        
        operations.insert(.Remove(.Item(index: index, section: inSection)))
    }
    
    /// Register that an item has been updated in the given section
    
    public mutating func itemUpdatedAt(index: Int, inSection: Int) {
        
        operations.insert(.Update(.Item(index: index, section: inSection)))
    }
}

extension CollectionEvent {
    
    public var containsResetOperation: Bool {
        
        return operations.contains(.Reset)
    }
}

// MARK: - Element Equatable

extension CollectionEvent.Element: Equatable {}

public func ==(lhs: CollectionEvent.Element, rhs: CollectionEvent.Element) -> Bool {
    
    switch (lhs, rhs) {
        
    case (let .Section(a), let .Section(b)):
        return a == b
    
    case (let .Item(indexA, sectionA), let .Item(indexB, sectionB)):
        return sectionA == sectionB && indexA == indexB
    
    default:
        return false
    }
}

// MARK: - Operation Equatable

extension CollectionEvent.Operation: Equatable {}

public func ==(lhs: CollectionEvent.Operation, rhs: CollectionEvent.Operation) -> Bool {
    
    switch (lhs, rhs) {
        
    case (.Reset, .Reset):
        return true
        
{'fraction_non_alphanumeric': 0.09352850539291217, 'fraction_numerical': 0.002465331278890601, 'mean_word_length': 2.0894811994288434, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '3300291', 'n_tokens_mistral': 1782, 'n_tokens_neox': 1721, 'n_words': 539}