ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ivsort.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 int_qsort(int *arr, unsigned n)
18 {
19  #define int_lt(a,b) ((*a)<(*b))
20  QSORT(int, arr, n, int_lt);
21 }
22 
26 void int_qsort2(int *arr, int *arr2, unsigned n)
27 {
28  #define int_lt(a,b) ((*a)<(*b))
29  QSORT2(int, int, arr, arr2, n, int_lt);
30 }
31 
40 ivector sort(const ivector& v, int NSTACK)
41 {
42  int lb = v.indexmin();
43  unsigned int size = v.size();
44  int* intarray = new int[size];
45  for(unsigned int i = 0; i < size; ++i)
46  {
47  intarray[i] = v(lb);
48  ++lb;
49  }
50  if (size > 1)
51  {
52  int_qsort(intarray, size);
53  }
54  lb = v.indexmin();
55  int ub = v.indexmax();
56  ivector arr(lb, ub);
57  for(unsigned int i = 0; i < size; ++i)
58  {
59  arr(lb) = intarray[i];
60  ++lb;
61  }
62 
63  delete [] intarray;
64  intarray = 0;
65 
66  return arr;
67 }
68 
78 ivector sort(const ivector& v, const ivector& _index, int NSTACK)
79 {
80  ivector& index = (ivector&)_index;
81  if (v.size() != index.size())
82  {
83  cerr << " Incompatible array sizes in vector v and ivector index\n"
84  << " in ivector sort(const ivector& v, const ivector& index)\n";
85  ad_exit(1);
86  }
87  int lb = v.indexmin();
88  unsigned int size = v.size();
89  int* intarray = new int[size];
90  for (unsigned int i = 0; i < size; ++i)
91  {
92  intarray[i] = v(lb);
93  ++lb;
94  }
95  lb = v.indexmin();
96  int* intarray2 = new int[size];
97  for (unsigned int i = 0; i < size; ++i)
98  {
99  intarray2[i] = lb;
100  ++lb;
101  }
102  if (size > 1)
103  {
104  int_qsort2(intarray, intarray2, size);
105  }
106  lb = v.indexmin();
107  int ub=v.indexmax();
108  ivector arr(lb, ub);
109  for (unsigned int i = 0; i < size; ++i)
110  {
111  arr(lb) = intarray[i];
112  ++lb;
113  }
114  lb = index.indexmin();
115  for (unsigned int i = 0; i < size; ++i)
116  {
117  index(lb) = intarray2[i];
118  ++lb;
119  }
120 
121  delete [] intarray;
122  intarray = 0;
123 
124  delete [] intarray2;
125  intarray2 = 0;
126 
127  return arr;
128 }
void int_qsort(int *arr, unsigned n)
A subroutine used in Quicksort.
Definition: ivsort.cpp:17
unsigned int size() const
Definition: ivector.h:109
#define int_lt(a, b)
#define QSORT2(QSORT_TYPE, QSORT_TYPE2, QSORT_BASE, QSORT_BASE2, QSORT_NELT, QSORT_LT)
Definition: qsort.h:317
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
void int_qsort2(int *arr, int *arr2, unsigned n)
A subroutine used in Quicksort.
Definition: ivsort.cpp:26
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
int indexmin() const
Definition: ivector.h:99
int indexmax() const
Definition: ivector.h:104
Header for qsort.