47 return (str.find (c) != std::string::npos
48 || str.find (std::toupper (c)) != std::string::npos);
52 struct icmp_char_lt :
public std::binary_function<char, char, bool>
55 {
return std::toupper (x) < std::toupper (y); }
58 struct icmp_char_gt :
public std::binary_function<char, char, bool>
61 {
return std::toupper (x) > std::toupper (y); }
69 stri_comp_lt (
const std::string& a,
const std::string& b)
71 return std::lexicographical_compare (a.begin (), a.end (),
78 stri_comp_gt (
const std::string& a,
const std::string& b)
80 return std::lexicographical_compare (a.begin (), a.end (),
93 if (n > 1 && desc_comp (array (0), array (n-1)))
103 #define INT_ARRAY_LOOKUP(TYPE) \
104 (table.is_ ## TYPE ## _type () && y.is_ ## TYPE ## _type ()) \
105 retval = do_numeric_lookup (table.TYPE ## _array_value (), \
106 y.TYPE ## _array_value (), \
107 left_inf, right_inf, \
108 match_idx, match_bool);
109 template <
class ArrayT>
112 bool left_inf,
bool right_inf,
113 bool match_idx,
bool match_bool)
128 match.xelem (i) = j != 0 && values(i) == array(j-1);
133 else if (match_idx || left_inf || right_inf)
142 ridx.xelem (i) = (j != 0 && values(i) == array(j-1)) ? j : 0;
147 else if (left_inf && right_inf)
192 @deftypefn {Built-in Function} {@var{idx} =} lookup (@var{table}, @var{y})\n\
193 @deftypefnx {Built-in Function} {@var{idx} =} lookup (@var{table}, @var{y}, @var{opt})\n\
194 Lookup values in a sorted table.\n\
196 This function is usually used as a prelude to interpolation.\n\
198 If table is increasing and @code{idx = lookup (table, y)}, then\n\
199 @code{table(idx(i)) <= y(i) < table(idx(i+1))} for all @code{y(i)} within\n\
200 the table. If @code{y(i) < table(1)} then @code{idx(i)} is 0. If\n\
201 @code{y(i) >= table(end)} or @code{isnan (y(i))} then @code{idx(i)} is\n\
204 If the table is decreasing, then the tests are reversed.\n\
205 For non-strictly monotonic tables, empty intervals are always skipped.\n\
206 The result is undefined if @var{table} is not monotonic, or if\n\
207 @var{table} contains a NaN.\n\
209 The complexity of the lookup is O(M*log(N)) where N is the size of\n\
210 @var{table} and M is the size of @var{y}. In the special case when @var{y}\n\
211 is also sorted, the complexity is O(min(M*log(N),M+N)).\n\
213 @var{table} and @var{y} can also be cell arrays of strings\n\
214 (or @var{y} can be a single string). In this case, string lookup\n\
215 is performed using lexicographical comparison.\n\
217 If @var{opts} is specified, it must be a string with letters indicating\n\
218 additional options.\n\
222 @code{table(idx(i)) == val(i)} if @code{val(i)}\n\
223 occurs in table; otherwise, @code{idx(i)} is zero.\n\
226 @code{idx(i)} is a logical 1 or 0, indicating whether\n\
227 @code{val(i)} is contained in table or not.\n\
230 For numeric lookups\n\
231 the leftmost subinterval shall be extended to infinity (i.e., all indices\n\
235 For numeric lookups\n\
236 the rightmost subinterval shall be extended to infinity (i.e., all indices\n\
243 int nargin = args.
length ();
245 if (nargin < 2 || nargin > 3 || (nargin == 3 && ! args(2).is_string ()))
254 warning (
"lookup: table is not a vector");
259 bool left_inf =
false;
260 bool right_inf =
false;
261 bool match_idx =
false;
262 bool match_bool =
false;
266 std::string opt = args(2).string_value ();
271 if (opt.find_first_not_of (
"lrmb") != std::string::npos)
273 error (
"lookup: unrecognized option: %c",
274 opt[opt.find_first_not_of (
"lrmb")]);
279 if ((match_idx || match_bool) && (left_inf || right_inf))
280 error (
"lookup: m, b cannot be specified with l or r");
281 else if (match_idx && match_bool)
282 error (
"lookup: only one of m or b can be specified");
283 else if (str_case && (left_inf || right_inf))
284 error (
"lookup: l, r are not recognized for string lookups");
296 table = table.
abs ();
316 match_idx, match_bool);
321 match_idx, match_bool);
326 match_idx, match_bool);
349 match.xelem (i) = j != 0 && str_y(i) == str_table(j-1);
362 ridx.xelem (i) = (j != 0 && str_y(i) == str_table(j-1)) ? j
charNDArray char_array_value(bool frc_str_conv=false) const
octave_idx_type rows(void) const
OCTINTERP_API void print_usage(void)
octave_idx_type numel(void) const
Number of elements in the array.
sortmode get_sort_mode(const Array< T > &array, typename octave_sort< T >::compare_fcn_type desc_comp=octave_sort< T >::descending_compare)
bool is_numeric_type(void) const
#define DEFUN(name, args_name, nargout_name, doc)
void error(const char *fmt,...)
octave_idx_type lookup(const T *x, octave_idx_type n, T y)
static octave_value do_numeric_lookup(const ArrayT &array, const ArrayT &values, bool left_inf, bool right_inf, bool match_idx, bool match_bool)
octave_idx_type lookup(const T &value, sortmode mode=UNSORTED) const
Do a binary lookup in a sorted array.
Array< std::string > cellstr_value(void) const
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
octave_idx_type columns(void) const
FloatNDArray float_array_value(bool frc_str_conv=false) const
bool is_char_matrix(void) const
std::string string_value(bool force=false) const
bool is_string(void) const
bool is_complex_type(void) const
bool is_cellstr(void) const
octave_value abs(void) const
octave_idx_type length(void) const
T & xelem(octave_idx_type n)
void warning(const char *fmt,...)
Handles the reference counting for all the derived classes.
charNDArray max(char d, const charNDArray &m)
static bool contains_char(const std::string &str, char c)
NDArray array_value(bool frc_str_conv=false) const
bool operator()(char x, char y) const
#define INT_ARRAY_LOOKUP(TYPE)
static bool match(const std::string &filename_arg, const std::string &path_elt_arg)
bool is_single_type(void) const
bool operator()(char x, char y) const
F77_RET_T const double * x
charNDArray min(char d, const charNDArray &m)