C 中小数据集的哈希映射的替代方案
我目前正在开发粒子模拟器的命令行界面。其解析器采用以下格式读取输入:
[command] [argument]* (-[flag] [flag argument])
目前,与各种已知命令相比,该命令是通过条件块发送的并将其对应的数据包发送到匹配函数。然而,这似乎笨重、低效且不优雅。
我正在考虑使用哈希图,将命令的字符串表示形式作为键,将函数指针作为值。然后,引用的函数将被发送一个包含参数、标志等的数据包。
在这种情况下,哈希映射是否过度?实施一项所需的额外基础设施是否超过了潜在的好处?我的目标是速度、优雅、功能,以及可扩展性(因为这是一个开源项目)。
感谢您的帮助。
I am currently working on a command line interface for a particle simulator. Its parser takes reads input in the following format:
[command] [argument]* (-[flag] [flag argument])
Currently, the command is sent through a conditional block, compared to various known commands and its corresponding data packet is sent to the matching function. This, however, seems clunky, inefficient and inelegant.
I am thinking about using a hashmap instead, with a string representation of a command as the key and a function pointer as the value. The function referenced would then be sent a data packet containing arguments, flags, etc.
Is a hash map overkill in this situation? Does the extra infrastructure required to implement one outweigh the potential benefits? I am aiming for speed, elegance, function, and, since this is an open-source project, extensibility.
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要考虑三元搜索树。具有良好的性能,高效利用存储;并且您不需要哈希函数或冲突策略。
链接的 Bentley/Sedgwick 文章对随附的 C 源代码进行了非常全面且可读的解释。
在我的 postscript 解释器的过去 3 个版本中,我一直使用 TST 进行名称查找。唯一需要的更改是由于内存管理的更改。 这是一个版本,我(稍微)修改为使用显式指针。我在我的 postscript 解释器中使用了另一个版本,即任何 xpost2*.zip文件 core.c 中的版本,它使用指针的字节偏移量(必须添加到用户内存字节指针以生成真实指针)。
You might want to consider the Ternary Search Tree. It has good performnce, efficient use of storage; and you don't need a hash function or a collision strategy.
The linked Bentley/Sedgwick article is a very thorough-yet-readable explanation of the accompanying C source.
I've been using a TST for name-lookup in the past 3 versions of my postscript interpreter. The only changes that have been needed have been due to changes in memory management. Here's a version I modified (lightly) to use explicit pointers. I use yet another version in my postscript interpreter, any of the xpost2*.zip versions, in the file core.c, which uses byte-offsets for pointers (have to be added to the user-memory byte-pointer to yield a real pointer).
获得的速度可能会很小,但您可以散列命令以将其转换为数字,然后使用 switch 语句。比哈希图更快。
Speed gained will probably be minimal, but you could hash the command to convert it to a number and then use a switch statement. Faster than a hash map.