forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSIndexPath.swift
110 lines (93 loc) · 3.78 KB
/
NSIndexPath.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
open class NSIndexPath : NSObject, NSCopying, NSSecureCoding {
internal var _indexes : [Int]
override public init() {
_indexes = []
}
public init(indexes: UnsafePointer<Int>!, length: Int) {
_indexes = Array(UnsafeBufferPointer(start: indexes, count: length))
}
private init(indexes: [Int]) {
_indexes = indexes
}
open override func copy() -> Any {
return copy(with: nil)
}
open func copy(with zone: NSZone? = nil) -> Any { NSUnimplemented() }
public convenience init(index: Int) {
self.init(indexes: [index])
}
open func encode(with aCoder: NSCoder) {
NSUnimplemented()
}
public required init?(coder aDecoder: NSCoder) {
NSUnimplemented()
}
public static var supportsSecureCoding: Bool { return true }
open func adding(_ index: Int) -> IndexPath {
return IndexPath(indexes: _indexes + [index])
}
open func removingLastIndex() -> IndexPath {
if _indexes.count <= 1 {
return IndexPath(indexes: [])
} else {
return IndexPath(indexes: [Int](_indexes[0..<_indexes.count - 1]))
}
}
open func index(atPosition position: Int) -> Int {
return _indexes[position]
}
open var length: Int {
return _indexes.count
}
/*!
@abstract Copies the indexes stored in this index path from the positions specified by positionRange into indexes.
@param indexes Buffer of at least as many NSUIntegers as specified by the length of positionRange. On return, this memory will hold the index path's indexes.
@param positionRange A range of valid positions within this index path. If the location plus the length of positionRange is greater than the length of this index path, this method raises an NSRangeException.
@discussion
It is the developer’s responsibility to allocate the memory for the C array.
*/
open func getIndexes(_ indexes: UnsafeMutablePointer<Int>, range positionRange: NSRange) {
for (pos, idx) in _indexes[positionRange.location ..< NSMaxRange(positionRange)].enumerated() {
indexes.advanced(by: pos).pointee = idx
}
}
// comparison support
// sorting an array of indexPaths using this comparison results in an array representing nodes in depth-first traversal order
open func compare(_ otherObject: IndexPath) -> ComparisonResult {
let thisLength = length
let otherLength = otherObject.count
let minLength = thisLength >= otherLength ? otherLength : thisLength
for pos in 0..<minLength {
let otherValue = otherObject[pos]
let thisValue = index(atPosition: pos)
if thisValue < otherValue {
return .orderedAscending
} else if thisValue > otherValue {
return .orderedDescending
}
}
if thisLength > otherLength {
return .orderedDescending
} else if thisLength < otherLength {
return .orderedAscending
}
return .orderedSame
}
}
extension NSIndexPath {
open func getIndexes(_ indexes: UnsafeMutablePointer<Int>) { NSUnimplemented() }
}
extension NSIndexPath : _StructTypeBridgeable {
public typealias _StructType = IndexPath
public func _bridgeToSwift() -> IndexPath {
return IndexPath._unconditionallyBridgeFromObjectiveC(self)
}
}