@@ -1596,14 +1596,14 @@ cat(n::Integer, x::Integer...) = reshape([x...], (ntuple(x->1, n-1)..., length(x
1596
1596
"""
1597
1597
findnext(A, i::Integer)
1598
1598
1599
- Find the next linear index >= `i` of a non-zero element of `A`, or `0` if not found.
1599
+ Find the next linear index >= `i` of a true element of `A`, or `0` if not found.
1600
1600
1601
1601
# Examples
1602
1602
```jldoctest
1603
- julia> A = [0 0; 1 0 ]
1604
- 2×2 Array{Int64 ,2}:
1605
- 0 0
1606
- 1 0
1603
+ julia> A = [false false; true false ]
1604
+ 2×2 Array{Bool ,2}:
1605
+ false false
1606
+ true false
1607
1607
1608
1608
julia> findnext(A,1)
1609
1609
2
@@ -1615,8 +1615,14 @@ julia> findnext(A,3)
1615
1615
function findnext (A, start:: Integer )
1616
1616
l = endof (A)
1617
1617
i = start
1618
+ warned = false
1618
1619
while i <= l
1619
- if A[i] != 0
1620
+ a = A[i]
1621
+ if ! warned && ! (a isa Bool)
1622
+ depwarn (" In the future `findnext` will only work on boolean collections. Use `findnext(x->x!=0, A)` instead." , :findnext )
1623
+ warned = true
1624
+ end
1625
+ if a != 0
1620
1626
return i
1621
1627
end
1622
1628
i = nextind (A, i)
@@ -1646,58 +1652,6 @@ julia> findfirst(zeros(3))
1646
1652
"""
1647
1653
findfirst (A) = findnext (A, 1 )
1648
1654
1649
- """
1650
- findnext(A, v, i::Integer)
1651
-
1652
- Find the next linear index >= `i` of an element of `A` equal to `v` (using `==`), or `0` if not found.
1653
-
1654
- # Examples
1655
- ```jldoctest
1656
- julia> A = [1 4; 2 2]
1657
- 2×2 Array{Int64,2}:
1658
- 1 4
1659
- 2 2
1660
-
1661
- julia> findnext(A,4,4)
1662
- 0
1663
-
1664
- julia> findnext(A,4,3)
1665
- 3
1666
- ```
1667
- """
1668
- function findnext (A, v, start:: Integer )
1669
- l = endof (A)
1670
- i = start
1671
- while i <= l
1672
- if A[i] == v
1673
- return i
1674
- end
1675
- i = nextind (A, i)
1676
- end
1677
- return 0
1678
- end
1679
- """
1680
- findfirst(A, v)
1681
-
1682
- Return the linear index of the first element equal to `v` in `A`.
1683
- Returns `0` if `v` is not found.
1684
-
1685
- # Examples
1686
- ```jldoctest
1687
- julia> A = [4 6; 2 2]
1688
- 2×2 Array{Int64,2}:
1689
- 4 6
1690
- 2 2
1691
-
1692
- julia> findfirst(A,2)
1693
- 2
1694
-
1695
- julia> findfirst(A,3)
1696
- 0
1697
- ```
1698
- """
1699
- findfirst (A, v) = findnext (A, v, 1 )
1700
-
1701
1655
"""
1702
1656
findnext(predicate::Function, A, i::Integer)
1703
1657
@@ -1754,14 +1708,14 @@ findfirst(testf::Function, A) = findnext(testf, A, 1)
1754
1708
"""
1755
1709
findprev(A, i::Integer)
1756
1710
1757
- Find the previous linear index <= `i` of a non-zero element of `A`, or `0` if not found.
1711
+ Find the previous linear index <= `i` of a true element of `A`, or `0` if not found.
1758
1712
1759
1713
# Examples
1760
1714
```jldoctest
1761
- julia> A = [0 0; 1 2 ]
1762
- 2×2 Array{Int64 ,2}:
1763
- 0 0
1764
- 1 2
1715
+ julia> A = [false false; true true ]
1716
+ 2×2 Array{Bool ,2}:
1717
+ false false
1718
+ true true
1765
1719
1766
1720
julia> findprev(A,2)
1767
1721
2
@@ -1772,8 +1726,14 @@ julia> findprev(A,1)
1772
1726
"""
1773
1727
function findprev (A, start:: Integer )
1774
1728
i = start
1729
+ warned = false
1775
1730
while i >= 1
1776
- A[i] != 0 && return i
1731
+ a = A[i]
1732
+ if ! warned && ! (a isa Bool)
1733
+ depwarn (" In the future `findprev` will only work on boolean collections. Use `findprev(x->x!=0, A)` instead." , :findprev )
1734
+ warned = true
1735
+ end
1736
+ a != 0 && return i
1777
1737
i = prevind (A, i)
1778
1738
end
1779
1739
return 0
@@ -1806,59 +1766,6 @@ julia> findlast(A)
1806
1766
"""
1807
1767
findlast (A) = findprev (A, endof (A))
1808
1768
1809
- """
1810
- findprev(A, v, i::Integer)
1811
-
1812
- Find the previous linear index <= `i` of an element of `A` equal to `v` (using `==`), or `0` if not found.
1813
-
1814
- # Examples
1815
- ```jldoctest
1816
- julia> A = [0 0; 1 2]
1817
- 2×2 Array{Int64,2}:
1818
- 0 0
1819
- 1 2
1820
-
1821
- julia> findprev(A, 1, 4)
1822
- 2
1823
-
1824
- julia> findprev(A, 1, 1)
1825
- 0
1826
- ```
1827
- """
1828
- function findprev (A, v, start:: Integer )
1829
- i = start
1830
- while i >= 1
1831
- A[i] == v && return i
1832
- i = prevind (A, i)
1833
- end
1834
- return 0
1835
- end
1836
-
1837
- """
1838
- findlast(A, v)
1839
-
1840
- Return the linear index of the last element equal to `v` in `A`.
1841
- Returns `0` if there is no element of `A` equal to `v`.
1842
-
1843
- # Examples
1844
- ```jldoctest
1845
- julia> A = [1 2; 2 1]
1846
- 2×2 Array{Int64,2}:
1847
- 1 2
1848
- 2 1
1849
-
1850
- julia> findlast(A,1)
1851
- 4
1852
-
1853
- julia> findlast(A,2)
1854
- 3
1855
-
1856
- julia> findlast(A,3)
1857
- 0
1858
- ```
1859
- """
1860
- findlast (A, v) = findprev (A, v, endof (A))
1861
-
1862
1769
"""
1863
1770
findprev(predicate::Function, A, i::Integer)
1864
1771
@@ -1952,9 +1859,7 @@ _index_remapper(iter) = OneTo(typemax(Int)) # safe for objects that don't imple
1952
1859
"""
1953
1860
find(A)
1954
1861
1955
- Return a vector of the linear indexes of the non-zeros in `A` (determined by `A[i]!=0`). A
1956
- common use of this is to convert a boolean array to an array of indexes of the `true`
1957
- elements. If there are no non-zero elements of `A`, `find` returns an empty array.
1862
+ Return a vector of the linear indices of the true values in `A`.
1958
1863
1959
1864
# Examples
1960
1865
```jldoctest
@@ -1968,7 +1873,7 @@ julia> find(A)
1968
1873
1
1969
1874
4
1970
1875
1971
- julia> find(zeros (3))
1876
+ julia> find(falses (3))
1972
1877
0-element Array{Int64,1}
1973
1878
```
1974
1879
"""
@@ -1977,7 +1882,12 @@ function find(A)
1977
1882
I = Vector {Int} (nnzA)
1978
1883
cnt = 1
1979
1884
inds = _index_remapper (A)
1885
+ warned = false
1980
1886
for (i,a) in enumerate (A)
1887
+ if ! warned && ! (a isa Bool)
1888
+ depwarn (" In the future `find(A)` will only work on boolean collections. Use `find(x->x!=0, A)` instead." , :find )
1889
+ warned = true
1890
+ end
1981
1891
if a != 0
1982
1892
I[cnt] = inds[i]
1983
1893
cnt += 1
@@ -1986,7 +1896,7 @@ function find(A)
1986
1896
return I
1987
1897
end
1988
1898
1989
- find (x:: Number ) = x == 0 ? Array {Int,1} (0 ) : [ 1 ]
1899
+ find (x:: Bool ) = x ? [ 1 ] : Array {Int,1} (0 )
1990
1900
find (testf:: Function , x:: Number ) = ! testf (x) ? Array {Int,1} (0 ) : [1 ]
1991
1901
1992
1902
findn (A:: AbstractVector ) = find (A)
0 commit comments