我正在写类似于糖果迷的游戏。我有一个“交换”类型来表示2个cookie的交换。
@interface Cookie : NSObject {
}
@property (strong) Coord2D *coord;
@end
@implementation Cookie {
}
@interface Swap : NSObject {
}
@property (strong) Cookie *cookieA;
@property (strong) Cookie *cookieB;
@end
@implementation Swap
- (BOOL)isEqual:(id)object {
Swap *other = object;
return ([_cookieA.coord isEqual:other.cookieA.coord] && [_cookieB.coord isEqual:other.cookieB.coord])
|| ([_cookieA.coord isEqual:other.cookieB.coord] && [_cookieB.coord isEqual:other.cookieA.coord]);
}
- (NSUInteger)hash {
// (A, B) and (B, A) must be interchangable (hash to the same value)
return (_cookieA.coord.row + _cookieB.coord.row) ^ (_cookieA.coord.col + _cookieB.coord.col);
}
- (NSString *)description
{
return [NSString stringWithFormat:@"[%@, %@]", _cookieA.coord, _cookieB.coord];
}
@end
cookie.coord
被简单地定义为具有Coord2D
和row
的类型col
@interface Coord2D : NSObject {
}
@property int row;
@property int col;
@end
@implement Coord2D
- (BOOL)isEqual:(id)object {
Coord2D *other = object;
return _row == other.row && _col == other.col;
}
- (NSUInteger)hash {
return _row ^ _col;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"(%i, %i)", _row, _col];
}
@end
现在,我需要测试NSSet allPossibleSwaps
是否包含新创建的swap
对象。
(lldb) po allPossibleSwaps
{(
[(2, 4), (2, 5)],
[(1, 1), (2, 1)],
[(1, 0), (2, 0)],
[(2, 4), (2, 3)],
[(1, 2), (2, 2)],
[(0, 4), (1, 4)],
[(1, 4), (2, 4)],
[(1, 4), (1, 5)],
[(2, 3), (2, 4)]
)}
(lldb) po swap
[(1, 4), (2, 4)]
(lldb) p [allPossibleSwaps containsObject:swap]
(bool) $64 = false
应该预期结果为是,但实际上为否。
以下是一些线索:
containsObject :( allObject [6]错误!)
同样奇怪的是它们被打印为true/false
而不是YES/NO
(lldb) p [allPossibleSwaps containsObject: allPossibleSwaps.allObjects[0]]
(bool) $41 = true
(lldb) p [allPossibleSwaps containsObject: allPossibleSwaps.allObjects[1]]
(bool) $40 = true
(lldb) p [allPossibleSwaps containsObject: allPossibleSwaps.allObjects[2]]
(bool) $42 = true
(lldb) p [allPossibleSwaps containsObject: allPossibleSwaps.allObjects[3]]
(bool) $39 = true
(lldb) p [allPossibleSwaps containsObject: allPossibleSwaps.allObjects[4]]
(bool) $43 = true
(lldb) p [allPossibleSwaps containsObject: allPossibleSwaps.allObjects[5]]
(bool) $44 = true
(lldb) p [allPossibleSwaps containsObject: allPossibleSwaps.allObjects[6]]
(bool) $45 = false
(lldb) p [allPossibleSwaps containsObject: allPossibleSwaps.allObjects[7]]
(bool) $46 = true
(lldb) p [allPossibleSwaps containsObject: allPossibleSwaps.allObjects[8]]
(bool) $47 = true
isEqual调用(似乎正确)
(lldb) p [(Swap *)(allPossibleSwaps.allObjects[0]) isEqual:swap]
(BOOL) $53 = NO
(lldb) p [(Swap *)(allPossibleSwaps.allObjects[1]) isEqual:swap]
(BOOL) $52 = NO
(lldb) p [(Swap *)(allPossibleSwaps.allObjects[2]) isEqual:swap]
(BOOL) $54 = NO
(lldb) p [(Swap *)(allPossibleSwaps.allObjects[3]) isEqual:swap]
(BOOL) $55 = NO
(lldb) p [(Swap *)(allPossibleSwaps.allObjects[4]) isEqual:swap]
(BOOL) $56 = NO
(lldb) p [(Swap *)(allPossibleSwaps.allObjects[5]) isEqual:swap]
(BOOL) $57 = NO
(lldb) p [(Swap *)(allPossibleSwaps.allObjects[6]) isEqual:swap]
(BOOL) $50 = YES
(lldb) p [(Swap *)(allPossibleSwaps.allObjects[7]) isEqual:swap]
(BOOL) $51 = NO
(lldb) p [(Swap *)(allPossibleSwaps.allObjects[8]) isEqual:swap]
(BOOL) $58 = NO
哈希调用:(似乎正确)
(lldb) p ((Swap *)allPossibleSwaps.allObjects[0]).hash
(NSUInteger) $30 = 13
(lldb) p ((Swap *)allPossibleSwaps.allObjects[1]).hash
(NSUInteger) $31 = 1
(lldb) p ((Swap *)allPossibleSwaps.allObjects[2]).hash
(NSUInteger) $32 = 3
(lldb) p ((Swap *)allPossibleSwaps.allObjects[3]).hash
(NSUInteger) $33 = 3
(lldb) p ((Swap *)allPossibleSwaps.allObjects[4]).hash
(NSUInteger) $34 = 7
(lldb) p ((Swap *)allPossibleSwaps.allObjects[5]).hash
(NSUInteger) $35 = 9
(lldb) p ((Swap *)allPossibleSwaps.allObjects[6]).hash
(NSUInteger) $36 = 11
(lldb) p ((Swap *)allPossibleSwaps.allObjects[7]).hash
(NSUInteger) $37 = 11
(lldb) p ((Swap *)allPossibleSwaps.allObjects[8]).hash
(NSUInteger) $65 = 3
(lldb) p swap.hash
(NSUInteger) $66 = 11
答案 0 :(得分:3)
您的哈希和isEquals逻辑看起来正确。从控制台输出中判断,哈希值和isEquals的行为均符合预期。因此,很可能在重新哈希处理期间将项目放入了错误的位置。
当您从集合中插入或删除项目时,会发生重新哈希。如果您不熟悉哈希的工作原理,那么这是一篇好文章:https://en.wikipedia.org/wiki/Extendible_hashing
您的Cookie交换逻辑是否有可能弄乱了集中已有的交换项目?插入后是否要修改坐标?