dongluan6784 2017-05-13 21:47
浏览 30
已采纳

哈希表:不进行转换的转换?

I am implementing a specialized hashtable. I'm trying store a lot of data in a single 64-bit int key, for space usage and performance reasons.

Each key should have this structure:

// Key structure, from LSB
// eval result (16 bits)
// move (16 bits)
// age (16 bits): the move of the game on which this position would have occurred
// depth (8 bits)
// node type (8 bits): from the three constants above

Here is a simple implementation:

var keys [1000]uint64
var values [1000]uint64

func Put(b *dragontoothmg.Board, m dragontoothmg.Move, eval int16, depth uint8, ntype uint8) {
    var value uint64 = uint64(eval) | (uint64(m) << 16) | (uint64(b.Fullmoveno) << 32) |
        (uint64(depth) << 48) | (uint64(ntype) << 56)
    hash := b.Hash()
    key := hash ^ value
    index := hash % uint64(len(keys))
    keys[index] = key
    values[index] = value
}

func Get(b *dragontoothmg.Board) (found bool, move dragontoothmg.Move,
    eval int16, depth uint8, ntype uint8) {
    hash := b.Hash()
    index := hash % uint64(len(keys))
    key := keys[index]
    value := values[index]
    found = (hash == (key ^ value))
    if !found {
        return false, 0, 0, 0, 0
    }
    eval = int16(value & 0xFFFF)
    move = dragontoothmg.Move((value >> 16) & 0xFFFF)
    depth = uint8((value >> 48) & 0xFF)
    ntype = uint8((value >> 56) & 0xFF)
    return
}

However, when I try to Get() the data, it comes back corrupted. I suspect this might be related to the fact that eval is a signed int, and the cast converts it to a signed uint64. What have I done wrong, and how can I fix it?

This is failing test result:

--- FAIL: TestSimpleTt (0.10s)
    transtable_test.go:37: Simple ttable test failed. 
        Put data: (board: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 0) e2e4 -30 6 2 
         Fetched data: true h8h8 -30 255 255
FAIL

h8h8 is the maximum value of the field, for what it's worth.

  • 写回答

2条回答 默认 最新

  • dongzaobei0942 2017-05-14 02:39
    关注

    Thanks to everyone who responded. This was the solution to my issue:

    uint64(eval) for negative values was moving the sign bit to the most significant bit of the 64-bit int, which then interfered with the data that was supposed to be stored at that bit. I solved it by first converting to uint16:

    uint64(uint16(eval))

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常