<commit_before>//
//  RFSectionDelta.swift
//  RFSectionDelta
//
//  Created by <NAME> on 3/5/15.
//  Copyright (c) 2015 ryanfitz. All rights reserved.
//

public struct MovedIndex : Equatable, CustomStringConvertible {
    public let oldIndex : Int
    public let newIndex : Int
    
    public init (oldIndex : Int, newIndex : Int) {
        self.oldIndex = oldIndex
        self.newIndex = newIndex
    }
    
    public var description: String {
        return "oldIndex: \(oldIndex) newIndex: \(newIndex)"
    }
}

public func ==(lhs: MovedIndex, rhs: MovedIndex) -> Bool {
    return lhs.oldIndex == rhs.oldIndex && lhs.newIndex == rhs.newIndex
}

public struct RFDelta {
    public let addedIndices : NSIndexSet?
    public let removedIndices : NSIndexSet?
    public let unchangedIndices : NSIndexSet?
    public let movedIndexes : [MovedIndex]?
}

public class RFSectionDelta {
    
    public init() {
        
    }
    
    public func generateDelta<T : Hashable>(fromOldArray oldArray : [T]?, toNewArray newArray: [T]?) -> RFDelta {
        let removeSets = NSMutableIndexSet()
        let insertSets = NSMutableIndexSet()
        let unchangedSets = NSMutableIndexSet()
        var movedIndexes = [MovedIndex]()
        
        if let oldData = oldArray {
            if let newData = newArray {
                
                // TODO switch to using native sets when support swift 1.2
                let newItemsSet = indexArray(newData)
                let oldItemSet = indexArray(oldData)
                
                for (item, newIdx) in newItemsSet {
                    if let oldIdx = oldItemSet[item] {
                        if newIdx == oldIdx {
                            unchangedSets.addIndex(newIdx)
                        } else {
                            movedIndexes.append(MovedIndex(oldIndex: oldIdx, newIndex: newIdx))
                        }
                    } else {
                        insertSets.addIndex(newIdx)
                    }
                }
                
                for (item, oldIdx) in oldItemSet {
                    if newItemsSet[item] == nil {
                       removeSets.addIndex(oldIdx)
                    }
                }
            } else {
                // new array is nil so remove all
                for (idx, _) in oldData.enumerate() {
                    removeSets.addIndex(idx)
                }
            }
        } else if let newData = newArray {
            for (idx, _) in newData.enumerate() {
                insertSets.addIndex(idx)
            }
        }
        
        let addedIndices : NSIndexSet? = insertSets.count > 0 ? insertSets : nil
        let removedIndices : NSIndexSet? = (removeSets.count > 0 ? removeSets : nil )
        let unchangedIndices : NSIndexSet? = (unchangedSets.count > 0 ? unchangedSets : nil )
        let movedIndices : [MovedIndex]? = (movedIndexes.count > 0 ? movedIndexes : nil )
        
        return RFDelta(addedIndices: addedIndices, removedIndices: removedIndices, unchangedIndices: unchangedIndices, movedIndexes: movedIndices)
    }
    
    private func indexArray<T : Hashable>(array : [T]) -> [T : Int] {
        var result = [T : Int]()
        
        for (idx, item) in array.enumerate() {
            result[item] = idx
        }
        
        return result
    }
}<commit_msg>storing only hashvalues in indexes<commit_after>//
//  RFSectionDelta.swift
//  RFSectionDelta
//
//  Created by <NAME> on 3/5/15.
//  Copyright (c) 2015 ryanfitz. All rights reserved.
//

public struct MovedIndex : Equatable, CustomStringConvertible {
    public let oldIndex : Int
    public let newIndex : Int
    
    public init (oldIndex : Int, newIndex : Int) {
        self.oldIndex = oldIndex
        self.newIndex = newIndex
    }
    
    public var description: String {
        return "oldIndex: \(oldIndex) newIndex: \(newIndex)"
    }
}

public func ==(lhs: MovedIndex, rhs: MovedIndex) -> Bool {
    return lhs.oldIndex == rhs.oldIndex && lhs.newIndex == rhs.newIndex
}

public struct RFDelta {
    public let addedIndices : NSIndexSet?
    public let removedIndices : NSIndexSet?
    public let unchangedIndices : NSIndexSet?
    public let movedIndexes : [MovedIndex]?
}

public class RFSectionDelta {
    
    public init() {
        
    }
    
    public func generateDelta<T : Hashable>(fromOldArray oldArray : [T]?, toNewArray newArray: [T]?) -> RFDelta {
        let removeSets = NSMutableIndexSet()
        let insertSets = NSMutableIndexSet()
        let unchangedSets = NSMutableIndexSet()
        var movedIndexes = [MovedIndex]()
        
        if let oldData = oldArray {
            if let newData = newArray {
                
                let newItemsSet = indexArray(newData)
                let oldItemSet = indexArray(oldData)
                
                for (item, newIdx) in newItemsSet {
                    if let oldIdx = oldItemSet[item] {
                        if newIdx == oldIdx {
                            unchangedSets.addIndex(newIdx)
                        } else {
                            movedIndexes.append(MovedIndex(oldIndex: oldIdx, newIndex: newIdx))
                        }
                    } else {
                        insertSets.addIndex(newIdx)
                    }
                }
                
                for (item, oldIdx) in oldItemSet {
                    if newItemsSet[item] == nil {
                       removeSets.addIndex(oldIdx)
                    }
                }
            } else {
                // new array is nil so remove all
                for (idx, _) in oldData.enumerate() {
                    removeSets.addIndex(idx)
                }
            }
        } else if let newData = newArray {
            for (idx, _) in newData.enumerate() {
                insertSets.addIndex(idx)
            }
        }
        
        let addedIndices : NSIndexSet? = insertSets.count > 0 ? insertSets : nil
        let removedIndices : NSIndexSet? = (removeSets.count > 0 ? removeSets : nil )
        let unchangedIndices : NSIndexSet? = (unchangedSets.count > 0 ? unchangedSets : nil )
        let movedIndices : [MovedIndex]? = (movedIndexes.count > 0 ? movedIndexes : nil )
        
        return RFDelta(addedIndices: addedIndices, removedIndices: removedIndices, unchangedIndices: unchangedIndices, movedIndexes: movedIndices)
    }
    
    private func indexArray<T : Hashable>(array : [T]) -> [Int : Int] {
        var result = [Int : Int]()
        
        for (idx, item) in array.enumerate() {
            result[item.hashValue] = idx
        }
        
        return result
    }
}
{'fraction_non_alphanumeric': 0.08799760550733314, 'fraction_numerical': 0.0038910505836575876, 'mean_word_length': 1.588303640588691, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, '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': '4720501', 'n_tokens_mistral': 1870, 'n_tokens_neox': 1817, 'n_words': 470}