标签: hashable

无法使用元组作为字典键?

代码有点复杂,抱歉.请关注该parallel_p功能.虽然sign是一个元组,但Python抱怨道:

if sign in hashtable并给出一个TypeError.为什么sign一个numpy.ndarray,而不是一个元组?我把它创建为一个元组.

p_dist = dict()

def parallel_prog(hashtable):
    def wrapper(func):
        if parallel_programming:
            @wraps(func)
            def parallel_p(*args, **kwargs):
                sign = tuple(args) 
                print type(sign)
                if sign in hashtable:
                    r = hashtable[sign] # hit.
                    return r
                r = func(*args, **kwargs)
                hashtable[tuple(sign)] = r # register for future hits.
                return r
            return parallel_p
        else:
            return func
    return wrapper


@parallel_prog(p_dist)
def euclidean(a, b, signature=None):

    val = np.sqrt(np.add.reduce((a - b)**2))
    return val
Run Code Online (Sandbox Code Playgroud)

在测试模块中

class …
Run Code Online (Sandbox Code Playgroud)

python typeerror hashable

5
推荐指数
1
解决办法
6156
查看次数

如何使用 Xcode 10 中提供的 API 使枚举符合 Hashable?

在我的 Swift 4.2.1 代码中,我有这个枚举:

enum MyEnum {

    case caseOne(Int)
    case caseTwo(String)
    case caseThree
}
Run Code Online (Sandbox Code Playgroud)

它符合Equatable

extension MyEnum: Equatable {

