Mercurial > octave-nkf
comparison liboctave/Array-f.cc @ 9922:3a8327d51ed4
optimize issorted for doubles & floats
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Sat, 05 Dec 2009 21:30:47 +0100 |
parents | 56fbe170d354 |
children | 1369f13ae6b2 |
comparison
equal
deleted
inserted
replaced
9921:7c8392a034e6 | 9922:3a8327d51ed4 |
---|---|
82 } | 82 } |
83 | 83 |
84 return result; | 84 return result; |
85 } | 85 } |
86 | 86 |
87 // The default solution using NaN-safe comparator is OK, but almost twice as | |
88 // slow than this code. | |
89 template <> | |
90 sortmode | |
91 Array<float>::is_sorted (sortmode mode) const | |
92 { | |
93 octave_idx_type n = numel (); | |
94 | |
95 const float *el = data (); | |
96 | |
97 if (n <= 1) | |
98 return mode ? mode : ASCENDING; | |
99 | |
100 if (! mode) | |
101 { | |
102 // Auto-detect mode. | |
103 if (el[n-1] < el[0] || xisnan (el[0])) | |
104 mode = DESCENDING; | |
105 else | |
106 mode = ASCENDING; | |
107 } | |
108 | |
109 if (mode == DESCENDING) | |
110 { | |
111 octave_idx_type j = 0; | |
112 float r; | |
113 // Sort out NaNs. | |
114 do | |
115 r = el[j++]; | |
116 while (xisnan (r) && j < n); | |
117 | |
118 // Orient the test so that NaN will not pass through. | |
119 for (; j < n; j++) | |
120 { | |
121 if (r >= el[j]) | |
122 r = el[j]; | |
123 else | |
124 { | |
125 mode = UNSORTED; | |
126 break; | |
127 } | |
128 } | |
129 | |
130 } | |
131 else if (mode == ASCENDING) | |
132 { | |
133 // Sort out NaNs. | |
134 while (n > 0 && xisnan (el[n-1])) | |
135 n--; | |
136 | |
137 if (n > 0) | |
138 { | |
139 // Orient the test so that NaN will not pass through. | |
140 float r = el[0]; | |
141 for (octave_idx_type j = 1; j < n; j++) | |
142 { | |
143 if (r <= el[j]) | |
144 r = el[j]; | |
145 else | |
146 { | |
147 mode = UNSORTED; | |
148 break; | |
149 } | |
150 } | |
151 } | |
152 } | |
153 | |
154 return mode; | |
155 } | |
156 | |
87 INSTANTIATE_ARRAY_SORT (float); | 157 INSTANTIATE_ARRAY_SORT (float); |
88 | 158 |
89 INSTANTIATE_ARRAY (float, OCTAVE_API); | 159 INSTANTIATE_ARRAY (float, OCTAVE_API); |
90 | 160 |
91 #include "Array2.h" | 161 #include "Array2.h" |