替代一个非常大的阵列? [英] Alternative to a very large array?

查看:58
本文介绍了替代一个非常大的阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是unsigned long long。作为标识我的程序中发生的各种

交易的标签。


标签在交易的生命周期内保留,然后

发布可重复使用。在任何

时间内可能会发生许多交易。我需要标记正在使用的标签,以便新标签

分配不会选择当前正在使用的标签。


如果可能的标签数量很小(比如100)然后我可以使用一个布尔数组(布尔标签[100])并将

对应的元素设置为true表示正在使用。


但标签是64位值,范围从0到0xffffffff。什么是
表示所有标签值的使用状态的最佳方式

而不必在大量阵列上浪费内存? (或许只是使用

内存来处理那些正在使用的值。)


我怀疑其中一个STL类可能是要走的路,但我是

不确定选择哪个。


有两种可能性。或者我只想要一个是/否答案

是否存在标签(即它是否在使用中),或者我也可能

想要存储一个字符串仅适用于那些正在使用的标签。


任何想法这两种可能性的最佳解决方案是什么?

I am using a "unsigned long long" as a label to identify various
transactions that are taking place in my program.

The label is reserved during the lifetime of the transaction and then
released for re-use. There may be many transactions taking place at any
time. I need to mark those labels which are in-use so that a new label
assignment does not choose one which is currently in-use.

If the number of possible labels was small (say 100) then I could
simply use an array of booleans (boolean labels[100]) and set the
corresponding element to true to denote in-use.

But the label is a 64 bit value ranging from 0 to 0xffffffff. What''s
the best way of representing the in-use state of all label values
without having to waste memory on a vast array? (Perhaps just using
memory for those values which are in-use.)

I suspect that one of the STL classes might be the way to go, but I''m
not sure which to choose.

There are two possibilities. Either I just want a yes/no answer as to
whether a label exists (i.e. whether it is in-use), or I might also
want to store a string for only those labels which are in-use.

Any ideas what is the best solution for these two possibilites?

推荐答案

cb****@my-deja.com 写道:
我使用的是unsigned long long。作为识别我的程序中发生的各种交易的标签。

标签在交易的生命周期内保留,然后发布以供重复使用。可能会在任何时间发生许多交易。我需要标记正在使用的标签,以便新标签
分配不会选择当前正在使用的标签。

如果可能的标签数量很少(比如说) 100)然后我可以简单地使用布尔数组(布尔标签[100])并将
对应元素设置为true以表示正在使用。

但标签是一个64位值,范围从0到0xffffffff。什么是表示所有标签值的使用状态的最佳方式
而不必在大量阵列上浪费内存? (或许只是使用
内存来处理那些正在使用的值。)

我怀疑其中一个STL类可能是要走的路,但我是
不确定选择哪个。

有两种可能性。或者我只想要一个是/否答案
是否存在标签(即它是否在使用中),或者我也可能想要仅为那些标签存储字符串 - 使用。

任何想法这两个可能性的最佳解决方案是什么?
I am using a "unsigned long long" as a label to identify various
transactions that are taking place in my program.

The label is reserved during the lifetime of the transaction and then
released for re-use. There may be many transactions taking place at any
time. I need to mark those labels which are in-use so that a new label
assignment does not choose one which is currently in-use.

If the number of possible labels was small (say 100) then I could
simply use an array of booleans (boolean labels[100]) and set the
corresponding element to true to denote in-use.

But the label is a 64 bit value ranging from 0 to 0xffffffff. What''s
the best way of representing the in-use state of all label values
without having to waste memory on a vast array? (Perhaps just using
memory for those values which are in-use.)

I suspect that one of the STL classes might be the way to go, but I''m
not sure which to choose.

There are two possibilities. Either I just want a yes/no answer as to
whether a label exists (i.e. whether it is in-use), or I might also
want to store a string for only those labels which are in-use.

Any ideas what is the best solution for these two possibilites?




听起来你需要一套或地图。一套可以存放标签,地图可以

将字符串附加到标签上。


Ben Pope

-

我不只是一个数字。对很多人来说,我被称为字符串...



Sounds like you need a set or map. A set can store the label, a map can
attach the string to the label.

Ben Pope
--
I''m not just a number. To many, I''m known as a string...


cb **** @ my-deja.com 写道:
我使用的是unsigned long long。作为识别我的程序中发生的各种交易的标签。

