File: Sort.cpp

package info (click to toggle)
freemat 4.2%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 142,116 kB
  • sloc: ansic: 126,788; cpp: 62,015; python: 2,080; perl: 1,255; sh: 1,146; yacc: 1,019; lex: 239; makefile: 107
file content (224 lines) | stat: -rw-r--r-- 6,628 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 * Copyright (c) 2009 Samit Basu
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "Operators.hpp"
#include "Math.hpp"
#include "Complex.hpp"

template <class T>
struct XNEntryReal {
  index_t n;
  T x;
};

template <class T>
static bool RealLess(const XNEntryReal<T>& a, const XNEntryReal<T>& b) {
  return a.x < b.x;
}

template <>
bool RealLess(const XNEntryReal<Array>& a, const XNEntryReal<Array>& b) {
  return a.x.asString() < b.x.asString();
}

template <class T>
static bool RealGreater(const XNEntryReal<T>& a, const XNEntryReal<T>& b) {
  return a.x > b.x;
}

template <>
bool RealGreater(const XNEntryReal<Array>& a, const XNEntryReal<Array>& b) {
  return a.x.asString() > b.x.asString();
}

template <class T>
struct XNEntryComplex {
  index_t n;
  T x;
  T y;
};

template <class T>
static bool ComplexLess(const XNEntryComplex<T>& a, const XNEntryComplex<T>& b) {
  return complex_lt(a.x,a.y,b.x,b.y);
}

template <class T>
static bool ComplexGreater(const XNEntryComplex<T>& a, const XNEntryComplex<T>& b) {
  return complex_gt(a.x,a.y,b.x,b.y);
}


struct OpVecSortAscend {
  template <typename T>
  static inline void func(ConstSparseIterator<T> &,
			  SparseSlice<T>&,
			  SparseSlice<index_t>&) {
    throw Exception("sort does not work for sparse matrices");
  }
  template <typename T>
  static inline void func(ConstComplexSparseIterator<T> &,
			  SparseSlice<T>&, SparseSlice<T>&,
			  SparseSlice<index_t>&) {
    throw Exception("sort does not work for sparse matrices");
  }
  template <typename T>
  static inline void func(const BasicArray<T> &src,
			  BasicArray<T> &dest,
			  BasicArray<index_t>& dest_index) {
    QVector<XNEntryReal<T> > tmp(int(src.length()));
    for (index_t i=1;i<=src.length();i++) {
      tmp[int(i-1)].n = i; tmp[int(i-1)].x = src[i];
    }
    qStableSort(tmp.begin(),tmp.end(),RealLess<T>);
    for (int i=0;i<tmp.size();i++) {
      dest[i+1] = tmp[i].x;
      dest_index[i+1] = tmp[i].n;
    }
  }
  template <typename T>
  static inline void func(const BasicArray<T> & src_real,
			  const BasicArray<T> & src_imag,
			  BasicArray<T> & dest_real,
			  BasicArray<T> & dest_imag,
			  BasicArray<index_t>& dest_index) {
    QVector<XNEntryComplex<T> > tmp(int(src_real.length()));
    for (index_t i=1;i<=src_real.length();i++) {
      tmp[int(i-1)].n = i; 
      tmp[int(i-1)].x = src_real[i];
      tmp[int(i-1)].y = src_imag[i];
    }
    qStableSort(tmp.begin(),tmp.end(),ComplexLess<T>);
    for (int i=0;i<tmp.size();i++) {
      dest_real[index_t(i+1)] = tmp[i].x;
      dest_imag[index_t(i+1)] = tmp[i].y;
      dest_index[index_t(i+1)] = tmp[i].n;
    }
  }
};

struct OpVecSortDescend {
  template <typename T>
  static inline void func(ConstSparseIterator<T> &,
			  SparseSlice<T>&,
			  SparseSlice<index_t>&) {
    throw Exception("sort does not work for sparse matrices");
  }
  template <typename T>
  static inline void func(ConstComplexSparseIterator<T> &,
			  SparseSlice<T>&, SparseSlice<T>&,
			  SparseSlice<index_t>&) {
    throw Exception("sort does not work for sparse matrices");
  }
  template <typename T>
  static inline void func(const BasicArray<T> &src,
			  BasicArray<T> &dest,
			  BasicArray<index_t>& dest_index) {
    QVector<XNEntryReal<T> > tmp(int(src.length()));
    for (index_t i=1;i<=src.length();i++) {
      tmp[int(i-1)].n = i; tmp[int(i-1)].x = src[i];
    }
    qStableSort(tmp.begin(),tmp.end(),RealGreater<T>);
    for (int i=0;i<tmp.size();i++) {
      dest[i+1] = tmp[i].x;
      dest_index[i+1] = tmp[i].n;
    }
  }
  template <typename T>
  static inline void func(const BasicArray<T> & src_real,
			  const BasicArray<T> & src_imag,
			  BasicArray<T> & dest_real,
			  BasicArray<T> & dest_imag,
			  BasicArray<index_t>& dest_index) {
    QVector<XNEntryComplex<T> > tmp(int(src_real.length()));
    for (index_t i=1;i<=src_real.length();i++) {
      tmp[int(i-1)].n = i; 
      tmp[int(i-1)].x = src_real[i];
      tmp[int(i-1)].y = src_imag[i];
    }
    qStableSort(tmp.begin(),tmp.end(),ComplexGreater<T>);
    for (int i=0;i<tmp.size();i++) {
      dest_real[index_t(i+1)] = tmp[i].x;
      dest_imag[index_t(i+1)] = tmp[i].y;
      dest_index[index_t(i+1)] = tmp[i].n;
    }
  }
};

//@@Signature
//function sort SortFunction
//inputs A dim mode
//outputs B Index
//DOCBLOCK array_sort
ArrayVector SortFunction(int nargout, const ArrayVector& arg) {
  // Get the data argument
  if (arg.size() < 1)
    throw Exception("sort requires at least one argument");
  Array input(arg[0]);
  if (input.isScalar()) {
    ArrayVector ret;
    ret.push_back(input);
    ret.push_back(Array(double(1)));
    return ret;
  }
  int workDim = 0;
  if ((arg.size() >= 2) && (!arg[1].isEmpty()))
    workDim = arg[1].asInteger()-1;
  else
    workDim = input.dimensions().firstNonsingular();
  bool ascendSort = true;
  if (arg.size() >= 3) {
    QString tdir(arg[2].asString().toLower());
    if (tdir[0] == 'a') 
      ascendSort = true;
    else if (tdir[0] == 'd')
      ascendSort = false;
    else
      throw Exception("String argument must be either 'ascend' or 'descend'");
  }
  if (IsCellStringArray(input)) {
    Array indx;
    Array F;
    if (ascendSort)
      F = BiVectorOp<Array,OpVecSortAscend>(input.constReal<Array>(),
					    int(input.dimensions()[workDim]),
					    workDim,indx);
    else
      F = BiVectorOp<Array,OpVecSortDescend>(input.constReal<Array>(),
					     int(input.dimensions()[workDim]),
					     workDim,indx);
    ArrayVector ret;
    ret.push_back(F);
    ret.push_back(indx);
    return ret;
  } else {
    ArrayVector ret;
    if (ascendSort)
      ret = BiVectorOp<OpVecSortAscend>(input,
					int(input.dimensions()[workDim]),
					workDim);
    else
      ret =  BiVectorOp<OpVecSortDescend>(input,
					  int(input.dimensions()[workDim]),
					  workDim);
    if (input.dataClass() == StringArray) {
      ret[0] = ret[0].toClass(StringArray);
    }
    return ret;
  }
}