03 2019 档案

摘要:参数类型是Constant Pointer也就是 UnsafePointer 可以传入的类型: UnsafePointer/UnsafeMutablePointer/AutoreleasingUnsafeMutablePointerString。如果Type 是 UInt8 或 Int8。可变类型的 Type 的 in-out 类型。[Type] 类型,被当作指向第一个元素的地址例子如下: f... 阅读全文

posted @ 2019-03-31 09:01 花老🐯 阅读(164) 评论(0) 推荐(0)

摘要:指向类的指针class PointerTestClass { var intNum = 3 var another = 56 var another1 = 59}下面是验证代码let pointer: UnsafeMutablePointer = UnsafeMutablePointer.allocate(capacity: 3)let testInstance = Pointe... 阅读全文

posted @ 2019-03-31 09:00 花老🐯 阅读(200) 评论(0) 推荐(0)

摘要:基础知识指针的内存状态typed?initiated?❌❌✅❌✅✅之前分配的内存可能被释放,使得指针指向了未被分配的内存。有两种方式可以使得指针指向的内存处于Uninitialized状态:刚刚被分配内存内存被deinitializedvar bytes: [UInt8] = [39, 77, 111, 111, 102, 33, 39, 0]let uint8Pointer = UnsafeM... 阅读全文

posted @ 2019-03-31 09:00 花老🐯 阅读(190) 评论(0) 推荐(0)

