Question

Motivation: I want to use bsearch (binary search) to quickly search through a sorted list of 121-bit non-negative integers (they all have exactly 121 bits, although they may have leading zeroes). These integers are too large to stored as individual ints, and so on, so I'm planning on making them mpz_t (using GMP).

Looking through the manual, GMP doesn't have a bsearch equivalent (although, correct me if I'm wrong), which leads me to:

Question: Can we use memcmp or something similar to compare two non-negative integers with an equal number of bits stored as mpz_t? If so, what is the correct syntax?

If this is possible, searching should be quite efficient.

I'm also open to alternative suggestions as to (a) data structures for storing these 121-bit integers that admit rapid searching in C++, (b) methods for searching through the integers that doesn't use memcmp.

Was it helpful?

Solution

If they're only 121 bit integers, why not use native 128 bit int extensions: http://gcc.gnu.org/onlinedocs/gcc/_005f_005fint128.html ? That should be much faster, as you can avoid any "costly" operations like memcpy, all of your comparisons should be one instruction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top