Skip to content

Commit 23a3863

Browse files
committed
Add some more methods
1 parent 700c6d2 commit 23a3863

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

src/TupleTools.jl

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,83 @@ ishift(t::Tuple{Vararg{Int}}, i::Int, s::Int) = map(n->(n < i ? n : n+s), t)
100100
101101
102102
Insert the elements of tuple t2 at location `i` in `t`, i.e. the output tuple will
103-
look as (t[1:i-1]..., t2..., t[i+1:end]). Note that element `t[i]` is deleted. See
104-
`splice` if you would also like to return `t[i]`
103+
look as (t[1:i-1]..., t2..., t[i+1:end]). Note that element `t[i]` is deleted. Use
104+
`setindex` for setting a single value at position `i`.
105105
"""
106106
@inline insertat(t::Tuple, i::Int, t2::Tuple) = 1 <= i <= length(t) ? _insertat(t, i, t2) : throw(BoundsError(t, i))
107107
@inline _insertat(t::Tuple, i::Int, t2::Tuple) = i == 1 ? (t2..., tail(t)...) : (t[1], _insertat(tail(t), i-1, t2)...)
108108
@inline _insertat(t::Tuple{}, i::Int, t2::Tuple) = throw(BoundsError(t, i))
109109

110+
"""
111+
minimum(t::Tuple)
112+
113+
Returns the smallest element of a tuple
114+
"""
115+
@inline minimum(t::Tuple{Any}) = t[1]
116+
@inline minimum(t::Tuple) = min(t[1], minimum(tail(t)))
117+
118+
"""
119+
maximum(t::Tuple)
120+
121+
Returns the largest element of a tuple
122+
"""
123+
@inline maximum(t::Tuple{Any}) = t[1]
124+
@inline maximum(t::Tuple) = max(t[1], maximum(tail(t)))
125+
126+
127+
"""
128+
indmin(t::Tuple)
129+
130+
Returns the index of the minimum element in a tuple. If there are multiple
131+
minimal elements, then the first one will be returned.
132+
"""
133+
indmin(t::Tuple) = findmin(t)[2]
134+
135+
"""
136+
findmin(t::Tuple)
137+
138+
Returns the value and index of the minimum element in a tuple. If there are multiple
139+
minimal elements, then the first one will be returned.
140+
"""
141+
findmin(t::Tuple{Any}) = (t[1], 1)
142+
findmin(t::Tuple) = _findmin(tail(t),2,t[1],1)
143+
@inline _findmin(t::Tuple{}, s, v, i) = (v, i)
144+
@inline function _findmin(t::Tuple, s, v, i)
145+
if t[1] < v
146+
_findmin(tail(t), s+1, t[1], s)
147+
else
148+
_findmin(tail(t), s+1, v, i)
149+
end
150+
end
151+
152+
"""
153+
findmax(t::Tuple)
154+
155+
Returns the value and index of the maximum element in a tuple. If there are multiple
156+
maximal elements, then the first one will be returned.
157+
"""
158+
indmax(t::Tuple) = findmax(t)[2]
159+
160+
findmax(::Tuple{Any}) = 1
161+
findmax(t::Tuple) = _findmax(tail(t),2,t[1],1)
162+
@inline _findmax(t::Tuple{}, s, v, i) = (v, i)
163+
@inline function _findmax(t::Tuple, s, v, i)
164+
if t[1] > v
165+
_findmax(tail(t), s+1, t[1], s)
166+
else
167+
_findmax(tail(t), s+1, v, i)
168+
end
169+
end
170+
171+
172+
110173
"""
111174
sort(t::Tuple; lt=isless, by=identity, rev::Bool=false) -> ::Tuple
112175
113176
Sorts the tuple `t`.
114177
"""
115-
@inline function sort(t::Tuple; lt=isless, by=identity, rev::Bool=false)
178+
sort(t::Tuple; lt=isless, by=identity, rev::Bool=false) = _sort(t, lt, by, rev)
179+
@inline function _sort(t::Tuple, lt=isless, by=identity, rev::Bool=false)
116180
i = 1
117181
if rev
118182
for k = 2:length(t)
@@ -127,9 +191,9 @@ Sorts the tuple `t`.
127191
end
128192
end
129193
end
130-
return (t[i], sort(_deleteat(t, i); lt = lt, by = by, rev = rev)...)
194+
return (t[i], _sort(_deleteat(t, i), lt, by, rev)...)
131195
end
132-
@inline sort(t::Tuple{Any}; lt=isless, by=identity, rev::Bool=false) = t
196+
@inline _sort(t::Tuple{Any}, lt=isless, by=identity, rev::Bool=false) = t
133197

134198
"""
135199
sortperm(t::Tuple; lt=isless, by=identity, rev::Bool=false) -> ::Tuple

test/runtests.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,17 @@ i = rand(1:n)
2323
@test @inferred(TupleTools.unsafe_front(())) == ()
2424

2525
@test @inferred(TupleTools.getindices(t, (1,2,3))) == t[1:3]
26-
@test @inferred(TupleTools.vcat((1,2,3),4,(5,),(),(6,7,8))) == (1,2,3,4,5,6,7,8)
2726

2827
@test @inferred(TupleTools.deleteat(t, i)) == (deleteat!(copy(p), i)...)
2928
@test @inferred(TupleTools.insertat(t, i, (1,2,3))) == (vcat(p[1:i-1], [1,2,3], p[i+1:n])...)
29+
@test @inferred(TupleTools.vcat((1,2,3),4,(5,),(),(6,7,8))) == (1,2,3,4,5,6,7,8)
30+
31+
@test @inferred(TupleTools.findmin(t)) == findmin(t)
32+
@test @inferred(TupleTools.findmax(t)) == findmax(t)
33+
@test @inferred(TupleTools.minimum(t)) == minimum(t)
34+
@test @inferred(TupleTools.maximum(t)) == maximum(t)
35+
@test @inferred(TupleTools.indmin(t)) == indmin(t)
36+
@test @inferred(TupleTools.indmax(t)) == indmin(t)
3037

3138
@test @inferred(TupleTools.sort(t; rev = true)) == (sort(p; rev = true)...)
3239
@test @inferred(TupleTools.sortperm(t)) == (sortperm(p)...)

0 commit comments

Comments
 (0)