    static func == (lhs: MyEnum, rhs: MyEnum) -> Bool {

        switch (lhs, rhs) {
        case (.caseOne, .caseOne), (.caseTwo, .caseTwo), (.caseThree, .caseThree):
            return true
        default:
            return false
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要让它符合Hashable,这就是为什么我添加了一个扩展:

extension MyEnum: Hashable {

    var hashValue: Int {

        switch self {
        case .caseOne:
            return 1
        case .caseTwo:
            return 2
        case .caseThree:
            return 3
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我想迁移到 …

hashable swift xcode10 swift4.2

4
推荐指数
2
解决办法
3464
查看次数

如何使用两个枚举变量实现 Identifiable

使用 Swift 5.3,如何Identifiable通过使其身份取决于两个枚举变量的组合来在结构上实现协议?

有问题的代码很简单,

struct Card: Identifiable {
    let suit: Suit
    let rank: Rank
    
    enum Suit {
        case spades, clubs, diamonds, hearts
    }
    
    enum Rank: Int {
        case one = 1, two, three, four, five, six, seven, jack, queen, king
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的结构体还不符合Identifiable协议。我如何将其身份实现为它的suitrank(仅创建一次)的唯一组合?本质上,它的身份可以是“黑桃-1”或“钻石杰克”。此外,如果可能的话,我希望将 保留rank为一种Int类型,以便稍后进行算术运算。先感谢您!

identity hashable swift swift-protocols identifiable

4
推荐指数
1
解决办法
681
查看次数

swift协议Hashable中hashValue的性能考虑因素

除了是一个唯一的整数外,是否有任何性能考虑因素可以选择一个可以插入到?hashValue中的swift Hashable类型Set?例如,我选择的整数值的大小会影响后备数组的大小吗?即,如果我分配hashValue4000一个Hashable类型,并插入到一个Set将所述背衬阵列必须至少4000在长度?

hashset hashable swift

3
推荐指数
1
解决办法
349
查看次数

使简单元组符合Hashable,因此可以是Dictionary Key

我想用一个非常简单的元组作为关键:

(Int, Int)
Run Code Online (Sandbox Code Playgroud)

字典键需要是Hashable.我学到了

但是找不到我如何使这个简单的元组Hashable,并且最好在协议一致性方面做斗争.

更深刻的是,CGPoint可以解决我的问题.它可以是这种格式,但不可清除.

是否可以扩展CGPoint以便它可以清洗?如果是这样,怎么样?

编辑:CGPoint选择的Int变体的图像.

在此输入图像描述

dictionary protocols hashable swift

3
推荐指数
1
解决办法
1491
查看次数

如何将自定义类与可选属性符合"hashable"协议

假设我有一个基类"Person",我想添加到Set(List),因此需要符合Hashable和Equatable:

class Person : Equatable, Hashable {
let firstName: String
let lastName: String
var nickname: String?
let dateOfBirth: NSDate
var hashValue: Int {
    if let nickname = nickname {
        return firstName.hashValue ^
               lastName.hashValue ^
               nickname.hashValue ^
               dateOfBirth.hashValue
    } else {
        return firstName.hashValue ^
               lastName.hashValue ^
               dateOfBirth.hashValue
    }

}

init (firstName: String, lastName: String, nickname: String, bornOn dateOfBirth: NSDate) {
    self.firstName = firstName
    self.lastName  = lastName
    self.nickname = nickname
    self.dateOfBirth = dateOfBirth
    }
}

func ==(lhs: Person, rhs: Person) -> Bool …
Run Code Online (Sandbox Code Playgroud)

optional hashable swift

3
推荐指数
1
解决办法
1140
查看次数

Set如何确保Swift的可用性?

我在读Set

当您需要有效地测试成员资格并且不关心集合中元素的顺序,或者需要确保每个元素在集合中只出现一次时,可以使用集合而不是数组.

基本上Set确保了独特性,它有一些方法和依赖Hashable

使用contains(_ :)方法测试集合是否包含特定元素.

使用减法(_ :)方法创建一个新集合,其中集合的元素不在另一个集合或序列中

但是2个不同的对象可以有相同的hashValue,就像在这篇文章Swift Hashable中一样

不要假设具有相同散列值的类型的两个实例相等.根据我们计算哈希值的方式,我们可以获得两个不同实例共享相同哈希值的冲突.Hashable协议只需要反向 - 两个相等的实例具有相同的哈希值.

那么,如果两个对象具有相同hashValue,并且Set只保存1,那么我们有问题呢?

set hashable swift

3
推荐指数
1
解决办法
643
查看次数

如何在Swift中从hashValue实现hash(into :)?

我对如何处理编译器不使用hashValue而不是实施的弃用警告一无所知hash(into:)

“ Hashable.hashValue”已作为协议要求弃用;通过实现'hash(into :)'来使类型'MenuItem'与'Hashable'一致

Swift的回答:'Hashable.hashValue'已被弃用为协议要求;有这个例子:

func hash(into hasher: inout Hasher) {
    switch self {
    case .mention: hasher.combine(-1)
    case .hashtag: hasher.combine(-2)
    case .url: hasher.combine(-3)
    case .custom(let regex): hasher.combine(regex) // assuming regex is a string, that already conforms to hashable
    }
}
Run Code Online (Sandbox Code Playgroud)

而且我有这个结构,可以自定义PagingItem羊皮纸(https://github.com/rechsteiner/Parchment)。

import Foundation

/// The PagingItem for Menus.
struct MenuItem: PagingItem, Hashable, Comparable {
    let index: Int
    let title: String
    let menus: Menus

    var hashValue: Int {
        return index.hashValue …
Run Code Online (Sandbox Code Playgroud)

hashable swift

3
推荐指数
2
解决办法
1700
查看次数

存储属性类型“CGPoint”不符合协议“Hashable”,

想象一下卡片板。所有卡片都位于我称之为 CardBoard 的视图中。

所有卡都在一个数组中:

var cards:[Card]
Run Code Online (Sandbox Code Playgroud)

每张牌都有自己的位置。

这是卡

struct Card: View {
  let id = UUID()
  
  var name:String
  var code:String
  var filename:String
  var position = CGPoint(x: 200, y: 250)
  
  
  init(name:String, code:String, filename:String) {
    self.name = name
    self.code = code
    self.filename = filename
  }
  
  
  var body: some View {
    Image(filename)
      .position(position)
  }
}
Run Code Online (Sandbox Code Playgroud)

现在我想把它们画在屏幕上。

var body: some View {
  ZStack {
    ForEach(cards, id:\.self) {card in
       
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试这个时,Xcode 告诉我

Generic struct 'ForEach' requires that 'Card' conform to 'Hashable'
Run Code Online (Sandbox Code Playgroud)

当我添加Hashable …

hashable swift swiftui

3
推荐指数
1
解决办法
1710
查看次数

为什么需要在类外定义等同协议?

当我实现Hashable协议时.需要在类外定义一个等同的协议函数,如下所示.如下.

func ==(lhs: Swap, rhs: Swap) -> Bool {
    return (lhs.cookieA == rhs.cookieA && lhs.cookieB == rhs.cookieB) ||
        (lhs.cookieB == rhs.cookieA && lhs.cookieA == rhs.cookieB)
}

class Swap: Printable,Hashable {
    var cookieA: Cookie
    var cookieB: Cookie

    init(cookieA: Cookie, cookieB: Cookie) {
        self.cookieA = cookieA
        self.cookieB = cookieB
    }
    var hashValue: Int {
    return cookieA.hashValue ^ cookieB.hashValue
    }

    var description: String {
    return "swap \(cookieA) with \(cookieB)"
    }
}
Run Code Online (Sandbox Code Playgroud)

这对我来说有点奇怪.在上面的例子中,func ==应该属于Swap类.那么为什么我们需要声明类外的func ==?

hashable swift

2
推荐指数
1
解决办法
429
查看次数