询问一个关于synedit中hashkey的问题。

huangjacky 2010-11-29 09:24:38
最近在看synedit的highlighter的时候,在关键字部分,synedit采用了hash表的形式。
hash表能很大的提高字符串的查找效率,用hash表也很正常。
但是在代码中,我有几个地方不明白。
举个例子,已synHighlighter.pas中为例
首先定义了一个关键字数组:

KeyWords: array[0..110] of UnicodeString = (
'absolute', 'abstract', 'and', 'array', 'as', 'asm', 'assembler',
'automated', 'begin', 'case', 'cdecl', 'class', 'const', 'constructor',
'contains', 'default', 'deprecated', 'destructor', 'dispid',
'dispinterface', 'div', 'do', 'downto', 'dynamic', 'else', 'end', 'except',
'export', 'exports', 'external', 'far', 'file', 'final', 'finalization',
'finally', 'for', 'forward', 'function', 'goto', 'helper', 'if',
'implementation', 'implements', 'in', 'index', 'inherited',
'initialization', 'inline', 'interface', 'is', 'label', 'library',
'message', 'mod', 'name', 'near', 'nil', 'nodefault', 'not', 'object', 'of',
'on', 'operator', 'or', 'out', 'overload', 'override', 'package', 'packed',
'pascal', 'platform', 'private', 'procedure', 'program', 'property',
'protected', 'public', 'published', 'raise', 'read', 'readonly', 'record',
'register', 'reintroduce', 'repeat', 'requires', 'resourcestring',
'safecall', 'sealed', 'set', 'shl', 'shr', 'stdcall', 'stored', 'string',
'stringresource', 'then', 'threadvar', 'to', 'try', 'type', 'unit', 'until',
'uses', 'var', 'virtual', 'while', 'with', 'write', 'writeonly', 'xor'
);

下面是计算hash的代码:

function TSynPasSyn.HashKey(Str: PWideChar): Cardinal;
begin
Result := 0;
while IsIdentChar(Str^) do
begin
Result := Result * 812 + Ord(Str^) * 76;
inc(Str);
end;
Result := Result mod 389;
fStringLen := Str - fToIdent;
end;

这里涉及到了3个数字 812 76 和 389.
这3个数字和数组个数111没有关系。
我想问这3个数字怎么确定的?
如果我有下面一个关键字数组:

const
Symbols:array[0..28] of string = (
'fun','begin','end','bnot','div','rem','band',
'bor','bxor','bsl','bsr','true','false','when','orelse','andalso','and','or',
'if','case','of','exit','throw','error','try','catch','finally',
'after','time'
);

那么hashkey这里这3个数字怎么计算。
谢谢
...全文
75 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangjacky 2010-11-30
  • 打赏
  • 举报
回复
早上起来顶一顶
huangjacky 2010-11-30
  • 打赏
  • 举报
回复
还是没人。

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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