实现一个固定大小的哈希映射,它将字符串键与任意数据对象引用相关联 [英] Implement a fixed-size hash map that associates string keys with arbitrary data object references

查看:117
本文介绍了实现一个固定大小的哈希映射,它将字符串键与任意数据对象引用相关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何解决这个问题?



问题

仅使用基本类型,实现一个固定大小的哈希映射关联具有任意数据对象引用的字符串键(您不需要复制该对象)。您的数据结构应针对算法运行时和内存使用情况进行优化。您不应该导入任何外部库,也不能在Python或Ruby等语言中使用原始哈希映射或字典类型。



解决方案应该在一个类中提供(或者您的语言相当于)提供以下功能:



构造函数(大小):返回具有给定数量对象的预分配空间的类的实例。

boolean set(key,value):将给定的键/值对存储在哈希映射中。返回一个布尔值,表示操作成功/失败。

get(key):返回与给定键关联的值,如果没有设置值,则返回null。

delete(key):删除与给定键关联的值,返回成功时的值;如果键没有值,则返回null。

float load():返回表示加载因子的浮点值( `(哈希映射中的项)/(哈希映射的大小)`)数据结构。由于dat结构的大小是固定的,因此不应大于1.

如果您的语言为字符串提供内置散列函数(例如Java中的`hashCode`或`__hash__`在Python中)欢迎您使用它。如果没有,欢迎您做一些天真的事情,或者使用您在网上找到的正确归属的东西。



说明

请提供资料来源,测试,可运行的命令行函数以及编译所需的所有资源(如有必要)并运行以下程序。您可以自由使用在* nix操作系统上编译/运行的任何编码语言,并且不需要许可软件。

Does anyone know how to solve this?

Problem
Using only primitive types, implement a fixed-size hash map that associates string keys with arbitrary data object references (you don't need to copy the object). Your data structure should be optimized for algorithmic runtime and memory usage. You should not import any external libraries, and may not use primitive hash map or dictionary types in languages like Python or Ruby.

The solution should be delivered in one class (or your language's equivalent) that provides the following functions:

constructor(size): return an instance of the class with pre-allocated space for the given number of objects.
boolean set(key, value): stores the given key/value pair in the hash map. Returns a boolean value indicating success / failure of the operation.
get(key): return the value associated with the given key, or null if no value is set.
delete(key): delete the value associated with the given key, returning the value on success or null if the key has no value.
float load(): return a float value representing the load factor (`(items in hash map)/(size of hash map)`) of the data structure. Since the size of the dat structure is fixed, this should never be greater than 1.
If your language provides a built-in hashing function for strings (ex. `hashCode` in Java or `__hash__` in Python) you are welcome to use that. If not, you are welcome to do something naive, or use something you find online with proper attribution.

Instructions
Please provide the source, tests, runnable command-line function and all the resources required to compile (if necessary) and run the following program. You are free to use any coding language that compiles/runs on *nix operating systems and requires no licensed software.

推荐答案

是的,这里有很多人知道怎么解决这个!但这是你的作业,只要给你答案我们就不会帮你。查看你的课程笔记,然后自己开始学习。



如果你决定开始工作并陷入困境,请展示你的内容以及你的具体问题。然后我想你会在这里得到很多帮助。最后你会发现它并不那么难。



如果你讨厌甚至试图做这个功课,我想编程不适合你。你可能最终会尝试不同的课程。所以,这取决于你。祝你好运。
Yes, there are quite some people around here who know how to solve this! But this is YOUR homework and we wouldn't do you a favor by just giving you the answer. Look into your course notes and start working on it yourself.

If you decide to start working and get stuck, present what you have and what your specific problem is. Then I think you will receive plenty of help here. And in the end you will find that it wasn't that hard at all.

If you hate even trying to do this homework, I guess programming is not for you. You might in the end be better off trying a different course. So, it's up to you. Good luck anyway.


这篇关于实现一个固定大小的哈希映射,它将字符串键与任意数据对象引用相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