ARTS  2.2.66
test_sorting.cc
Go to the documentation of this file.
1 /* Copyright (C) 2003-2012 Oliver Lemke <olemke@core-dump.info>
2 
3  This program is free software; you can redistribute it and/or
4  modify it under the terms of the GNU General Public License as
5  published by the Free Software Foundation; either version 2 of the
6  License, or (at your option) any later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
16  USA. */
17 
18 #include <ctime>
19 #include <cstdlib>
20 #include <iostream>
21 
22 #include "arts.h"
23 
24 #if HAVE_UNISTD_H
25 # include <sys/types.h>
26 # include <unistd.h>
27 #endif
28 
29 #include "matpackI.h"
30 #include "sorting.h"
31 
32 void
34 {
35  // Array for output of sorted indexes
36  ArrayOfIndex i;
37 
38  Vector v (10);
39  v [0] = 2.2;
40  v [1] = 1.1;
41  v [2] = 3.3;
42  v [3] = 7.7;
43  v [4] = 6.6;
44  v [5] = 9.9;
45  v [6] = 4.4;
46  v [7] = 8.8;
47  v [8] = 5.5;
48  v [9] = 10.01;
49 
50  cout << "Vector before sort: " << v << endl;
51  get_sorted_indexes (i, v);
52  cout << "Index array after sort: " << i << endl;
53  cout << "Sorted Vector: ";
54  for (Index j = 0; j < v.nelem (); j++)
55  cout << " " << setw (3) << v[i[j]];
56  cout << endl << endl;
57 
58 }
59 
60 #ifdef _POSIX_VERSION
61 void
62 testArray ()
63 {
64  // Array for output of sorted indexes
65  ArrayOfIndex i;
66 
67  ArrayOfIndex a (10);
68  a [0] = 2;
69  a [1] = 1;
70  a [2] = 3;
71  a [3] = 7;
72  a [4] = 6;
73  a [5] = 9;
74  a [6] = 4;
75  a [7] = 8;
76  a [8] = 5;
77  a [9] = 10;
78 
79  cout << "Array before sort: " << a << endl;
80  get_sorted_indexes (i, a);
81  cout << "Index array after sort: " << i << endl;
82  cout << "Sorted Array: ";
83  for (Index j = 0; j < a.nelem (); j++)
84  cout << " " << setw (3) << a[i[j]];
85  cout << endl << endl;
86 }
87 
88 void
89 profileVector (Index n)
90 {
91  cout << "Creating Vector with random numbers" << endl;
92 
93  srandom ((unsigned int)time (NULL));
94  Vector v(n);
95  for (Index i = 0; i < n; i++)
96  v[i] = Numeric (random ());
97 
98  cout << "Now sorting" << endl;
99  ArrayOfIndex i;
100  get_sorted_indexes (i, v);
101 }
102 #endif
103 
104 int
105 main (void)
106 {
107 #ifdef _POSIX_VERSION
108  testVector ();
109  testArray ();
110 #else
111  cerr << "This test is only available when compiled with POSIX support."
112  << endl;
113 #endif
114 
115 // profileVector (100 * 100 * 20 * 20);
116 
117  return (0);
118 }
119 
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:35
Index nelem() const
Number of elements.
Definition: array.h:176
The Vector class.
Definition: matpackI.h:556
void testVector()
Definition: test_sorting.cc:33
int main(void)
Index nelem() const
Returns the number of elements.
Definition: matpackI.cc:180
Contains sorting routines.
The global header file for ARTS.
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:29
void get_sorted_indexes(ArrayOfIndex &sorted, const T &data)
get_sorted_indexes
Definition: sorting.h:79
This can be used to make arrays out of anything.
Definition: array.h:40