后缀(from :)和dropFirst(_ :)之间有什么区别吗? [英] Is there any difference at all between suffix(from:) and dropFirst(_:)?
问题描述
我只是想到在Swift中处理子序列时,
It just occurred to me that when working with subsequences in Swift,
func suffix(from: Int)
似乎与dropFirst(_:)
相同(很明显,对于长度为"10"的数组,您只需将输入值从"3"更改为"7".)
func suffix(from: Int)
seems to be identical to just dropFirst(_:)
(Obviously, you just change the input value from say "3" to "7" in the case of an array of length "10".)
只需重复一遍.所以:当然,对于长度为10的数组.例如,我的意思是带有"2"的func suffix(from: Int)
和带有"8"的dropFirst(_:)
.
Just to repeat that. So: of course, for an array of say length ten. What I mean is func suffix(from: Int)
with "2" would be the same as dropFirst(_:)
with "8", for example.
类似地upTo
/through
似乎与dropLast(_:)
除了方便以外,有什么区别吗?
(也许是在错误条件,性能还是?)
(Perhaps in error conditions, performance, or?)
我想知道,实际上,在Swift的另一个内部是否只是通过调用另一个实现的?
I was wondering whether, in fact, inside Swift one or the other is just implemented by calling the other?
推荐答案
它们是完全不同的.
- 由
Collection
协议定义. - 从给定的开始返回
Subsequence
href ="https://developer.apple.com/reference/swift/indexablebase/index" rel ="noreferrer">Index
. - 记录的O(1)时间复杂度(您可以
- Defined by the
Collection
protocol. - Returns a
Subsequence
from a given startingIndex
. - Documented time complexity of O(1) (you can see its default implementation here).
- Runtime error if the index you pass is out of range.
- 由
Sequence
协议定义. - 返回
SubSequence
,其中给定的最大个元素从序列的开头删除. - 已记录的时间复杂度为O(n)*.尽管其默认实现实际上具有一个O(1)的时间复杂度,这只是将O(n)遍历所放置的元素直到迭代.
- 如果您输入的数字大于序列的长度,则返回一个空的子序列.
- Defined by the
Sequence
protocol. - Returns a
SubSequence
with a given maximum number of elements removed from the head of the sequence. - Has a documented time complexity of O(n)*. Although its default implementation actually has a time complexity of O(1), this just postpones the O(n) walk through the dropped elements until iteration.
- Returns an empty subsequence if the number you input is greater than the sequence's length.
*与所有协议要求记录的时间复杂度一样,一致类型的实现可能具有较低的时间复杂度.例如, RandomAccessCollection
的dropFirst(_:)
方法将在O( 1)时间.
*As with all protocol requirement documented time complexities, it's possible for the conforming type to have an implementation with a lower time complexity. For example, a RandomAccessCollection
's dropFirst(_:)
method will run in O(1) time.
但是,当涉及到Array
时,这些方法只是 happen 表现相同(除了处理超出范围的输入).
However, when it comes to Array
, these methods just happen to behave identically (except for the handling of out of range inputs).
这是因为Array
具有类型Int
的Index
,该类型从0
开始并依次计数到array.count - 1
,因此,意味着删除了第一个n
元素的子序列为从索引n
开始的相同子序列.
This is because Array
has an Index
of type Int
that starts from 0
and sequentially counts up to array.count - 1
, therefore meaning that a subsequence with the first n
elements dropped is the same subsequence that starts from the index n
.
此外,由于Array
是RandomAccessCollection
,因此这两种方法都将在O(1)时间运行.
Also because Array
is a RandomAccessCollection
, both methods will run in O(1) time.
这篇关于后缀(from :)和dropFirst(_ :)之间有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!