<commit_before>"""

"""
type MeshSet
  params::Dict

  N::Int64            # number of meshes in the set
  M::Int64            # number of matches in the set - (a -> b) and (b -> a) are distinct

  indices::Array{Index, 1}        # wafer, section, row, column as a tuple - if tileIndex is 0 then denotes entire section

  n::Int64            # number of nodes in the set across the whole set
  m::Int64            # number of edges in the set across the whole set
  m_i::Int64            # number of internal edges in the set across the whole set
  m_e::Int64            # number of edges between meshes
  
  meshes::Array{Mesh, 1}          # vector of meshes in the set
  nodes_indices::Array{Int64, 1}        # vector of number of nodes before the n-th mesh. To get index at i-th node of n-th mesh, nodes_indices[n] + i.

  matches::Array{Matches, 1}        # vector of matches in the set
  matches_pairs::Pairings       # vector of index (in meshes) - (a, b) means the match is between (meshes[a] -> meshes[b])
end


function find_node(Ms, mesh_ind, node_ind)
  return Ms.nodes_indices[mesh_ind] + node_ind
end

function find_index(Ms, mesh_index_tuple::Index)
  return findfirst(this -> mesh_index_tuple == this.index, Ms.meshes)
end

function find_section(Ms, section_num)
  return findfirst(this -> section_num == this.index[2], Ms.meshes)
end

function MeshSet(params::Dict)
  N = 0
  M = 0

  indices = Array{Index, 1}(0)
  
  n = 0
  m = 0
  m_i = 0
  m_e = 0

  meshes = Array{Mesh, 1}(0)
  nodes_indices = Array{Int64, 1}(0)

  matches = Array{Matches, 1}(0)
  matches_pairs = Array{Pair, 1}(0)

  return MeshSet(params, N, M, indices, n, m, m_i, m_e, meshes, nodes_indices, matches, matches_pairs)
end

function add_mesh(Am, Ms)
  push!(Ms.indices, Am.index)
  push!(Ms.meshes, Am)
  if length(Ms.nodes_indices) == 0 push!(Ms.nodes_indices, 0)
  else push!(Ms.nodes_indices, Ms.n)
  end
  Ms.N += 1
  Ms.m_i += Am.m
  Ms.m += Am.m
  Ms.n += Am.n
  return
end

function add_matches(M, Ms)
  if (typeof(M) == Void || M == Void) return; end
  push!(Ms.matches, M)
  push!(Ms.matches_pairs, (find_index(Ms, M.src_index), find_index(Ms, M.dst_index)))
  Ms.M += 1
  Ms.n
  Ms.m += M.n
  Ms.m_e += M.n
  return
end

# JLD SAVE
function save(filename::String, Ms::MeshSet)
  println("Saving meshset to ", filename)
  jldopen(filename, "w") do file
    write(file, "MeshSet", Ms)
  end
end

function save(Ms::MeshSet)
  firstindex = Ms.meshes[1].index
  lastindex = Ms.meshes[Ms.N].index


  if (is_prealigned(firstindex) && is_montaged(lastindex)) || (is_montaged(firstindex) && is_montaged(lastindex))
    filename = joinpath(PREALIGNED_DIR, string(join(firstindex[1:2], ","), "-", join(lastindex[1:2], ","), "_prealigned.jld"))
  elseif (is_prealigned(firstindex) && is_prealigned(lastindex)) || (is_aligned(firstindex) && is_prealigned(lastindex))
    filename = joinpath(ALIGNED_DIR, string(join(firstindex[1:2], ","),  "-", join(lastindex[1:2], ","),"_aligned.jld"))
  else 
    filename = joinpath(MONTAGED_DIR, string(join(firstindex[1:2], ","), "_montaged.jld"))
  end

  save(filename, Ms);
end
# JLS SAVE
#=
function save(filename::String, Ms::MeshSet)
  println("Saving meshset to ", filename)
  open(filename, "w") do file
    serialize(file, Ms)
  end
end

function save(Ms::MeshSet)
  firstindex = Ms.meshes[1].index
  lastindex = Ms.meshes[Ms.N].index


  if (is_prealigned(firstindex) && is_montaged(lastindex)) || (is_montaged(firstindex) && is_montaged(lastindex))
    filename = joinpath(PREALIGNED_DIR, string(join(firstindex[1:2], ","), "-", join(lastindex[1:2], ","), "_prealigned.jls"))
  elseif (is_prealigned(firstindex) && is_prealigned(lastindex)) || (is_aligned(firstindex) && is_prealigned(lastindex))
    filename = joinpath(ALIGNED_DIR, string(join(firstindex[1:2], ","),  "-", join(lastindex[1:2], ","),"_aligned.jls"))
  else 
    filename = joinpath(MONTAGED_DIR, string(join(firstindex[1:2], ","), "_montaged.jls"))
  end

  save(filename, Ms);
end
=#
"""
Load montaged meshset for given wafer and section

`load_montaged(wafer_num, sec_num)`
"""
function load_montaged(wafer_num, sec_num)
  index = (wafer_num, sec_num, 1, 1)
	return load(index, index)
end

"""
Load prealigned meshset for given wafer and section<commit_msg>Readability change<commit_after>"""

