473,782 Members | 2,699 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is this the correct code for a bitset map

Hi,

Is the code below the best way to have the less than function for an 80 bit
bitset or is there something faster / better?

When I populate this map with millions (... and millions) of sets it gets
slower and slower, I'm looking to make it faster.

Thanks
typedef bitset<80> myBitSet;

struct ci_less : binary_function <myBitSet, myBitSet, bool>
{
bool operator() (const myBitSet & s1, const myBitSet & s2) const
{
for(int x= 79; x >= 0; x--)
{
if(s1[x] != s2[x])
return s1[x] < s2[x];
}
}
}; // end of ci_less

typedef map<myBitSet, int,ci_less> Results_Map;

Aug 18 '05 #1
5 4375
Rich S. wrote:
Hi,

Is the code below the best way to have the less than function for an 80 bit
bitset or is there something faster / better?

When I populate this map with millions (... and millions) of sets it gets
slower and slower, I'm looking to make it faster.

Thanks
typedef bitset<80> myBitSet;

struct ci_less : binary_function <myBitSet, myBitSet, bool>
{
bool operator() (const myBitSet & s1, const myBitSet & s2) const
{
for(int x= 79; x >= 0; x--)
{
if(s1[x] != s2[x])
return s1[x] < s2[x];
}
}
}; // end of ci_less

typedef map<myBitSet, int,ci_less> Results_Map;


I did not see a method in the bitset interface to let you compare the
bits in groups larger than a single bit. There may be vendor-specific
extensions to the bitset class that you are using that offer such
access.

You may wish to consider using a vector<bool> instead of a bitset since
it has the same compact storage as a bitset, but defines the less than
operator (<) to compare two vectors lexicographical ly (which is the
type comparison the routine above performs).

Since a vector<bool> would no doubt compare the bits as words, it
should run up to 32 times faster than your current comparison routine.

Greg

Aug 18 '05 #2

"Greg" <gr****@pacbell .net> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
Rich S. wrote:
Hi,

Is the code below the best way to have the less than function for an 80
bit
bitset or is there something faster / better?

When I populate this map with millions (... and millions) of sets it gets
slower and slower, I'm looking to make it faster.

Thanks
typedef bitset<80> myBitSet;

struct ci_less : binary_function <myBitSet, myBitSet, bool>
{
bool operator() (const myBitSet & s1, const myBitSet & s2) const
{
for(int x= 79; x >= 0; x--)
{
if(s1[x] != s2[x])
return s1[x] < s2[x];
}
}
}; // end of ci_less

typedef map<myBitSet, int,ci_less> Results_Map;


I did not see a method in the bitset interface to let you compare the
bits in groups larger than a single bit. There may be vendor-specific
extensions to the bitset class that you are using that offer such
access.

You may wish to consider using a vector<bool> instead of a bitset since
it has the same compact storage as a bitset, but defines the less than
operator (<) to compare two vectors lexicographical ly (which is the
type comparison the routine above performs).

Since a vector<bool> would no doubt compare the bits as words, it
should run up to 32 times faster than your current comparison routine.

Greg


Can you show me how that would work in this case. I have patterns like
1101010101010 etc etc etc, that I use the bitset to represent and toss in
the map to calculate how many I have of each.
How would vector<bool> work like that?

Thanks for your time.

Aug 18 '05 #3
Rich S. wrote:
"Greg" <gr****@pacbell .net> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
Rich S. wrote:
Hi,

Is the code below the best way to have the less than function for an 80
bit
bitset or is there something faster / better?

When I populate this map with millions (... and millions) of sets it gets
slower and slower, I'm looking to make it faster.

Thanks
typedef bitset<80> myBitSet;

struct ci_less : binary_function <myBitSet, myBitSet, bool>
{
bool operator() (const myBitSet & s1, const myBitSet & s2) const
{
for(int x= 79; x >= 0; x--)
{
if(s1[x] != s2[x])
return s1[x] < s2[x];
}
}
}; // end of ci_less

typedef map<myBitSet, int,ci_less> Results_Map;


I did not see a method in the bitset interface to let you compare the
bits in groups larger than a single bit. There may be vendor-specific
extensions to the bitset class that you are using that offer such
access.