标签在交易的生命周期内保留,然后发布以供重复使用。可能会在任何时间发生许多交易。我需要标记正在使用的标签,以便新标签
分配不会选择当前正在使用的标签。

如果可能的标签数量很少(比如说) 100)然后我可以简单地使用布尔数组(布尔标签[100])并将
对应元素设置为true以表示正在使用。

但标签是一个64位值,范围从0到0xffffffff。什么是表示所有标签值的使用状态的最佳方式
而不必在大量阵列上浪费内存? (或许只是使用
内存来处理那些正在使用的值。)

我怀疑其中一个STL类可能是要走的路,但我是
不确定选择哪个。

有两种可能性。或者我只想要一个是/否答案
是否存在标签(即它是否在使用中),或者我也可能想要仅为那些标签存储字符串 - 使用。

任何想法这两个可能性的最佳解决方案是什么?
I am using a "unsigned long long" as a label to identify various
transactions that are taking place in my program.

The label is reserved during the lifetime of the transaction and then
released for re-use. There may be many transactions taking place at any
time. I need to mark those labels which are in-use so that a new label
assignment does not choose one which is currently in-use.

If the number of possible labels was small (say 100) then I could
simply use an array of booleans (boolean labels[100]) and set the
corresponding element to true to denote in-use.

But the label is a 64 bit value ranging from 0 to 0xffffffff. What''s
the best way of representing the in-use state of all label values
without having to waste memory on a vast array? (Perhaps just using
memory for those values which are in-use.)

I suspect that one of the STL classes might be the way to go, but I''m
not sure which to choose.

There are two possibilities. Either I just want a yes/no answer as to
whether a label exists (i.e. whether it is in-use), or I might also
want to store a string for only those labels which are in-use.

Any ideas what is the best solution for these two possibilites?




您可以使用某种动态分配的列表(例如, std :: list

或std :: set)来保存当前使用的标签的副本。那么当你需要一个新的标签时,只需要你的标签生成器选择一个(随机?),

确保它不在正在使用的列表中,并分配它。如果确实

出现在列表中,那么只需生成一个新标签,看看它是否在列表中是否为



干杯! --M



You could use some sort of dynamically allocated list (e.g., std::list
or std::set) to hold a copy of the currently used labels. Then when you
need a new label, just have your label generator pick one (randomly?),
make sure it''s not in the being-used list, and assign it. If it does
appear in the list, then just generate a new label and see if it''s in
the list.

Cheers! --M


cb **** @ my- deja.com 写道:
我使用的是unsigned long long。作为识别我的程序中发生的各种交易的标签。

标签在交易的生命周期内保留,然后发布以供重复使用。可能会在任何时间发生许多交易。我需要标记正在使用的标签,以便新标签
分配不会选择当前正在使用的标签。
...

我怀疑其中一个STL课程可能是要走的路,但我不确定选择哪个。

有两种可能性。或者我只想要一个是/否答案
是否存在标签(即它是否在使用中),或者我也可能想要仅为那些标签存储字符串 - 使用。

任何想法这两种可能性的最佳解决方案是什么?


std :: set为您提供了这个

应用程序的最佳开箱即用性能,如果考虑到内存使用情况。


它存储唯一键(您的标签)并为您提供对数复杂度

用于插入,删除和查找键的存在

线性内存需求成本。


i我不确定你的意思:

我也可能想要只为那些标签存储一个字符串正在使用。
I am using a "unsigned long long" as a label to identify various
transactions that are taking place in my program.

The label is reserved during the lifetime of the transaction and then
released for re-use. There may be many transactions taking place at any
time. I need to mark those labels which are in-use so that a new label
assignment does not choose one which is currently in-use.
...

I suspect that one of the STL classes might be the way to go, but I''m
not sure which to choose.

There are two possibilities. Either I just want a yes/no answer as to
whether a label exists (i.e. whether it is in-use), or I might also
want to store a string for only those labels which are in-use.

Any ideas what is the best solution for these two possibilites?
std::set gives you the best out-of-the-box performance for this
application if you are considered about memory usage.

it stores unique keys (your label) and gives you logarithmic complexity
for insertion, deletion and lookups for the existance of keys with
linear memory requirement cost.

i am not sure what you mean with:
I might also
want to store a string for only those labels which are in-use.




但是无论你的其他要求是什么,继承/包装std :: set

都应该给你一个良好的开端高效的实施。


- 彼得



but whatever your other requirements, inheriting/wrapping std::set
should give you a good start for a performant implementation.

-- peter


这篇关于替代一个非常大的阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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