GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
Array-f.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1994-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 // Instantiate Arrays of float values.
31 
32 #include "lo-mappers.h"
33 #include "Array.h"
34 
35 // Prevent implicit instantiations on some systems (Windows, others?)
36 // that can lead to duplicate definitions of static data members.
37 
38 extern template class OCTAVE_EXTERN_TEMPLATE_API Array<octave::idx_vector>;
39 extern template class Array<octave_idx_type>;
40 
41 #include "Array-base.cc"
42 #include "oct-locbuf.h"
43 
44 #define INLINE_ASCENDING_SORT 1
45 #define INLINE_DESCENDING_SORT 1
46 #include "oct-sort.cc"
47 
48 template <>
49 inline bool
50 sort_isnan<float> (float x)
51 {
52  return octave::math::isnan (x);
53 }
54 
55 static bool
56 nan_ascending_compare (float x, float y)
57 {
58  return octave::math::isnan (y) ? ! octave::math::isnan (x) : x < y;
59 }
60 
61 static bool
62 nan_descending_compare (float x, float y)
63 {
64  return octave::math::isnan (x) ? ! octave::math::isnan (y) : x > y;
65 }
66 
68 safe_comparator (sortmode mode, const Array<float>& a, bool allow_chk)
69 {
70  Array<float>::compare_fcn_type result = nullptr;
71 
72  if (allow_chk)
73  {
74  octave_idx_type k = 0;
75  for (; k < a.numel () && ! octave::math::isnan (a(k)); k++) ;
76  if (k == a.numel ())
77  {
78  if (mode == ASCENDING)
80  else if (mode == DESCENDING)
82  }
83  }
84 
85  if (! result)
86  {
87  if (mode == ASCENDING)
88  result = nan_ascending_compare;
89  else if (mode == DESCENDING)
90  result = nan_descending_compare;
91  }
92 
93  return result;
94 }
95 
96 // The default solution using NaN-safe comparator is OK, but almost twice as
97 // slow than this code.
98 template <>
100 sortmode
102 {
103  octave_idx_type n = numel ();
104 
105  const float *el = data ();
106 
107  if (n <= 1)
108  return (mode == UNSORTED) ? ASCENDING : mode;
109 
110  if (mode == UNSORTED)
111  {
112  // Auto-detect mode.
113  if (el[n-1] < el[0] || octave::math::isnan (el[0]))
114  mode = DESCENDING;
115  else
116  mode = ASCENDING;
117  }
118 
119  if (mode == DESCENDING)
120  {
121  octave_idx_type j = 0;
122  float r;
123  // Sort out NaNs.
124  do
125  r = el[j++];
126  while (octave::math::isnan (r) && j < n);
127 
128  // Orient the test so that NaN will not pass through.
129  for (; j < n; j++)
130  {
131  if (r >= el[j])
132  r = el[j];
133  else
134  {
135  mode = UNSORTED;
136  break;
137  }
138  }
139 
140  }
141  else // mode == ASCENDING
142  {
143  // Sort out NaNs.
144  while (n > 0 && octave::math::isnan (el[n-1]))
145  n--;
146 
147  if (n > 0)
148  {
149  // Orient the test so that NaN will not pass through.
150  float r = el[0];
151  for (octave_idx_type j = 1; j < n; j++)
152  {
153  if (r <= el[j])
154  r = el[j];
155  else
156  {
157  mode = UNSORTED;
158  break;
159  }
160  }
161  }
162  }
163 
164  return mode;
165 }
166 
167 template class octave_sort<float>;
168 
169 INSTANTIATE_ARRAY (float, OCTAVE_CLASS_TEMPLATE_INSTANTIATION_API);
170 
171 template OCTAVE_API std::ostream& operator << (std::ostream&,
172  const Array<float>&);
173 
174 #include "DiagArray2.h"
175 #include "DiagArray2.cc"
176 
177 template class DiagArray2<float>;
#define INSTANTIATE_ARRAY(T, API)
Definition: Array-base.cc:2782
template OCTAVE_API std::ostream & operator<<(std::ostream &, const Array< float > &)
Array< float >::compare_fcn_type safe_comparator(sortmode mode, const Array< float > &a, bool allow_chk)
Definition: Array-f.cc:68
bool sort_isnan< float >(float x)
Definition: Array-f.cc:50
static bool nan_ascending_compare(float x, float y)
Definition: Array-f.cc:56
static bool nan_descending_compare(float x, float y)
Definition: Array-f.cc:62
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:130
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:414
OCTARRAY_API sortmode issorted(sortmode mode=UNSORTED) const
Ordering is auto-detected or can be specified.
Definition: Array-base.cc:2049
bool isnan(bool)
Definition: lo-mappers.h:178
F77_RET_T const F77_DBLE * x
#define OCTAVE_API
Definition: main.in.cc:55
octave_idx_type n
Definition: mx-inlines.cc:753
T * r
Definition: mx-inlines.cc:773
sortmode
Definition: oct-sort.h:97
@ UNSORTED
Definition: oct-sort.h:97
@ ASCENDING
Definition: oct-sort.h:97
@ DESCENDING
Definition: oct-sort.h:97
T::size_type numel(const T &str)
Definition: oct-string.cc:71