You may wish to consider using a vector<bool> instead of a bitset since
it has the same compact storage as a bitset, but defines the less than
operator (<) to compare two vectors lexicographical ly (which is the
type comparison the routine above performs).

Since a vector<bool> would no doubt compare the bits as words, it
should run up to 32 times faster than your current comparison routine.

Greg


Can you show me how that would work in this case. I have patterns like
1101010101010 etc etc etc, that I use the bitset to represent and toss in
the map to calculate how many I have of each.
How would vector<bool> work like that?

Thanks for your time.

Since I'm not quite sure from your question whether the bit vector is
to be initialized from a character array of ASCII 1's and 0's, a
character array with 1 and 0 values, or as an 80-bit long bit field
packed as integer values, I will provide examples for each one of those
cases.

First, declare and size the std::vector<boo l> instance:

std::vector<boo l> s1(80);

// Example 1
// initializer is an 80 character array of ASCII '1' and '0'
// characters

const char binaryData[80] = "010101010101.. .01010101010101 ";

for (i = 0; i < sizeof(binaryDa ta); i++)
s1.push_back( binaryData[i] == '1' );
// Example 2
// initializer is an 80 element array of 1's and 0's

const char binaryData[80] = { 0, 1, 0, 1, 0, ... 1, 1, 0};

for (i = 0; i < sizeof(binaryDa ta); i++)
s1.push_back( binaryData[i] );

// Example 3
// initializer is s packed 80 bit bitfield declared as an array of
// four 32-bit unsigned integers.
// The code below uses the binary integer format which not all
// compilers recognize. Of course hexadecimal, decimal or octal
// notation to specify each of the 32 bit integer values would
// work just as well.

const u_int32_t binaryData[4] = { 0b0010011001001 1...1001100110} ;

for (u_int32_t i = 0; i < 4; i++)
for (u_int32_t j = (1 << 31); j != 0; j >>= 1)
s1.push_back( binaryData[i] & j );

Once the vector<bool> object has been initialized just use it as the
key in the std::map<std::v ector<bool>, int>. Note that no custom
comparision routine for the vector<bool> needs to be written in order
for the std::map to sort its elements. std::vector<boo l> already
defines a less than comparison function.

I was too lazy to compile these examples myself. I did try and be
careful in writing them. Hopefully they are good enough to at least
point you in the right direction.

Greg

Aug 19 '05 #4

Greg wrote:
Rich S. wrote:
"Greg" <gr****@pacbell .net> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
Rich S. wrote:
> Hi,
>
> Is the code below the best way to have the less than function for an 80
> bit
> bitset or is there something faster / better?
>
> When I populate this map with millions (... and millions) of sets it gets
> slower and slower, I'm looking to make it faster.
>
> Thanks
>
>
> typedef bitset<80> myBitSet;
>
> struct ci_less : binary_function <myBitSet, myBitSet, bool>
> {
> bool operator() (const myBitSet & s1, const myBitSet & s2) const
> {
> for(int x= 79; x >= 0; x--)
> {
> if(s1[x] != s2[x])
> return s1[x] < s2[x];
> }
> }
> }; // end of ci_less
>
> typedef map<myBitSet, int,ci_less> Results_Map;

I did not see a method in the bitset interface to let you compare the
bits in groups larger than a single bit. There may be vendor-specific
extensions to the bitset class that you are using that offer such
access.

You may wish to consider using a vector<bool> instead of a bitset since
it has the same compact storage as a bitset, but defines the less than
operator (<) to compare two vectors lexicographical ly (which is the
type comparison the routine above performs).

Since a vector<bool> would no doubt compare the bits as words, it
should run up to 32 times faster than your current comparison routine.

Greg


Can you show me how that would work in this case. I have patterns like
1101010101010 etc etc etc, that I use the bitset to represent and toss in
the map to calculate how many I have of each.
How would vector<bool> work like that?

Thanks for your time.

Since I'm not quite sure from your question whether the bit vector is
to be initialized from a character array of ASCII 1's and 0's, a
character array with 1 and 0 values, or as an 80-bit long bit field
packed as integer values, I will provide examples for each one of those
cases.

First, declare and size the std::vector<boo l> instance:

std::vector<boo l> s1(80);

// Example 1
// initializer is an 80 character array of ASCII '1' and '0'
// characters

const char binaryData[80] = "010101010101.. .01010101010101 ";

