<commit_before>//
//  RAMCollectionViewLayout.swift
//  RAMReel
//
//  Created by <NAME> on 4/9/15.
//  Copyright (c) 2015 Ramotion. All rights reserved.
//

import UIKit

/**
  Example collection view layout
*/
@objc(RAMCollectionViewLayout)
class RAMCollectionViewLayout: UICollectionViewLayout {
    
    internal override func prepareLayout() {
        super.prepareLayout()
        
        updateInsets()
    }
    
    func updateInsets() {
        if let collectionView = self.collectionView {
            let insets = (collectionView.bounds.height - itemHeight)/2
            collectionView.contentInset = UIEdgeInsets(top: insets, left: 0, bottom: insets, right: 0)
        }
    }
    
    internal override func initialLayoutAttributesForAppearingItemAtIndexPath(itemIndexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
        return self.layoutAttributesForItemAtIndexPath(itemIndexPath)
    }
    
    internal override func layoutAttributesForItemAtIndexPath(indexPath:NSIndexPath) -> UICollectionViewLayoutAttributes {
        let attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
        self.modifyLayoutAttributes(attributes)
        
        return attributes
    }
    
    internal override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes] {
        
        var allAttributesInRect = [(UICollectionViewLayoutAttributes, CGFloat)]()
        
        if let numberOfItems = collectionView?.numberOfItemsInSection(0) {
            for item in 0 ..< numberOfItems {
                let indexPath = NSIndexPath(forItem: item, inSection: 0)
                
                let attributes = self.layoutAttributesForItemAtIndexPath(indexPath)
                
                if rect.intersects(attributes.frame) {
                    let intersection = CGRectIntersection(rect, attributes.frame)
                    allAttributesInRect.append((attributes, intersection.area))
                }
            }
        }
        
        allAttributesInRect.sortInPlace {
            let (_, a1) = $0
            let (_, a2) = $1
            
            return a1 > a2
        }
        
        let attributes = allAttributesInRect.map ({ (attr, _) -> UICollectionViewLayoutAttributes in
            attr
        })

        return attributes
    }
    
    var itemHeight: CGFloat = 44
    func modifyLayoutAttributes(layoutattributes: UICollectionViewLayoutAttributes) {
        
        if
            let collectionView = self.collectionView
        {
            let number = collectionView.numberOfItemsInSection(0)
            let height = CGFloat(number) * itemHeight
            
            var frame               = layoutattributes.frame
            frame.size.height       = itemHeight
            frame.size.width        = collectionView.bounds.width
            frame.origin.x          = collectionView.bounds.origin.x
            frame.origin.y          = itemHeight * CGFloat(layoutattributes.indexPath.item)
            layoutattributes.frame  = frame
        }
        <commit_msg>small fixed<commit_after>//
//  RAMCollectionViewLayout.swift
//  RAMReel
//
//  Created by <NAME> on 4/9/15.
//  Copyright (c) 2015 Ramotion. All rights reserved.
//

import UIKit

/**
  Example collection view layout
*/
@objc(RAMCollectionViewLayout)
class RAMCollectionViewLayout: UICollectionViewLayout {
    
    internal override func prepareLayout() {
        super.prepareLayout()
        
        updateInsets()
    }
    
    func updateInsets() {
        if let collectionView = self.collectionView {
            let insets = (collectionView.bounds.height - itemHeight)/2
            collectionView.contentInset = UIEdgeInsets(top: insets, left: 0, bottom: insets, right: 0)
        }
    }
    
    internal override func initialLayoutAttributesForAppearingItemAtIndexPath(itemIndexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
        return self.layoutAttributesForItemAtIndexPath(itemIndexPath)
    }
    
    internal override func layoutAttributesForItemAtIndexPath(indexPath:NSIndexPath) -> UICollectionViewLayoutAttributes {
        let attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
        self.modifyLayoutAttributes(attributes)
        
        return attributes
    }
    
    internal override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes] {
        
        var allAttributesInRect = [(UICollectionViewLayoutAttributes, CGFloat)]()
        
        if let numberOfItems = collectionView?.numberOfItemsInSection(0) {
            for item in 0 ..< numberOfItems {
                let indexPath = NSIndexPath(forItem: item, inSection: 0)
                
                let attributes = self.layoutAttributesForItemAtIndexPath(indexPath)
                
                if rect.intersects(attributes.frame) {
                    let intersection = CGRectIntersection(rect, attributes.frame)
                    allAttributesInRect.append((attributes, intersection.area))
                }
            }
        }
        
        allAttributesInRect.sortInPlace {
            let (_, a1) = $0
            let (_, a2) = $1
            
            return a1 > a2
        }
        
        let attributes = allAttributesInRect.map ({ (attr, _) -> UICollectionViewLayoutAttributes in
            attr
        })

        return attributes
    }
    
    var itemHeight: CGFloat = 44
    func modifyLayoutAttributes(layoutattributes: UICollectionViewLayoutAttributes) {
        
        if
            let collectionView = self.collectionView
        {
            var frame               = layoutattributes.frame
            frame.size.height       = itemHeight
            frame.size.width        = collectionView.bounds.width
            frame.origin.x          = collectionView.bounds.origin.x
            frame.origin.y          = itemHeight * CGFloat(layoutattributes.indexPath.item)
            layoutattributes.frame  = frame
        }
        
{'fraction_non_alphanumeric': 0.0722653027553209, 'fraction_numerical': 0.007424517406368586, 'mean_word_length': 2.058526740665994, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '3423655', 'n_tokens_mistral': 1546, 'n_tokens_neox': 1515, 'n_words': 308}