<commit_before>use Search;

class DefaultSparseDom: BaseSparseDom {
  param rank : int;
  type idxType;
  var parentDom: domain(rank, idxType);
  var dist: DefaultDist;
  var nnz = 0;  // intention is that user might specify this to avoid reallocs

  var nnzDomSize = nnz;
  var nnzDom = [1..nnzDomSize];

  var indices: [nnzDom] index(rank);

  def linksDistribution() param return false;

  def DefaultSparseDom(param rank, type idxType, 
                               dist: DefaultDist,
                               parentDom: domain(rank, idxType)) {
    this.parentDom = parentDom;
    this.dist = dist;
    nnz = 0;
  }

  def dsiNumIndices return nnz;

  def dsiBuildArray(type eltType)
    return new DefaultSparseArr(eltType=eltType, rank=rank, idxType=idxType,
                                dom=this);

  def dsiIndsIterSafeForRemoving() {
    for i in 1..nnz by -1 {
      yield indices(i);
    }
  }

  def these() {
    for i in 1..nnz {
      yield indices(i);
    }
  }

  def these(param tag: iterator) where tag == iterator.leader {
    compilerWarning("parallel iteration over sparse domain has been serialized (see note in $CHPL_HOME/STATUS)");
    yield this;
  }

  def these(param tag: iterator, follower: DefaultSparseDom) where tag == iterator.follower {
    var sameDomain = (follower == this);
    if (!sameDomain) then
      halt("Sparse domains can't be zippered with anything other than themselves and their arrays");

    for i in 1..nnz do
      yield indices(i);
  }

  def these(param tag: iterator, follower: DefaultSparseArr) where tag == iterator.follower {
    var sameDomain = false;
    for a in _arrs do
      if (follower == a) then sameDomain = true;
    if (!sameDomain) then
      halt("Sparse domains can't be zippered with anything other than themselves and their arrays");

    for i in 1..nnz do
      yield indices(i);
  }

  def these(param tag: iterator, follower) where tag == iterator.follower {
    compilerError("Sparse iterators can't yet be zippered with others");
  }

  def dsiDim(d : int) {
    return parentDom.dim(d);
  }

  // private
  def find(ind) {
    //
    // sjd: unfortunate specialization for rank == 1
    // sjd: would it be better if indices were an array of rank*idxType?
    // sjd: isn't it a bug as is because the idxType may not match index(rank)?
    //<commit_msg>Making a small improvement based on Steve's observation<commit_after>use Search;

class DefaultSparseDom: BaseSparseDom {
  param rank : int;
  type idxType;
  var parentDom: domain(rank, idxType);
  var dist: DefaultDist;
  var nnz = 0;  // intention is that user might specify this to avoid reallocs

  var nnzDomSize = nnz;
  var nnzDom = [1..nnzDomSize];

  var indices: [nnzDom] index(rank);

  def linksDistribution() param return false;

  def DefaultSparseDom(param rank, type idxType, 
                               dist: DefaultDist,
                               parentDom: domain(rank, idxType)) {
    this.parentDom = parentDom;
    this.dist = dist;
    nnz = 0;
  }

  def dsiNumIndices return nnz;

  def dsiBuildArray(type eltType)
    return new DefaultSparseArr(eltType=eltType, rank=rank, idxType=idxType,
                                dom=this);

  def dsiIndsIterSafeForRemoving() {
    for i in 1..nnz by -1 {
      yield indices(i);
    }
  }

  def these() {
    for i in 1..nnz {
      yield indices(i);
    }
  }

  def these(param tag: iterator) where tag == iterator.leader {
    compilerWarning("parallel iteration over sparse domain has been serialized (see note in $CHPL_HOME/STATUS)");
    yield this;
  }

  def these(param tag: iterator, follower: DefaultSparseDom) where tag == iterator.follower {
    if (follower != this) then
      halt("Sparse domains can't be zippered with anything other than themselves and their arrays");

    for i in 1..nnz do
      yield indices(i);
  }

  def these(param tag: iterator, follower: DefaultSparseArr) where tag == iterator.follower {
    if (follower.dom != this) then
      halt("Sparse domains can't be zippered with anything other than themselves and their arrays");

    for i in 1..nnz do
      yield indices(i);
  }

  def these(param tag: iterator, follower) where tag == iterator.follower {
    compilerError("Sparse iterators can't yet be zippered with others");
  }

  def dsiDim(d : int) {
    return parentDom.dim(d);
  }

  // private
  def find(ind) {
    //
    // sjd: unfortunate specialization for rank == 1
    // sjd: would it be better if indices were an array of rank*idxType?
    // sjd: isn't it a bug as is because the idxType may not match index(rank)?
    //
{'fraction_non_alphanumeric': 0.09437229437229437, 'fraction_numerical': 0.003896103896103896, 'mean_word_length': 2.6968, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '505810', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1380, 'n_words': 493}