annotate src/DLD-FUNCTIONS/lookup.cc @ 8617:4b26e9fbbb37

fix lookup's docstring
author Jaroslav Hajek <highegg@gmail.com>
date Wed, 28 Jan 2009 13:53:49 +0100
parents 4976f66d469b
children e01c6355304f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 // normal ascending comparator
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
53 static bool
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
54 ov_str_less (const octave_value& a, const octave_value& b)
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 return a.string_value () < b.string_value ();
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
57 }
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
58
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
59 // normal descending comparator
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
60 static bool
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
61 ov_str_greater (const octave_value& a, const octave_value& b)
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
62 {
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
63 return a.string_value () > b.string_value ();
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
64 }
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
65
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
66 // case-insensitive character comparison functors
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
67 struct icmp_char_lt : public std::binary_function<char, char, bool>
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
68 {
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
69 bool operator () (char x, char y) const
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
70 { return std::toupper (x) < std::toupper (y); }
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
71 };
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
72
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
73 struct icmp_char_gt : public std::binary_function<char, char, bool>
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 bool operator () (char x, char y) const
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
76 { return std::toupper (x) > std::toupper (y); }
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
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
79 // case-insensitive ascending comparator
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
80 static bool
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
81 ov_stri_less (const octave_value& a, const octave_value& b)
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
82 {
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
83 std::string as = a.string_value ();
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
84 std::string bs = b.string_value ();
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
85
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
86 return std::lexicographical_compare (as.begin (), as.end (),
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
87 bs.begin (), bs.end (),
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
88 icmp_char_lt());
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
89 }
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 // case-insensitive descending comparator
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
92 static bool
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
93 ov_stri_greater (const octave_value& a, const octave_value& b)
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
94 {
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
95 std::string as = a.string_value ();
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
96 std::string bs = b.string_value ();
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 return std::lexicographical_compare (as.begin (), as.end (),
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
99 bs.begin (), bs.end (),
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
100 icmp_char_gt());
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
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
103 DEFUN_DLD (lookup, args, ,
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
104 "-*- texinfo -*-\n\
8617
4b26e9fbbb37 fix lookup's docstring
Jaroslav Hajek <highegg@gmail.com>
parents: 7924
diff changeset
105 @deftypefn {Loadable Function} {@var{idx} =} lookup (@var{table}, @var{y}, @var{opt})\n\
7671
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
106 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
107 interpolation.\n\
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
108 \n\
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
109 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
110 @code{table(idx(i)) <= y(i) < table(idx(i+1))} for all @code{y(i)}\n\
8617
4b26e9fbbb37 fix lookup's docstring
Jaroslav Hajek <highegg@gmail.com>
parents: 7924
diff changeset
111 within the table. If @code{y(i) < table (1)} then\n\
4b26e9fbbb37 fix lookup's docstring
Jaroslav Hajek <highegg@gmail.com>
parents: 7924
diff changeset
112 @code{idx(i)} is 0. If @code{y(i) >= table(end)} then\n\
7671
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
113 @code{idx(i)} is @code{table(n)}.\n\
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
114 \n\
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
115 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
116 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
117 strictly monotonic.\n\
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
118 \n\
8617
4b26e9fbbb37 fix lookup's docstring
Jaroslav Hajek <highegg@gmail.com>
parents: 7924
diff changeset
119 The algorithm used by lookup is standard binary search, with optimizations\n\
4b26e9fbbb37 fix lookup's docstring
Jaroslav Hajek <highegg@gmail.com>
parents: 7924
diff changeset
120 to speed up the case of partially ordered arrays (dense downsampling).\n\
4b26e9fbbb37 fix lookup's docstring
Jaroslav Hajek <highegg@gmail.com>
parents: 7924
diff changeset
121 In particular, looking up a single entry is of binary complexity.\n\
4b26e9fbbb37 fix lookup's docstring
Jaroslav Hajek <highegg@gmail.com>
parents: 7924
diff changeset
122 \n\
7671
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
123 @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
124 (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
125 is performed using lexicographical comparison.\n\
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
126 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
127 additional options.\n\
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
128 \n\
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
129 For numeric lookup, 'l' in @var{opts} indicates that\n\
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
130 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
131 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
132 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
133 \n\
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
134 For string lookup, 'i' indicates case-insensitive comparison.\n\
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
135 @end deftypefn")
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
136 {
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
137 octave_value_list retval;
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
138
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
139 int nargin = args.length ();
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
140
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
141 if (nargin < 2 || nargin > 3 || (nargin == 3 && ! args(2).is_string ()))
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
142 {
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
143 print_usage ();
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
144 return retval;
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
145 }
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
146
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
147 octave_value argtable = args(0), argy = args(1);
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
148 if (argtable.ndims () > 2 || (argtable.columns () > 1 && argtable.rows () > 1))
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
149 warning ("lookup: table is not a vector");
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
150
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
151 bool num_case = argtable.is_numeric_type () && argy.is_numeric_type ();
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
152 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
153
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
154 if (num_case)
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 bool left_inf = false;
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
157 bool right_inf = false;
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
158
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
159 if (nargin == 3)
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 std::string opt = args(2).string_value ();
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
162 left_inf = contains_char (opt, 'l');
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
163 right_inf = contains_char (opt, 'r');
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
164 }
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 // 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
167 // (though it's not too meaningful).
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
168 ArrayN<octave_idx_type> idx;
7671
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
169
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
170 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
171 {
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
172 FloatNDArray table = (argtable.is_complex_type ())
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
173 ? argtable.float_complex_array_value ().abs ()
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
174 : argtable.float_array_value ();
7671
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
175
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
176 FloatNDArray y = (argy.is_complex_type ())
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
177 ? argy.float_complex_array_value ().abs ()
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
178 : argy.float_array_value ();
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
179
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
180 idx = ArrayN<octave_idx_type> (y.dims ());
7671
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
181
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
182 // determine whether the array is descending.
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
183 bool desc = is_descending (table.data (), table.length ());
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
184 octave_idx_type offset = left_inf ? 1 : 0;
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
185 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
186 if (size < 0)
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
187 size = 0;
7671
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
188
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
189 if (desc)
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
190 seq_lookup (table.data (), offset, size,
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
191 y.data (), y.length (), idx.fortran_vec (),
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
192 std::greater<float> ());
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
193 else
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
194 seq_lookup (table.data (), offset, size,
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
195 y.data (), y.length (), idx.fortran_vec (),
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
196 std::less<float> ());
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
197 }
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
198 else
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
199 {
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
200 NDArray table = (argtable.is_complex_type ())
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
201 ? argtable.complex_array_value ().abs ()
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
202 : argtable.array_value ();
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
203
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
204 NDArray y = (argy.is_complex_type ())
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
205 ? argy.complex_array_value ().abs ()
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
206 : argy.array_value ();
7671
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
207
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
208 idx = ArrayN<octave_idx_type> (y.dims ());
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
209
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
210 // determine whether the array is descending.
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
211 bool desc = is_descending (table.data (), table.length ());
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
212 octave_idx_type offset = left_inf ? 1 : 0;
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
213 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
214 if (size < 0)
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
215 size = 0;
7671
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
216
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
217 if (desc)
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
218 seq_lookup (table.data (), offset, size,
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
219 y.data (), y.length (), idx.fortran_vec (),
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
220 std::greater<double> ());
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
221 else
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
222 seq_lookup (table.data (), offset, size,
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
223 y.data (), y.length (), idx.fortran_vec (),
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
224 std::less<double> ());
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7671
diff changeset
225 }
7671
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
226
7919
9d080df0c843 new NDArray constructor for ArrayN<octave_idx_type>
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
227 retval(0) = NDArray (idx);
7671
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
228 }
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
229 else if (str_case)
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
230 {
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
231 Cell table = argtable.cell_value ();
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
232
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
233 bool (*ov_str_comp) (const octave_value&, const octave_value&);
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
234
7924
4976f66d469b miscellaneous cleanup
John W. Eaton <jwe@octave.org>
parents: 7919
diff changeset
235 bool icase = false;
7671
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
236
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
237 // check for case-insensitive option
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
238 if (nargin == 3)
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
239 {
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
240 std::string opt = args(2).string_value ();
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
241 icase = contains_char (opt, 'i');
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
242 }
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
243
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
244 // pick the correct comparator
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
245 if (icase)
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 if (is_descending (table.data (), table.length (), ov_stri_less))
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
248 ov_str_comp = ov_stri_greater;
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
249 else
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
250 ov_str_comp = ov_stri_less;
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
251 }
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
252 else
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
253 {
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
254 if (is_descending (table.data (), table.length (), ov_str_less))
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
255 ov_str_comp = ov_str_greater;
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
256 else
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
257 ov_str_comp = ov_str_less;
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
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
261 // query just the first cell to verify it's a string
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
262 if (table(0).is_string ())
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
263 {
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
264 if (argy.is_cell ())
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
265 {
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
266 Cell y = argy.cell_value ();
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
267 ArrayN<octave_idx_type> idx (y.dims ());
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
268
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
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
271 for (int i = 0; i < y.numel (); i++)
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
272 idx(i) = bin_lookup (table.data (), table.length (), y(i),
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
273 std::ptr_fun (ov_str_comp));
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
274
7919
9d080df0c843 new NDArray constructor for ArrayN<octave_idx_type>
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
275 retval(0) = NDArray (idx);
7671
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 else
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
278 {
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
279 octave_idx_type idx;
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
280
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
281 idx = bin_lookup (table.data (), table.length (), argy,
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
282 std::ptr_fun (ov_str_comp));
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
283
7919
9d080df0c843 new NDArray constructor for ArrayN<octave_idx_type>
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
284 retval(0) = static_cast<double> (idx);
7671
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 else
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
288 error("lookup: table is not a cell array of strings.");
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
289 }
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
290 else
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
291 print_usage ();
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
292
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
293 return retval;
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
294
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
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 %!assert (real(lookup(1:3, 0.5)), 0) # value before table
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
299 %!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
300 %!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
301 %!assert (real(lookup(1:3, [3,2,1])), [3,2,1])
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
302 %!assert (real(lookup([1:4]', [1.2, 3.5]')), [1, 3]');
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
303 %!assert (real(lookup([1:4], [1.2, 3.5]')), [1, 3]');
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
304 %!assert (real(lookup([1:4]', [1.2, 3.5])), [1, 3]);
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
305 %!assert (real(lookup([1:4], [1.2, 3.5])), [1, 3]);
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
306 %!assert (real(lookup(1:3, [3, 2, 1])), [3, 2, 1]);
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
307 %!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
308 %!assert (isempty(lookup([1:3], [])))
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
309 %!assert (isempty(lookup([1:3]', [])))
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
310 %!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
311 %!
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
312 %!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
313 %!assert (real(lookup({"apple","lemon","orange"}, "potato")), 3)
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
314 %!assert (real(lookup({"orange","lemon","apple"}, "potato")), 0)
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
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
317 /*
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
318 ;;; Local Variables: ***
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
319 ;;; mode: C++ ***
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
320 ;;; End: ***
4fbaba9abec1 implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
321 */