python的hash计算,不懂

「已注销」 2011-11-07 10:10:57
def tesHash(fileName):
"""Returns tes4's two hash values for filename.
Based on TimeSlips code with cleanup and pythonization."""
root,ext = os.path.splitext(fileName.lower()) #--"bob.dds" >> root = "bob", ext = ".dds"
#--Hash1
chars = map(ord,root) #--'bob' >> chars = [98,11,98]
hash1 = chars[-1] | (0,chars[-2])[len(chars)>2]<<8 | len(chars)<<16 | chars[0]<<24
#--(a,b)[test] is similar to test?a:b in C. (Except that evaluation is not shortcut.)
if ext == '.kf': hash1 |= 0x80
elif ext == '.nif': hash1 |= 0x8000
elif ext == '.dds': hash1 |= 0x8080
elif ext == '.wav': hash1 |= 0x80000000
#--Hash2
#--Python integers have no upper limit. Use uintMask to restrict these to 32 bits.
uintMask, hash2, hash3 = 0xFFFFFFFF, 0, 0
for char in chars[1:-2]: #--Slice of the chars array
hash2 = ((hash2 * 0x1003f) + char ) & uintMask
for char in map(ord,ext):
hash3 = ((hash3 * 0x1003F) + char ) & uintMask
hash2 = (hash2 + hash3) & uintMask
#--Done
return (hash2<<32) + hash1 #--Return as uint64


这是一段计算值文件名hash的代码,小弟初学python实在是看不懂,哪位大哥能帮我看看是什么意思么,如果可以的话,写个伪代码或者C代码都行,上面的根据map函数啊,(0,chars[-2])[len(chars)>2]<<8是什么意思都不清楚。
...全文
559 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiaweiqq123 2011-11-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jha334201553 的回复:]

还有python的优先级是什么呢?先移位后与或非,还是先与或非再移位?
[/Quote]
移位运算的优先级应该低于与或运算
先乘除,后加减,最后再移位
「已注销」 2011-11-07
  • 打赏
  • 举报
回复
还有python的优先级是什么呢?先移位后与或非,还是先与或非再移位?
panghuhu250 2011-11-07
  • 打赏
  • 举报
回复
你的问题在注释里都有回答了:

1. map: chars = map(ord,root) #--'bob' >> chars = [98,11,98]

map(function, list) = [function(list[0]), function(list[1]), ...]

2. 第二个问题相关的代码行下有注释:

#--(a,b)[test] is similar to test?a:b in C. (Except that evaluation is not shortcut.)
「已注销」 2011-11-07
  • 打赏
  • 举报
回复
谢谢各位了,原来算法已经变了,就用原来的hash吧,我就不计算hash了,结贴~
livesguan 2011-11-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jha334201553 的回复:]

还有python的优先级是什么呢?先移位后与或非,还是先与或非再移位?
[/Quote]

亮相可以自己试一下.

楼主贴的代码出自C或者C++程序的手笔吧?

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