<commit_before>//
//  ReuseContainer.swift
//  PageKit
//
//  Created by Jack on 4/24/17.
//  Copyright © 2017 Jack. All rights reserved.
//

import Foundation

/// Data source for container
public protocol ReuseContainerDataSource: ContainerDataSource {
    /// Asks for page by given index
    ///
    /// - Parameters:
    ///   - container: Container instanse
    ///   - index: Index
    /// - Returns: Page
    func container(_ container: ReuseContainer, pageForIndexAt index: Int) -> Page
}

/// Container with reuseable
open class ReuseContainer: Container {
    
    /// Aleardy registed pages
    private var registedPages: [String: Page.Type] = [:]
    /// Reusable pages
    private var reuseablePages: [String: Page] = [:]
    /// Visible pages
    private var visiblePages: [Page?] = []
    
    open override func reloadData() {
        super.reloadData()
        reuseablePages = [:]
        visiblePages = Array(repeating: nil, count: numberOfPages)
        visiblePages.reserveCapacity(numberOfPages)
        dynamicPage()
    }
    
    open func dynamicPage() {
        let visibleIndexs = visiblePagesIndexs()
        ///*********************************************************************
        ///                     (page is visible)
        ///             true                         false
        ///         (page == nil)                (page == nil)
        ///    true              false       true              false
        /// (new page)             (do nothing)            (remove old page)
        ///*********************************************************************
        for (index, page) in visiblePages.enumerated() {
            
            if visibleIndexs.contains(index),
                page == nil {
                load((dataSource as! ReuseContainerDataSource).container(self, pageForIndexAt: index), withIndex: index)
            }
            
            if !visibleIndexs.contains(index),
                let inVisiblePage = page {<commit_msg>Bug fix for ReuseContainer<commit_after>//
//  ReuseContainer.swift
//  PageKit
//
//  Created by Jack on 4/24/17.
//  Copyright © 2017 Jack. All rights reserved.
//

import Foundation

/// Data source for container
public protocol ReuseContainerDataSource: ContainerDataSource {
    /// Asks for page by given index
    ///
    /// - Parameters:
    ///   - container: Container instanse
    ///   - index: Index
    /// - Returns: Page
    func container(_ container: ReuseContainer, pageForIndexAt index: Int) -> Page
}

/// Container with reuseable
open class ReuseContainer: Container {
    
    /// Aleardy registed pages
    private var registedPages: [String: Page.Type] = [:]
    /// Reusable pages
    private var reuseablePages: [String: Page] = [:]
    /// Visible pages
    private var visiblePages: [Page?] = []
    
    open override func reloadData() {
        super.reloadData()
        reuseablePages = [:]
        visiblePages.filter{ $0 != nil }.forEach{ removeSubPage($0!) }
        visiblePages = Array(repeating: nil, count: numberOfPages)
        visiblePages.reserveCapacity(numberOfPages)
        dynamicPage()
    }
    
    open func dynamicPage() {
        let visibleIndexs = visiblePagesIndexs()
        ///*********************************************************************
        ///                     (page is visible)
        ///             true                         false
        ///         (page == nil)                (page == nil)
        ///    true              false       true              false
        /// (new page)             (do nothing)            (remove old page)
        ///*********************************************************************
        for (index, page) in visiblePages.enumerated() {
            
            if visibleIndexs.contains(index),
                page == nil {
                load((dataSource as! ReuseContainerDataSource).container(self, pageForIndexAt: index), withIndex: index)
            }
            
            if !visibleIndexs.contains(index),
                let inVisiblePage = page {
{'fraction_non_alphanumeric': 0.168212890625, 'fraction_numerical': 0.0048828125, 'mean_word_length': 1.7965870307167235, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5081279', 'n_tokens_mistral': 1039, 'n_tokens_neox': 979, 'n_words': 278}