<commit_before>///
///             \    |         | |
///            _ \   __|  _ \  | |
///           ___ \  |   (   | | |
///         _/    _\\__|\___/ _|_|
///
///
///  MIT License | Copyright 2017 BVR, LLC

import Foundation
import Accelerate
import simd

/// Represents an ordered, fixed sized, reference semantic list of `SignedNumeric` elements that conforms to
/// `MutableCollection` and `RandomAccessCollection`.
///
public struct List<Element: Real> {
    internal var value: UnsafeMutablePointer<Element>

    public var pointer: UnsafeMutablePointer<Element> {
        return value
    }

    public let count: Int

    public var countPointer: [Int32] {
        return [Int32(count)]
    }

    public var vDSP_Length: vDSP_Length {
        return UInt(count)
    }

    public var halfIndex: List.Index {
        return self.endIndex / 2
    }

    public var firstHalf: MutableRandomAccessSlice<List<Element>> {
        return self[startIndex..<halfIndex]
    }

    public init(repeating element: Element, count: Int) {
        self.count = count
        self.value = UnsafeMutablePointer<Element>.allocate(capacity: count)
        _ = UnsafeMutableBufferPointer(start: value, count: count).initialize(from: (0..<count).map({ _ in return element }))
    }

    public init(count: Int) {
        self.init(repeating: 0, count: count)
    }

    public init(ones count: Int) {
        self.init(repeating: 1, count: count)
    }

    public func copy() -> List<Element> {
        let result = List<Element>.init(repeating: 0, count: count)
        result.value.assign(from: value, count: count)
        return result
    }

    /// Takes the first half of the list, reverses it, and replaces the later half with the resulting values.
    public func mirrored() -> List<Element> {
        var result = self.copy()
        result.mirror()
        return result
    }
    
    /// Takes the first half of the list, reverses it, and replaces the later half with the resulting values.
    public mutating func mirror() {
        let range: Range<List.Index> = self.endIndex % 2 == 0 ? self.halfIndex..<endIndex : self.halfIndex + 1..<endIndex
        self.replaceSubrange(range, with: firstHalf.reversed())
    }
}

extension List: ExpressibleByArrayLiteral {
    public typealias ArrayLiteralElement = Element

    public init(arrayLiteral elements: Element...) {
        self.init(repeating: 0, count: elements.count)
        for (i, element) in elements.enumerated() {
            self[i] = element
        }
    }
}

extension List: MutableCollection, RandomAccessCollection, RangeReplaceableCollection {

    public init() {
        self.init(count: 0)
    }

    public mutating func replaceSubrange<C: Collection>(
        _ subrange: Range<Index>,
        with newElements: C
    ) where C.Iterator.Element == Element {
        assert(subrange.lowerBound >= startIndex && subrange.upperBound <= endIndex)
        _ = UnsafeMutableBufferPointer(start: value + subrange.lowerBound, count: count - subrange.lowerBound).initialize(from: newElements)
    }

    public typealias Index = Int

    public subscript(index: Index) -> Element {
        get {
            return UnsafeMutableBufferPointer(start: value, count: count)[index]
        }
        set(newValue) {
            UnsafeMutableBufferPointer(start: value, count: count)[index] = newValue
        }
    }

    public var startIndex: Int {
        return UnsafeMutableBufferPointer(start: value, count: count).startIndex
    }

    public var endIndex: Int {
        return UnsafeMutableBufferPointer(start: value, count: count).endIndex
    }
}

extension List: CustomDebugStringConvertible {
    public var debugDescription: String {
        return self.map({ $0 }).debugDescription
    }
}
<commit_msg>Wrapping pointer to fix potential leak<commit_after>///
///             \    |         | |
///            _ \   __|  _ \  | |
///           ___ \  |   (   | | |
///         _/    _\\__|\___/ _|_|
///
///
///  MIT License | Copyright 2017 BVR, LLC

import Foundation
import Accelerate
import simd

