哪些数据结构可用于实现整数池?
这些整数可以是 IP 地址 (DHCP)、会话 ID 或隧道 ID(例如在 L2TP 中)。 每个整数都可以是空闲的或已使用的。我们需要它能够有效地找到免费的。
还定义了最小值和最大值。
These integers can be IP addresses (DHCP) or session IDs or tunnel IDs (in L2TP for example).
Each integer can be free or used. We need it to be efficient for finding free ones.
There's also a min and max defined.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,既然你有最大值和最小值,我有以下想法:
您可以动态维护这个最大值或最小值,并拥有一个可用整数列表。
首先,您从一个空列表和完整范围开始。
当有人租用一个整数时,如果列表为空(如果我们不从列表中获取),则范围的大小会减一。
如果他释放了他的整数,则有两种可能性:
这应该使您有可能保持还具有低成本的高和低自由整数计数。
当然,您也可以尝试保存多个范围以将整数聚集在一起,但这需要更复杂的操作。
Ok as you have a maximum and minimum I have following Idea:
You maintain this maximum or minimum dynamically and have a list of free integers.
At first you start with an empty list and the full range.
When someone leases an integer the range decreases in size by one if the list is empty if not we take from the list.
If he releases his integer there are 2 possibilities:
This should give you the possibility to maintain also high and low free integer count with low cost.
Of course you could also try to save several ranges to cluster the integers together but that would need more complicated operations.
我会保留一个空闲列表和一个已用列表。分配一个数字意味着将其从空闲列表移至已用列表,反之则释放。
维护列表会产生成本,但找到免费号码会很快
I would keep a free list and a used list. Allocating a number would mean moving it from free list to the used list, and deallocation the reverse.
There would be a cost to maintaining the lists, but finding a free number would be fast
您希望有更多的空闲整数还是更多使用的整数?
您想同时保存 IP、SessIds 和 TunIds 还是分别保存其他的?
对我来说,最平衡的是一棵树,但正如你所知,如果没有频繁的更改,数组的最大大小就足够了。
当您不关心某些顺序时,动态列表是最好的选择。
Do you expect to have more free or more used integers?
And do you want to save IPs, SessIds and TunIds at the same time or each excluding the others?
For me the most balanced would be a tree but as you know about a maximal size an array could be sufficient if there are no frequent changes.
When you dont care for some order then dynamic lists would be best.