Mercurial > octave
annotate src/DLD-FUNCTIONS/lookup.cc @ 7789:82be108cc558
First attempt at single precision tyeps
* * *
corrections to qrupdate single precision routines
* * *
prefer demotion to single over promotion to double
* * *
Add single precision support to log2 function
* * *
Trivial PROJECT file update
* * *
Cache optimized hermitian/transpose methods
* * *
Add tests for tranpose/hermitian and ChangeLog entry for new transpose code
author | David Bateman <dbateman@free.fr> |
---|---|
date | Sun, 27 Apr 2008 22:34:17 +0200 |
parents | 4fbaba9abec1 |
children | 9d080df0c843 |
rev | line source |
---|---|
7671
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
1 /* |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
2 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
3 Copyright (C) 2008 VZLU Prague a.s., Czech Republic |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
4 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
5 This file is part of Octave. |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
6 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
7 Octave is free software; you can redistribute it and/or modify it |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
8 under the terms of the GNU General Public License as published by |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
9 the Free Software Foundation; either version 3 of the License, or (at |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
10 your option) any later version. |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
11 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
12 Octave is distributed in the hope that it will be useful, but |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
13 WITHOUT ANY WARRANTY; without even the implied warranty of |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
15 General Public License for more details. |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
16 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
17 You should have received a copy of the GNU General Public License |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
18 along with Octave; see the file COPYING. If not, see |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
19 <http://www.gnu.org/licenses/>. |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
20 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
21 */ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
22 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
23 // Author: Jaroslav Hajek <highegg@gmail.com> |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
24 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
25 #include <cctype> |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
26 #include <functional> |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
27 #include <algorithm> |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
28 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
29 #ifdef HAVE_CONFIG_H |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
30 #include <config.h> |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
31 #endif |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
32 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
33 #include "dNDArray.h" |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
34 #include "CNDArray.h" |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
35 #include "oct-lookup.h" |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
36 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
37 #include "Cell.h" |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
38 #include "defun-dld.h" |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
39 #include "error.h" |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
40 #include "gripes.h" |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
41 #include "oct-obj.h" |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
42 #include "ov.h" |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
43 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
44 static |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
45 bool |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
46 contains_char (const std::string& str, char c) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
47 { |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
48 return (str.find (c) != std::string::npos |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
49 || str.find (std::toupper (c)) != std::string::npos); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
50 } |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
51 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
52 // FIXME -- remove these one once octave_value supports octave_idx_type. |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
53 static octave_value& |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
54 assign (octave_value& ov, octave_idx_type idx) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
55 { |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
56 double tmp = idx; |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
57 ov = tmp; |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
58 return ov; |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
59 } |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
60 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
61 static octave_value& |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
62 assign (octave_value& ov, const ArrayN<octave_idx_type>& ida) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
63 { |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
64 NDArray tmp (ida.dims ()); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
65 for (int i = 0; i < ida.numel (); i++) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
66 tmp(i) = ida(i); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
67 ov = tmp; |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
68 return ov; |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
69 } |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
70 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
71 // normal ascending comparator |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
72 static bool |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
73 ov_str_less (const octave_value& a, const octave_value& b) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
74 { |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
75 return a.string_value () < b.string_value (); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
76 } |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
77 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
78 // normal descending comparator |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
79 static bool |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
80 ov_str_greater (const octave_value& a, const octave_value& b) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
81 { |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
82 return a.string_value () > b.string_value (); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
83 } |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
84 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
85 // case-insensitive character comparison functors |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
86 struct icmp_char_lt : public std::binary_function<char, char, bool> |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
87 { |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
88 bool operator () (char x, char y) const |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
89 { return std::toupper (x) < std::toupper (y); } |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
90 }; |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
91 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
92 struct icmp_char_gt : public std::binary_function<char, char, bool> |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
93 { |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
94 bool operator () (char x, char y) const |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
95 { return std::toupper (x) > std::toupper (y); } |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
96 }; |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
97 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
98 // case-insensitive ascending comparator |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
99 static bool |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
100 ov_stri_less (const octave_value& a, const octave_value& b) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
101 { |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
102 std::string as = a.string_value (); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
103 std::string bs = b.string_value (); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
104 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
105 return std::lexicographical_compare (as.begin (), as.end (), |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
106 bs.begin (), bs.end (), |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
107 icmp_char_lt()); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
108 } |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
109 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
110 // case-insensitive descending comparator |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
111 static bool |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
112 ov_stri_greater (const octave_value& a, const octave_value& b) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
113 { |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
114 std::string as = a.string_value (); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
115 std::string bs = b.string_value (); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
116 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
117 return std::lexicographical_compare (as.begin (), as.end (), |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
118 bs.begin (), bs.end (), |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
119 icmp_char_gt()); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
120 } |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
121 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
122 DEFUN_DLD (lookup, args, , |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
123 "-*- texinfo -*-\n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
124 @deftypefn {Function File} {@var{idx} =} lookup (@var{table}, @var{y}, @var{opt})\n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
125 Lookup values in a sorted table. Usually used as a prelude to\n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
126 interpolation.\n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
127 \n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
128 If table is strictly increasing and @code{idx = lookup (table, y)}, then\n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
129 @code{table(idx(i)) <= y(i) < table(idx(i+1))} for all @code{y(i)}\n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
130 within the table. If @code{y(i)} is before the table, then\n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
131 @code{idx(i)} is 0. If @code{y(i)} is after the table then\n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
132 @code{idx(i)} is @code{table(n)}.\n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
133 \n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
134 If the table is strictly decreasing, then the tests are reversed.\n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
135 There are no guarantees for tables which are non-monotonic or are not\n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
136 strictly monotonic.\n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
137 \n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
138 @var{table} and @var{y} can also be a cell array of strings\n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
139 (or @var{y} can be a single string). In this case, string lookup\n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
140 is performed using lexicographical comparison.\n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
141 If @var{opts} is specified, it shall be a string with letters indicating\n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
142 additional options.\n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
143 \n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
144 For numeric lookup, 'l' in @var{opts} indicates that\n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
145 the leftmost subinterval shall be extended to infinity (i.e. all indices\n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
146 at least 1), and 'r' indicates that the rightmost subinterval shall be\n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
147 extended to infinity (i.e. all indices at most n-1).\n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
148 \n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
149 For string lookup, 'i' indicates case-insensitive comparison.\n\ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
150 @end deftypefn") |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
151 { |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
152 octave_value_list retval; |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
153 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
154 int nargin = args.length (); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
155 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
156 if (nargin < 2 || nargin > 3 || (nargin == 3 && ! args(2).is_string ())) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
157 { |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
158 print_usage (); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
159 return retval; |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
160 } |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
161 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
162 octave_value argtable = args(0), argy = args(1); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
163 if (argtable.ndims () > 2 || (argtable.columns () > 1 && argtable.rows () > 1)) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
164 warning ("lookup: table is not a vector"); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
165 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
166 bool num_case = argtable.is_numeric_type () && argy.is_numeric_type (); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
167 bool str_case = argtable.is_cell () && (argy.is_cell () || argy.is_string ()); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
168 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
169 if (num_case) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
170 { |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
171 bool left_inf = false; |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
172 bool right_inf = false; |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
173 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
174 if (nargin == 3) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
175 { |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
176 std::string opt = args(2).string_value (); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
177 left_inf = contains_char (opt, 'l'); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
178 right_inf = contains_char (opt, 'r'); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
179 } |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
180 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
181 // in the case of a complex array, absolute values will be used for compatibility |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
182 // (though it's not too meaningful). |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
183 ArrayN<octave_idx_type> idx; |
7671
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
184 |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
185 if (argtable.is_single_type () || argy.is_single_type ()) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
186 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
187 FloatNDArray table = (argtable.is_complex_type ()) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
188 ? argtable.float_complex_array_value ().abs () |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
189 : argtable.float_array_value (); |
7671
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
190 |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
191 FloatNDArray y = (argy.is_complex_type ()) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
192 ? argy.float_complex_array_value ().abs () |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
193 : argy.float_array_value (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
194 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
195 idx = ArrayN<octave_idx_type> (y.dims ()); |
7671
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
196 |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
197 // determine whether the array is descending. |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
198 bool desc = is_descending (table.data (), table.length ()); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
199 octave_idx_type offset = left_inf ? 1 : 0; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
200 octave_idx_type size = table.length () - offset - (right_inf ? 1 : 0); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
201 if (size < 0) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
202 size = 0; |
7671
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
203 |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
204 if (desc) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
205 seq_lookup (table.data (), offset, size, |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
206 y.data (), y.length (), idx.fortran_vec (), |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
207 std::greater<float> ()); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
208 else |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
209 seq_lookup (table.data (), offset, size, |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
210 y.data (), y.length (), idx.fortran_vec (), |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
211 std::less<float> ()); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
212 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
213 else |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
214 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
215 NDArray table = (argtable.is_complex_type ()) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
216 ? argtable.complex_array_value ().abs () |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
217 : argtable.array_value (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
218 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
219 NDArray y = (argy.is_complex_type ()) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
220 ? argy.complex_array_value ().abs () |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
221 : argy.array_value (); |
7671
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
222 |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
223 idx = ArrayN<octave_idx_type> (y.dims ()); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
224 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
225 // determine whether the array is descending. |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
226 bool desc = is_descending (table.data (), table.length ()); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
227 octave_idx_type offset = left_inf ? 1 : 0; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
228 octave_idx_type size = table.length () - offset - (right_inf ? 1 : 0); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
229 if (size < 0) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
230 size = 0; |
7671
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
231 |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
232 if (desc) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
233 seq_lookup (table.data (), offset, size, |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
234 y.data (), y.length (), idx.fortran_vec (), |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
235 std::greater<double> ()); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
236 else |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
237 seq_lookup (table.data (), offset, size, |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
238 y.data (), y.length (), idx.fortran_vec (), |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
239 std::less<double> ()); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7671
diff
changeset
|
240 } |
7671
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
241 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
242 //retval(0) = idx; |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
243 assign (retval(0), idx); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
244 } |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
245 else if (str_case) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
246 { |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
247 Cell table = argtable.cell_value (); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
248 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
249 bool (*ov_str_comp) (const octave_value&, const octave_value&); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
250 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
251 bool icase = false, desc; |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
252 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
253 // check for case-insensitive option |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
254 if (nargin == 3) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
255 { |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
256 std::string opt = args(2).string_value (); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
257 icase = contains_char (opt, 'i'); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
258 } |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
259 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
260 // pick the correct comparator |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
261 if (icase) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
262 { |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
263 if (is_descending (table.data (), table.length (), ov_stri_less)) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
264 ov_str_comp = ov_stri_greater; |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
265 else |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
266 ov_str_comp = ov_stri_less; |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
267 } |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
268 else |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
269 { |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
270 if (is_descending (table.data (), table.length (), ov_str_less)) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
271 ov_str_comp = ov_str_greater; |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
272 else |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
273 ov_str_comp = ov_str_less; |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
274 } |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
275 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
276 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
277 // query just the first cell to verify it's a string |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
278 if (table(0).is_string ()) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
279 { |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
280 if (argy.is_cell ()) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
281 { |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
282 Cell y = argy.cell_value (); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
283 ArrayN<octave_idx_type> idx (y.dims ()); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
284 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
285 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
286 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
287 for (int i = 0; i < y.numel (); i++) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
288 idx(i) = bin_lookup (table.data (), table.length (), y(i), |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
289 std::ptr_fun (ov_str_comp)); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
290 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
291 //retval(0) = idx; |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
292 assign (retval(0), idx); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
293 } |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
294 else |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
295 { |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
296 octave_idx_type idx; |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
297 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
298 idx = bin_lookup (table.data (), table.length (), argy, |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
299 std::ptr_fun (ov_str_comp)); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
300 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
301 //retval(0) = idx; |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
302 assign (retval(0), idx); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
303 } |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
304 } |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
305 else |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
306 error("lookup: table is not a cell array of strings."); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
307 } |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
308 else |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
309 print_usage (); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
310 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
311 return retval; |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
312 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
313 } |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
314 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
315 /* |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
316 %!assert (real(lookup(1:3, 0.5)), 0) # value before table |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
317 %!assert (real(lookup(1:3, 3.5)), 3) # value after table error |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
318 %!assert (real(lookup(1:3, 1.5)), 1) # value within table error |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
319 %!assert (real(lookup(1:3, [3,2,1])), [3,2,1]) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
320 %!assert (real(lookup([1:4]', [1.2, 3.5]')), [1, 3]'); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
321 %!assert (real(lookup([1:4], [1.2, 3.5]')), [1, 3]'); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
322 %!assert (real(lookup([1:4]', [1.2, 3.5])), [1, 3]); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
323 %!assert (real(lookup([1:4], [1.2, 3.5])), [1, 3]); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
324 %!assert (real(lookup(1:3, [3, 2, 1])), [3, 2, 1]); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
325 %!assert (real(lookup([3:-1:1], [3.5, 3, 1.2, 2.5, 2.5])), [0, 1, 2, 1, 1]) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
326 %!assert (isempty(lookup([1:3], []))) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
327 %!assert (isempty(lookup([1:3]', []))) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
328 %!assert (real(lookup(1:3, [1, 2; 3, 0.5])), [1, 2; 3, 0]); |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
329 %! |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
330 %!assert (real(lookup({"apple","lemon","orange"}, {"banana","kiwi"; "ananas","mango"})), [1,1;0,2]) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
331 %!assert (real(lookup({"apple","lemon","orange"}, "potato")), 3) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
332 %!assert (real(lookup({"orange","lemon","apple"}, "potato")), 0) |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
333 */ |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
334 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
335 /* |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
336 ;;; Local Variables: *** |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
337 ;;; mode: C++ *** |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
338 ;;; End: *** |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
339 */ |