<commit_before>// Copyright 2016 The Shear Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

import Foundation

/// A protocol defining the interface for all multi-dimensional array.
public protocol TensorProtocol: CustomStringConvertible {
    
    // MARK: - Associated Types
    
    /// The type of elements stored by this `TensorProtocol`.
    associatedtype Element
    
    // MARK: - Properties
    
    /// The shape (length in each demision) of this `TensorProtocol`.
    /// The last element is the count of columns; the first is the count along the `TensorProtocol`'s highest dimension.
    ///
    /// e.g.
    ///     If the Tensor is a 3 by 4 matrix, its shape is [3, 4]
    ///     If the Tensor is a vector of 7 elements, its shape is [7]
    ///     If the Tensor is a scalar, its shape is []
    ///     If the Tensor is empty, its shape is also []
    var shape: [Int] { get }
    
    /// A view that provides a `CollectionType` over all the items stored in the array.
    ///
    /// The first element is at the all-zeros index of the array.
    /// Elements thereafter are in row-major ordering.
    var allElements: AnyRandomAccessCollection<Element> { get }
    // TODO: Consider renaming "elementsView" "flatView" "linearView", something else that makes it clear you lose the position information
    
    /// Returns true iff TensorProtocol refers to parts of another array or the wholes of multiple other array.
    /// I.e. if there's any reason (decreasing memory usage, improving locality) to realloc the array.
    var unified: Bool { get }
    
    // MARK: - Methods
    
    /// Returns the element for the given set of indices.
    subscript(indices: Int...) -> Element { get }
    
    /// Returns the element for the given set of indices.
    subscript(indices: [Int]) -> Element { get }
    
    /// Returns an `TensorSlice` view into the base `TensorProtocol` determined by the set of `TensorIndex`s.
    subscript(indices: TensorIndex...) -> Tensor<Element> { get }
    
    /// Returns an `TensorSlice` view into the base `TensorProtocol` determined by the set of `TensorIndex`s.
    subscript(indices: [TensorIndex]) -> Tensor<Element> { get }
    
    /// Returns the element for the given linear index.
    subscript(linear linear: Int) -> Element { get }
    
    /// Returns a sequence containing pairs of cartesian indices and `Element`s.
    func coordinate() -> AnySequence<([Int], Element)>
    
}

// MARK: - Basic informational queries
public extension TensorProtocol {
    
    /// The number of non-unitary demensions of this TensorProtocol.
    ///
    /// e.g.
    ///     If the TensorProtocol represents a 3 by 4 matrix, its rank is 2
    ///     If the TensorProtocol is a vector of 5 elements, its rank is 1
    ///     If the TensorProtocol is a scalar or the Empty TensorProtocol, its rank is 0
    var rank: Int {
        return shape.count
    }
    
    /// Returns true iff `self` is empty.
    var isEmpty: Bool {
        return allElements.isEmpty
    }
    
    /// Returns true iff `self` is scalar.
    var isScalar: Bool {
        return !isEmpty && rank == 0
    }
    
    /// If `self` is a scalar in an TensorProtocol box, returns the scalar value.
    /// Otherwise returns nil.
    var scalar: Element? {
        guard isScalar else { return nil }
        
        return allElements.first
    }
    
    /// Returns true iff `self` is a vector.
    var isVector: Bool {<commit_msg>Cleanup: .scalar as self[] instead of allElements.first<commit_after>// Copyright 2016 The Shear Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

import Foundation

/// A protocol defining the interface for all multi-dimensional array.
public protocol TensorProtocol: CustomStringConvertible {
    
    // MARK: - Associated Types
    
    /// The type of elements stored by this `TensorProtocol`.
    associatedtype Element
    
    // MARK: - Properties
    
    /// The shape (length in each demision) of this `TensorProtocol`.
    /// The last element is the count of columns; the first is the count along the `TensorProtocol`'s highest dimension.
    ///
    /// e.g.
    ///     If the Tensor is a 3 by 4 matrix, its shape is [3, 4]
    ///     If the Tensor is a vector of 7 elements, its shape is [7]
    ///     If the Tensor is a scalar, its shape is []
    ///     If the Tensor is empty, its shape is also []
    var shape: [Int] { get }
    
    /// A view that provides a `CollectionType` over all the items stored in the array.
    ///
    /// The first element is at the all-zeros index of the array.
    /// Elements thereafter are in row-major ordering.
    var allElements: AnyRandomAccessCollection<Element> { get }
    // TODO: Consider renaming "elementsView" "flatView" "linearView", something else that makes it clear you lose the position information
    
    /// Returns true iff TensorProtocol refers to parts of another array or the wholes of multiple other array.
    /// I.e. if there's any reason (decreasing memory usage, improving locality) to realloc the array.
    var unified: Bool { get }
    
    // MARK: - Methods
    
    /// Returns the element for the given set of indices.
    subscript(indices: Int...) -> Element { get }
    
    /// Returns the element for the given set of indices.
    subscript(indices: [Int]) -> Element { get }
    
    /// Returns an `TensorSlice` view into the base `TensorProtocol` determined by the set of `TensorIndex`s.
    subscript(indices: TensorIndex...) -> Tensor<Element> { get }
    
    /// Returns an `TensorSlice` view into the base `TensorProtocol` determined by the set of `TensorIndex`s.
    subscript(indices: [TensorIndex]) -> Tensor<Element> { get }
    
    /// Returns the element for the given linear index.
    subscript(linear linear: Int) -> Element { get }
    
    /// Returns a sequence containing pairs of cartesian indices and `Element`s.
    func coordinate() -> AnySequence<([Int], Element)>
    
}

// MARK: - Basic informational queries
public extension TensorProtocol {
    
    /// The number of non-unitary demensions of this TensorProtocol.
    ///
    /// e.g.
    ///     If the TensorProtocol represents a 3 by 4 matrix, its rank is 2
    ///     If the TensorProtocol is a vector of 5 elements, its rank is 1
    ///     If the TensorProtocol is a scalar or the Empty TensorProtocol, its rank is 0
    var rank: Int {
        return shape.count
    }
    
    /// Returns true iff `self` is empty.
    var isEmpty: Bool {
        return allElements.isEmpty
    }
    
    /// Returns true iff `self` is scalar.
    var isScalar: Bool {
        return !isEmpty && rank == 0
    }
    
    /// If `self` is a scalar in an TensorProtocol box, returns the scalar value.
    /// Otherwise returns nil.
    var scalar: Element? {
        guard isScalar else { return nil }
        
        return self[]
    }
    
    /// Returns true iff `self` is a vector.
    var isVector: Bool {
{'fraction_non_alphanumeric': 0.09237680752491928, 'fraction_numerical': 0.00477326968973747, 'mean_word_length': 2.2829493087557604, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, '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': '2102011', 'n_tokens_mistral': 1926, 'n_tokens_neox': 1851, 'n_words': 874}