utilities-pthread/mergesort-pthread/mergesort-pthread.c
static void merge(merge_arg_t *ma){
  size_t first_ix, second_ix, cat_ix;
  size_t elt_size = ma->elt_size;
  if (ma->ap == C_SIZE_MAX && ma->ar == C_SIZE_MAX){
    /* a is empty */
    memcpy(elt_ptr(ma->cat_seg_elts, ma->cs, elt_size),
	   elt_ptr(ma->elts, ma->bp, elt_size),
	   (ma->br - ma->bp + 1) * elt_size);
  }else if (ma->bp == C_SIZE_MAX && ma->br == C_SIZE_MAX){
    /* b is empty */
    memcpy(elt_ptr(ma->cat_seg_elts, ma->cs, elt_size),
	   elt_ptr(ma->elts, ma->ap, elt_size),
	   (ma->ar - ma->ap + 1) * elt_size);
  }else{
    /* a and b are each not empty */
    first_ix = ma->ap;
    second_ix = ma->bp;
    cat_ix = ma->cs;
    while(first_ix <= ma->ar && second_ix <= ma->br){
      if (ma->cmp(elt_ptr(ma->elts, first_ix, elt_size),
		  elt_ptr(ma->elts, second_ix, elt_size)) < 0){
	memcpy(elt_ptr(ma->cat_seg_elts, cat_ix, elt_size),
	       elt_ptr(ma->elts, first_ix, elt_size),
	       elt_size);
	cat_ix++;
	first_ix++;
      }else{
	memcpy(elt_ptr(ma->cat_seg_elts, cat_ix, elt_size),
	       elt_ptr(ma->elts, second_ix, elt_size),
	       elt_size);
	cat_ix++;
	second_ix++;
      }
    }
    if (second_ix == ma->br + 1){
      memcpy(elt_ptr(ma->cat_seg_elts, cat_ix, elt_size),
	     elt_ptr(ma->elts, first_ix, elt_size),
	     (ma->ar - first_ix + 1) * elt_size);
    }else{
      memcpy(elt_ptr(ma->cat_seg_elts, cat_ix, elt_size),
	     elt_ptr(ma->elts, second_ix, elt_size),
	     (ma->br - second_ix + 1) * elt_size);
    }
  }
}
/**
   Merges two sorted subarrays onto a concatenation array as the base case
   of parallel merge.
*/
Merges two sorted subarrays onto a concatenation array as the base case
of parallel merge.
Merges two sorted subarrays onto a concatenation array as the base case of parallel merge .
static void merge(merge_arg_t *ma){
  size_t first_ix, second_ix, cat_ix;
  size_t elt_size = ma->elt_size;
  if (ma->ap == C_SIZE_MAX && ma->ar == C_SIZE_MAX){
    memcpy(elt_ptr(ma->cat_seg_elts, ma->cs, elt_size),
	   elt_ptr(ma->elts, ma->bp, elt_size),
	   (ma->br - ma->bp + 1) * elt_size);
  }else if (ma->bp == C_SIZE_MAX && ma->br == C_SIZE_MAX){
    memcpy(elt_ptr(ma->cat_seg_elts, ma->cs, elt_size),
	   elt_ptr(ma->elts, ma->ap, elt_size),
	   (ma->ar - ma->ap + 1) * elt_size);
  }else{
    first_ix = ma->ap;
    second_ix = ma->bp;
    cat_ix = ma->cs;
    while(first_ix <= ma->ar && second_ix <= ma->br){
      if (ma->cmp(elt_ptr(ma->elts, first_ix, elt_size),
		  elt_ptr(ma->elts, second_ix, elt_size)) < 0){
	memcpy(elt_ptr(ma->cat_seg_elts, cat_ix, elt_size),
	       elt_ptr(ma->elts, first_ix, elt_size),
	       elt_size);
	cat_ix++;
	first_ix++;
      }else{
	memcpy(elt_ptr(ma->cat_seg_elts, cat_ix, elt_size),
	       elt_ptr(ma->elts, second_ix, elt_size),
	       elt_size);
	cat_ix++;
	second_ix++;
      }
    }
    if (second_ix == ma->br + 1){
      memcpy(elt_ptr(ma->cat_seg_elts, cat_ix, elt_size),
	     elt_ptr(ma->elts, first_ix, elt_size),
	     (ma->ar - first_ix + 1) * elt_size);
    }else{
      memcpy(elt_ptr(ma->cat_seg_elts, cat_ix, elt_size),
	     elt_ptr(ma->elts, second_ix, elt_size),
	     (ma->br - second_ix + 1) * elt_size);
    }
  }
}
static void merge ( merge_arg_t * ma ) { size_t first_ix , second_ix , cat_ix ; size_t elt_size = ma -> elt_size ; if ( ma -> ap == C_SIZE_MAX && ma -> ar == C_SIZE_MAX ) { memcpy ( elt_ptr ( ma -> cat_seg_elts , ma -> cs , elt_size ) , elt_ptr ( ma -> elts , ma -> bp , elt_size ) , ( ma -> br - ma -> bp + 1 ) * elt_size ) ; } else if ( ma -> bp == C_SIZE_MAX && ma -> br == C_SIZE_MAX ) { memcpy ( elt_ptr ( ma -> cat_seg_elts , ma -> cs , elt_size ) , elt_ptr ( ma -> elts , ma -> ap , elt_size ) , ( ma -> ar - ma -> ap + 1 ) * elt_size ) ; } else { first_ix = ma -> ap ; second_ix = ma -> bp ; cat_ix = ma -> cs ; while ( first_ix <= ma -> ar && second_ix <= ma -> br ) { if ( ma -> cmp ( elt_ptr ( ma -> elts , first_ix , elt_size ) , elt_ptr ( ma -> elts , second_ix , elt_size ) ) < 0 ) { memcpy ( elt_ptr ( ma -> cat_seg_elts , cat_ix , elt_size ) , elt_ptr ( ma -> elts , first_ix , elt_size ) , elt_size ) ; cat_ix ++ ; first_ix ++ ; } else { memcpy ( elt_ptr ( ma -> cat_seg_elts , cat_ix , elt_size ) , elt_ptr ( ma -> elts , second_ix , elt_size ) , elt_size ) ; cat_ix ++ ; second_ix ++ ; } } if ( second_ix == ma -> br + 1 ) { memcpy ( elt_ptr ( ma -> cat_seg_elts , cat_ix , elt_size ) , elt_ptr ( ma -> elts , first_ix , elt_size ) , ( ma -> ar - first_ix + 1 ) * elt_size ) ; } else { memcpy ( elt_ptr ( ma -> cat_seg_elts , cat_ix , elt_size ) , elt_ptr ( ma -> elts , second_ix , elt_size ) , ( ma -> br - second_ix + 1 ) * elt_size ) ; } } }
Merges two sorted subarrays onto a concatenation array as the base case
of parallel merge.
Merges two sorted subarrays onto a concatenation array as the base case of parallel merge .
/* a is empty */ /* b is empty */ /* a and b are each not empty */