摘要:Dictionary uses two storage schemes: native storage and Cocoa storage. 只看 native storage 的,也就是和 OC 无关的。类图 内存分布分配内存的地方: static internal func allocate( scale: Int8, age: Int32?, seed: Int... 阅读全文

posted @ 2019-03-24 13:57 花老🐯 阅读(277) 评论(0) 推荐(0)

摘要:Adding Elements internal func _unsafeInsertNew(_ element: __owned Element) { _internalInvariant(count + 1 Bool { if _fastPath(capacity Element { _internalInvariant(hashTable.isOccupied(buc... 阅读全文

posted @ 2019-03-24 13:12 花老🐯 阅读(288) 评论(0) 推荐(0)

摘要:即contains操作 /// - Parameter member: An element to look for in the set. /// - Returns: `true` if `member` exists in the set; otherwise, `false`. /// /// - Complexity: O(1) @inlinable public fun... 阅读全文

posted @ 2019-03-24 12:44 花老🐯 阅读(233) 评论(0) 推荐(0)

摘要:isEmpty /// A Boolean value that indicates whether the set is empty. @inlinable public var isEmpty: Bool { return count == 0 }count /// The number of elements in the set. /// /// - Complexit... 阅读全文

posted @ 2019-03-24 12:19 花老🐯 阅读(234) 评论(0) 推荐(0)

摘要:类图Set 是一个结构体,持有另一个结构体_Variant。最终所有的元素存储在一个叫做__RawSetStorage的类里。 内存布局结构体分配在栈上,和__RawSetStorage相关的变量分配在堆里。__RawSetStorage 只有一些基本的属性,比如count、capacity等。在初始化__RawSetStorage时,会在类的尾部继续分配内存,真正的存储Set里的对象。 ... 阅读全文

posted @ 2019-03-24 12:18 花老🐯 阅读(311) 评论(0) 推荐(0)

摘要:_UnsafeBitset是一个固定大小的 bitmap,用来确定指定位置是否有元素存在。HashTable具体的 hash 碰撞算法在HashTable里实现,目前使用的是简单的开放地址法,使用算法是Linear probing。 HashTable 的属性其实只有若干个 UInt,每一位用来表示状态,指定位置有没有被占用。 @usableFromInline internal v... 阅读全文

posted @ 2019-03-24 11:06 花老🐯 阅读(345) 评论(0) 推荐(0)

摘要:根据下标取值关键代码如下: func _getElement( _ index: Int, wasNativeTypeChecked: Bool, matchingSubscriptCheck: _DependenceToken ) -> Element {#if _runtime(_ObjC) return _buffer.getElement(index, wasN... 阅读全文

posted @ 2019-03-22 00:50 花老🐯 阅读(247) 评论(0) 推荐(0)

摘要:只看 Swift Array 到 NSArrayArray 里的源代码extension Array { @inlinable public // @SPI(Foundation) func _bridgeToObjectiveCImpl() -> AnyObject { return _buffer._asCocoaArray() }}这里看到,调用了_asCocoaArray ... 阅读全文

posted @ 2019-03-22 00:48 花老🐯 阅读(674) 评论(0) 推荐(0)

摘要:判断是否为空使用的是Collection协议里isEmpty的判断。 public var isEmpty: Bool { return startIndex == endIndex }startIndex 总是返回 0。 public var startIndex: Int { return 0 }endIndex代码如下: @inlinable public va... 阅读全文

posted @ 2019-03-20 22:57 花老🐯 阅读(140) 评论(0) 推荐(0)

摘要:init() 函数在 Array 里 public init() { _buffer = _Buffer() }以Buffer 是 _ContiguousArrayBuffer 为例。即初始化了一个_ContiguousArrayBuffer。在_ContiguousArrayBuffer里 /// Create an empty buffer. @inlinable intern... 阅读全文

posted @ 2019-03-20 22:33 花老🐯 阅读(511) 评论(0) 推荐(0)

摘要:public struct Array: _DestructorSafeContainer { #if _runtime(_ObjC) @usableFromInline internal typealias _Buffer = _ArrayBuffer #else @usableFromInline internal typealias _Buffer = _ContiguousAr... 阅读全文

posted @ 2019-03-20 22:02 花老🐯 阅读(645) 评论(0) 推荐(0)

摘要:to OC func _bridgeToObjectiveCImpl() -> AnyObject { if _guts.isSmall { return _guts.asSmall.withUTF8 { bufPtr in // TODO(String bridging): worth isASCII check for different encoding? ... 阅读全文

posted @ 2019-03-20 20:09 花老🐯 阅读(562) 评论(0) 推荐(0)

摘要:以append操作为例 public mutating func append(_ other: String) { if self.isEmpty && !_guts.hasNativeStorage { self = other return } self._guts.append(other._guts) }_StringGuts 做了实际的工作... 阅读全文

posted @ 2019-03-20 08:56 花老🐯 阅读(245) 评论(0) 推荐(0)

摘要:即以 UTF16 编码的格式来查看字符串。 UTF16View 是一个结构体 @_fixed_layout public struct UTF16View { @usableFromInline internal var _guts: _StringGuts @inlinable internal init(_ guts: _StringGuts) { se... 阅读全文

posted @ 2019-03-20 08:29 花老🐯 阅读(204) 评论(0) 推荐(0)

摘要:即以 Unicode Scarlar 的方式来查看字符串。 /// let flag = "🇵🇷" /// for v in flag.unicodeScalars { /// print(v.value) /// } /// // 127477 /// // 127479UnicodeScalarView 是一个结构体e... 阅读全文

posted @ 2019-03-20 08:25 花老🐯 阅读(374) 评论(0) 推荐(0)

摘要:在 String 里,用来索引 Character 的,不是整数,而是StringIndex 内部结构extension String { /// A position of a character or code unit in a string. @_fixed_layout public struct Index { @usableFromInline internal ... 阅读全文

posted @ 2019-03-20 08:02 花老🐯 阅读(683) 评论(0) 推荐(0)

摘要:Native strings have tail-allocated storage, which begins at an offset of nativeBias from the storage object's address. String literals, which reside in the constant section, are encoded as their start... 阅读全文

posted @ 2019-03-20 07:57 花老🐯 阅读(209) 评论(0) 推荐(0)

摘要:Shared strings do not have tail-allocated storage, but can provide access upon query to contiguous UTF-8 code units. Lazily-bridged NSStrings capable of providing access to contiguous ASCII/UTF-8 set ... 阅读全文

posted @ 2019-03-20 07:57 花老🐯 阅读(310) 评论(0) 推荐(0)

摘要:最终都要走到__StringStorage 的 create(realCodeUnitCapacity,countAndFlags) 方法里去。 默认实现是 UTF8 internal static func _fromCodeUnits( _ input: Input, encoding: Encoding.Type, repair: Bool ) -> (String... 阅读全文

posted @ 2019-03-20 07:56 花老🐯 阅读(202) 评论(0) 推荐(0)

摘要:small string, 只有两个 UInt64 的字,这里面存储了所有的信息。内存布局如下:第二个 UInt64存储了标记位和长度信息,以及部分字符串的值 // Get an integer equivalent to the _StringObject.discriminatedObjectRawBits // computed property. @inlinable @inli... 阅读全文

posted @ 2019-03-20 07:56 花老🐯 阅读(311) 评论(0) 推荐(0)

摘要:对于普通的字符串,对应的_StringObject 有两个存储属性:_countAndFlagsBits: UInt64_object: Builtin.BridgeObject_countAndFlagsBits存储者字符串的长度和一些标记位。 ┌─────────┬───────┬──────────────────┬─────────────────┬────────┬───────┐│ ... 阅读全文

posted @ 2019-03-20 07:56 花老🐯 阅读(334) 评论(0) 推荐(0)

摘要:感受一下字符串相关的源文件个数String 概览是一个结构体只有一个变量,类型是 _StringGuts如上所示,String 真正的内容在__StringStorage或者__SharedStringStorage里面。 private static func create( realCodeUnitCapacity: Int, countAndFlags: CountAndFl... 阅读全文

posted @ 2019-03-20 07:55 花老🐯 阅读(536) 评论(0) 推荐(0)

摘要:  SequenceA type that provides sequential, iterated access to its elements.是最基础的协议,可以通过迭代来获取它的元素。有两个关联类型: /// A type representing the sequence's elements. associatedtype Element /// A type that... 阅读全文

posted @ 2019-03-04 08:25 花老🐯 阅读(392) 评论(0) 推荐(0)

导航