"""
type MeshSet
  params::Dict

  N::Int64            # number of meshes in the set
  M::Int64            # number of matches in the set - (a -> b) and (b -> a) are distinct

  indices::Array{Index, 1}        # wafer, section, row, column as a tuple - if tileIndex is 0 then denotes entire section

  n::Int64            # number of nodes in the set across the whole set
  m::Int64            # number of edges in the set across the whole set
  m_i::Int64            # number of internal edges in the set across the whole set
  m_e::Int64            # number of edges between meshes
  
  meshes::Array{Mesh, 1}          # vector of meshes in the set
  nodes_indices::Array{Int64, 1}        # vector of number of nodes before the n-th mesh. To get index at i-th node of n-th mesh, nodes_indices[n] + i.

  matches::Array{Matches, 1}        # vector of matches in the set
  matches_pairs::Pairings       # vector of index (in meshes) - (a, b) means the match is between (meshes[a] -> meshes[b])
end


function find_node(Ms, mesh_ind, node_ind)
  return Ms.nodes_indices[mesh_ind] + node_ind
end

function find_index(Ms, mesh_index_tuple::Index)
  return findfirst(this -> mesh_index_tuple == this.index, Ms.meshes)
end

function find_section(Ms, section_num)
  return findfirst(this -> section_num == this.index[2], Ms.meshes)
end

function MeshSet(params::Dict)
  N = 0
  M = 0

  indices = Array{Index, 1}(0)
  
  n = 0
  m = 0
  m_i = 0
  m_e = 0

  meshes = Array{Mesh, 1}(0)
  nodes_indices = Array{Int64, 1}(0)

  matches = Array{Matches, 1}(0)
  matches_pairs = Array{Pair, 1}(0)

  return MeshSet(params, N, M, indices, n, m, m_i, m_e, meshes, nodes_indices, matches, matches_pairs)
end

function add_mesh(Am, Ms)
  push!(Ms.indices, Am.index)
  push!(Ms.meshes, Am)
  if length(Ms.nodes_indices) == 0 push!(Ms.nodes_indices, 0)
  else push!(Ms.nodes_indices, Ms.n)
  end
  Ms.N += 1
  Ms.m_i += Am.m
  Ms.m += Am.m
  Ms.n += Am.n
  return
end

function add_matches(M, Ms)
  if (typeof(M) == Void || M == Void) return; end
  push!(Ms.matches, M)
  push!(Ms.matches_pairs, (find_index(Ms, M.src_index), find_index(Ms, M.dst_index)))
  Ms.M += 1
  Ms.n
  Ms.m += M.n
  Ms.m_e += M.n
  return
end
# JLD SAVE
function save(filename::String, Ms::MeshSet)
  println("Saving meshset to ", filename)
  jldopen(filename, "w") do file
    write(file, "MeshSet", Ms)
  end
end

function save(Ms::MeshSet)
  firstindex = Ms.meshes[1].index
  lastindex = Ms.meshes[Ms.N].index


  if (is_prealigned(firstindex) && is_montaged(lastindex)) || (is_montaged(firstindex) && is_montaged(lastindex))
    filename = joinpath(PREALIGNED_DIR, string(join(firstindex[1:2], ","), "-", join(lastindex[1:2], ","), "_prealigned.jld"))
  elseif (is_prealigned(firstindex) && is_prealigned(lastindex)) || (is_aligned(firstindex) && is_prealigned(lastindex))
    filename = joinpath(ALIGNED_DIR, string(join(firstindex[1:2], ","),  "-", join(lastindex[1:2], ","),"_aligned.jld"))
  else 
    filename = joinpath(MONTAGED_DIR, string(join(firstindex[1:2], ","), "_montaged.jld"))
  end

  save(filename, Ms);
end
#=
# JLS SAVE
function save(filename::String, Ms::MeshSet)
  println("Saving meshset to ", filename)
  open(filename, "w") do file
    serialize(file, Ms)
  end
end

function save(Ms::MeshSet)
  firstindex = Ms.meshes[1].index
  lastindex = Ms.meshes[Ms.N].index


  if (is_prealigned(firstindex) && is_montaged(lastindex)) || (is_montaged(firstindex) && is_montaged(lastindex))
    filename = joinpath(PREALIGNED_DIR, string(join(firstindex[1:2], ","), "-", join(lastindex[1:2], ","), "_prealigned.jls"))
  elseif (is_prealigned(firstindex) && is_prealigned(lastindex)) || (is_aligned(firstindex) && is_prealigned(lastindex))
    filename = joinpath(ALIGNED_DIR, string(join(firstindex[1:2], ","),  "-", join(lastindex[1:2], ","),"_aligned.jls"))
  else 
    filename = joinpath(MONTAGED_DIR, string(join(firstindex[1:2], ","), "_montaged.jls"))
  end

  save(filename, Ms);
end

=#
"""
Load montaged meshset for given wafer and section

`load_montaged(wafer_num, sec_num)`
"""
function load_montaged(wafer_num, sec_num)
  index = (wafer_num, sec_num, 1, 1)
	return load(index, index)
end

"""
Load prealigned meshset for given wafer and section
{'fraction_non_alphanumeric': 0.1471242825348483, 'fraction_numerical': 0.015462106126273866, 'mean_word_length': 4.034198113207547, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '3253442', 'n_tokens_mistral': 3189, 'n_tokens_neox': 3080, 'n_words': 784}