for (i = 0; i < sizeof(binaryDa ta); i++)
s1.push_back( binaryData[i] == '1' );
// Example 2
// initializer is an 80 element array of 1's and 0's

const char binaryData[80] = { 0, 1, 0, 1, 0, ... 1, 1, 0};

for (i = 0; i < sizeof(binaryDa ta); i++)
s1.push_back( binaryData[i] );

// Example 3
// initializer is s packed 80 bit bitfield declared as an array of
// four 32-bit unsigned integers.
// The code below uses the binary integer format which not all
// compilers recognize. Of course hexadecimal, decimal or octal
// notation to specify each of the 32 bit integer values would
// work just as well.

const u_int32_t binaryData[4] = { 0b0010011001001 1...1001100110} ;

for (u_int32_t i = 0; i < 4; i++)
for (u_int32_t j = (1 << 31); j != 0; j >>= 1)
s1.push_back( binaryData[i] & j );

Once the vector<bool> object has been initialized just use it as the
key in the std::map<std::v ector<bool>, int>. Note that no custom
comparision routine for the vector<bool> needs to be written in order
for the std::map to sort its elements. std::vector<boo l> already
defines a less than comparison function.

I was too lazy to compile these examples myself. I did try and be
careful in writing them. Hopefully they are good enough to at least
point you in the right direction.

Greg


Of course just after I post I find my first mistake. Change Example 3
to declare an array of 5 unsigned 16-bit shorts and change the loop to
match. Four 32 bit integers has far too many bits.

Greg

Aug 19 '05 #5

"Greg" <gr****@pacbell .net> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .

Greg wrote:
Rich S. wrote:
> "Greg" <gr****@pacbell .net> wrote in message
> news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
> > Rich S. wrote:
> >> Hi,
> >>
> >> Is the code below the best way to have the less than function for an
> >> 80
> >> bit
> >> bitset or is there something faster / better?
> >>
> >> When I populate this map with millions (... and millions) of sets it
> >> gets
> >> slower and slower, I'm looking to make it faster.
> >>
> >> Thanks
> >>
> >>
> >> typedef bitset<80> myBitSet;
> >>
> >> struct ci_less : binary_function <myBitSet, myBitSet, bool>
> >> {
> >> bool operator() (const myBitSet & s1, const myBitSet & s2) const
> >> {
> >> for(int x= 79; x >= 0; x--)
> >> {
> >> if(s1[x] != s2[x])
> >> return s1[x] < s2[x];
> >> }
> >> }
> >> }; // end of ci_less
> >>
> >> typedef map<myBitSet, int,ci_less> Results_Map;
> >
> > I did not see a method in the bitset interface to let you compare the
> > bits in groups larger than a single bit. There may be vendor-specific
> > extensions to the bitset class that you are using that offer such
> > access.
> >
> > You may wish to consider using a vector<bool> instead of a bitset
> > since
> > it has the same compact storage as a bitset, but defines the less
> > than
> > operator (<) to compare two vectors lexicographical ly (which is the
> > type comparison the routine above performs).
> >
> > Since a vector<bool> would no doubt compare the bits as words, it
> > should run up to 32 times faster than your current comparison
> > routine.
> >
> > Greg
> >
>
> Can you show me how that would work in this case. I have patterns like
> 1101010101010 etc etc etc, that I use the bitset to represent and toss
> in
> the map to calculate how many I have of each.
> How would vector<bool> work like that?
>
> Thanks for your time.

Since I'm not quite sure from your question whether the bit vector is
to be initialized from a character array of ASCII 1's and 0's, a
character array with 1 and 0 values, or as an 80-bit long bit field
packed as integer values, I will provide examples for each one of those
cases.

First, declare and size the std::vector<boo l> instance:

std::vector<boo l> s1(80);

// Example 1
// initializer is an 80 character array of ASCII '1' and '0'
// characters

const char binaryData[80] = "010101010101.. .01010101010101 ";

for (i = 0; i < sizeof(binaryDa ta); i++)
s1.push_back( binaryData[i] == '1' );
// Example 2
// initializer is an 80 element array of 1's and 0's

const char binaryData[80] = { 0, 1, 0, 1, 0, ... 1, 1, 0};

