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语言里的数 ...
随机推荐
- OSGi karaf-maven-plugin
karaf-maven-plugin 1. 配制 karaf 启动时加载 bundle 项目中需要在 karaf 中集成 cxf-dosgi-discovery-distributed 特性,所以需要 ...
- asio 广播代码示例
代码网络收集 修改了一个编译的小问题 客户端 // Client.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include < ...
- android 网站上下的 adt 不能显示没有安装的
问题描述 使用SDK Manager更新时出现问题Failed to fetch URL https://dl-ssl.google.com/android/repository/repository ...
- Java中创建数组的几种方法
Java中创建数组的几种方法 public static void main(String[] args) { //创建数组的第一种方法 int[] arr=new int[6]; int intVa ...
- 2018.09.25 poj3070 Fibonacci(矩阵快速幂)
传送门 矩阵快速幂板题,写一道来练练手. 这一次在poj做题总算没忘了改万能库. 代码: #include<iostream> #include<cstdio> #define ...
- PHP二个高精确度数字相加减
1.相加 string bcadd(string left operand, string right operand, int [scale]); 2.相减 string bcsub(string ...
- Netty学习第一节Netty的总体概况
一.Netty简介 什么是Netty? 1.高性能事件驱动,异步非阻塞的IO加载开源框架. 它是由JBoss提供,用于建立TCP等底层链接.基于Netty可以建立高性能的HTTP服务器,快速开发高性能 ...
- HDU 3247 Resource Archiver (AC自动机+BFS+状压DP)
题意:给定 n 个文本串,m个病毒串,文本串重叠部分可以合并,但合并后不能含有病毒串,问所有文本串合并后最短多长. 析:先把所有的文本串和病毒都插入到AC自动机上,不过标记不一样,可以给病毒标记-1, ...
- java获取变量的类型
对于简单类型变量,是无法直接获得变量类型的://要想获取,必须自定义函数进行返回 对于包装类型变量,是可以直接获得的,变量名称.getClass().getName(); java中获取变量的类型可以 ...
- WriteableBitmap(二) 实例
使用前面定义的WriteableBitmap,我们可以很容易地创建一个足够容纳整个100 x 100图像的数组: byte[] pixels = new byte[wbmap.PixelHeight* ...