/// Represents an ordered, fixed sized, reference semantic list of `SignedNumeric` elements that conforms to
/// `MutableCollection` and `RandomAccessCollection`.
///
public struct List<Element: Real> {
    
    internal class WrappedValue {
        let pointer: UnsafeMutablePointer<Element>
        let count: Int
        init(count: Int) {
            self.count = count
            self.pointer = UnsafeMutablePointer<Element>.allocate(capacity: count)
        }
        
        deinit {
            pointer.deallocate(capacity: count)
        }
    }
    fileprivate var wrappedValue: WrappedValue
    
    internal var value: UnsafeMutablePointer<Element> {
        return self.wrappedValue.pointer
    }

    public var pointer: UnsafeMutablePointer<Element> {
        return value
    }

    public let count: Int

    public var countPointer: [Int32] {
        return [Int32(count)]
    }

    public var vDSP_Length: vDSP_Length {
        return UInt(count)
    }

    public var halfIndex: List.Index {
        return self.endIndex / 2
    }

    public var firstHalf: MutableRandomAccessSlice<List<Element>> {
        return self[startIndex..<halfIndex]
    }

    public init(repeating element: Element, count: Int) {
        self.count = count
        self.wrappedValue = WrappedValue(count: count)
        _ = UnsafeMutableBufferPointer(start: value, count: count).initialize(from: (0..<count).map({ _ in return element }))
    }

    public init(count: Int) {
        self.init(repeating: 0, count: count)
    }

    public init(ones count: Int) {
        self.init(repeating: 1, count: count)
    }

    public func copy() -> List<Element> {
        let result = List<Element>.init(repeating: 0, count: count)
        result.value.assign(from: value, count: count)
        return result
    }

    /// Takes the first half of the list, reverses it, and replaces the later half with the resulting values.
    public func mirrored() -> List<Element> {
        var result = self.copy()
        result.mirror()
        return result
    }
    
    /// Takes the first half of the list, reverses it, and replaces the later half with the resulting values.
    public mutating func mirror() {
        let range: Range<List.Index> = self.endIndex % 2 == 0 ? self.halfIndex..<endIndex : self.halfIndex + 1..<endIndex
        self.replaceSubrange(range, with: firstHalf.reversed())
    }
}

extension List: ExpressibleByArrayLiteral {
    public typealias ArrayLiteralElement = Element

    public init(arrayLiteral elements: Element...) {
        self.init(repeating: 0, count: elements.count)
        for (i, element) in elements.enumerated() {
            self[i] = element
        }
    }
}

extension List: MutableCollection, RandomAccessCollection, RangeReplaceableCollection {

    public init() {
        self.init(count: 0)
    }

    public mutating func replaceSubrange<C: Collection>(
        _ subrange: Range<Index>,
        with newElements: C
    ) where C.Iterator.Element == Element {
        assert(subrange.lowerBound >= startIndex && subrange.upperBound <= endIndex)
        _ = UnsafeMutableBufferPointer(start: value + subrange.lowerBound, count: count - subrange.lowerBound).initialize(from: newElements)
    }

    public typealias Index = Int

    public subscript(index: Index) -> Element {
        get {
            return UnsafeMutableBufferPointer(start: value, count: count)[index]
        }
        set(newValue) {
            UnsafeMutableBufferPointer(start: value, count: count)[index] = newValue
        }
    }

    public var startIndex: Int {
        return UnsafeMutableBufferPointer(start: value, count: count).startIndex
    }

    public var endIndex: Int {
        return UnsafeMutableBufferPointer(start: value, count: count).endIndex
    }
}

extension List: CustomDebugStringConvertible {
    public var debugDescription: String {
        return self.map({ $0 }).debugDescription
    }
}

{'fraction_non_alphanumeric': 0.10359012715033658, 'fraction_numerical': 0.004736973323360758, 'mean_word_length': 2.4626672421234357, 'pattern_counts': {'":': 0, '<': 38, '<?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': '756540', 'n_tokens_mistral': 2339, 'n_tokens_neox': 2231, 'n_words': 608}