@@ -100,19 +100,83 @@ ishift(t::Tuple{Vararg{Int}}, i::Int, s::Int) = map(n->(n < i ? n : n+s), t)
100
100
101
101
102
102
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`.
105
105
"""
106
106
@inline insertat (t:: Tuple , i:: Int , t2:: Tuple ) = 1 <= i <= length (t) ? _insertat (t, i, t2) : throw (BoundsError (t, i))
107
107
@inline _insertat (t:: Tuple , i:: Int , t2:: Tuple ) = i == 1 ? (t2... , tail (t)... ) : (t[1 ], _insertat (tail (t), i- 1 , t2)... )
108
108
@inline _insertat (t:: Tuple{} , i:: Int , t2:: Tuple ) = throw (BoundsError (t, i))
109
109
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
+
110
173
"""
111
174
sort(t::Tuple; lt=isless, by=identity, rev::Bool=false) -> ::Tuple
112
175
113
176
Sorts the tuple `t`.
114
177
"""
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 )
116
180
i = 1
117
181
if rev
118
182
for k = 2 : length (t)
@@ -127,9 +191,9 @@ Sorts the tuple `t`.
127
191
end
128
192
end
129
193
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)... )
131
195
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
133
197
134
198
"""
135
199
sortperm(t::Tuple; lt=isless, by=identity, rev::Bool=false) -> ::Tuple
0 commit comments