473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Stuff a pair of size_t into an uint32_t

Hi,

I apologise for starting a new thread on a partially answered question.
My previous problem has gotten more serious :)

So I have a function defined as follows:

bool observe(const WordID* dataword);

where WordID is

typedef uint32_t WordID;
My purpose is to use this function with some different data I already
have: the problem is, this data is NOT in the WordID format.

What I have is *pairs* of values, that I would like to input together
into observe(). In other words, I have pairs of numbers whose type is size_t

(size_t, size_t), (size_t, size_t), ... etc.

How can I input one of these pairs into observe()? I've heard that on
gcc size_t can be bigger than one uint32_t, so stuffing two into that
would definitely be a problem. What if I redefine the typedef such that
WordID is

typedef uint64_t WordID;

? And then maybe I could concatenate the two size_t and cast them into
that uint64_t? Would a uint64_t be enough? I was thinking about this:

observe(uint64_t* wordid);

and then

uint64_t chain(uint32_t one, uint32_t two) {
// get two 32bit rands and concatenate
uint64_t longrand = one;
longrand <<= 32;
longrand |= two;
return longrand;
}

The problem is this is a global typedef, used by many more function and
I'm not sure if this would harm something.

Any ideas would be greatly appreciated.

Cheers
Giuseppe
Jul 12 '08 #1
3 4207
Sam
Giuseppe: writes:
? And then maybe I could concatenate the two size_t and cast them into
that uint64_t? Would a uint64_t be enough? I was thinking about this:

observe(uint64_t* wordid);

and then

uint64_t chain(uint32_t one, uint32_t two) {
// get two 32bit rands and concatenate
uint64_t longrand = one;
longrand <<= 32;
longrand |= two;
return longrand;
}
Well, that might work, but what's going to happen is that a few years from
now your code will be the featured article on http://www.thedailywtf.com,
and everyone in the world will be laughing at it.

If you have a requirement that you must, somehow, process either a uint64_t
or a pair of size_t's, then you do exactly that, instead of trying to invent
some rogue mutation:

bool observe(uint64_t* wordid)
{
// …
}

bool observe(size_t word1, size_t word2)
{
// …
}

and factor out the common logic into a separate set of classes or functions,
that get invoked by the overloaded function code. Overloading, in C++, is
there precisely to address this situation.

There are many ways other to do this that are clean, and the resulting code
is readable and maintainable. Another way, that comes to mind, for example:

struct observe_params {
uint64_t *wordptr;
size_t word1;
size_t word2;
};

bool observe(uint64_t *wordptr)
{
struct observe_params parms;

parms.wordptr=wordptr;
parms.word1=0;
parms.word2=0;
return observe(parms);
}

bool observe(size_t word1, size_t word2)
{
struct observe_params parms;

parms.wordptr=NULL;
parms.word1=word1;
parms.word2=word2;
return observe(parms);
}

bool observe(struct observe_parms &parms)
{
// …
}

Then you supply the required logic here. If the wordptr class member is not
null (presuming that observe(int64_t *) never receives a null ptr), your
code knows that it received a uint64_t ptr. If the wordptr class member is
null, your code uses the two size_t parameters.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkh4vocACgkQx9p3GYHlUOJaDgCfSvtDUjJWCV nlQPKevp+F5vKv
l28Ani5mdNrO4SoZTtLACReeHG/Q4wA/
=digB
-----END PGP SIGNATURE-----

Jul 12 '08 #2
Sam wrote:
Then you supply the required logic here. If the wordptr class member is
not null (presuming that observe(int64_t *) never receives a null ptr),
your code knows that it received a uint64_t ptr. If the wordptr class
member is null, your code uses the two size_t parameters.
Thanks Sam, I guess I get the philosophy behind this now.

Only thing is, how would you go about creating a unique piece of data
from the two size_t?

In my program, while size_t word1 may appear several times, and size_t
word2 may appear several times, the combination of the two is unique.

Observe() is part of a Bloom filter, and what I'm trying to do is
storing (size_t 1, size_t 2) into the filter, and then later test the
filter for membership of the pair.

Could I, for instance, append one size_t to the other?

Regards
Giuseppe
Jul 12 '08 #3
Sam
Giuseppe: writes:
Sam wrote:
>Then you supply the required logic here. If the wordptr class member is
not null (presuming that observe(int64_t *) never receives a null ptr),
your code knows that it received a uint64_t ptr. If the wordptr class
member is null, your code uses the two size_t parameters.

Thanks Sam, I guess I get the philosophy behind this now.

Only thing is, how would you go about creating a unique piece of data
from the two size_t?
Define a class that contains these two objects, then proceed to overload all
the comparison operators for your class, as needed.

With most STL containers, you only need to overload operator<:

class twosizes {

public:
size_t a;
size_t b;

bool operator<(const twosizes &o) const
{
return a < o.a || (a == o.a && b < o.b);
}
};

This is good enough for all STL containers.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkh4+I0ACgkQx9p3GYHlUOL+OQCcDJmz4bw52S P1EFoCewdc0Vx1
R4EAnApI3XpfSr4oCSaFLszTqs/WBe+3
=My5B
-----END PGP SIGNATURE-----

Jul 12 '08 #4

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

Similar topics

4
by: Glutinous | last post by:
I've been studying this for hours, searching the www & usenet, and still can't figure out why 'each' returns an array of four key/value pairs, when it looks like just two pairs would suffice... ...
14
by: Neil Zanella | last post by:
Hello, I would like to ask how come the design of C++ includes std::pair. First of all I don't think many programmers would use it. For starters, what the first and second members are depends...
5
by: ma740988 | last post by:
For starters, Happy New Year to all!! I created a vector of pairs where pair first is a primitive and pair second is a vector of ints. So now: # include <iostream> # include <vector>
4
by: Florent Garcin | last post by:
Hello! I would like to use the map structure with a key of Pair<string, string> and an int as the value. Pair is defined as: template <class T1, class T2> class Pair {
12
by: Alex Vinokur | last post by:
Why was the size_t type defined in compilers in addition to unsigned int/long? When/why should one use size_t? Alex Vinokur email: alex DOT vinokur AT gmail DOT com...
7
by: meshko | last post by:
I'm just curious what people do with the code like this: size_t len = strlen(sz_message); write(fd, sz_message, len); on Windows where in write() the third argument to write is unsigned int now...
89
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the...
3
by: n.torrey.pines | last post by:
I'd like to be able to view two contiguous elements of a vector as a pair. Assuming I'm not accessing the last element, of course, and the element type is not bool, when is it safe to do so,...
10
by: Alex Vinokur | last post by:
Hi, Is it possible to do C++-casting from const pair<const unsigned char*, size_t>* to const pair<unsigned char*, size_t>* ? Alex Vinokur
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
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...

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.