<commit_before>//
//  RadfanLayout.swift
//  RadfanCollectionView
//
//  Created by <NAME> on 2016-05-01.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit

// MARK: Constants

struct RadfanConstants {
    
    // padding insets between collection view cell and collection view
    struct Padding {
        static let portrait = CGSize(width: 25.0, height: 80.0)
        static let landscape = CGSize(width: 25.0, height: 40.0)
    }
    
    // constants for cell scrolling animation
    struct ScrollConstants {
        // set the vertical translation distance (use the sign to change direction)
        static let translation: CGFloat = 60.0
        
        // set the x and y scale factors during scrolling
        static let scaleX: CGFloat = 2.0
        static let scaleY: CGFloat = 0.4
        
        // set the rotation angle (in radians)
        static let rotate: CGFloat = .pi / 6.0
    }
}

// MARK: RadfanLayout 

class RadfanLayout: UICollectionViewLayout {
    
    private var oldBounds = CGRect.zero
    private var cache = [UICollectionViewLayoutAttributes]()
    
    override var collectionViewContentSize: CGSize {
        get {
            
            guard let collectionView = collectionView else {
                return CGSize.zero
            }
            
            // compute the content size for our collection view
            let width = collectionView.bounds.width * CGFloat( collectionView.numberOfItems(inSection: 0) )
            return CGSize(width: width, height: collectionView.bounds.height)
        }
    }

    override func prepare() {
        
        guard let collectionView = collectionView else {
            // nothing to do if we have a nil collection view
            return
        }
        
        // compute layout frames if we dont already have them in our cache

        if cache.isEmpty {
        
            let collectionViewSize = collectionView.bounds.size
            
            var padding = RadfanConstants.Padding.portrait
            if collectionViewSize.width > collectionViewSize.height {
                // landscape orientation
                padding = RadfanConstants.Padding.landscape
            }
            
            for i in 0 ..< collectionView.numberOfItems(inSection: 0) {
                
                let xOffset = CGFloat(i) * collectionViewSize.width
                let width = collectionViewSize.width
                let height = collectionViewSize.height
                
                // create the cell's frame at (xOffset, 0.0)
                let frame = CGRect(x: xOffset, y: 0.0, width: width, height: height)
                
                // add the padding to the cell's frame
                let frameWithPadding = frame.insetBy(dx: padding.width, dy: padding.height)
                
                let indexPath = IndexPath(item: i, section: 0)
                
                let layoutAttributes = layoutAttributesForItem(at: indexPath) ?? UICollectionViewLayoutAttributes(forCellWith: indexPath)
                layoutAttributes.frame = frameWithPadding
                
                // add the layout attributes to the cache array
                cache.append(layoutAttributes)
            }
            
        }
        
        // always compute the affine transforms for visible cells
        computeTransforms()
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        
        return cache.filter{ $0.frame.intersects(rect) }
    }
    
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        
        return cache.filter{ $0.indexPath == indexPath }.first
    }
    
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        // clear the layout cache for a bounds change (ie: switching portrait <-> landscape)
        if oldBounds.size != newBounds.size {
            cache = []
            oldBounds = newBounds
        }
        return true
    }
    
    override func invalidateLayout() {
        super.invalidateLayout()
        
        cache = []
    }
    
    override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
        
        guard let collectionView = collectionView else {
            return proposedContentOffset
        }
        
        // 'snap to' and center the nearest 'page' (collection view item)<commit_msg>some code cleanup and reversion to more efficient lookups of layout attributes in the cache<commit_after>//
//  RadfanLayout.swift
//  RadfanCollectionView
//
//  Created by <NAME> on 2016-05-01.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit

// MARK: Constants

struct RadfanConstants {
    
    // padding insets between collection view cell and collection view
    struct Padding {
        static let portrait = CGSize(width: 25.0, height: 80.0)
        static let landscape = CGSize(width: 25.0, height: 40.0)
    }
    
    // constants for cell scrolling animation
    struct ScrollConstants {
        // set the vertical translation distance (use the sign to change direction)
        static let translation: CGFloat = 60.0
        
        // set the x and y scale factors during scrolling
        static let scaleX: CGFloat = 2.0
        static let scaleY: CGFloat = 0.4
        
        // set the rotation angle (in radians)
        static let rotate: CGFloat = .pi / 6.0
    }
}

// MARK: RadfanLayout 

class RadfanLayout: UICollectionViewLayout {
    
    private var oldBounds = CGRect.zero
    private var cache = [UICollectionViewLayoutAttributes]()
    
    override var collectionViewContentSize: CGSize {
        
        guard let collectionView = collectionView else {
            return CGSize.zero
        }
        
        // compute the content size for our collection view
        let width = collectionView.bounds.width * CGFloat( collectionView.numberOfItems(inSection: 0) )
        return CGSize(width: width, height: collectionView.bounds.height)
    }

    override func prepare() {
        
        guard let collectionView = collectionView else {
            // nothing to do if we have a nil collection view
            return
        }
        
        // compute layout frames if we dont already have them in our cache

        if cache.isEmpty {
        
            let collectionViewSize = collectionView.bounds.size
            
            var padding = RadfanConstants.Padding.portrait
            if collectionViewSize.width > collectionViewSize.height {
                // landscape orientation
                padding = RadfanConstants.Padding.landscape
            }
            
            for i in 0 ..< collectionView.numberOfItems(inSection: 0) {
                
                let xOffset = CGFloat(i) * collectionViewSize.width
                let width = collectionViewSize.width
                let height = collectionViewSize.height
                
                // create the cell's frame at (xOffset, 0.0)
                let frame = CGRect(x: xOffset, y: 0.0, width: width, height: height)
                
                // add the padding to the cell's frame
                let frameWithPadding = frame.insetBy(dx: padding.width, dy: padding.height)
                
                let indexPath = IndexPath(item: i, section: 0)
                
                let layoutAttributes = layoutAttributesForItem(at: indexPath) ?? UICollectionViewLayoutAttributes(forCellWith: indexPath)
                layoutAttributes.frame = frameWithPadding
                
                // add the layout attributes to the cache array
                cache.append(layoutAttributes)
            }
            
        }
        
        // always compute the affine transforms for visible cells
        computeTransforms()
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        
        return cache.filter{ $0.frame.intersects(rect) }
    }
    
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        
        for attributes in cache {
            if attributes.indexPath == indexPath {
                return attributes
            }
        }
        
        return nil
    }
    
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        // clear the layout cache for a bounds change (ie: switching portrait <-> landscape)
        if oldBounds.size != newBounds.size {
            cache = []
            oldBounds = newBounds
        }
        return true
    }
    
    override func invalidateLayout() {
        super.invalidateLayout()
        
        cache = []
    }
    
    override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
        
        guard let collectionView = collectionView else {
            return proposedContentOffset
        }
        
        // 'snap to' and center the nearest 'page' (collection view item)
{'fraction_non_alphanumeric': 0.0675998263888889, 'fraction_numerical': 0.009223090277777778, 'mean_word_length': 1.8057838660578387, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, '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': '4809206', 'n_tokens_mistral': 2352, 'n_tokens_neox': 2220, 'n_words': 708}