Mercurial > hg > octave-lyh
comparison liboctave/Array.cc @ 8814:de16ebeef93d
improve lookup, provide Array<T>::lookup
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Thu, 19 Feb 2009 15:19:59 +0100 |
parents | f6dc6eb57045 |
children | eb63fbe60fab |
comparison
equal
deleted
inserted
replaced
8813:70d06ed27c08 | 8814:de16ebeef93d |
---|---|
2379 mode = UNSORTED; | 2379 mode = UNSORTED; |
2380 } | 2380 } |
2381 | 2381 |
2382 return mode; | 2382 return mode; |
2383 | 2383 |
2384 } | |
2385 | |
2386 // Do a binary lookup in a sorted array. | |
2387 template <class T> | |
2388 octave_idx_type | |
2389 Array<T>::lookup (const T& value, sortmode mode) const | |
2390 { | |
2391 octave_idx_type n = numel (); | |
2392 octave_sort<T> lsort; | |
2393 | |
2394 if (mode == UNSORTED) | |
2395 { | |
2396 // auto-detect mode | |
2397 if (n > 1 && lsort.descending_compare (elem (0), elem (n-1))) | |
2398 mode = DESCENDING; | |
2399 else | |
2400 mode = ASCENDING; | |
2401 } | |
2402 | |
2403 lsort.set_compare (mode); | |
2404 | |
2405 return lsort.lookup (data (), n, value); | |
2406 } | |
2407 | |
2408 // Ditto, but for an array of values, specializing on long runs. | |
2409 // Adds optional offset to all indices. | |
2410 template <class T> | |
2411 Array<octave_idx_type> | |
2412 Array<T>::lookup (const Array<T>& values, sortmode mode, | |
2413 bool linf, bool rinf) const | |
2414 { | |
2415 octave_idx_type n = numel (); | |
2416 octave_sort<T> lsort; | |
2417 Array<octave_idx_type> idx (values.dims ()); | |
2418 | |
2419 if (mode == UNSORTED) | |
2420 { | |
2421 // auto-detect mode | |
2422 if (n > 1 && lsort.descending_compare (elem (0), elem (n-1))) | |
2423 mode = DESCENDING; | |
2424 else | |
2425 mode = ASCENDING; | |
2426 } | |
2427 | |
2428 lsort.set_compare (mode); | |
2429 | |
2430 // set offset and shift size. | |
2431 octave_idx_type offset = 0; | |
2432 | |
2433 if (linf && n > 0) | |
2434 { | |
2435 offset++; | |
2436 n--; | |
2437 } | |
2438 if (rinf && n > 0) | |
2439 n--; | |
2440 | |
2441 lsort.lookup (data () + offset, n, values.data (), values.numel (), | |
2442 idx.fortran_vec (), offset); | |
2443 | |
2444 return idx; | |
2384 } | 2445 } |
2385 | 2446 |
2386 #define INSTANTIATE_ARRAY_SORT(T) template class octave_sort<T>; | 2447 #define INSTANTIATE_ARRAY_SORT(T) template class octave_sort<T>; |
2387 | 2448 |
2388 #define NO_INSTANTIATE_ARRAY_SORT(T) \ | 2449 #define NO_INSTANTIATE_ARRAY_SORT(T) \ |
2407 { return Array<octave_idx_type> (); } \ | 2468 { return Array<octave_idx_type> (); } \ |
2408 \ | 2469 \ |
2409 template <> sortmode \ | 2470 template <> sortmode \ |
2410 Array<T>::is_sorted_rows (sortmode) const \ | 2471 Array<T>::is_sorted_rows (sortmode) const \ |
2411 { return UNSORTED; } \ | 2472 { return UNSORTED; } \ |
2473 \ | |
2474 template <> octave_idx_type \ | |
2475 Array<T>::lookup (const T&, sortmode) const \ | |
2476 { return 0; } \ | |
2477 template <> Array<octave_idx_type> \ | |
2478 Array<T>::lookup (const Array<T>&, sortmode, bool, bool) const \ | |
2479 { return Array<octave_idx_type> (); } \ | |
2412 | 2480 |
2413 | 2481 |
2414 | 2482 |
2415 template <class T> | 2483 template <class T> |
2416 Array<T> | 2484 Array<T> |