<commit_before>use "collections"
class Matrix[A, n: USize, dims: Vector[USize, #n] val]
  embed _data: Vector[A, # _alloc()]

  fun tag _alloc(): USize =>
    var i: USize = 0
    var acc: USize = 1
    while i < n do
      acc = acc * dims._apply(i = i + 1)
    end
    acc

  new undefined[B: (A & Real[B] val & Number) = A]() =>
    """
    Create a matrix of elements, populating them with random memory. This
    is only allowed for a vector of numbers.
    """
    _data = Vector[A, # _alloc()]._create()

  new generate(f: {ref(USize): A^ ?} ref) ? =>
    """
    Create a matrix initiliased using a generator function
    """
    _data = Vector[A, # _alloc()].generate(f)

  fun size(i: USize): USize ? =>
    dims(i)

  fun _calculate_address(indices: Vector[USize, #n]): USize ? =>
    if n == 0 then return 0 end
    var address: USize = 0
    var i: USize = 0
    while i < (n - 1) do
      if indices(i) > dims(i) then error end
      address = (address + indices(i)) * dims(i + 1)
      i = i + 1
    end
    if indices(i) > dims(i) then error end
    address + indices(i)
    
  fun apply(indices: Vector[USize, #n]): this->A ? =>
    _data._apply(_calculate_address(indices))

  fun ref update(indices: Vector[USize, #n], value: A): A^ ? =>
    _data._update(_calculate_address(indices), consume value)<commit_msg>making the matrix api more consistent<commit_after>use "collections"
class Matrix[A, n: USize, dims: Vector[USize, # n] val]
  embed _data: Vector[A, # _alloc()]

  fun tag _alloc(): USize =>
    var i: USize = 0
    var acc: USize = 1
    while i < n do
      acc = acc * dims._apply(i = i + 1)
    end
    acc

  new undefined[B: (A & Real[B] val & Number) = A]() =>
    """
    Create a matrix of elements, populating them with random memory. This
    is only allowed for a vector of numbers.
    """
    _data = Vector[A, # _alloc()]._create()

  new generate(f: {ref(USize): A^ ?} ref) ? =>
    """
    Create a matrix initiliased using a generator function
    """
    _data = Vector[A, # _alloc()].generate(f)

  fun size(i: USize): USize ? =>
    dims(i)

  fun _calculate_address(indices: Vector[USize, # n]): USize ? =>
    if n == 0 then return 0 end
    var address: USize = 0
    var i: USize = 0
    while i < (n - 1) do
      if indices(i) > dims(i) then error end
      address = (address + indices(i)) * dims(i + 1)
      i = i + 1
    end
    if indices(i) > dims(i) then error end
    address + indices(i)
    
  fun apply(indices: Vector[USize, # n]): this->A ? =>
    _data._apply(_calculate_address(indices))

  fun ref update(indices: Vector[USize, # n], value: A): A^ ? =>
    _data._update(_calculate_address(indices), consume value)
{'fraction_non_alphanumeric': 0.14434523809523808, 'fraction_numerical': 0.00744047619047619, 'mean_word_length': 2.755586592178771, 'pattern_counts': {'":': 0, '<': 7, '<?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': '7561372', 'n_tokens_mistral': 976, 'n_tokens_neox': 942, 'n_words': 308}