473,473 Members | 1,822 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Alternative to a very large array?

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?

Mar 16 '06 #1
10 3024
cb****@my-deja.com wrote:
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?


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...
Mar 16 '06 #2
cb****@my-deja.com wrote:
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?


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

Mar 16 '06 #3
cb****@my-deja.com wrote:
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.


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

-- peter

Mar 16 '06 #4

cb****@my-deja.com wrote:
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?


I wonder if rather than keep track of whether a given label
is in use you keep note of two things

1) A std::set of labels that have been released and are available for
reuse.

2) A counter of the value of a label to be given out if there are no
available
labels in the set described in set 1.

Or you could just be a spendthrift and not reuse labels. Unless
your program generates an unspeakable number of labels, 64 bits
is likely to last you a lifetime... And if you generate an unspeakable
number of labels, unless any transaction might last the time needed
to wrap around a 64 bit counter, wrapping around would be fine...

-Daivd

Mar 16 '06 #5
Thanks everyone. Looks like a set will do fine for now.

Mar 16 '06 #6
David Resnick wrote:
cb****@my-deja.com wrote:
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.)


<snip>

1) A std::set of labels that have been released and are available for
reuse.


Presumably a set of 2^64 elements will be at least as big as an array 2^64?

A set with the used values would likely be more appropriate. In fact, a
map of used labels and transaction pointers would likely be very useful.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 16 '06 #7

Ben Pope wrote:
David Resnick wrote:
cb****@my-deja.com wrote:
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.)


<snip>

1) A std::set of labels that have been released and are available for
reuse.


Presumably a set of 2^64 elements will be at least as big as an array 2^64?

A set with the used values would likely be more appropriate. In fact, a
map of used labels and transaction pointers would likely be very useful.


I guess I wasn't clear, that was what I meant "that have been released"
and
"reuse". The set would start empty, the counter at 1. As labels are
released, they'd be added to the set and available for reuse.

-David

Mar 16 '06 #8
David Resnick wrote:
Ben Pope wrote:
David Resnick wrote:
cb****@my-deja.com wrote:
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.)
<snip>

1) A std::set of labels that have been released and are available for
reuse.

Presumably a set of 2^64 elements will be at least as big as an array 2^64?

A set with the used values would likely be more appropriate. In fact, a
map of used labels and transaction pointers would likely be very useful.


I guess I wasn't clear, that was what I meant "that have been released"
and "reuse". The set would start empty, the counter at 1. As labels are
released, they'd be added to the set and available for reuse.


So you can choose from the released labels. What about the initial
state when the set is empty? I'm also unclear as to what your counter
does, does it keep track of the greatest label value so far? OK, I
re-read your original post, I didn't understand point 2 initially.

So when finding a new label, you check the set, if not empty, grab a
value (assign it's value to the label and remove it from the set), if
set is empty, assign count++. When a transaction finishes, it's label
is added to the set.

What happens if count++ reaches it's maximum value? I guess that's not
likely unless you have 2^64 simultaneous transactions at any point in
time. OK, so as a side effect, the counter also gives you the maximum
number of simultaneous transactions to date.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 16 '06 #9
[...]
But the label is a 64 bit value ranging from 0 to 0xffffffff. What's


64 bit = 8 byte
0 to 0xFFFFFFFFFFFFFFFF

regards
Mar 16 '06 #10
cb****@my-deja.com wrote:
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?

Why do you need to keep track of already used numbers? Look at the
maximum number that can be stored in your 64 bit value. You could
just increment a counter and forget about ever reusing a number.

Take 2^64 and divide it by the number of seconds in a year to get an
idea of many transcation numbers are contained in that number.

Mar 17 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: valued customer | last post by:
Is there a more concise way to do something like the the desired code below? The gripe is with the try-catch syntax. It takes *way* too many lines of code to evaluate a conditional expression...
1
by: Mike | last post by:
My users have to select an value from a fixed selection of values. The obvious choice of control for such a requirement is to use a <select> (i.e. a combo box). My problem is that sometimes,...
8
by: btober | last post by:
MY version 7.3 setup uses the default cluster data directory of /var/lib/pgsql/data and I'm aware that one suggested alternative is /usr/local/pgsql/data In my reading of server setup...
18
by: Ramprasad A Padmanabhan | last post by:
In my program I am checking wether a emailid exists in a list I have in the complete_list a string like "<aa@abc.com> <ab@abc.com> <bc@abc.com> ... <xy@abc.com>" that is a string of emailids...
20
by: Sushil | last post by:
Hi gurus I was reading FAQ "alloca cannot be written portably, and is difficult to implement on machines without a conventional stack." I understand that the standard does not mandate...
43
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's...
2
by: Tristan | last post by:
Hi, My code reads through a file of shapes adding each one to an ArrayList as it does so. The problem is that for files with large numbers of shapes, calling ArrayList.Add(MyShape) is...
6
by: Doug | last post by:
I have data that looks something like this when returned from a stored procedure (3 columns, X rows): Key1 Name1 Value1 Key1 Name2 Value2 Key1 Name3 Value3 Key1 Name4 ...
7
by: ultr | last post by:
I need a large 3D array of structures: struct s { char a; int b; }; s s_array; s_array is declared as global.
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.