-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathNSIndexPath.swift
95 lines (84 loc) · 3.48 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
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 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
//
public class NSIndexPath : NSObject, NSCopying, NSSecureCoding {
internal var _indexes : [Int]
override public init() {
_indexes = []
}
public init(indexes: UnsafePointer<Int>, length: Int) {
_indexes = []
for var idx = 0; idx < length; idx++ {
_indexes.append(indexes[idx])
}
}
private init(indexes: [Int]) {
_indexes = indexes
}
public func copyWithZone(zone: NSZone) -> AnyObject { NSUnimplemented() }
public convenience init(index: Int) {
self.init(indexes: [index])
}
public func encodeWithCoder(aCoder: NSCoder) {
NSUnimplemented()
}
public required init?(coder aDecoder: NSCoder) {
NSUnimplemented()
}
public static func supportsSecureCoding() -> Bool { return true }
public func indexPathByAddingIndex(index: Int) -> NSIndexPath {
return NSIndexPath(indexes: _indexes + [index])
}
public func indexPathByRemovingLastIndex() -> NSIndexPath {
if _indexes.count <= 1 {
return NSIndexPath(indexes: [])
} else {
return NSIndexPath(indexes: [Int](_indexes[0..<_indexes.count - 1]))
}
}
public func indexAtPosition(position: Int) -> Int {
return _indexes[position]
}
public 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.
*/
public func getIndexes(indexes: UnsafeMutablePointer<Int>, range positionRange: NSRange) {
for (pos, idx) in _indexes[positionRange.location ..< NSMaxRange(positionRange)].enumerate() {
indexes.advancedBy(pos).memory = idx
}
}
// comparison support
// sorting an array of indexPaths using this comparison results in an array representing nodes in depth-first traversal order
public func compare(otherObject: NSIndexPath) -> NSComparisonResult {
let thisLength = length
let otherLength = otherObject.length
let minLength = thisLength >= otherLength ? otherLength : thisLength
for pos in 0..<minLength {
let otherValue = otherObject.indexAtPosition(pos)
let thisValue = indexAtPosition(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
}
}