ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dvsort.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Copyright (c) 2009-2012 ADMB Foundation
5  */
11 #include <fvar.hpp>
12 #include "qsort.h"
13 
17 void double_qsort(double *arr, unsigned n)
18 {
19  #define double_lt(a,b) ((*a)<(*b))
20  QSORT(double, arr, n, double_lt);
21 }
22 
26 void double_qsort2(double *arr, int *arr2, unsigned n)
27 {
28  #define double_lt(a,b) ((*a)<(*b))
29  QSORT2(double, int, arr, arr2, n, double_lt);
30 }
31 
40 dvector sort(const dvector& v, int NSTACK)
41 {
42  int lb = v.indexmin();
43  unsigned int size = v.size();
44 
45  double* doublearray = new double[size];
46  for (unsigned int i = 0; i < size; ++i)
47  {
48  doublearray[i] = v(lb);
49  ++lb;
50  }
51  if (size > 1)
52  {
53  double_qsort(doublearray, size);
54  }
55  lb = v.indexmin();
56  int ub = v.indexmax();
57  dvector arr(lb, ub);
58  for(unsigned int i = 0; i < size; ++i)
59  {
60  arr(lb) = doublearray[i];
61  ++lb;
62  }
63 
64  delete [] doublearray;
65  doublearray = 0;
66 
67  return arr;
68 }
69 
79 dvector sort(const dvector& v, const ivector& _index, int NSTACK)
80 {
81  ivector& index = (ivector&) _index;
82  if (v.size() != index.size())
83  {
84  cerr << " Incompatible array sizes in vector v and ivector index\n"
85  << " in ivector sort(const ivector& v, const ivector& index)\n";
86  ad_exit(1);
87  }
88  int lb = v.indexmin();
89  unsigned int size = v.size();
90  double* doublearray = new double[size];
91  for (unsigned int i = 0; i < size; ++i)
92  {
93  doublearray[i] = v(lb);
94  ++lb;
95  }
96  lb = v.indexmin();
97  int* intarray = new int[size];
98  for (unsigned int i = 0; i < size; ++i)
99  {
100  intarray[i] = lb;
101  ++lb;
102  }
103  if (size > 1)
104  {
105  double_qsort2(doublearray, intarray, size);
106  }
107  lb = v.indexmin();
108  int ub = v.indexmax();
109  dvector arr(lb, ub);
110  for (unsigned int i = 0; i < size; ++i)
111  {
112  arr(lb) = doublearray[i];
113  ++lb;
114  }
115  lb = index.indexmin();
116  for (unsigned int i = 0; i < size; ++i)
117  {
118  index(lb) = intarray[i];
119  ++lb;
120  }
121 
122  delete[] doublearray;
123  doublearray = 0;
124 
125  delete[] intarray;
126  intarray = 0;
127 
128  return arr;
129 }
unsigned int size() const
Definition: ivector.h:109
#define QSORT2(QSORT_TYPE, QSORT_TYPE2, QSORT_BASE, QSORT_BASE2, QSORT_NELT, QSORT_LT)
Definition: qsort.h:317
void double_qsort(double *arr, unsigned n)
A subroutine used in Quicksort.
Definition: dvsort.cpp:17
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
exitptr ad_exit
Definition: gradstrc.cpp:53
dmatrix sort(const dmatrix &m, int column, int NSTACK)
Description not yet available.
Definition: dmsort.cpp:17
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
#define QSORT(QSORT_TYPE, QSORT_BASE, QSORT_NELT, QSORT_LT)
Definition: qsort.h:156
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
int indexmin() const
Definition: ivector.h:99
unsigned int size() const
Get number of elements in array.
Definition: dvector.h:209
void double_qsort2(double *arr, int *arr2, unsigned n)
A subroutine used in Quicksort.
Definition: dvsort.cpp:26
Header for qsort.
#define double_lt(a, b)