for (i = 0; i < sizeof(binaryDa ta); i++)
s1.push_back( binaryData[i] );

// Example 3
// initializer is s packed 80 bit bitfield declared as an array of
// four 32-bit unsigned integers.
// The code below uses the binary integer format which not all
// compilers recognize. Of course hexadecimal, decimal or octal
// notation to specify each of the 32 bit integer values would
// work just as well.

const u_int32_t binaryData[4] = { 0b0010011001001 1...1001100110} ;

for (u_int32_t i = 0; i < 4; i++)
for (u_int32_t j = (1 << 31); j != 0; j >>= 1)
s1.push_back( binaryData[i] & j );

Once the vector<bool> object has been initialized just use it as the
key in the std::map<std::v ector<bool>, int>. Note that no custom
comparision routine for the vector<bool> needs to be written in order
for the std::map to sort its elements. std::vector<boo l> already
defines a less than comparison function.

I was too lazy to compile these examples myself. I did try and be
careful in writing them. Hopefully they are good enough to at least
point you in the right direction.

Greg


Of course just after I post I find my first mistake. Change Example 3
to declare an array of 5 unsigned 16-bit shorts and change the loop to
match. Four 32 bit integers has far too many bits.

Greg


Thanks for the samples. I'll try them out and see what works best for me.

Thanks again
Aug 22 '05 #6

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

Similar topics

6
2331
by: Hunter Hou | last post by:
Hello, I have this very simple program, but it can't be compiled. What's wrong here? thanks, hunter #include <iostream>
2
5383
by: shaun roe | last post by:
As a follow up to my question about STL and bitset<64> I'd like to share a quirk (bug?) about unsigned long long support and the bitset. I'm using gcc 3.2 on linux or gcc 3.3 on mac, the answer is the same. unsigned long long int f; f=3; bitset<64> g(f); cout << f << endl; cout << g << endl; f<<=32;
5
5505
by: SpOiLeR | last post by:
Hi. q1: Is std::bitset<N> part of standard or it's compiler extension? q2: Is std::bitset::to_string() part of standard? q3: My documentation say this about std::bitset::to_string(): "...each character is 1 if the corresponding bit is set, and 0 if it is not. In general, character position i corresponds to bit position N - 1 -
2
7341
by: Michal Wyrebski | last post by:
Hello, I was thinking how is the fastest and the best way of passing bitset variable to some function. Only one method came to my mind. It's about passing converted value: #v+ void func(unsigned long UL) { bitset<8> B(UL); cout << "B: " << B << endl;
7
2244
by: Shapper | last post by:
Hello, When creating an ASP.NET/VB web site I place my code in a code behind page, i.e., aspx.vb. Sometimes I have functions which are common to all aspx files. Is it possible in page1.aspx to use page1.aspx.vb and also a common file named global.aspx.vb which will hold all the functions common to all aspx files in a web site.
19
2372
by: felixnielsen | last post by:
Some might remember that i, not so long ago, started a treath or two about a weird 3d labyrinth. I now have a working code, that i want to share, hear comments, advice, ect., but first let me explain what its all about. The whole labyrinth is a cubic in its self and it contains x^3 cubic rooms. The labyrinth is infinite/finite, it has no borders, but still have a size. fx. if the size of the labytrint is 2^3 and you find yourself at
4
2496
by: Sarath | last post by:
>From the documentation of MSDN, it is saying that bitset is not a STL container Unlike the similar vector<boolClass, the bitset class does not have iterators and is not an Standard Template Library container. Actaully what's so special in STL containers? What I know is that there will be certain operators overloaded, supports template meta programming, having iterators, compatible with
5
2132
by: arnuld | last post by:
i have solved the problem. any comments on it: /* C++ Primer 4/e * chapter 3 * * exercise 3.24 * STATEMENT: * consider the sequence 1,2,3,5,13,21. Initialize a "bitset<32>" object that has a one bit in each position corresponding to a number in this sequence. */
5
2346
by: swcamry | last post by:
class bitset::reference { friend class bitset; reference(); // no public constructor public: ~reference(); operator bool () const; // convert to bool reference& operator= ( bool x ); // assign from bool reference& operator= ( const reference& x ); // assign from bit reference& flip(); // flip bit value
0
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10308
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10143
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10076
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6729
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5507
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4040
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2870
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.