<commit_before>public struct ImageSlice<Pixel> : ImageProtocol {
    internal var image: Image<Pixel>
    internal let xRange: CountableRange<Int>
    internal let yRange: CountableRange<Int>
    
    public init(image: Image<Pixel>, xRange: CountableRange<Int>, yRange: CountableRange<Int>) {
        precondition((0..<image.width).isSuperset(of: xRange), "`xRange` is out of bounds: \(xRange)")
        precondition((0..<image.height).isSuperset(of: yRange), "`yRange` is out of bounds: \(yRange)")
        self.image = image
        self.xRange = xRange
        self.yRange = yRange
    }
    
    public var width: Int {
        return xRange.count
    }
    
    public var height: Int {
        return yRange.count
    }

    public var pixels: [Pixel] {
        return map { $0 }
    }
}

extension ImageSlice {
    public var count: Int {
        return width * height
    }
}

extension ImageSlice {
    public subscript(x: Int, y: Int) -> Pixel {
        get {
            precondition(xRange.contains(x), "`x` is out of bounds: \(x)")
            precondition(yRange.contains(y), "`y` is out of bounds: \(y)")
            return image[x, y]
        }
        
        set {
            precondition(xRange.contains(x), "`x` is out of bounds: \(x)")
            precondition(yRange.contains(y), "`y` is out of bounds: \(y)")
            image[x, y] = newValue
        }
    }
}

extension ImageSlice { // safe get
    public func pixelAt(x: Int, y: Int) -> Pixel? {
        guard xRange.contains(x) else { return nil }
        guard yRange.contains(y) else { return nil }
        return image[x, y]
    }
}

extension ImageSlice {
    public func makeIterator() -> PixelGenerator<Pixel> {
        return PixelGenerator(image: image, xRange: validRange(xRange, maxValue: image.width), yRange: validRange(yRange, maxValue: image.height))
    }
}

private func validRange(_ range: CountableRange<Int>, maxValue: Int) -> CountableRange<Int> {
    return max(0, range.lowerBound)..<min(maxValue, range.upperBound)
}

public struct PixelGenerator<Pixel>: IteratorProtocol {
    public typealias Element = Pixel
    
    private let image: Image<Pixel>
    
    private let xRange: CountableRange<Int>
    private let yRange: CountableRange<Int>
    <commit_msg>Improve the implementation of `PixelIterator`<commit_after>public struct ImageSlice<Pixel> : ImageProtocol {
    internal var image: Image<Pixel>
    internal let xRange: CountableRange<Int>
    internal let yRange: CountableRange<Int>
    
    public init(image: Image<Pixel>, xRange: CountableRange<Int>, yRange: CountableRange<Int>) {
        precondition((0..<image.width).isSuperset(of: xRange), "`xRange` is out of bounds: \(xRange)")
        precondition((0..<image.height).isSuperset(of: yRange), "`yRange` is out of bounds: \(yRange)")
        self.image = image
        self.xRange = xRange
        self.yRange = yRange
    }
    
    public var width: Int {
        return xRange.count
    }
    
    public var height: Int {
        return yRange.count
    }

    public var pixels: [Pixel] {
        return map { $0 }
    }
}

extension ImageSlice {
    public var count: Int {
        return width * height
    }
}

extension ImageSlice {
    public subscript(x: Int, y: Int) -> Pixel {
        get {
            precondition(xRange.contains(x), "`x` is out of bounds: \(x)")
            precondition(yRange.contains(y), "`y` is out of bounds: \(y)")
            return image[x, y]
        }
        
        set {
            precondition(xRange.contains(x), "`x` is out of bounds: \(x)")
            precondition(yRange.contains(y), "`y` is out of bounds: \(y)")
            image[x, y] = newValue
        }
    }
}

extension ImageSlice { // safe get
    public func pixelAt(x: Int, y: Int) -> Pixel? {
        guard xRange.contains(x) else { return nil }
        guard yRange.contains(y) else { return nil }
        return image[x, y]
    }
}

extension ImageSlice {
    public func makeIterator() -> PixelIterator<Pixel> {
        return PixelIterator(image: image, xRange: xRange, yRange: yRange)
    }
}

public struct PixelIterator<Pixel>: IteratorProtocol {
    public typealias Element = Pixel
    
    private let image: Image<Pixel>
    
    private let xRange: CountableRange<Int>
    private let yRange: CountableRange<Int>
    
{'fraction_non_alphanumeric': 0.11849109002545707, 'fraction_numerical': 0.0016199953714417959, 'mean_word_length': 2.519543973941368, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 36, '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': '953147', 'n_tokens_mistral': 1340, 'n_tokens_neox': 1315, 'n_words': 368}