30
30
end
31
31
32
32
if VERSION >= v " 0.7-" # to fix type instability in (f)indmin / (f)indmax
33
- Base. Pair (p:: Tuple{Any,Any} ) = p[1 ] => p[2 ]
34
- Base. pairs (collection) = Base. Generator (=> , zip (keys (collection), values (collection)))
33
+ Base. pairs (collection) = (k=> v for (k,v) in zip (keys (collection), values (collection)))
35
34
end
36
35
37
36
@inline argtail2 (a, b, c... ) = c
@@ -82,12 +81,20 @@ not expanded.
82
81
Delete the element at location `i` in `t`; if a list `I` of indices is specified
83
82
(again as a tuple), the elements of these different positions are deleted.
84
83
"""
85
- @inline deleteat (t:: Tuple , I:: Tuple{Int} ) = deleteat (t, I[1 ])
86
- @inline deleteat (t:: Tuple , I:: Tuple{Int, Int, Vararg{Int}} ) = deleteat (deleteat (t, I[1 ]), map (i-> (i< I[1 ] ? i : i- 1 ), tail (I)))
87
- @inline deleteat (t:: Tuple , i:: Int ) = 1 <= i <= length (t) ? _deleteat (t, i) : throw (BoundsError (t, i))
84
+ deleteat (t:: Tuple , I:: Tuple{Int} ) = deleteat (t, I[1 ])
85
+ function deleteat (t:: Tuple , I:: Tuple{Int, Int, Vararg{Int}} )
86
+ any (i-> (1 <= i <= length (t)), I) && throw (BoundsError (t, I))
87
+ _deleteat (_deleteat (t, I[1 ]), ishift (tail (I), I[1 ], - 1 ))
88
+ end
89
+ deleteat (t:: Tuple , i:: Int ) = 1 <= i <= length (t) ? _deleteat (t, i) : throw (BoundsError (t, i))
88
90
@inline _deleteat (t:: Tuple , i:: Int ) = i == 1 ? tail (t) : (t[1 ], _deleteat (tail (t), i- 1 )... )
89
91
@inline _deleteat (t:: Tuple{} , i:: Int ) = throw (BoundsError (t, i))
90
92
93
+ @inline _deleteat (t:: Tuple , I:: Tuple{Int} ) = _deleteat (t, I[1 ])
94
+ @inline _deleteat (t:: Tuple , I:: Tuple{Int,Int,Vararg{Int}} ) = _deleteat (_deleteat (t, I[1 ]), tail (I)) # assumes sorted from big to small
95
+
96
+ ishift (t:: Tuple{Vararg{Int}} , i:: Int , s:: Int ) = map (n-> (n < i ? n : n+ s), t)
97
+
91
98
"""
92
99
insertat(t::Tuple, i::Int, t2::Tuple) -> ::Tuple
93
100
@@ -100,7 +107,6 @@ look as (t[1:i-1]..., t2..., t[i+1:end]). Note that element `t[i]` is deleted. S
100
107
@inline _insertat (t:: Tuple , i:: Int , t2:: Tuple ) = i == 1 ? (t2... , tail (t)... ) : (t[1 ], _insertat (tail (t), i- 1 , t2)... )
101
108
@inline _insertat (t:: Tuple{} , i:: Int , t2:: Tuple ) = throw (BoundsError (t, i))
102
109
103
-
104
110
"""
105
111
sort(t::Tuple; lt=isless, by=identity, rev::Bool=false) -> ::Tuple
106
112
@@ -121,7 +127,7 @@ Sorts the tuple `t`.
121
127
end
122
128
end
123
129
end
124
- return (t[i], sort (deleteat (t, i); lt = lt, by = by, rev = rev)... )
130
+ return (t[i], sort (_deleteat (t, i); lt = lt, by = by, rev = rev)... )
125
131
end
126
132
@inline sort (t:: Tuple{Any} ; lt= isless, by= identity, rev:: Bool = false ) = t
127
133
131
137
132
138
Computes a tuple that contains the permutation required to sort `t`.
133
139
"""
134
- @inline function sortperm (t:: Tuple ; lt= isless, by= identity, rev:: Bool = false )
140
+ sortperm (t:: Tuple ; lt= isless, by= identity, rev:: Bool = false ) = _sortperm (t, lt, by, rev)
141
+ @inline function _sortperm (t:: Tuple , lt= isless, by= identity, rev:: Bool = false )
135
142
i:: Int = 1
136
143
if rev
137
144
for k = 2 : length (t)
@@ -146,10 +153,10 @@ Computes a tuple that contains the permutation required to sort `t`.
146
153
end
147
154
end
148
155
end
149
- r = sortperm ( deleteat (t, i); lt = lt , by = by, rev = rev)
150
- return (i, map (n -> (n < i ? n : n + 1 ), r )... )
156
+ r = _sortperm ( _deleteat (t, i), lt, by, rev)
157
+ return (i, ishift (r, i, + 1 )... )
151
158
end
152
- @inline sortperm (t:: Tuple{Any} ; lt= isless, by= identity, rev:: Bool = false ) = (1 ,)
159
+ @inline _sortperm (t:: Tuple{Any} , lt= isless, by= identity, rev:: Bool = false ) = (1 ,)
153
160
154
161
"""
155
162
getindices(t::Tuple, I::Tuple{Vararg{Int}}) -> ::Tuple
@@ -164,8 +171,8 @@ Get the indices `t[i] for i in I`, again as tuple.
164
171
165
172
Permute the elements of tuple `t` according to the permutation in `p`.
166
173
"""
167
- permute (t:: NTuple{N} , p:: NTuple{N,Int} ) where {N} = isperm (p) ? getindices (t, p) : throw (ArgumentError (" not a valid permutation: $p " ))
168
- permute (t:: NTuple{N} , p) where {N} = isperm (p) && length (p) == N ? ntuple (n-> t[p[n]], StaticLength (N)) : throw (ArgumentError (" not a valid permutation: $p " ))
174
+ @inline permute (t:: NTuple{N} , p:: NTuple{N,Int} ) where {N} = isperm (p) ? getindices (t, p) : throw (ArgumentError (" not a valid permutation: $p " ))
175
+ @inline permute (t:: NTuple{N} , p) where {N} = isperm (p) && length (p) == N ? ntuple (n-> t[p[n]], StaticLength (N)) : throw (ArgumentError (" not a valid permutation: $p " ))
169
176
170
177
"""
171
178
invperm(p::NTuple{N,Int}) -> ::NTuple{N,Int}
0 commit comments