<commit_before>//
//  Utility.swift
//  Carpaccio
//
//  Created by <NAME> on 30/07/16.
//  Copyright © 2016 <NAME> & Co. All rights reserved.
//

import Foundation
import QuartzCore

public enum PrecisionScheme {
    /** Return precise value as-is. */
    case precise

    /** Return precise value floored. */
    case floored

    /** Return precise value rounded. */
    case rounded

    func applied<T: FloatingPoint>(to preciseValue: T) -> T {
        switch self {
        case .precise:
            return preciseValue
        case .floored:
            return floor(preciseValue)
        case .rounded:
            return round(preciseValue)
        }
    }

    public static var defaultPrecisionScheme: PrecisionScheme {
        return .rounded
    }
}

public extension CGFloat {
    static var unconstrained: CGFloat {
        infinity
    }
}

public extension CGSize {
    init(constrainWidth width: CGFloat) {
        self.init(width: width, height: CGFloat.unconstrained)
    }

    init(constrainHeight height: CGFloat) {
        self.init(width: CGFloat.unconstrained, height: height)
    }<commit_msg>For #15, adjust precision schemes to hopefully match what Image I/O does with kCGImageSourceThumbnailMaxPixelSize<commit_after>//
//  Utility.swift
//  Carpaccio
//
//  Created by <NAME> on 30/07/16.
//  Copyright © 2016 <NAME> & Co. All rights reserved.
//

import Foundation
import QuartzCore

public enum PrecisionScheme {
    /// Return precise value as-is.
    case precise

    /// Return precise value rounded down, using `FloatingPointRoundingRule.down`. Equivalent to `floor(preciseValue)`.
    case floored

    /// Return precise value rounded, as per "schoolbook rounding" rule of `FloatingPointRoundingRule.toNearestOrAwayFromZero`.
    case rounded

    /// Return precise value rounded up, using `FloatingPointRoundingRule.up`. Equivalent to `ceil(preciseValue)`.
    case roundedUp

    func applied<T: FloatingPoint>(to preciseValue: T) -> T {
        switch self {
        case .precise:
            return preciseValue
        case .floored:
            return preciseValue.rounded(.down)
        case .rounded:
            return preciseValue.rounded(.toNearestOrAwayFromZero)
        case .roundedUp:
            return preciseValue.rounded(.up)
        }
    }

    ///
    /// Rounding scheme that should match Image I/O's behavior when loading images using `CGImageSourceCreateThumbnailAtIndex()`,
    /// providing a scaled target size via the `kCGImageSourceThumbnailMaxPixelSize` option.
    ///
    /// Unfortunately this is not explicitly documented, but has been observed to match `.roundedUp`, which is returned here.
    ///
    public static var imageIOMaxPixelSizePrecisionScheme: PrecisionScheme {
        return .roundedUp
    }

    /// Default precision scheme to use. Returns `.imageIOMaximumPixelDimensionPrecisionScheme`.
    public static var defaultPrecisionScheme: PrecisionScheme {
        return .imageIOMaxPixelSizePrecisionScheme
    }
}

public extension CGFloat {
    static var unconstrained: CGFloat {
        infinity
    }
}

public extension CGSize {
    init(constrainWidth width: CGFloat) {
        self.init(width: width, height: CGFloat.unconstrained)
    }

    init(constrainHeight height: CGFloat) {
        self.init(width: CGFloat.unconstrained, height: height)
    }
{'fraction_non_alphanumeric': 0.08738443185207277, 'fraction_numerical': 0.00656128839844915, 'mean_word_length': 2.6456521739130436, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '1355890', 'n_tokens_mistral': 1005, 'n_tokens_neox': 928, 'n_words': 281}