[eso-midas] 01/03: Replace heapsort by qsort

Ole Streicher olebole at moszumanska.debian.org
Tue Mar 31 19:52:43 UTC 2015


This is an automated email from the git hooks/post-receive script.

olebole pushed a commit to branch debian
in repository eso-midas.

commit 81af0fc597d6be952bd1fb28f81a7b24e00fa3f7
Author: Ole Streicher <debian at liska.ath.cx>
Date:   Tue Mar 31 21:50:44 2015 +0200

    Replace heapsort by qsort
---
 debian/patches/qsort.patch | 234 +++++++++++++++++++++++++++++++++++++++++++++
 debian/patches/series      |   1 +
 2 files changed, 235 insertions(+)

diff --git a/debian/patches/qsort.patch b/debian/patches/qsort.patch
new file mode 100644
index 0000000..676fd16
--- /dev/null
+++ b/debian/patches/qsort.patch
@@ -0,0 +1,234 @@
+Author: Ole Streicher <olebole at debian.org>
+Description: Replace heapsort by qsort
+ The heapsort code used in MIDAS is buggy: it creates wrong results, and it
+ may corrupt the stack. The simplest solution is to replace it with the
+ qsort() function provided by standard libm.
+--- /dev/null
++++ b/libsrc/math/quicksort.c
+@@ -0,0 +1,115 @@
++/*===========================================================================
++  Copyright (C) 1995 European Southern Observatory (ESO)
++
++  This program is free software; you can redistribute it and/or
++  modify it under the terms of the GNU General Public License as
++  published by the Free Software Foundation; either version 2 of
++  the License, or (at your option) any later version.
++
++  This program is distributed in the hope that it will be useful,
++  but WITHOUT ANY WARRANTY; without even the implied warranty of
++  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
++  GNU General Public License for more details.
++
++  You should have received a copy of the GNU General Public
++  License along with this program; if not, write to the Free
++  Software Foundation, Inc., 675 Massachusetss Ave, Cambridge,
++  MA 02139, USA.
++
++  Corresponding concerning ESO-MIDAS should be addressed as follows:
++        Internet e-mail: midas at eso.org
++        Postal address: European Southern Observatory
++                        Data Management Division
++                        Karl-Schwarzschild-Strasse 2
++                        D 85748 Garching bei Muenchen
++                        GERMANY
++===========================================================================*/
++
++#include <stdio.h>
++#include <stdlib.h>
++#include "mutil.h"
++
++typedef struct {
++    int number;
++    int rank;
++} int_sort_s;
++
++static int compar_int(const void *a1, const void *a2) {
++    int n1 = ((int_sort_s *)a1)->number;
++    int n2 = ((int_sort_s *)a2)->number;
++    return (n1 < n2)?-1:(n1 > n2)?1:0;
++}
++
++void quickSortInt(int array_size, int numbers[], int rank[]) {
++    int i;
++    int_sort_s *a = malloc(array_size * sizeof(int_sort_s));
++    for (i = 0; i < array_size; i++) {
++	a[i].number = numbers[i];
++	a[i].rank = rank[i];
++    }
++    qsort(a, array_size, sizeof(int_sort_s), compar_int);
++    for (i = 0; i < array_size; i++) {
++	numbers[i] = a[i].number;
++	rank[i] = a[i].rank;
++    }
++    free(a);
++}
++
++typedef struct {
++    float number;
++    int rank;
++} float_sort_s;
++
++static int compar_float(const void *a1, const void *a2) {
++    float n1 = ((float_sort_s *)a1)->number;
++    float n2 = ((float_sort_s *)a2)->number;
++    return (n1 < n2)?-1:(n1 > n2)?1:0;
++}
++
++void quickSortFloat(int array_size, float numbers[], int rank[]) {
++    int i;
++    float_sort_s *a = malloc(array_size * sizeof(float_sort_s));
++    for (i = 0; i < array_size; i++) {
++	a[i].number = numbers[i];
++	a[i].rank = rank[i];
++    }
++    qsort(a, array_size, sizeof(float_sort_s), compar_float);
++    for (i = 0; i < array_size; i++) {
++	numbers[i] = a[i].number;
++	rank[i] = a[i].rank;
++    }
++    free(a);
++}
++
++typedef struct {
++    double number;
++    int rank;
++} double_sort_s;
++
++static int compar_double(const void *a1, const void *a2) {
++    double n1 = ((double_sort_s *)a1)->number;
++    double n2 = ((double_sort_s *)a2)->number;
++    return (n1 < n2)?-1:(n1 > n2)?1:0;
++}
++
++void quickSortDouble(int array_size, double numbers[], int rank[]) {
++    int i;
++    double_sort_s *a = malloc(array_size * sizeof(double_sort_s));
++    for (i = 0; i < array_size; i++) {
++	a[i].number = numbers[i];
++	a[i].rank = rank[i];
++    }
++    qsort(a, array_size, sizeof(double_sort_s), compar_double);
++    for (i = 0; i < array_size; i++) {
++	numbers[i] = a[i].number;
++	rank[i] = a[i].rank;
++    }
++    free(a);
++}
++
++void  quickFillRank(int array_size, int rank[], int offset)
++{
++  int i;
++  for (i=0; i<array_size; i++)
++       rank[i] = i + offset;
++}
+--- a/libsrc/math/makefile
++++ b/libsrc/math/makefile
+@@ -18,7 +18,7 @@
+ LIB = $(LIBDIR)/libmidmath.a
+ 
+ OBJ0 =  rebu01.o
+-OBJ1 =  sort.o heapsort.o cpl_matrix.o nrutil.o mutil.o mpfit.o fitnol.o spline.o
++OBJ1 =  sort.o quicksort.o cpl_matrix.o nrutil.o mutil.o mpfit.o fitnol.o spline.o
+ 
+ 
+ # DEPENDENCIES:
+--- a/incl/mutil.h
++++ b/incl/mutil.h
+@@ -111,25 +111,25 @@
+ #endif
+ );
+ 
+-extern void heapSortInt(
++extern void quickSortInt(
+ #ifdef __STDC__
+   int array_size, int numbers[], int rank[]
+ #endif
+ );
+ 
+-extern void heapSortFloat(
++extern void quickSortFloat(
+ #ifdef __STDC__
+   int array_size, float numbers[], int rank[]
+ #endif
+ );
+ 
+-extern void heapSortDouble(
++extern void quickSortDouble(
+ #ifdef __STDC__
+   int array_size, double numbers[], int rank[]
+ #endif
+ );
+ 
+-extern void heapFillRank(
++extern void quickFillRank(
+ #ifdef __STDC__
+   int array_size, int rank[], int offset
+ #endif
+--- a/libsrc/math/sort.c
++++ b/libsrc/math/sort.c
+@@ -51,8 +51,8 @@
+ {
+ 
+   int *ia = (int*)malloc((size_t)n*sizeof(int)); 
+-  heapFillRank(n,ia,0);   
+-  heapSortFloat(n,ra,ia); 
++  quickFillRank(n,ia,0);
++  quickSortFloat(n,ra,ia);
+   free(ia); 
+ 
+ }
+--- a/prim/general/libsrc/iqefunc.c
++++ b/prim/general/libsrc/iqefunc.c
+@@ -456,9 +456,9 @@
+  
+   heap_copy(n, arrin, b); 
+ 
+-  heapFillRank(n,indx,0); 
+-  // heapSortFloat(n, arrin, indx); 
+-  heapSortFloat(n, b, indx); 
++  quickFillRank(n,indx,0);
++  // quickSortFloat(n, arrin, indx);
++  quickSortFloat(n, b, indx);
+ 
+ }
+ 
+--- a/prim/general/libsrc/sort.c
++++ b/prim/general/libsrc/sort.c
+@@ -35,7 +35,7 @@
+ .VERSION 
+  090630		creation 
+  110517         last modif
+- 150215         making use of hsort and heapSort modules (libsrc/math)
++ 310315         making use of hsort and quickSort modules (libsrc/math)
+ ---------------------------------------------------------------------*/
+ 
+ #include <mutil.h>
+@@ -51,7 +51,7 @@
+ -----------------------------------------------------------------------*/
+ 
+ {
+-  heapSortFloat(nsiz, ra, ia); 
++  quickSortFloat(nsiz, ra, ia);
+ }
+ 
+ 
+--- a/stdred/ccdred/libsrc/sort.c
++++ b/stdred/ccdred/libsrc/sort.c
+@@ -88,7 +88,7 @@
+ int   *xval, *yval;
+ 
+ {
+-  heapSortInt(n, xval, yval); 
++  quickSortInt(n, xval, yval);
+ }
+ 
+ /*
+@@ -99,7 +99,7 @@
+ float *xval;
+ int   *yval;
+ {
+-  heapSortFloat(n,xval,yval); 
++  quickSortFloat(n,xval,yval);
+ }
+ 
+ 
diff --git a/debian/patches/series b/debian/patches/series
index 4a391b3..227c2ce 100644
--- a/debian/patches/series
+++ b/debian/patches/series
@@ -27,3 +27,4 @@ fix_md5_c.patch
 fix_txdisplay_c.patch
 fix_modgcur_c.patch
 fix_thelp_c.patch
+qsort.patch

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-astro/packages/eso-midas.git



More information about the debian-science-commits mailing list