@@ -1497,21 +1497,35 @@ cat(n::Integer, x::Integer...) = reshape([x...], (ntuple(x->1, n-1)..., length(x
1497
1497
1498
1498
Find the next linear index >= `i` of a `true` element of `A`, or `nothing` if not found.
1499
1499
1500
+ Indices are of the same type as those returned by [`keys(A)`](@ref)
1501
+ and [`pairs(A)`](@ref).
1502
+
1500
1503
# Examples
1501
1504
```jldoctest
1505
+ julia> A = [false, false, true, false]
1506
+ 4-element Array{Bool,1}:
1507
+ false
1508
+ false
1509
+ true
1510
+ false
1511
+
1512
+ julia> findnext(A, 1)
1513
+ 3
1514
+
1515
+ julia> findnext(A, 4) == nothing
1516
+ true
1517
+
1502
1518
julia> A = [false false; true false]
1503
1519
2×2 Array{Bool,2}:
1504
1520
false false
1505
1521
true false
1506
1522
1507
- julia> findnext(A, 1)
1508
- 2
1509
-
1510
- julia> findnext(A, 3)
1523
+ julia> findnext(A, CartesianIndex(1, 1))
1524
+ CartesianIndex(2, 1)
1511
1525
```
1512
1526
"""
1513
- function findnext (A, start:: Integer )
1514
- l = endof (A )
1527
+ function findnext (A, start)
1528
+ l = last ( keys (A) )
1515
1529
i = start
1516
1530
warned = false
1517
1531
while i <= l
@@ -1531,48 +1545,74 @@ end
1531
1545
"""
1532
1546
findfirst(A)
1533
1547
1534
- Return the linear index of the first `true` value in `A`.
1548
+ Return the index of the first `true` value in `A`.
1535
1549
Return `nothing` if no such value is found.
1536
1550
To search for other kinds of values, pass a predicate as the first argument.
1537
1551
1552
+ Indices are of the same type as those returned by [`keys(A)`](@ref)
1553
+ and [`pairs(A)`](@ref).
1554
+
1538
1555
# Examples
1539
1556
```jldoctest
1557
+ julia> A = [false, false, true, false]
1558
+ 4-element Array{Bool,1}:
1559
+ false
1560
+ false
1561
+ true
1562
+ false
1563
+
1564
+ julia> findfirst(A)
1565
+ 3
1566
+
1567
+ julia> findfirst(falses(3)) == nothing
1568
+ true
1569
+
1540
1570
julia> A = [false false; true false]
1541
1571
2×2 Array{Bool,2}:
1542
1572
false false
1543
1573
true false
1544
1574
1545
1575
julia> findfirst(A)
1546
- 2
1547
-
1548
- julia> findfirst(falses(3)) == nothing
1549
- true
1576
+ CartesianIndex(2, 1)
1550
1577
```
1551
1578
"""
1552
- findfirst (A) = findnext (A, 1 )
1579
+ findfirst (A) = isempty (A) ? nothing : findnext (A, first ( keys (A)) )
1553
1580
1554
1581
"""
1555
- findnext(predicate::Function, A, i::Integer )
1582
+ findnext(predicate::Function, A, i)
1556
1583
1557
- Find the next linear index >= `i` of an element of `A` for which `predicate` returns `true`,
1584
+ Find the next index >= `i` of an element of `A` for which `predicate` returns `true`,
1558
1585
or `nothing` if not found.
1559
1586
1587
+ Indices are of the same type as those returned by [`keys(A)`](@ref)
1588
+ and [`pairs(A)`](@ref).
1589
+
1560
1590
# Examples
1561
1591
```jldoctest
1562
- julia> A = [1 4; 2 2]
1563
- 2×2 Array{Int64,2}:
1564
- 1 4
1565
- 2 2
1592
+ A = [1, 4, 2, 2]
1593
+ 4-element Array{Int64,1}:
1594
+ 1
1595
+ 4
1596
+ 2
1597
+ 2
1566
1598
1567
1599
julia> findnext(isodd, A, 1)
1568
1600
1
1569
1601
1570
1602
julia> findnext(isodd, A, 2) == nothing
1571
1603
true
1604
+
1605
+ julia> A = [1 4; 2 2]
1606
+ 2×2 Array{Int64,2}:
1607
+ 1 4
1608
+ 2 2
1609
+
1610
+ julia> findnext(isodd, A, CartesianIndex(1, 1))
1611
+ CartesianIndex(1, 1)
1572
1612
```
1573
1613
"""
1574
- function findnext (testf:: Function , A, start:: Integer )
1575
- l = endof (A )
1614
+ function findnext (testf:: Function , A, start)
1615
+ l = last ( keys (A) )
1576
1616
i = start
1577
1617
while i <= l
1578
1618
if testf (A[i])
@@ -1586,15 +1626,20 @@ end
1586
1626
"""
1587
1627
findfirst(predicate::Function, A)
1588
1628
1589
- Return the linear index of the first element of `A` for which `predicate` returns `true`.
1629
+ Return the index of the first element of `A` for which `predicate` returns `true`.
1590
1630
Return `nothing` if there is no such element.
1591
1631
1632
+ Indices are of the same type as those returned by [`keys(A)`](@ref)
1633
+ and [`pairs(A)`](@ref).
1634
+
1592
1635
# Examples
1593
1636
```jldoctest
1594
- julia> A = [1 4; 2 2]
1595
- 2×2 Array{Int64,2}:
1596
- 1 4
1597
- 2 2
1637
+ julia> A = [1, 4, 2, 2]
1638
+ 4-element Array{Int64,1}:
1639
+ 1
1640
+ 4
1641
+ 2
1642
+ 2
1598
1643
1599
1644
julia> findfirst(iseven, A)
1600
1645
2
@@ -1603,34 +1648,55 @@ julia> findfirst(x -> x>10, A) == nothing
1603
1648
true
1604
1649
1605
1650
julia> findfirst(equalto(4), A)
1606
- 3
1651
+ 2
1652
+
1653
+ julia> A = [1 4; 2 2]
1654
+ 2×2 Array{Int64,2}:
1655
+ 1 4
1656
+ 2 2
1657
+
1658
+ julia> findfirst(iseven, A)
1659
+ CartesianIndex(2, 1)
1607
1660
```
1608
1661
"""
1609
- findfirst (testf:: Function , A) = findnext (testf, A, 1 )
1662
+ findfirst (testf:: Function , A) = isempty (A) ? nothing : findnext (testf, A, first ( keys (A)) )
1610
1663
1611
1664
"""
1612
- findprev(A, i::Integer)
1665
+ findprev(A, i)
1666
+
1667
+ Find the previous index <= `i` of a `true` element of `A`, or `nothing` if not found.
1613
1668
1614
- Find the previous linear index <= `i` of a `true` element of `A`, or `nothing` if not found.
1669
+ Indices are of the same type as those returned by [`keys(A)`](@ref)
1670
+ and [`pairs(A)`](@ref).
1615
1671
1616
1672
# Examples
1617
1673
```jldoctest
1674
+ julia> A = [false, false, true, true]
1675
+ 4-element Array{Bool,1}:
1676
+ false
1677
+ false
1678
+ true
1679
+ true
1680
+
1681
+ julia> findprev(A, 3)
1682
+ 3
1683
+
1684
+ julia> findprev(A, 1) == nothing
1685
+ true
1686
+
1618
1687
julia> A = [false false; true true]
1619
1688
2×2 Array{Bool,2}:
1620
1689
false false
1621
1690
true true
1622
1691
1623
- julia> findprev(A,2)
1624
- 2
1625
-
1626
- julia> findprev(A,1) == nothing
1627
- true
1692
+ julia> findprev(A, CartesianIndex(2, 1))
1693
+ CartesianIndex(2, 1)
1628
1694
```
1629
1695
"""
1630
- function findprev (A, start:: Integer )
1696
+ function findprev (A, start)
1631
1697
i = start
1632
1698
warned = false
1633
- while i >= 1
1699
+ while i >= first ( keys (A))
1634
1700
a = A[i]
1635
1701
if ! warned && ! (a isa Bool)
1636
1702
depwarn (" In the future `findprev` will only work on boolean collections. Use `findprev(x->x!=0, A, start)` instead." , :findprev )
@@ -1645,50 +1711,76 @@ end
1645
1711
"""
1646
1712
findlast(A)
1647
1713
1648
- Return the linear index of the last `true` value in `A`.
1714
+ Return the index of the last `true` value in `A`.
1649
1715
Return `nothing` if there is no `true` value in `A`.
1650
1716
1717
+ Indices are of the same type as those returned by [`keys(A)`](@ref)
1718
+ and [`pairs(A)`](@ref).
1719
+
1651
1720
# Examples
1652
1721
```jldoctest
1653
- julia> A = [true false; true false]
1654
- 2×2 Array{Bool,2}:
1655
- true false
1656
- true false
1722
+ julia> A = [true, false, true, false]
1723
+ 4-element Array{Bool,1}:
1724
+ true
1725
+ false
1726
+ true
1727
+ false
1657
1728
1658
1729
julia> findlast(A)
1659
- 2
1730
+ 3
1660
1731
1661
1732
julia> A = falses(2,2);
1662
1733
1663
1734
julia> findlast(A) == nothing
1664
1735
true
1736
+
1737
+ julia> A = [true false; true false]
1738
+ 2×2 Array{Bool,2}:
1739
+ true false
1740
+ true false
1741
+
1742
+ julia> findlast(A)
1743
+ CartesianIndex(2, 1)
1665
1744
```
1666
1745
"""
1667
- findlast (A) = findprev (A, endof (A ))
1746
+ findlast (A) = isempty (A) ? nothing : findprev (A, last ( keys (A) ))
1668
1747
1669
1748
"""
1670
- findprev(predicate::Function, A, i::Integer )
1749
+ findprev(predicate::Function, A, i)
1671
1750
1672
- Find the previous linear index <= `i` of an element of `A` for which `predicate` returns `true`, or
1751
+ Find the previous index <= `i` of an element of `A` for which `predicate` returns `true`, or
1673
1752
`nothing` if not found.
1674
1753
1754
+ Indices are of the same type as those returned by [`keys(A)`](@ref)
1755
+ and [`pairs(A)`](@ref).
1756
+
1675
1757
# Examples
1676
1758
```jldoctest
1677
- julia> A = [4 6; 1 2]
1678
- 2×2 Array{Int64,2}:
1679
- 4 6
1680
- 1 2
1759
+ julia> A = [4, 6, 1, 2]
1760
+ 4-element Array{Int64,1}:
1761
+ 4
1762
+ 6
1763
+ 1
1764
+ 2
1681
1765
1682
1766
julia> findprev(isodd, A, 1) == nothing
1683
1767
true
1684
1768
1685
1769
julia> findprev(isodd, A, 3)
1686
- 2
1770
+ 3
1771
+
1772
+ julia> A = [4 6; 1 2]
1773
+ 2×2 Array{Int64,2}:
1774
+ 4 6
1775
+ 1 2
1776
+
1777
+ julia> findprev(isodd, A, CartesianIndex(1, 2))
1778
+ CartesianIndex(2, 1)
1687
1779
```
1688
1780
"""
1689
- function findprev (testf:: Function , A, start:: Integer )
1781
+ function findprev (testf:: Function , A, start)
1690
1782
i = start
1691
- while i >= 1
1783
+ while i >= first ( keys (A))
1692
1784
testf (A[i]) && return i
1693
1785
i = prevind (A, i)
1694
1786
end
@@ -1698,35 +1790,46 @@ end
1698
1790
"""
1699
1791
findlast(predicate::Function, A)
1700
1792
1701
- Return the linear index of the last element of `A` for which `predicate` returns `true`.
1793
+ Return the index of the last element of `A` for which `predicate` returns `true`.
1702
1794
Return `nothing` if there is no such element.
1703
1795
1796
+ Indices are of the same type as those returned by [`keys(A)`](@ref)
1797
+ and [`pairs(A)`](@ref).
1798
+
1704
1799
# Examples
1705
1800
```jldoctest
1801
+ julia> A = [1, 2, 3, 4]
1802
+ 4-element Array{Int64,1}:
1803
+ 1
1804
+ 2
1805
+ 3
1806
+ 4
1807
+
1808
+ julia> findlast(isodd, A)
1809
+ 3
1810
+
1811
+ julia> findlast(x -> x > 5, A) == nothing
1812
+ true
1813
+
1706
1814
julia> A = [1 2; 3 4]
1707
1815
2×2 Array{Int64,2}:
1708
1816
1 2
1709
1817
3 4
1710
1818
1711
1819
julia> findlast(isodd, A)
1712
- 2
1713
-
1714
- julia> findlast(x -> x > 5, A) == nothing
1715
- true
1820
+ CartesianIndex(2, 1)
1716
1821
```
1717
1822
"""
1718
- findlast (testf:: Function , A) = findprev (testf, A, endof (A ))
1823
+ findlast (testf:: Function , A) = isempty (A) ? nothing : findprev (testf, A, last ( keys (A) ))
1719
1824
1720
1825
"""
1721
1826
find(f::Function, A)
1722
1827
1723
1828
Return a vector `I` of the indices or keys of `A` where `f(A[I])` returns `true`.
1724
1829
If there are no such elements of `A`, return an empty array.
1725
1830
1726
- Indices or keys are of the same type as those returned by [`keys(A)`](@ref)
1727
- and [`pairs(A)`](@ref) for `AbstractArray`, `AbstractDict`, `AbstractString`
1728
- `Tuple` and `NamedTuple` objects, and are linear indices starting at `1`
1729
- for other iterables.
1831
+ Indices are of the same type as those returned by [`keys(A)`](@ref)
1832
+ and [`pairs(A)`](@ref).
1730
1833
1731
1834
# Examples
1732
1835
```jldoctest
0 commit comments