Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parity: NSArray.sortedArray(…) #2271

Merged
merged 2 commits into from May 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion CoreFoundation/Base.subproj/CFSortFunctions.c
Expand Up @@ -271,8 +271,14 @@ static void __CFSortIndexesN(VALUE_TYPE listp[], INDEX_TYPE count, int32_t ncore
}
#endif

#if DEPLOYMENT_RUNTIME_SWIFT
#define _CF_SORT_INDEXES_EXPORT CF_CROSS_PLATFORM_EXPORT
#else
#define _CF_SORT_INDEXES_EXPORT
#endif

// fills an array of indexes (of length count) giving the indexes 0 - count-1, as sorted by the comparator block
void CFSortIndexes(CFIndex *indexBuffer, CFIndex count, CFOptionFlags opts, CFComparisonResult (^cmp)(CFIndex, CFIndex)) {
_CF_SORT_INDEXES_EXPORT void CFSortIndexes(CFIndex *indexBuffer, CFIndex count, CFOptionFlags opts, CFComparisonResult (^cmp)(CFIndex, CFIndex)) {
if (count < 1) return;
if (INTPTR_MAX / sizeof(CFIndex) < count) return;
int32_t ncores = 0;
Expand Down
2 changes: 2 additions & 0 deletions CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h
Expand Up @@ -393,6 +393,8 @@ CF_EXPORT _CFThreadRef _CFMainPThread;

CF_EXPORT CFHashCode __CFHashDouble(double d);

CF_CROSS_PLATFORM_EXPORT void CFSortIndexes(CFIndex *indexBuffer, CFIndex count, CFOptionFlags opts, CFComparisonResult (^cmp)(CFIndex, CFIndex));

CF_EXPORT CFTypeRef _Nullable _CFThreadSpecificGet(_CFThreadSpecificKey key);
CF_EXPORT void _CFThreadSpecificSet(_CFThreadSpecificKey key, CFTypeRef _Nullable value);
CF_EXPORT _CFThreadSpecificKey _CFThreadSpecificKeyCreate(void);
Expand Down
35 changes: 25 additions & 10 deletions Foundation/NSArray.swift
Expand Up @@ -525,20 +525,35 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
}

internal func sortedArray(from range: NSRange, options: NSSortOptions, usingComparator cmptr: (Any, Any) -> ComparisonResult) -> [Any] {
// The sort options are not available. We use the Array's sorting algorithm. It is not stable neither concurrent.
guard options.isEmpty else {
NSUnimplemented()
}

let count = self.count
if range.length == 0 || count == 0 {
return []
}

let swiftRange = Range(range)!
return allObjects[swiftRange].sorted { lhs, rhs in
return cmptr(lhs, rhs) == .orderedAscending
let objects = subarray(with: range)

let indexes = UnsafeMutableBufferPointer<CFIndex>.allocate(capacity: range.length)
withoutActuallyEscaping(cmptr) { (cmptr) in
CFSortIndexes(indexes.baseAddress!, range.length, CFOptionFlags(options.rawValue)) { (a, b) -> CFComparisonResult in
switch cmptr(objects[a], objects[b]) {
case .orderedAscending: return kCFCompareLessThan
case .orderedDescending: return kCFCompareGreaterThan
case .orderedSame: return kCFCompareEqualTo
}
}
}

let result = Array<Any>(unsafeUninitializedCapacity: range.length) { (buffer, initializedCount) in
var destinationIndex = 0
for index in indexes {
buffer.baseAddress?.advanced(by: destinationIndex).initialize(to: objects[index])
destinationIndex += 1
}
initializedCount = range.length
}

indexes.deallocate()
return result
}

open func sortedArray(comparator cmptr: (Any, Any) -> ComparisonResult) -> [Any] {
Expand Down Expand Up @@ -828,7 +843,7 @@ open class NSMutableArray : NSArray {
if type(of: self) === NSMutableArray.self {
_storage.swapAt(idx1, idx2)
} else {
NSUnimplemented()
NSRequiresConcreteImplementation()
}
}

Expand Down Expand Up @@ -906,7 +921,7 @@ open class NSMutableArray : NSArray {
_storage.insert(__SwiftValue.store(otherArray[idx]), at: idx + range.location)
}
} else {
NSUnimplemented()
NSRequiresConcreteImplementation()
}
}

Expand Down