GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Array-b.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2015 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 the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef 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 #define INLINE_ASCENDING_SORT
32 #define INLINE_DESCENDING_SORT
33 #include "oct-sort.cc"
34 
35 // Specialize bool sorting (aka stable partitioning).
36 
37 template<bool desc>
38 static void do_bool_partition (bool *data, octave_idx_type nel)
39 {
40  octave_idx_type k = 0;
41  for (octave_idx_type i = 0; i < nel; i++)
42  if (data[i] == desc)
43  data[k++] = desc;
44  for (octave_idx_type i = k; i < nel; i++)
45  data[i] = ! desc;
46 }
47 
48 template<bool desc>
49 static void do_bool_partition (bool *data, octave_idx_type *idx,
50  octave_idx_type nel)
51 {
52  // FIXME: This is essentially a simple bucket sort.
53  // Can it be efficiently done by std::partition?
55  octave_idx_type k = 0;
56  octave_idx_type l = 0;
57  for (octave_idx_type i = 0; i < nel; i++)
58  {
59  if (data[i] == desc)
60  {
61  data[k] = desc;
62  idx[k++] = idx[i];
63  }
64  else
65  jdx[l++] = idx[i];
66  }
67 
68  for (octave_idx_type i = k; i < nel; i++)
69  {
70  data[i] = ! desc;
71  idx[i] = jdx[i-k];
72  }
73 }
74 
75 template <> template <>
76 void
78  std::less<bool>)
79 {
80  do_bool_partition<false> (data, nel);
81 }
82 
83 template <> template <>
84 void
86  std::greater<bool>)
87 {
88  do_bool_partition<true> (data, nel);
89 }
90 
91 template <> template <>
92 void
94  std::less<bool>)
95 {
96  do_bool_partition<false> (data, idx, nel);
97 }
98 
99 template <> template <>
100 void
102  std::greater<bool>)
103 {
104  do_bool_partition<true> (data, idx, nel);
105 }
106 
107 template class OCTAVE_API octave_sort<bool>;
108 
109 INSTANTIATE_ARRAY (bool, OCTAVE_API);
110 
111 template OCTAVE_API std::ostream& operator << (std::ostream&,
112  const Array<bool>&);
113 
114 #include "DiagArray2.h"
115 #include "DiagArray2.cc"
116 
117 template class OCTAVE_API DiagArray2<bool>;
static void do_bool_partition(bool *data, octave_idx_type nel)
Definition: Array-b.cc:38
#define INSTANTIATE_ARRAY(T, API)
Definition: Array.cc:2773
void sort(T *data, octave_idx_type nel)
Definition: oct-sort.cc:1514
template OCTAVE_API std::ostream & operator<<(std::ostream &, const Array< bool > &)
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
Definition: oct-locbuf.h:197