Skip to content

Sort and enumeration labels #565

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

Merged
merged 2 commits into from
Aug 18, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Foundation/Dictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ extension Dictionary : _ObjectTypeBridgeable {
var failedConversion = false

if type(of: source) == NSDictionary.self || type(of: source) == NSMutableDictionary.self {
source.enumerateKeysAndObjects([]) { key, value, stop in
source.enumerateKeysAndObjects(options: []) { key, value, stop in
guard let key = key as? Key, let value = value as? Value else {
failedConversion = true
stop.pointee = true
Expand Down
26 changes: 13 additions & 13 deletions Foundation/NSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -386,13 +386,13 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
}

open func sortedArray(_ comparator: (Any, Any, UnsafeMutableRawPointer?) -> Int, context: UnsafeMutableRawPointer?) -> [Any] {
return sortedArray([]) { lhs, rhs in
return sortedArray(options: []) { lhs, rhs in
return ComparisonResult(rawValue: comparator(lhs, rhs, context))!
}
}

open func sortedArray(_ comparator: (Any, Any, UnsafeMutableRawPointer?) -> Int, context: UnsafeMutableRawPointer?, hint: Data?) -> [Any] {
return sortedArray([]) { lhs, rhs in
return sortedArray(options: []) { lhs, rhs in
return ComparisonResult(rawValue: comparator(lhs, rhs, context))!
}
}
Expand Down Expand Up @@ -426,13 +426,13 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
return object(at: idx)
}

public func enumerateObjects(_ block: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Void) {
self.enumerateObjects([], using: block)
open func enumerateObjects(_ block: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
self.enumerateObjects(options: [], using: block)
}
public func enumerateObjects(_ opts: NSEnumerationOptions = [], using block: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
open func enumerateObjects(options opts: NSEnumerationOptions = [], using block: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
self.enumerateObjects(at: IndexSet(integersIn: 0..<count), options: opts, using: block)
}
public func enumerateObjects(at s: IndexSet, options opts: NSEnumerationOptions = [], using block: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Void) {
open func enumerateObjects(at s: IndexSet, options opts: NSEnumerationOptions = [], using block: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
guard !opts.contains(.concurrent) else {
NSUnimplemented()
}
Expand All @@ -459,9 +459,9 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
}

open func indexesOfObjects(passingTest predicate: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> IndexSet {
return indexesOfObjects([], passingTest: predicate)
return indexesOfObjects(options: [], passingTest: predicate)
}
open func indexesOfObjects(_ opts: NSEnumerationOptions = [], passingTest predicate: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> IndexSet {
open func indexesOfObjects(options opts: NSEnumerationOptions = [], passingTest predicate: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> IndexSet {
return indexesOfObjects(at: IndexSet(integersIn: 0..<count), options: opts, passingTest: predicate)
}
open func indexesOfObjects(at s: IndexSet, options opts: NSEnumerationOptions = [], passingTest predicate: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> IndexSet {
Expand All @@ -474,7 +474,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
return result
}

internal func sortedArrayFromRange(_ range: NSRange, options: SortOptions, usingComparator cmptr: (Any, Any) -> ComparisonResult) -> [Any] {
internal func sortedArrayFromRange(_ 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()
Expand All @@ -495,7 +495,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
return sortedArrayFromRange(NSMakeRange(0, count), options: [], usingComparator: cmptr)
}

open func sortedArray(_ opts: SortOptions = [], usingComparator cmptr: (Any, Any) -> ComparisonResult) -> [Any] {
open func sortedArray(options opts: NSSortOptions = [], usingComparator cmptr: (Any, Any) -> ComparisonResult) -> [Any] {
return sortedArrayFromRange(NSMakeRange(0, count), options: opts, usingComparator: cmptr)
}

Expand Down Expand Up @@ -834,11 +834,11 @@ open class NSMutableArray : NSArray {
}

open func sortUsingComparator(_ cmptr: Comparator) {
self.sortWithOptions([], usingComparator: cmptr)
self.sortWithOptions(options: [], usingComparator: cmptr)
}

open func sortWithOptions(_ opts: SortOptions, usingComparator cmptr: Comparator) {
self.setArray(self.sortedArray(opts, usingComparator: cmptr))
open func sortWithOptions(options opts: NSSortOptions, usingComparator cmptr: Comparator) {
self.setArray(self.sortedArray(options: opts, usingComparator: cmptr))
}

public convenience init?(contentsOfFile path: String) { NSUnimplemented() }
Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSCFDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ internal func _CFSwiftDictionaryGetValuesAndKeys(_ dictionary: AnyObject, valueb
idx += 1
}
} else {
dict.enumerateKeysAndObjects([]) { k, v, _ in
dict.enumerateKeysAndObjects(options: []) { k, v, _ in
let key = _SwiftValue.store(k)
let value = _SwiftValue.store(v)
valuebuf?[idx] = Unmanaged<AnyObject>.passUnretained(value)
Expand All @@ -189,7 +189,7 @@ internal func _CFSwiftDictionaryGetValuesAndKeys(_ dictionary: AnyObject, valueb
}

internal func _CFSwiftDictionaryApplyFunction(_ dictionary: AnyObject, applier: @convention(c) (AnyObject, AnyObject, UnsafeMutableRawPointer) -> Void, context: UnsafeMutableRawPointer) {
(dictionary as! NSDictionary).enumerateKeysAndObjects([]) { key, value, _ in
(dictionary as! NSDictionary).enumerateKeysAndObjects(options: []) { key, value, _ in
applier(_SwiftValue.store(key), _SwiftValue.store(value), context)
}
}
Expand Down
24 changes: 12 additions & 12 deletions Foundation/NSDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,

open func allKeys(for anObject: Any) -> [Any] {
var matching = Array<Any>()
enumerateKeysAndObjects([]) { key, value, _ in
enumerateKeysAndObjects(options: []) { key, value, _ in
if let val = value as? AnyHashable,
let obj = anObject as? AnyHashable {
if val == obj {
Expand Down Expand Up @@ -422,11 +422,11 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
open func write(toFile path: String, atomically useAuxiliaryFile: Bool) -> Bool { NSUnimplemented() }
open func write(to url: URL, atomically: Bool) -> Bool { NSUnimplemented() } // the atomically flag is ignored if url of a type that cannot be written atomically.

public func enumerateKeysAndObjects(_ block: (Any, Any, UnsafeMutablePointer<ObjCBool>) -> Void) {
enumerateKeysAndObjects([], using: block)
open func enumerateKeysAndObjects(_ block: (Any, Any, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
enumerateKeysAndObjects(options: [], using: block)
}

public func enumerateKeysAndObjects(_ opts: NSEnumerationOptions = [], using block: (Any, Any, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
open func enumerateKeysAndObjects(options opts: NSEnumerationOptions = [], using block: (Any, Any, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
let count = self.count
var keys = [Any]()
var objects = [Any]()
Expand All @@ -444,25 +444,25 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
}

open func keysSortedByValue(comparator cmptr: (Any, Any) -> ComparisonResult) -> [Any] {
return keysSortedByValue([], usingComparator: cmptr)
return keysSortedByValue(options: [], usingComparator: cmptr)
}

open func keysSortedByValue(_ opts: SortOptions = [], usingComparator cmptr: (Any, Any) -> ComparisonResult) -> [Any] {
open func keysSortedByValue(options opts: NSSortOptions = [], usingComparator cmptr: (Any, Any) -> ComparisonResult) -> [Any] {
let sorted = allKeys.sorted { lhs, rhs in
return cmptr(lhs, rhs) == .orderedSame
}
return sorted
}

open func keysOfEntries(passingTest predicate: (Any, Any, UnsafeMutablePointer<ObjCBool>) -> Bool) -> Set<NSObject> {
return keysOfEntries([], passingTest: predicate)
open func keysOfEntries(passingTest predicate: (Any, Any, UnsafeMutablePointer<ObjCBool>) -> Bool) -> Set<AnyHashable> {
return keysOfEntries(options: [], passingTest: predicate)
}

open func keysOfEntries(_ opts: NSEnumerationOptions = [], passingTest predicate: (Any, Any, UnsafeMutablePointer<ObjCBool>) -> Bool) -> Set<NSObject> {
var matching = Set<NSObject>()
enumerateKeysAndObjects(opts) { key, value, stop in
open func keysOfEntries(options opts: NSEnumerationOptions = [], passingTest predicate: (Any, Any, UnsafeMutablePointer<ObjCBool>) -> Bool) -> Set<AnyHashable> {
var matching = Set<AnyHashable>()
enumerateKeysAndObjects(options: opts) { key, value, stop in
if predicate(key, value, stop) {
matching.insert(key as! NSObject)
matching.insert(key as! AnyHashable)
}
}
return matching
Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ open class NSError : NSObject, NSCopying, NSSecureCoding, NSCoding {
if let info = aDecoder.decodeObject(of: [NSSet.self, NSDictionary.self, NSArray.self, NSString.self, NSNumber.self, NSData.self, NSURL.self], forKey: "NSUserInfo") as? NSDictionary {
var filteredUserInfo = [String : Any]()
// user info must be filtered so that the keys are all strings
info.enumerateKeysAndObjects([]) {
info.enumerateKeysAndObjects(options: []) {
if let key = $0.0 as? NSString {
filteredUserInfo[key._swiftObject] = $0.1
}
Expand All @@ -74,7 +74,7 @@ open class NSError : NSObject, NSCopying, NSSecureCoding, NSCoding {
if let info = aDecoder.decodeObject() as? NSDictionary {
var filteredUserInfo = [String : Any]()
// user info must be filtered so that the keys are all strings
info.enumerateKeysAndObjects([]) {
info.enumerateKeysAndObjects(options: []) {
if let key = $0.0 as? NSString {
filteredUserInfo[key._swiftObject] = $0.1
}
Expand Down
2 changes: 1 addition & 1 deletion Foundation/NSLocale.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ extension NSLocale {
open class func components(fromLocaleIdentifier string: String) -> [String : String] {
var comps = Dictionary<String, String>()
let values = CFLocaleCreateComponentsFromLocaleIdentifier(kCFAllocatorSystemDefault, string._cfObject)._nsObject
values.enumerateKeysAndObjects([]) { (k, v, stop) in
values.enumerateKeysAndObjects(options: []) { (k, v, stop) in
let key = (k as! NSString)._swiftObject
let value = (v as! NSString)._swiftObject
comps[key] = value
Expand Down
6 changes: 3 additions & 3 deletions Foundation/NSObjCRuntime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ public enum NSQualityOfService : Int {
case `default`
}

public struct SortOptions: OptionSet {
public struct NSSortOptions: OptionSet {
public let rawValue : UInt
public init(rawValue: UInt) { self.rawValue = rawValue }

public static let concurrent = SortOptions(rawValue: UInt(1 << 0))
public static let stable = SortOptions(rawValue: UInt(1 << 4))
public static let concurrent = NSSortOptions(rawValue: UInt(1 << 0))
public static let stable = NSSortOptions(rawValue: UInt(1 << 4))
}

public struct NSEnumerationOptions: OptionSet {
Expand Down
6 changes: 3 additions & 3 deletions Foundation/NSOrderedSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ extension NSOrderedSet {
open func index(of object: Any, inSortedRange range: NSRange, options opts: NSBinarySearchingOptions = [], usingComparator cmp: (Any, Any) -> ComparisonResult) -> Int { NSUnimplemented() } // binary search

open func sortedArray(comparator cmptr: (Any, Any) -> ComparisonResult) -> [Any] { NSUnimplemented() }
open func sortedArray(options opts: SortOptions = [], usingComparator cmptr: (Any, Any) -> ComparisonResult) -> [Any] { NSUnimplemented() }
open func sortedArray(options opts: NSSortOptions = [], usingComparator cmptr: (Any, Any) -> ComparisonResult) -> [Any] { NSUnimplemented() }

public func description(withLocale locale: Locale?) -> String { NSUnimplemented() }
public func description(withLocale locale: Locale?, indent level: Int) -> String { NSUnimplemented() }
Expand Down Expand Up @@ -528,11 +528,11 @@ extension NSMutableOrderedSet {
sortRange(NSMakeRange(0, count), options: [], usingComparator: cmptr)
}

open func sort(options opts: SortOptions = [], usingComparator cmptr: (Any, Any) -> ComparisonResult) {
open func sort(options opts: NSSortOptions = [], usingComparator cmptr: (Any, Any) -> ComparisonResult) {
sortRange(NSMakeRange(0, count), options: opts, usingComparator: cmptr)
}

open func sortRange(_ range: NSRange, options opts: SortOptions = [], usingComparator cmptr: (Any, Any) -> ComparisonResult) {
open func sortRange(_ range: NSRange, options opts: NSSortOptions = [], usingComparator cmptr: (Any, Any) -> ComparisonResult) {
// The sort options are not available. We use the Array's sorting algorithm. It is not stable neither concurrent.
guard opts.isEmpty else {
NSUnimplemented()
Expand Down
32 changes: 16 additions & 16 deletions Foundation/NSSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ extension NSSet {

extension NSSet {

public var allObjects: [Any] {
open var allObjects: [Any] {
if type(of: self) === NSSet.self || type(of: self) === NSMutableSet.self {
return _storage.map { _SwiftValue.fetch($0) }
} else {
Expand All @@ -189,15 +189,15 @@ extension NSSet {
}
}

public func anyObject() -> Any? {
open func anyObject() -> Any? {
return objectEnumerator().nextObject()
}

public func contains(_ anObject: Any) -> Bool {
open func contains(_ anObject: Any) -> Bool {
return member(anObject) != nil
}

public func intersects(_ otherSet: Set<AnyHashable>) -> Bool {
open func intersects(_ otherSet: Set<AnyHashable>) -> Bool {
if count < otherSet.count {
for item in self {
if otherSet.contains(item as! AnyHashable) {
Expand All @@ -210,11 +210,11 @@ extension NSSet {
}
}

public func isEqual(to otherSet: Set<AnyHashable>) -> Bool {
open func isEqual(to otherSet: Set<AnyHashable>) -> Bool {
return count == otherSet.count && isSubset(of: otherSet)
}

public func isSubset(of otherSet: Set<AnyHashable>) -> Bool {
open func isSubset(of otherSet: Set<AnyHashable>) -> Bool {
// `true` if we don't contain any object that `otherSet` doesn't contain.
for item in self {
if !otherSet.contains(item as! AnyHashable) {
Expand All @@ -224,11 +224,11 @@ extension NSSet {
return true
}

public func adding(_ anObject: Any) -> Set<AnyHashable> {
open func adding(_ anObject: Any) -> Set<AnyHashable> {
return self.addingObjects(from: [anObject])
}

public func addingObjects(from other: Set<AnyHashable>) -> Set<AnyHashable> {
open func addingObjects(from other: Set<AnyHashable>) -> Set<AnyHashable> {
var result = Set<AnyHashable>(minimumCapacity: Swift.max(count, other.count))
if type(of: self) === NSSet.self || type(of: self) === NSMutableSet.self {
result.formUnion(_storage.map { _SwiftValue.fetch($0) as! AnyHashable })
Expand All @@ -240,7 +240,7 @@ extension NSSet {
return result.union(other)
}

public func addingObjects(from other: [Any]) -> Set<AnyHashable> {
open func addingObjects(from other: [Any]) -> Set<AnyHashable> {
var result = Set<AnyHashable>(minimumCapacity: count)
if type(of: self) === NSSet.self || type(of: self) === NSMutableSet.self {
result.formUnion(_storage.map { _SwiftValue.fetch($0) as! AnyHashable })
Expand All @@ -255,11 +255,11 @@ extension NSSet {
return result
}

public func enumerateObjects(_ block: (Any, UnsafeMutablePointer<ObjCBool>) -> Void) {
enumerateObjects([], using: block)
open func enumerateObjects(_ block: (Any, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
enumerateObjects(options: [], using: block)
}

public func enumerateObjects(_ opts: NSEnumerationOptions = [], using block: (Any, UnsafeMutablePointer<ObjCBool>) -> Void) {
open func enumerateObjects(options opts: NSEnumerationOptions = [], using block: (Any, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
var stop : ObjCBool = false
for obj in self {
withUnsafeMutablePointer(to: &stop) { stop in
Expand All @@ -271,13 +271,13 @@ extension NSSet {
}
}

public func objects(passingTest predicate: (Any, UnsafeMutablePointer<ObjCBool>) -> Bool) -> Set<AnyHashable> {
return objects([], passingTest: predicate)
open func objects(passingTest predicate: (Any, UnsafeMutablePointer<ObjCBool>) -> Bool) -> Set<AnyHashable> {
return objects(options: [], passingTest: predicate)
}

public func objects(_ opts: NSEnumerationOptions = [], passingTest predicate: (Any, UnsafeMutablePointer<ObjCBool>) -> Bool) -> Set<AnyHashable> {
open func objects(options opts: NSEnumerationOptions = [], passingTest predicate: (Any, UnsafeMutablePointer<ObjCBool>) -> Bool) -> Set<AnyHashable> {
var result = Set<AnyHashable>()
enumerateObjects(opts) { obj, stopp in
enumerateObjects(options: opts) { obj, stopp in
if predicate(obj, stopp) {
result.insert(obj as! AnyHashable)
}
Expand Down
2 changes: 1 addition & 1 deletion Foundation/Set.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extension Set : _ObjectTypeBridgeable {
var failedConversion = false

if type(of: source) == NSSet.self || type(of: source) == NSMutableSet.self {
source.enumerateObjects([]) { obj, stop in
source.enumerateObjects(options: []) { obj, stop in
if let o = obj as? Element {
set.insert(o)
} else {
Expand Down
6 changes: 3 additions & 3 deletions TestFoundation/TestNSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,12 @@ class TestNSArray : XCTestCase {
return l.localizedCaseInsensitiveCompare(r)
}
let result1 = NSArray(array: input.sortedArray(comparator: comparator))
let result2 = input.sortedArray([], usingComparator: comparator)
let result2 = input.sortedArray(options: [], usingComparator: comparator)

XCTAssertTrue(result1.isEqual(to: result2))

// sort empty array
let emptyArray = NSArray().sortedArray([]) { _,_ in .orderedSame }
let emptyArray = NSArray().sortedArray(options: []) { _,_ in .orderedSame }
XCTAssertTrue(emptyArray.isEmpty)
}

Expand Down Expand Up @@ -397,7 +397,7 @@ class TestNSArray : XCTestCase {
return l.localizedCaseInsensitiveCompare(r)
}
mutableStringsInput1.sortUsingComparator(comparator)
mutableStringsInput2.sortWithOptions([], usingComparator: comparator)
mutableStringsInput2.sortWithOptions(options: [], usingComparator: comparator)
XCTAssertTrue(mutableStringsInput1.isEqual(to: mutableStringsInput2.map { $0 }))
}

Expand Down