Implement abstract data types of a set using bitvectors

Source: Internet
Author: User
Tags bitwise operators

# Include <assert. h>
# Include <iostream>
Using namespace STD;

Const int defaultsize = 100;

Class Set
{
Public:
Set (INT maxsize = defaultsize); // Constructor
~ Set () {Delete [] bitvector;} // destructor
Void makeempty () // empty set
{
For (INT I = 0; I <maxsize; I ++)
{
Bitvector [I] = 0;
}
}
Bool addmember (const int X); // Add new member X
Bool delmember (const int X); // deletes the old member X
Set & operator = (set & right); // The set right value is assigned to the set this
Set & operator + (set & right); // union of set right and set this
Set & operator-(set & right); // difference between set right and set this
Set & operator * (set & right); // intersection of set right and set this
Bool operator = (set & right); // determines whether the set this is equal to the set right
Bool contains (const int X); // determines whether X is a member of the set this.
Bool subset (set & right); // determines whether the set this is a subset of the set right.
PRIVATE:
Int * bitvector; // stores the bitvector and element of the set.
Int maxsize; // vector size
};

Set: Set (INT maxsetsize): maxsize (maxsetsize)
{
Assert (maxsize> 0); // check the parameter Rationality
Bitvector = new int [maxsize]; // assign an integer array to the bit vector
Assert (bitvector! = 0); // check whether the storage allocation is successful
Makeempty (); // empty set for initialization
}

// Add X to the set this, that is, to set the position X of the bitvector to 1 (the range of X must be between 0 and maxsize)
// If the first X position is already 1, false is returned; otherwise, true is returned.
Bool set: addmember (const int X)
{
Assert (x> = 0 & x <maxsize); // check the rationality of X
If (! Bitvector [x])
{
Bitvector [x] = 1;
Return true;
}
Return false;
}

Bool set: delmember (const int X)
{
// Delete X from the set this
Assert (x> = 0 & x <= maxsize); // judge the rationality of element x
If (bitvector [x])
{
Bitvector = 0;
Return true;
}
Return false;
}

Set & set: Operator = (set & right)
{
// Assign the set right value to the set this
Assert (maxsize = right. maxsize); // determines whether the two sets are of the same size.
For (INT I = 0; I <maxsize; I ++)
{
Bitvector [I] = right. bitvector [I];
}
Return * this;
}

Set & set: Operator + (set & right)
{
// Union of set right and set this
Assert (maxsize = right. maxsize); // determines whether the two sets are of the same size.
For (INT I = 0; I <maxsize; I ++)
{
Bitvector [I] = bitvector [I] | right. bitvector [I]; // calculate "or" by bit"
}
Return * this;
}

Set & set: Operator-(set & right)
{
// Difference between set right and set this
Assert (maxsize = right. maxsize); // determines whether the two sets are of the same size.
For (INT I = 0; I <maxsize; I ++)
{
Bitvector [I] = bitvector [I] &! Right. bitvector [I]; // calculate the bitwise XOR"
}
Return * this;
}

Set & set: Operator * (set & right)
{
// Intersection of set right and set this
Assert (maxsize = right. maxsize); // determines whether the two sets are of the same size.
For (INT I = 0; I <maxsize; I ++)
Bitvector [I] = bitvector [I] & right. bitvector [I]; // bitvector and"
Return * this;
}

Bool set: Operator = (set & right)
{
// Determine whether the set this is equal to the set right
Assert (maxsize = right. maxsize); // determines whether the two sets are of the same size.
For (INT I = 0; I <maxsize; I ++)
If (bitvector [I]! = Right. bitvector [I])
Return false;
Return true;
}

Bool set: Contains (const int X)
{
// Determine whether X is a member of the set this.
Assert (x> = 0 & x <maxsize); // check the rationality of X
Return bitvector [x]! = 0;
}

Bool set: subset (set & right)
{
// Determine whether the set this is a subset of the set right.
Assert (maxsize = right. maxsize); // determines whether the two sets are of the same size.
For (INT I = 0; I <maxsize; I ++)
{
If (bitvector [I] &! Right. bitvector [I])
Return false;
}
Return true;
}

Int main ()
{
Set a (10), B (10 );
A. addmember (4 );
B. addmember (4 );
If (A = B) cout <"A = B" <Endl;
Else cout <"! = B "<Endl;
Getchar ();
Return 0;
}





Bit operations:

Bitwise operators can process individual bits of a certain number. They only target bits, and their thoughts are the same as those of &, |
& (And operations)
| (Or operation)
^ (XOR Operation)
~ (A non-operation, also called a complement operator, is an inverse operation)
Except ~, Others can be used with "=", for example, & =,

Shift Operator
<Move the left value to the left by bit,> move the left value to the right by bit
Remember the following formula.
A <X, [moving X places to the Left] can be considered as a = A * (2 ^ X) A multiplied by the X power of 2
A> X, [move x places to the right] can be seen as a = A/(2 ^ X) A divided by the X power of 2



Bitvector should refer to <bitset>. <vector> although the original English meaning is "vector", it is generally called a dynamic array in the standard library.


here "bit" refers to the data bit, it is not a digit.
in C/C ++, char is 8 bits (1 byte), short is 16 bits (2 bytes ), int, long, and float are
32-bit (4 bytes), double is 64-bit (8 bytes), and long double is 80-bit (10 bytes ).
for example, 2 is binary 10, 2 bits, 4 is binary 65535, 3 bits, and 1111 is binary 1111 1111 1111,
16 bits. The maximum value of an unsigned integer is 4,294,967,295, And the binary value is exactly 32 characters.
1 <27 (1 shifted to 27 places) = 1 multiplied by (2 to the power of 27) = 134 217 728
quiz1 | = 1 <27 → quizl = quizl | (1
<27) → quizl = 0 | 134 217 728 =
134 217 728

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.