Swift 里集合类型协议的关系


Sequence
A type that provides sequential, iterated access to its elements.
是最基础的协议,可以通过迭代来获取它的元素。
有两个关联类型:
/// A type representing the sequence's elements.
associatedtype Element
/// A type that provides the sequence's iteration interface and
/// encapsulates its iteration state.
associatedtype Iterator : IteratorProtocol where Iterator.Element == Element
遵守Sequence
协议,只需要实现makeIterator()
方法即可。
在迭代过程中,Sequence
的序列是否被破坏,并没有要求。因此,两次迭代同一个Sequence
,可能会得到不同的结果。
Collection
A sequence whose elements can be traversed multiple times, nondestructively, and accessed by an indexed subscript.
在sequence
基础上,增加了几个性质
- 多次遍历,不会对序列的结构造成影响
- 可以通过下标索引
引入了几个和索引相关的关联类型
associatedtype Index : Comparable
/// A type that represents the indices that are valid for subscripting the
/// collection, in ascending order.
associatedtype Indices : Collection = DefaultIndices<Self>
where Indices.Element == Index,
Indices.Index == Index,
Indices.SubSequence == Indices
通过下标获取值的方法如下,复杂度是O(1)
subscript(position: Index) -> Element { get }
可以通过下标来遍历,那么必须定义下标的successor
func index(after i: Self.Index) -> Self.Index
BidirectionalCollection
A collection that supports backward as well as forward traversal.
继承了Collection
协议,增加了可以反向遍历的功能。
通过一个下标,可以找到上一个下标。
func index(before i: Index) -> Index
这也为和last
相关的方法提供了基础
RandomAccessCollection
A collection that supports efficient random-access index traversal.
继承了BidirectionalCollection
,因此可以正向/反向遍历。还对遍历的效率做出来要求,
Random-access collections can move indices any distance and measure the distance between indices in O(1) time
MutableCollection
A collection that supports subscript assignment.
继承了collection
协议,提供了可以改变元素值的能力。
override subscript(position: Index) -> Element { get set }
这个下标操作,提供了set
方法。
对应的,提供了默认的分片方法(O(n))
/// - Complexity: O(*n*), where *n* is the length of the collection.
mutating func partition(
by belongsInSecondPartition: (Element) throws -> Bool
) rethrows -> Index
也提供了交换方法
/// - Complexity: O(1)
mutating func swapAt(_ i: Index, _ j: Index)
RangeReplaceableCollection
A collection that supports replacement of an arbitrary subrange of elements with the elements of another collection.
继承了collection
,增加了可以某一个子区间内的元素,可以被另一个collection
替代的能力。
为collection
增加了插入和删除的能力。
mutating func append(_ newElement: __owned Element)
mutating func append<S : Sequence>(contentsOf newElements: __owned S)
where S.Element == Element
mutating func insert(_ newElement: __owned Element, at i: Index)
mutating func insert<S : Collection>(contentsOf newElements: __owned S, at i: Index)
where S.Element == Element
mutating func remove(at i: Index) -> Element
mutating func removeSubrange(_ bounds: Range<Index>)
Swift 里集合类型协议的关系的更多相关文章
- Java中的集合类型的继承关系图
Java中的集合类型的继承关系图
- [Swift]遍历集合类型(数组、集合和字典)
Swift提供了三种主要的集合类型,称为数组,集合和字典,用于存储值集合. 数组是有序的值集合. 集是唯一值的无序集合. 字典是键值关联的无序集合. Swift中无法再使用传统形式的for循环. // ...
- Swift 进阶 第 4 课 集合类型协议
• Read 序列 一个序列 (sequence) 代表的是一系列具有相同类型 的值,你可以对这些值进行迭代.遍历一个序列最简单的方式是使用 for 循环: 123 for element in so ...
- Swift中集合类型indexOf(Element)提示错误的解决办法
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 初学Swift,会遇到一些潜在的小问题,比如我们在某个集合对象 ...
- Swift中的集合类型
一.引子: 在2014年10月TIOBE编程语言排行榜中,Swift位居第18位,从2014WWDC发布会首次公布至今不到半年时间,swift一直受到编程人 员的追捧,其热衷程度并不亚于当红巨星Tay ...
- swift 集合类型(二)
说到swift的集合类型,就不得不谈到Dictionary.包含一个键值对组合的集合. var air = ["name":"warner","tit ...
- Swift 集合类型
Swift语言提供数组和字典的集合类型 Swift 语言里的数组和字典中存储的数据值类型必须明确 ,即数组中只能存放同类型的数据. 1: 数组 数组的创建 var shoppingList: St ...
- Swift的基础,操作符,字符串和集合类型
这篇文章主要讲解苹果Swift官方指南的第二章前四节的要点内容,如果想看完整的英文文档可以去苹果开发者页面下载. Basic 声明常量let 声明变量var 注释依旧使用"//" ...
- The Swift Programming Language-官方教程精译Swift(5)集合类型 -- Collection Types
Swift语言提供经典的数组和字典两种集合类型来存储集合数据.数组用来按顺序存储相同类型的数据.字典虽然无序存储相同类型数据值但是需要由独有的标识符引用和寻址(就是键值对). Swift语言里的数 ...
随机推荐
- 电商类Web原型制作分享-IKEA
IKEA是一个家居整合大型零售商,属于电商类官网.电商以展示商品.售后服务.购物流程为主.根据网站的图文方式排版,主导航栏使用的标签组,区域导航栏使用的是垂直选项卡,实现下拉弹出面板交互的功能. 本原 ...
- Microsoft DirectX SDK 2010 版本下载
Microsoft DirectX SDK 2010 版本下载 Version:Date Published:9.29.19626/7/2010File name:File size:DXSDK_Ju ...
- activemq.bat 在window7 x64下启动(安装)报错解决方案
在启动 apache-activemq-5.15.2/activemq.bat 时候报错,提示以下信息: wrapper | --> Wrapper Started as Consolewr ...
- JSP 介绍
Servlet进行逻辑处理效率高,但是页面响应效率低,不太方便. 问题: 在学习了Servlet之后,使用Servlet进行页面的展现,代码书写过于麻烦. 极大的影响了开发的效率,那么有没有一种方式可 ...
- 2018.10.19 NOIP训练 桌子(快速幂优化dp)
传送门 勉强算一道dp好题. 显然第kkk列和第k+nk+nk+n列放的棋子数是相同的. 因此只需要统计出前nnn列的选法数. 对于前mmm%nnn列,一共有(m−1)/n+1(m-1)/n+1(m− ...
- 第二章:冠词(Les articles)
★定冠词(Les articles définis ): 阳性单数:le(l') 阴性单数:la(l') 阴阳性复数:les ()表示前面已经提到的人或事物: ()有关的名词已被其它的成分(补语,关系 ...
- (转)EF Power tool用法
转自:http://msdn.microsoft.com/zh-cn/data/jj593170.aspx 命令摘要 安装 EF Power Tools 后,将提供以下上下文菜单项.本演练将详细讨论这 ...
- QGIS 2014年7月18日版本
4. Building on Windows 4.1. Building with Microsoft Visual Studio This section describes how to buil ...
- readLine() 和 "\r","\n" 问题
很多输入流中都有一个函数readLine(),我们也经常使用这个函数,但有时如果不认真考虑,这个函数也会带来一些小麻烦. 如果我们是从控制台读入的话,我们也许没有想过readLine函数到底是根据&q ...
- hdu 5009 离散化
http://acm.hdu.edu.cn/showproblem.php?pid=5009 有一段序列,涂连续一段子序列的代价为该子序列出现不同数字个数的平方,求最小代价涂完整个序列. ai有10^ ...