我有一个CGPoint
的数组,
pointArray = [(532.7, 150.0), (66.6, 150.0), (129.2, 150.0), (129.2, 150.0), (301.2, 150.0), (444.2, 150.0), (532.7, 150.0), (532.7, 150.0), (532.7, 150.0)]
如何获取每个点重复出现的次数?
我有一个CGPoint
的数组,
pointArray = [(532.7, 150.0), (66.6, 150.0), (129.2, 150.0), (129.2, 150.0), (301.2, 150.0), (444.2, 150.0), (532.7, 150.0), (532.7, 150.0), (532.7, 150.0)]
如@Alexander在评论中所说,你应该使用NSCountedSet
,你可以像这样使用它:
let array = [
CGPoint(x: 532.7, y: 150.0),
CGPoint(x: 66.6, y: 150.0),
CGPoint(x: 129.2, y: 150.0),
CGPoint(x: 129.2, y: 150.0),
CGPoint(x: 301.2, y: 150.0),
CGPoint(x: 444.2, y: 150.0),
CGPoint(x: 532.7, y: 150.0),
CGPoint(x: 532.7, y: 150.0),
CGPoint(x: 532.7, y: 150.0)
]
let countedSet = NSCountedSet(array: array)
countedSet.count(for: array.last!) //returns 4
如果您不想使用NSCountedSet
,可以将每个点存储在字典中作为键,并将计数作为值。棘手的问题是CGPoint
不符合Hashable
,您可以像这样处理:
extension CGPoint:Hashable{
public var hashValue: Int {
let x = Double(self.x)
let y = Double(self.y)
return Int(((x + y)*(x + y + 1)/2) + y)
//this hash function may not be the best for your data
}
}
var map = [CGPoint:Int]()
for point in array {
if let count = map[point] {
map[point] = count + 1
} else {
map[point] = 1
}
}
map[array.last!]//returns 4
NSCountedSet
。NSCountedSet
也需要Hashable
。幕后的技巧是将值转换为NSValue
,我相信这样做。 - SulthanNSCountedSet
有两个初始化方法:
public convenience init(array: [Any])
和
public convenience init(set: Set<AnyHashable>)
你只需要将一个Set
传递给init
,并确保它符合Hashable
协议
(我使用的是Swift 3.0.2和Xcode 8.2.1) - Rodrigo Ruiz Murguíalet points = [CGPoint(x: 532.7, y: 150.0), CGPoint(x: 66.6, y: 150.0), CGPoint(x: 129.2, y: 150.0), CGPoint(x: 129.2, y: 150.0), CGPoint(x: 301.2, y: 150.0), CGPoint(x: 444.2, y: 150.0), CGPoint(x: 532.7, y: 150.0), CGPoint(x: 532.7, y: 150.0), CGPoint(x: 532.7, y: 150.0)]
var d : [NSValue:Int] = [:]
for p in points {
let v = NSValue(cgPoint:p)
if let ct = d[v] {
d[v] = ct+1
} else {
d[v] = 1
}
}
// how many times does `points[0]` appear in `points`?
d[NSValue(cgPoint:points[0])] // 4
我同意Matt的看法,认为已接受的解决方案似乎有些过度。我建议采用类似于他所采取的函数式方法:
let dictionary:[String:Int] = array.reduce([:]){
var dict = $0.0, key = String(describing: $0.1)
dict[key] = (dict[key] ?? 0 ) + 1
return dict
}
let pointArray:[(CGFloat, CGFloat)] = [(532.7, 150.0), (66.6, 150.0), (129.2, 150.0), (129.2, 150.0), (301.2, 150.0), (444.2, 150.0), (532.7, 150.0), (532.7, 150.0), (532.7, 150.0)]
let dictionary:[String:Int] = pointArray.reduce([:]){
var dict = $0.0, key = String(describing: $0.1)
dict[key] = (dict[key] ?? 0 ) + 1
return dict
}
print(dictionary) //output is ["(129.2, 150.0)": 2, "(66.6, 150.0)": 1, "(532.7, 150.0)": 4, "(444.2, 150.0)": 1, "(301.2, 150.0)": 1]
String(describing:)
作为字典键可以使其与元组一起使用,就像您的示例代码中一样,以及其他不一定是CGPoint
或Hashable
实例的变化。这将为字典提供一个Hashable
键,而无需编写自定义代码;然而,正如亚历山大在下面的评论中指出的那样,这并不是生成Hashable
值的最有效方法,如果您正在处理成千上万个点或更多,或者在循环中频繁运行此操作,则使用String(describing:)
与使用自定义函数从您的点元组生成哈希之间的差异可能是显着的。(CGFloat, CGFloat)
值数组不会发生碰撞,因此您无法使元组Hashable
。使用CGPoint
上的Hashable
扩展或将CGPoint
包装在NSValue
中是可以替换其他情况的实现,但是这个答案提供了:1)算法的功能风格2)适用于OP示例输入的解决方案,这两者都没有被以前的答案涵盖。 - Daniel HallInt
hashValue
来表示CGPoint
的2个Int
。不存在任何函数可以进行1:1映射。您的算法很好,但是如果为CGPoint
定义适当的哈希函数而不是使用String(describing:)
,它将更加优秀。 - AlexanderInt
hashValue表示2个64位值(Double
s)。您必须有碰撞,因此最好使用将它们在被散列的数据域中保持尽可能不频繁的散列算法。 String(describing:)输出一个字符串,该字符串对于元组中每个唯一的Float组合是唯一的
。这是正确的。 并且不会与其他组合的String值的哈希发生冲突。
那是不对的。可能的字符串比唯一哈希值多。 根据鸽笼原理,无法避免碰撞。 - Alexander