Skip to content

Commit

Permalink
Change findfirst/findlast/findnext/findprev to return the same index …
Browse files Browse the repository at this point in the history
…type as keys() (#25577)

* Change findfirst/findlast/findnext/findprev to return the same index type as keys()

This is consistent with find(). We need to check whether the iterator is
empty first, since calling first(keys(A)) may throw an error. Also
add needed last(::EachStringIndex) and prevind(::AbstractArray, ::CartesianIndex) methods.

* Use more generic approach for findfirst and findlast

Also add tests, and remove @inferred calls which did not actually check
whether functions were inferred, but only whether the equality test was.
Change the _pairs() fallback to use Iterators.countfrom() so that an error
is raised by findlast() rather than assuming the last index is typemax(Int),
and use length() for iterators which support it so that findlast() works.
  • Loading branch information
nalimilan authored and JeffBezanson committed Jan 18, 2018
1 parent f82f82f commit 87cf094
Show file tree
Hide file tree
Showing 12 changed files with 327 additions and 90 deletions.
12 changes: 6 additions & 6 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,12 +376,12 @@ This section lists changes that do not have deprecation warnings.
* `findn(x::AbstractArray)` has been deprecated in favor of `findall(!iszero, x)`, which
now returns cartesian indices for multidimensional arrays (see below, [#25532]).

* `find` has been renamed to `findall`, and now returns the same type of indices
as `keys`/`pairs` for `AbstractArray`, `AbstractDict`, `AbstractString`, `Tuple`
and `NamedTuple` objects ([#24774], [#25545]).
In particular, this means that it returns `CartesianIndex` objects for matrices
and higher-dimensional arrays instead of linear indices as was previously the case.
Use `LinearIndices(a)[findall(f, a)]` to compute linear indices.
* `find` has been renamed to `findall`. `findall`, `findfirst`, `findlast`, `findnext`
now take and/or return the same type of indices as `keys`/`pairs` for `AbstractArray`,
`AbstractDict`, `AbstractString`, `Tuple` and `NamedTuple` objects ([#24774], [#25545]).
In particular, this means that they use `CartesianIndex` objects for matrices
and higher-dimensional arrays insted of linear indices as was previously the case.
Use `LinearIndices(a)[findall(f, a)]` and similar constructs to compute linear indices.

* `AbstractSet` objects are now considered equal by `==` and `isequal` if all of their
elements are equal ([#25368]). This has required changing the hashing algorithm
Expand Down
Loading

0 comments on commit 87cf094

Please sign in to comment.