<commit_before>#ifndef CSR_H
#define CSR_H

#include <stdlib.h>
#include <math.h>
#include <omp.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>

#include "sparse.h"
#include "quickSort.h"

/*** binary CSR ***/
struct BinaryCSR
{
  int nrow;
  int ncol;
  long nnz;
  int* row_ptr; /* points to the row starts for each row */
  int* cols;
};

void free_bcsr(struct BinaryCSR* bcsr) {
  assert(bcsr);
  free(bcsr->row_ptr);
  free(bcsr->cols);
}

static inline void new_bcsr(struct BinaryCSR* restrict A, long nnz, int nrow, int ncol, int* rows, int* cols) {
  assert(A);
  //struct BinaryCSR *A = (struct BinaryCSR*)malloc(sizeof(struct BinaryCSR));
  A->nnz  = nnz;
  A->nrow = nrow;
  A->ncol = ncol;
  A->cols = (int*)malloc(nnz * sizeof(int));
  A->row_ptr = (int*)malloc( (nrow + 1) * sizeof(int));

  // sorting by row:
  long* h = (long*)malloc(A->nnz * sizeof(long));
#pragma omp parallel for schedule(static)
  for (int i = 0; i < A->nnz; i++) {
      h[i] = rows[i] * (long)ncol + (long)cols[i];
  }
  quickSort(h, 0, nnz - 1);
  int row_prev = -1;
  for (int i = 0; i < nnz; i++) {
    A->cols[i] = h[i] % ncol;
    if (A->cols[i] < 0) {
       printf("A->cols[%d] = %d, h[%d]=%ld\n", i, A->cols[i], i, h[i]);
    }
    int row = h[i] / ncol;
    while (row > row_prev) {
      row_prev++;
      A->row_ptr[row_prev] = i;
    }
  }
  for (int row = row_prev + 1; row <= nrow; row++) {
    A->row_ptr[row] = nnz;
  }
  free(h);
}

static inline void bcsr_from_sbm(struct BinaryCSR* restrict A,
                                       struct SparseBinaryMatrix* restrict sbm) {
  assert(A);
  assert(sbm);
  new_bcsr(A, sbm->nnz, sbm->nrow, sbm->ncol, sbm->rows, sbm->cols);
}

/**********************************************************************/
/************* SERIALIZATION ******************************************/
/**********************************************************************/<commit_msg>added necessary inline-s, restrict to __restrict__<commit_after>#ifndef CSR_H
#define CSR_H

#include <stdlib.h>
#include <math.h>
#include <omp.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>

#include "sparse.h"
#include "quickSort.h"

/*** binary CSR ***/
struct BinaryCSR
{
  int nrow;
  int ncol;
  long nnz;
  int* row_ptr; /* points to the row starts for each row */
  int* cols;
};

inline void free_bcsr(struct BinaryCSR* bcsr) {
  assert(bcsr);
  free(bcsr->row_ptr);
  free(bcsr->cols);
}

static inline void new_bcsr(struct BinaryCSR* __restrict__ A, long nnz, int nrow, int ncol, int* rows, int* cols) {
  assert(A);
  //struct BinaryCSR *A = (struct BinaryCSR*)malloc(sizeof(struct BinaryCSR));
  A->nnz  = nnz;
  A->nrow = nrow;
  A->ncol = ncol;
  A->cols = (int*)malloc(nnz * sizeof(int));
  A->row_ptr = (int*)malloc( (nrow + 1) * sizeof(int));

  // sorting by row:
  long* h = (long*)malloc(A->nnz * sizeof(long));
#pragma omp parallel for schedule(static)
  for (int i = 0; i < A->nnz; i++) {
      h[i] = rows[i] * (long)ncol + (long)cols[i];
  }
  quickSort(h, 0, nnz - 1);
  int row_prev = -1;
  for (int i = 0; i < nnz; i++) {
    A->cols[i] = h[i] % ncol;
    if (A->cols[i] < 0) {
       printf("A->cols[%d] = %d, h[%d]=%ld\n", i, A->cols[i], i, h[i]);
    }
    int row = h[i] / ncol;
    while (row > row_prev) {
      row_prev++;
      A->row_ptr[row_prev] = i;
    }
  }
  for (int row = row_prev + 1; row <= nrow; row++) {
    A->row_ptr[row] = nnz;
  }
  free(h);
}

static inline void bcsr_from_sbm(struct BinaryCSR* __restrict__ A,
                                 struct SparseBinaryMatrix* __restrict__ sbm) {
  assert(A);
  assert(sbm);
  new_bcsr(A, sbm->nnz, sbm->nrow, sbm->ncol, sbm->rows, sbm->cols);
}

/**********************************************************************/
/************* SERIALIZATION ******************************************/
/**********************************************************************/
{'fraction_non_alphanumeric': 0.26970317297850566, 'fraction_numerical': 0.0040941658137154556, 'mean_word_length': 3.421945701357466, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4830275', 'n_tokens_mistral': 1577, 'n_tokens_neox': 1531, 'n_words': 362}