Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何将double或float重新解释为整数来创建散列?_Objective C_Xcode_Reinterpret Cast - Fatal编程技术网

Objective c 如何将double或float重新解释为整数来创建散列?

Objective c 如何将double或float重新解释为整数来创建散列?,objective-c,xcode,reinterpret-cast,Objective C,Xcode,Reinterpret Cast,这是我的isEqual和hash自定义运算符 - (BOOL)isEqual:(id)object; { BGSearchParameter * theOther = (BGSearchParameter *)object; BOOL isTheOtherEqual; isTheOtherEqual = isTheOtherEqual && [self.Location isEqual:theOther.Location]; isTheOther

这是我的isEqual和hash自定义运算符

- (BOOL)isEqual:(id)object;
{
    BGSearchParameter * theOther = (BGSearchParameter *)object;

    BOOL isTheOtherEqual;
    isTheOtherEqual = isTheOtherEqual && [self.Location isEqual:theOther.Location];
    isTheOtherEqual = isTheOtherEqual && [self.keyword isEqual:theOther.keyword];
    isTheOtherEqual = isTheOtherEqual && (self.Distance == theOther.Distance);
    isTheOtherEqual = isTheOtherEqual && (self.SortByWhat == theOther.SortByWhat);
    isTheOtherEqual = isTheOtherEqual && (self.startFrom == theOther.startFrom);
    isTheOtherEqual = isTheOtherEqual && (self.numberOfIDstoGrab == theOther.numberOfIDstoGrab);

    return isTheOtherEqual;
}
- (NSUInteger)hash
{
    NSUInteger returnValue=0;
    returnValue ^= self.Location.hash;
    returnValue ^= self.keyword.hash;

    return returnValue;
}
那一个就行了。但是,假设我想将距离和startfrom合并到散列中

我想我只想补充一下:

returnValue ^= self.Distance;
这是一个错误,因为它不兼容


那么我应该怎么做呢?

这是
CFNumber
/
NSNumber
用作
float
double
值的哈希值,请参见Mac OS X 10.7.5源代码中的示例

#define HASHFACTOR 2654435761U

CF_INLINE CFHashCode _CFHashDouble(double d) {
    double dInt;
    if (d < 0) d = -d;
    dInt = floor(d+0.5);
    CFHashCode integralHash = HASHFACTOR * (CFHashCode)fmod(dInt, (double)ULONG_MAX);
    return (CFHashCode)(integralHash + (CFHashCode)((d - dInt) * ULONG_MAX));
}

最后我把数字变成了NSNumber,得到了哈希:

   returnValue ^= @(self.Distance).hash;
   returnValue ^= @(self.SortByWhat).hash;
   returnValue ^= @(self.startFrom).hash;
   returnValue ^= @(self.numberOfIDstoGrab).hash;
马丁回答得很好。但是,结果应该是一样的,我不想实现另一个复杂的函数。

试试这个:

static NSUInteger doubleHash(float value) {
    return *(NSUInteger *)&value;//gets bits
}

请注意转换为float。它会降低精度,但sizeof(float)=sizeof(NSUInteger)。

该函数以33;。我们能用它吗?这是一个私有函数吗?@HaryantoCiu:该函数由CoreFoundation框架内部使用。如果你想使用它,我建议你自己用一个不同的名字来实现它。你当然可以这样做。但是您应该知道,您每次分配/init并释放一个
NSNumber
对象,因此开销更大。我知道。但仍然是O(1);您的答案仍处于选中状态。实现起来太复杂了。您的解决方案真正展示了新@()符号的优雅。正如您所建议的,首先关注简单、可读、健壮、优雅的代码。然后关注最需要的性能。请注意,
IsTheOthereEqual
没有初始化为
YES
。应该注意的是,由于浮点数通常不精确,在与
=
比较无用的相同情况下,哈希值将是无用的。
static NSUInteger doubleHash(float value) {
    return *(NSUInteger *)&value;//gets bits
}