comparison src/DLD-FUNCTIONS/find.cc @ 10154:40dfc0c99116

DLD-FUNCTIONS/*.cc: untabify
author John W. Eaton <jwe@octave.org>
date Wed, 20 Jan 2010 17:33:41 -0500
parents 83bd7f34f9da
children d0ce5e973937
comparison
equal deleted inserted replaced
10153:2c28f9d0360f 10154:40dfc0c99116
37 // output arguments. If N_TO_FIND is -1, find all nonzero elements. 37 // output arguments. If N_TO_FIND is -1, find all nonzero elements.
38 38
39 template <typename T> 39 template <typename T>
40 octave_value_list 40 octave_value_list
41 find_nonzero_elem_idx (const Array<T>& nda, int nargout, 41 find_nonzero_elem_idx (const Array<T>& nda, int nargout,
42 octave_idx_type n_to_find, int direction) 42 octave_idx_type n_to_find, int direction)
43 { 43 {
44 octave_value_list retval ((nargout == 0 ? 1 : nargout), Matrix ()); 44 octave_value_list retval ((nargout == 0 ? 1 : nargout), Matrix ());
45 45
46 Array<octave_idx_type> idx; 46 Array<octave_idx_type> idx;
47 if (n_to_find >= 0) 47 if (n_to_find >= 0)
78 return retval; 78 return retval;
79 } 79 }
80 80
81 #define INSTANTIATE_FIND_ARRAY(T) \ 81 #define INSTANTIATE_FIND_ARRAY(T) \
82 template octave_value_list find_nonzero_elem_idx (const Array<T>&, int, \ 82 template octave_value_list find_nonzero_elem_idx (const Array<T>&, int, \
83 octave_idx_type, int) 83 octave_idx_type, int)
84 84
85 INSTANTIATE_FIND_ARRAY(double); 85 INSTANTIATE_FIND_ARRAY(double);
86 INSTANTIATE_FIND_ARRAY(float); 86 INSTANTIATE_FIND_ARRAY(float);
87 INSTANTIATE_FIND_ARRAY(Complex); 87 INSTANTIATE_FIND_ARRAY(Complex);
88 INSTANTIATE_FIND_ARRAY(FloatComplex); 88 INSTANTIATE_FIND_ARRAY(FloatComplex);
97 INSTANTIATE_FIND_ARRAY(octave_uint64); 97 INSTANTIATE_FIND_ARRAY(octave_uint64);
98 98
99 template <typename T> 99 template <typename T>
100 octave_value_list 100 octave_value_list
101 find_nonzero_elem_idx (const Sparse<T>& v, int nargout, 101 find_nonzero_elem_idx (const Sparse<T>& v, int nargout,
102 octave_idx_type n_to_find, int direction) 102 octave_idx_type n_to_find, int direction)
103 { 103 {
104 octave_value_list retval ((nargout == 0 ? 1 : nargout), Matrix ()); 104 octave_value_list retval ((nargout == 0 ? 1 : nargout), Matrix ());
105 105
106 106
107 octave_idx_type nc = v.cols(); 107 octave_idx_type nc = v.cols();
122 count = nz; 122 count = nz;
123 } 123 }
124 else if (direction > 0) 124 else if (direction > 0)
125 { 125 {
126 for (octave_idx_type j = 0; j < nc; j++) 126 for (octave_idx_type j = 0; j < nc; j++)
127 { 127 {
128 OCTAVE_QUIT; 128 OCTAVE_QUIT;
129 if (v.cidx(j) == 0 && v.cidx(j+1) != 0) 129 if (v.cidx(j) == 0 && v.cidx(j+1) != 0)
130 start_nc = j; 130 start_nc = j;
131 if (v.cidx(j+1) >= n_to_find) 131 if (v.cidx(j+1) >= n_to_find)
132 { 132 {
133 end_nc = j + 1; 133 end_nc = j + 1;
134 break; 134 break;
135 } 135 }
136 } 136 }
137 } 137 }
138 else 138 else
139 { 139 {
140 for (octave_idx_type j = nc; j > 0; j--) 140 for (octave_idx_type j = nc; j > 0; j--)
141 { 141 {
142 OCTAVE_QUIT; 142 OCTAVE_QUIT;
143 if (v.cidx(j) == nz && v.cidx(j-1) != nz) 143 if (v.cidx(j) == nz && v.cidx(j-1) != nz)
144 end_nc = j; 144 end_nc = j;
145 if (nz - v.cidx(j-1) >= n_to_find) 145 if (nz - v.cidx(j-1) >= n_to_find)
146 { 146 {
147 start_nc = j - 1; 147 start_nc = j - 1;
148 break; 148 break;
149 } 149 }
150 } 150 }
151 } 151 }
152 152
153 count = (n_to_find > v.cidx(end_nc) - v.cidx(start_nc) ? 153 count = (n_to_find > v.cidx(end_nc) - v.cidx(start_nc) ?
154 v.cidx(end_nc) - v.cidx(start_nc) : n_to_find); 154 v.cidx(end_nc) - v.cidx(start_nc) : n_to_find);
155 155
156 // If the original argument was a row vector, force a row vector of 156 // If the original argument was a row vector, force a row vector of
157 // the overall indices to be returned. But see below for scalar 157 // the overall indices to be returned. But see below for scalar
158 // case... 158 // case...
159 159
181 { 181 {
182 // Search for elements to return. Only search the region where 182 // Search for elements to return. Only search the region where
183 // there are elements to be found using the count that we want 183 // there are elements to be found using the count that we want
184 // to find. 184 // to find.
185 for (octave_idx_type j = start_nc, cx = 0; j < end_nc; j++) 185 for (octave_idx_type j = start_nc, cx = 0; j < end_nc; j++)
186 for (octave_idx_type i = v.cidx(j); i < v.cidx(j+1); i++ ) 186 for (octave_idx_type i = v.cidx(j); i < v.cidx(j+1); i++ )
187 { 187 {
188 OCTAVE_QUIT; 188 OCTAVE_QUIT;
189 if (direction < 0 && i < nz - count) 189 if (direction < 0 && i < nz - count)
190 continue; 190 continue;
191 i_idx(cx) = static_cast<double> (v.ridx(i) + 1); 191 i_idx(cx) = static_cast<double> (v.ridx(i) + 1);
192 j_idx(cx) = static_cast<double> (j + 1); 192 j_idx(cx) = static_cast<double> (j + 1);
193 idx(cx) = j * nr + v.ridx(i) + 1; 193 idx(cx) = j * nr + v.ridx(i) + 1;
194 val(cx) = v.data(i); 194 val(cx) = v.data(i);
195 cx++; 195 cx++;
196 if (cx == count) 196 if (cx == count)
197 break; 197 break;
198 } 198 }
199 } 199 }
200 else if (scalar_arg) 200 else if (scalar_arg)
201 { 201 {
202 idx.resize (0, 0); 202 idx.resize (0, 0);
203 203
238 238
239 return retval; 239 return retval;
240 } 240 }
241 241
242 template octave_value_list find_nonzero_elem_idx (const Sparse<double>&, int, 242 template octave_value_list find_nonzero_elem_idx (const Sparse<double>&, int,
243 octave_idx_type, int); 243 octave_idx_type, int);
244 244
245 template octave_value_list find_nonzero_elem_idx (const Sparse<Complex>&, int, 245 template octave_value_list find_nonzero_elem_idx (const Sparse<Complex>&, int,
246 octave_idx_type, int); 246 octave_idx_type, int);
247 247
248 template octave_value_list find_nonzero_elem_idx (const Sparse<bool>&, int, 248 template octave_value_list find_nonzero_elem_idx (const Sparse<bool>&, int,
249 octave_idx_type, int); 249 octave_idx_type, int);
250 250
251 octave_value_list 251 octave_value_list
252 find_nonzero_elem_idx (const PermMatrix& v, int nargout, 252 find_nonzero_elem_idx (const PermMatrix& v, int nargout,
253 octave_idx_type n_to_find, int direction) 253 octave_idx_type n_to_find, int direction)
254 { 254 {
255 // There are far fewer special cases to handle for a PermMatrix. 255 // There are far fewer special cases to handle for a PermMatrix.
256 octave_value_list retval ((nargout == 0 ? 1 : nargout), Matrix ()); 256 octave_value_list retval ((nargout == 0 ? 1 : nargout), Matrix ());
257 257
258 octave_idx_type nc = v.cols(); 258 octave_idx_type nc = v.cols();
435 if (nargin > 1) 435 if (nargin > 1)
436 { 436 {
437 double val = args(1).scalar_value (); 437 double val = args(1).scalar_value ();
438 438
439 if (error_state || (! xisinf (val) && (val < 0 || val != xround (val)))) 439 if (error_state || (! xisinf (val) && (val < 0 || val != xround (val))))
440 { 440 {
441 error ("find: expecting second argument to be a nonnegative integer"); 441 error ("find: expecting second argument to be a nonnegative integer");
442 return retval; 442 return retval;
443 } 443 }
444 else 444 else
445 n_to_find = val; 445 n_to_find = val;
446 } 446 }
447 447
448 // Direction to do the searching (1 == forward, -1 == reverse). 448 // Direction to do the searching (1 == forward, -1 == reverse).
452 direction = 0; 452 direction = 0;
453 453
454 std::string s_arg = args(2).string_value (); 454 std::string s_arg = args(2).string_value ();
455 455
456 if (! error_state) 456 if (! error_state)
457 { 457 {
458 if (s_arg == "first") 458 if (s_arg == "first")
459 direction = 1; 459 direction = 1;
460 else if (s_arg == "last") 460 else if (s_arg == "last")
461 direction = -1; 461 direction = -1;
462 } 462 }
463 463
464 if (direction == 0) 464 if (direction == 0)
465 { 465 {
466 error ("find: expecting third argument to be \"first\" or \"last\""); 466 error ("find: expecting third argument to be \"first\" or \"last\"");
467 return retval; 467 return retval;
468 } 468 }
469 } 469 }
470 470
471 octave_value arg = args(0); 471 octave_value arg = args(0);
472 472
473 if (arg.is_bool_type ()) 473 if (arg.is_bool_type ())
474 { 474 {
475 if (arg.is_sparse_type ()) 475 if (arg.is_sparse_type ())
476 { 476 {
477 SparseBoolMatrix v = arg.sparse_bool_matrix_value (); 477 SparseBoolMatrix v = arg.sparse_bool_matrix_value ();
478 478
479 if (! error_state) 479 if (! error_state)
480 retval = find_nonzero_elem_idx (v, nargout, 480 retval = find_nonzero_elem_idx (v, nargout,
481 n_to_find, direction); 481 n_to_find, direction);
482 } 482 }
483 else if (nargout <= 1 && n_to_find == -1 && direction == 1) 483 else if (nargout <= 1 && n_to_find == -1 && direction == 1)
484 { 484 {
485 // This case is equivalent to extracting indices from a logical 485 // This case is equivalent to extracting indices from a logical
486 // matrix. Try to reuse the possibly cached index vector. 486 // matrix. Try to reuse the possibly cached index vector.
488 } 488 }
489 else 489 else
490 { 490 {
491 boolNDArray v = arg.bool_array_value (); 491 boolNDArray v = arg.bool_array_value ();
492 492
493 if (! error_state) 493 if (! error_state)
494 retval = find_nonzero_elem_idx (v, nargout, 494 retval = find_nonzero_elem_idx (v, nargout,
495 n_to_find, direction); 495 n_to_find, direction);
496 } 496 }
497 } 497 }
498 else if (arg.is_integer_type ()) 498 else if (arg.is_integer_type ())
499 { 499 {
500 #define DO_INT_BRANCH(INTT) \ 500 #define DO_INT_BRANCH(INTT) \
501 else if (arg.is_ ## INTT ## _type ()) \ 501 else if (arg.is_ ## INTT ## _type ()) \
502 { \ 502 { \
503 INTT ## NDArray v = arg.INTT ## _array_value (); \ 503 INTT ## NDArray v = arg.INTT ## _array_value (); \
504 \ 504 \
505 if (! error_state) \ 505 if (! error_state) \
506 retval = find_nonzero_elem_idx (v, nargout, \ 506 retval = find_nonzero_elem_idx (v, nargout, \
507 n_to_find, direction);\ 507 n_to_find, direction);\
508 } 508 }
509 509
510 if (false) 510 if (false)
511 ; 511 ;
512 DO_INT_BRANCH (int8) 512 DO_INT_BRANCH (int8)
521 panic_impossible (); 521 panic_impossible ();
522 } 522 }
523 else if (arg.is_sparse_type ()) 523 else if (arg.is_sparse_type ())
524 { 524 {
525 if (arg.is_real_type ()) 525 if (arg.is_real_type ())
526 { 526 {
527 SparseMatrix v = arg.sparse_matrix_value (); 527 SparseMatrix v = arg.sparse_matrix_value ();
528 528
529 if (! error_state) 529 if (! error_state)
530 retval = find_nonzero_elem_idx (v, nargout, 530 retval = find_nonzero_elem_idx (v, nargout,
531 n_to_find, direction); 531 n_to_find, direction);
532 } 532 }
533 else if (arg.is_complex_type ()) 533 else if (arg.is_complex_type ())
534 { 534 {
535 SparseComplexMatrix v = arg.sparse_complex_matrix_value (); 535 SparseComplexMatrix v = arg.sparse_complex_matrix_value ();
536 536
537 if (! error_state) 537 if (! error_state)
538 retval = find_nonzero_elem_idx (v, nargout, 538 retval = find_nonzero_elem_idx (v, nargout,
539 n_to_find, direction); 539 n_to_find, direction);
540 } 540 }
541 else 541 else
542 gripe_wrong_type_arg ("find", arg); 542 gripe_wrong_type_arg ("find", arg);
543 } 543 }
544 else if (arg.is_perm_matrix ()) { 544 else if (arg.is_perm_matrix ()) {
545 PermMatrix P = arg.perm_matrix_value (); 545 PermMatrix P = arg.perm_matrix_value ();
546 546
547 if (! error_state) 547 if (! error_state)
548 retval = find_nonzero_elem_idx (P, nargout, n_to_find, direction); 548 retval = find_nonzero_elem_idx (P, nargout, n_to_find, direction);
549 } 549 }
550 else 550 else
551 { 551 {
552 if (arg.is_single_type ()) 552 if (arg.is_single_type ())
553 { 553 {
554 if (arg.is_real_type ()) 554 if (arg.is_real_type ())
555 { 555 {
556 FloatNDArray nda = arg.float_array_value (); 556 FloatNDArray nda = arg.float_array_value ();
557 557
558 if (! error_state) 558 if (! error_state)
559 retval = find_nonzero_elem_idx (nda, nargout, 559 retval = find_nonzero_elem_idx (nda, nargout,
560 n_to_find, direction); 560 n_to_find, direction);
561 } 561 }
562 else if (arg.is_complex_type ()) 562 else if (arg.is_complex_type ())
563 { 563 {
564 FloatComplexNDArray cnda = arg.float_complex_array_value (); 564 FloatComplexNDArray cnda = arg.float_complex_array_value ();
565 565
566 if (! error_state) 566 if (! error_state)
567 retval = find_nonzero_elem_idx (cnda, nargout, 567 retval = find_nonzero_elem_idx (cnda, nargout,
568 n_to_find, direction); 568 n_to_find, direction);
569 } 569 }
570 } 570 }
571 else 571 else
572 { 572 {
573 if (arg.is_real_type ()) 573 if (arg.is_real_type ())
574 { 574 {
575 NDArray nda = arg.array_value (); 575 NDArray nda = arg.array_value ();
576 576
577 if (! error_state) 577 if (! error_state)
578 retval = find_nonzero_elem_idx (nda, nargout, 578 retval = find_nonzero_elem_idx (nda, nargout,
579 n_to_find, direction); 579 n_to_find, direction);
580 } 580 }
581 else if (arg.is_complex_type ()) 581 else if (arg.is_complex_type ())
582 { 582 {
583 ComplexNDArray cnda = arg.complex_array_value (); 583 ComplexNDArray cnda = arg.complex_array_value ();
584 584
585 if (! error_state) 585 if (! error_state)
586 retval = find_nonzero_elem_idx (cnda, nargout, 586 retval = find_nonzero_elem_idx (cnda, nargout,
587 n_to_find, direction); 587 n_to_find, direction);
588 } 588 }
589 else if (arg.is_string ()) 589 else if (arg.is_string ())
590 { 590 {
591 charNDArray cnda = arg.char_array_value (); 591 charNDArray cnda = arg.char_array_value ();
592 592
593 if (! error_state) 593 if (! error_state)
594 retval = find_nonzero_elem_idx (cnda, nargout, 594 retval = find_nonzero_elem_idx (cnda, nargout,
595 n_to_find, direction); 595 n_to_find, direction);
596 } 596 }
597 else 597 else
598 { 598 {
599 gripe_wrong_type_arg ("find", arg); 599 gripe_wrong_type_arg ("find", arg);
600 } 600 }
601 } 601 }
602 } 602 }
603 603
604 return retval; 604 return retval;
605 } 605 }
606 606