GNU Octave  3.8.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-2013 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, l = 0;
56  for (octave_idx_type i = 0; i < nel; i++)
57  {
58  if (data[i] == desc)
59  {
60  data[k] = desc;
61  idx[k++] = idx[i];
62  }
63  else
64  jdx[l++] = idx[i];
65  }
66 
67  for (octave_idx_type i = k; i < nel; i++)
68  {
69  data[i] = ! desc;
70  idx[i] = jdx[i-k];
71  }
72 }
73 
74 template <> template <>
75 void
77  std::less<bool>)
78 {
79  do_bool_partition<false> (data, nel);
80 }
81 
82 template <> template <>
83 void
85  std::greater<bool>)
86 {
87  do_bool_partition<true> (data, nel);
88 }
89 
90 template <> template <>
91 void
93  std::less<bool>)
94 {
95  do_bool_partition<false> (data, idx, nel);
96 }
97 
98 template <> template <>
99 void
101  std::greater<bool>)
102 {
103  do_bool_partition<true> (data, idx, nel);
104 }
105 
107 
108 INSTANTIATE_ARRAY (bool, OCTAVE_API);
109 
110 template OCTAVE_API std::ostream& operator << (std::ostream&,
111  const Array<bool>&);
112 
113 #include "DiagArray2.h"
114 #include "DiagArray2.cc"
115 
116 template class OCTAVE_API DiagArray2<bool>;