GNU Octave 7.1.0
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-2022 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 bool values.
31
32#include "Array.h"
33#include "Array.cc"
34
35#define INLINE_ASCENDING_SORT 1
36#define INLINE_DESCENDING_SORT 1
37#include "oct-sort.cc"
38
39// Prevent implicit instantiations on some systems (Windows, others?)
40// that can lead to duplicate definitions of static data members.
41
42extern template class Array<octave::idx_vector>;
43extern template class Array<octave_idx_type>;
44
45// Specialize bool sorting (aka stable partitioning).
46
47template <bool desc>
48static void do_bool_partition (bool *data, octave_idx_type nel)
49{
50 octave_idx_type k = 0;
51 for (octave_idx_type i = 0; i < nel; i++)
52 if (data[i] == desc)
53 data[k++] = desc;
54 for (octave_idx_type i = k; i < nel; i++)
55 data[i] = ! desc;
56}
57
58template <bool desc>
59static void do_bool_partition (bool *data, octave_idx_type *idx,
61{
62 // FIXME: This is essentially a simple bucket sort.
63 // Can it be efficiently done by std::partition?
65 octave_idx_type k = 0;
66 octave_idx_type l = 0;
67 for (octave_idx_type i = 0; i < nel; i++)
68 {
69 if (data[i] == desc)
70 {
71 data[k] = desc;
72 idx[k++] = idx[i];
73 }
74 else
75 jdx[l++] = idx[i];
76 }
77
78 for (octave_idx_type i = k; i < nel; i++)
79 {
80 data[i] = ! desc;
81 idx[i] = jdx[i-k];
82 }
83}
84
85template <>
86template <>
87void
89 std::less<bool>)
90{
91 do_bool_partition<false> (data, nel);
92}
93
94template <>
95template <>
96void
98 std::greater<bool>)
99{
100 do_bool_partition<true> (data, nel);
101}
102
103template <>
104template <>
105void
107 std::less<bool>)
108{
109 do_bool_partition<false> (data, idx, nel);
110}
111
112template <>
113template <>
114void
116 std::greater<bool>)
117{
118 do_bool_partition<true> (data, idx, nel);
119}
120
121template class octave_sort<bool>;
122
124
125template OCTAVE_API std::ostream& operator << (std::ostream&,
126 const Array<bool>&);
127
128#include "DiagArray2.h"
129#include "DiagArray2.cc"
130
131template class DiagArray2<bool>;
static void do_bool_partition(bool *data, octave_idx_type nel)
Definition: Array-b.cc:48
template OCTAVE_API std::ostream & operator<<(std::ostream &, const Array< bool > &)
#define INSTANTIATE_ARRAY(T, API)
Definition: Array.cc:2767
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:129
void sort(T *data, octave_idx_type nel)
Definition: oct-sort.cc:1520
#define OCTAVE_API
Definition: main.in.cc:55
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
Definition: oct-locbuf.h:44