Array-b.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1996-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 // Instantiate Arrays of bool values.
00028 
00029 #include "Array.h"
00030 #include "Array.cc"
00031 #define INLINE_ASCENDING_SORT
00032 #define INLINE_DESCENDING_SORT
00033 #include "oct-sort.cc"
00034 
00035 // Specialize bool sorting (aka stable partitioning).
00036 
00037 template<bool desc>
00038 static void do_bool_partition (bool *data, octave_idx_type nel)
00039 {
00040   octave_idx_type k = 0;
00041   for (octave_idx_type i = 0; i < nel; i++)
00042     if (data[i] == desc)
00043       data[k++] = desc;
00044   for (octave_idx_type i = k; i < nel; i++)
00045     data[i] = ! desc;
00046 }
00047 
00048 template<bool desc>
00049 static void do_bool_partition (bool *data, octave_idx_type *idx,
00050                                octave_idx_type nel)
00051 {
00052   // FIXME: This is essentially a simple bucket sort.
00053   // Can it be efficiently done by std::partition?
00054   OCTAVE_LOCAL_BUFFER (octave_idx_type, jdx, nel);
00055   octave_idx_type k = 0, l = 0;
00056   for (octave_idx_type i = 0; i < nel; i++)
00057     {
00058       if (data[i] == desc)
00059         {
00060           data[k] = desc;
00061           idx[k++] = idx[i];
00062         }
00063       else
00064         jdx[l++] = idx[i];
00065     }
00066 
00067   for (octave_idx_type i = k; i < nel; i++)
00068     {
00069       data[i] = ! desc;
00070       idx[i] = jdx[i-k];
00071     }
00072 }
00073 
00074 template <> template <>
00075 void
00076 octave_sort<bool>::sort (bool *data, octave_idx_type nel,
00077                          std::less<bool>)
00078 {
00079   do_bool_partition<false> (data, nel);
00080 }
00081 
00082 template <> template <>
00083 void
00084 octave_sort<bool>::sort (bool *data, octave_idx_type nel,
00085                          std::greater<bool>)
00086 {
00087   do_bool_partition<true> (data, nel);
00088 }
00089 
00090 template <> template <>
00091 void
00092 octave_sort<bool>::sort (bool *data, octave_idx_type *idx, octave_idx_type nel,
00093                          std::less<bool>)
00094 {
00095   do_bool_partition<false> (data, idx, nel);
00096 }
00097 
00098 template <> template <>
00099 void
00100 octave_sort<bool>::sort (bool *data, octave_idx_type *idx, octave_idx_type nel,
00101                          std::greater<bool>)
00102 {
00103   do_bool_partition<true> (data, idx, nel);
00104 }
00105 
00106 INSTANTIATE_ARRAY_SORT (bool);
00107 
00108 INSTANTIATE_ARRAY (bool, OCTAVE_API);
00109 
00110 template OCTAVE_API std::ostream& operator << (std::ostream&, const Array<bool>&);
00111 
00112 #include "DiagArray2.h"
00113 #include "DiagArray2.cc"
00114 
00115 template class OCTAVE_API DiagArray2<bool>;
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines