GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
Array-b.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2018 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 // Instantiate Arrays of bool values.
28 
29 #include "Array.h"
30 #include "Array.cc"
31 
32 #define INLINE_ASCENDING_SORT 1
33 #define INLINE_DESCENDING_SORT 1
34 #include "oct-sort.cc"
35 
36 // Prevent implicit instantiations on some systems (Windows, others?)
37 // that can lead to duplicate definitions of static data members.
38 
39 extern template class OCTAVE_API Array<idx_vector>;
40 extern template class OCTAVE_API Array<octave_idx_type>;
41 
42 // Specialize bool sorting (aka stable partitioning).
43 
44 template <bool desc>
45 static void do_bool_partition (bool *data, octave_idx_type nel)
46 {
47  octave_idx_type k = 0;
48  for (octave_idx_type i = 0; i < nel; i++)
49  if (data[i] == desc)
50  data[k++] = desc;
51  for (octave_idx_type i = k; i < nel; i++)
52  data[i] = ! desc;
53 }
54 
55 template <bool desc>
56 static void do_bool_partition (bool *data, octave_idx_type *idx,
57  octave_idx_type nel)
58 {
59  // FIXME: This is essentially a simple bucket sort.
60  // Can it be efficiently done by std::partition?
62  octave_idx_type k = 0;
63  octave_idx_type l = 0;
64  for (octave_idx_type i = 0; i < nel; i++)
65  {
66  if (data[i] == desc)
67  {
68  data[k] = desc;
69  idx[k++] = idx[i];
70  }
71  else
72  jdx[l++] = idx[i];
73  }
74 
75  for (octave_idx_type i = k; i < nel; i++)
76  {
77  data[i] = ! desc;
78  idx[i] = jdx[i-k];
79  }
80 }
81 
82 template <>
83 template <>
84 void
86  std::less<bool>)
87 {
88  do_bool_partition<false> (data, nel);
89 }
90 
91 template <>
92 template <>
93 void
95  std::greater<bool>)
96 {
97  do_bool_partition<true> (data, nel);
98 }
99 
100 template <>
101 template <>
102 void
104  std::less<bool>)
105 {
106  do_bool_partition<false> (data, idx, nel);
107 }
108 
109 template <>
110 template <>
111 void
113  std::greater<bool>)
114 {
115  do_bool_partition<true> (data, idx, nel);
116 }
117 
118 template class OCTAVE_API octave_sort<bool>;
119 
120 INSTANTIATE_ARRAY (bool, OCTAVE_API);
121 
122 template OCTAVE_API std::ostream& operator << (std::ostream&,
123  const Array<bool>&);
124 
125 #include "DiagArray2.h"
126 #include "DiagArray2.cc"
127 
128 template class OCTAVE_API DiagArray2<bool>;
for large enough k
Definition: lu.cc:617
static void do_bool_partition(bool *data, octave_idx_type nel)
Definition: Array-b.cc:45
#define INSTANTIATE_ARRAY(T, API)
Definition: Array.cc:2768
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:125
void sort(T *data, octave_idx_type nel)
Definition: oct-sort.cc:1506
template OCTAVE_API std::ostream & operator<<(std::ostream &, const Array< bool > &)
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
Definition: oct-locbuf.h:41
for i
Definition: data.cc:5264