<commit_before>//
//  CanvasView.swift
//  TortoiseGraphics
//
//  Created by temoki on 2017/04/22.
//  Copyright © 2017 temoki. All rights reserved.
//

#if os(OSX)
    import AppKit
    public typealias View = NSView
    public typealias Image = NSImage
#elseif os(iOS)
    import UIKit
    public typealias View = UIView
    public typealias Image = UIImage
#endif

#if os(OSX) || os(iOS)
    fileprivate class DrawingStack {

        private var images: [CGImage] = []

        func set(_ image: CGImage) {
            objc_sync_enter(self)
            images.removeAll(keepingCapacity: false)
            images.append(image)
            objc_sync_exit(self)
        }

        func clear() {
            objc_sync_enter(self)
            images.removeAll(keepingCapacity: false)
            objc_sync_exit(self)
        }

        func push(_ image: CGImage) {
            objc_sync_enter(self)
            images.append(image)
            objc_sync_exit(self)
        }

        func pop() -> CGImage? {
            guard !images.isEmpty else { return nil }
            objc_sync_enter(self)
            let image = images.removeFirst()
            objc_sync_exit(self)
            return image
        }

    }
#endif

#if os(OSX)
    fileprivate extension NSImage {
        var cgImage: CGImage? {
            var rect = CGRect(origin: .zero, size: self.size)
            return cgImage(forProposedRect: &rect, context: nil, hints: nil)
        }
    }
#endif

#if os(OSX) || os(iOS)
    /// Canvas View
    public class CanvasView: View {

        // MARK: - Properties

        /// Canvas
        public let canvas: Canvas

        private var drawingStack = DrawingStack()

        /// Initializer
        /// - parameter canvasSize: Canvas size
        /// - parameter tortoise: Tortoise icon image
        public init(canvasSize: CGSize, tortoise image: Image? = nil) {
            self.canvas = Canvas(size: canvasSize, tortoise: image?.cgImage)
            super.init(frame: CGRect(origin: .zero, size: canvasSize))
        }

        public required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }

        // MARK: - Draw

        /// Draw
        public func draw() {
            guard let image = canvas.draw() else { return }
            drawingStack.set(image)
            updateDisplay()
        }

        /// Draw with animation
        /// - parameter atTimeInterval : time interval of animation
        public func animate(atTimeInterval interval: TimeInterval, completion: (() -> Void)? = nil) {
            drawingStack.clear()
            DispatchQueue.global().async { [unowned self] in
                self.canvas.draw(oneByOne: { (image) in
                    Thread.sleep(forTimeInterval: interval)
                    self.drawingStack.push(image)
                    DispatchQueue.main.async {
                        self.updateDisplay()
                    }
                })
                DispatchQueue.main.async {
                    completion?()
                }
            }
        }

        // MARK: - Override

        #if os(OSX)
        public override func draw(_ dirtyRect: NSRect) {
            super.draw(dirtyRect)
            guard let cgContext = NSGraphicsContext.current()?.cgContext else { return }
            draw(with: cgContext)
        }
        #elseif os(iOS)
        public override func draw(_ rect: CGRect) {
        super.draw(rect)
        guard let cgContext = UIGraphicsGetCurrentContext() else { return }
        draw(with: cgContext)
        }
        #endif

        // MARK: - Private

        private func draw(with cgContext: CGContext) {
            guard let image = drawingStack.pop() else { return }
            cgContext.saveGState()
            cgContext.draw(image, in: self.bounds)
            cgContext.restoreGState()
<commit_msg>re-indent<commit_after>//
//  CanvasView.swift
//  TortoiseGraphics
//
//  Created by temoki on 2017/04/22.
//  Copyright © 2017 temoki. All rights reserved.
//

#if os(OSX)
    import AppKit
    public typealias View = NSView
    public typealias Image = NSImage
#elseif os(iOS)
    import UIKit
    public typealias View = UIView
    public typealias Image = UIImage
#endif

#if os(OSX) || os(iOS)
    fileprivate class DrawingStack {

        private var images: [CGImage] = []

        func set(_ image: CGImage) {
            objc_sync_enter(self)
            images.removeAll(keepingCapacity: false)
            images.append(image)
            objc_sync_exit(self)
        }

        func clear() {
            objc_sync_enter(self)
            images.removeAll(keepingCapacity: false)
            objc_sync_exit(self)
        }

        func push(_ image: CGImage) {
            objc_sync_enter(self)
            images.append(image)
            objc_sync_exit(self)
        }

        func pop() -> CGImage? {
            guard !images.isEmpty else { return nil }
            objc_sync_enter(self)
            let image = images.removeFirst()
            objc_sync_exit(self)
            return image
        }

    }
#endif

#if os(OSX)
    fileprivate extension NSImage {
        var cgImage: CGImage? {
            var rect = CGRect(origin: .zero, size: self.size)
            return cgImage(forProposedRect: &rect, context: nil, hints: nil)
        }
    }
#endif

#if os(OSX) || os(iOS)
    /// Canvas View
    public class CanvasView: View {

        // MARK: - Properties

        /// Canvas
        public let canvas: Canvas

        private var drawingStack = DrawingStack()

        /// Initializer
        /// - parameter canvasSize: Canvas size
        /// - parameter tortoise: Tortoise icon image
        public init(canvasSize: CGSize, tortoise image: Image? = nil) {
            self.canvas = Canvas(size: canvasSize, tortoise: image?.cgImage)
            super.init(frame: CGRect(origin: .zero, size: canvasSize))
        }

        public required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }

        // MARK: - Draw

        /// Draw
        public func draw() {
            guard let image = canvas.draw() else { return }
            drawingStack.set(image)
            updateDisplay()
        }

        /// Draw with animation
        /// - parameter atTimeInterval : time interval of animation
        public func animate(atTimeInterval interval: TimeInterval, completion: (() -> Void)? = nil) {
            drawingStack.clear()
            DispatchQueue.global().async { [unowned self] in
                self.canvas.draw(oneByOne: { (image) in
                    Thread.sleep(forTimeInterval: interval)
                    self.drawingStack.push(image)
                    DispatchQueue.main.async {
                        self.updateDisplay()
                    }
                })
                DispatchQueue.main.async {
                    completion?()
                }
            }
        }

        // MARK: - Override

        #if os(OSX)
        public override func draw(_ dirtyRect: NSRect) {
            super.draw(dirtyRect)
            guard let cgContext = NSGraphicsContext.current()?.cgContext else { return }
            draw(with: cgContext)
        }
        #elseif os(iOS)
        public override func draw(_ rect: CGRect) {
            super.draw(rect)
            guard let cgContext = UIGraphicsGetCurrentContext() else { return }
            draw(with: cgContext)
        }
        #endif

        // MARK: - Private

        private func draw(with cgContext: CGContext) {
            guard let image = drawingStack.pop() else { return }
            cgContext.saveGState()
            cgContext.draw(image, in: self.bounds)
            cgContext.restoreGState()

{'fraction_non_alphanumeric': 0.09598041741819119, 'fraction_numerical': 0.003091986601391394, 'mean_word_length': 1.6476807639836288, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2327183', 'n_tokens_mistral': 2225, 'n_tokens_neox': 2098, 'n_words': 534}