Implement a set of bit vector - a data structure Xiaodong

Wang Xiaodong, a collection of data structures in the chapter, the set of things look very ignorant about recording with the bit vector.

N is a fixed integer when small, {1,2 ... N} is a subset of N If N = 10000, can be an array A [N] to indicate the presence of this set, in which case the size of the array A [ N], such as a [1] = 1 indicates a first set of elements present.

As the name suggests it is to use bit vector bits to store elements. Unsigned short type in a book, for example, is represented by the following US. US occupying 2 bytes, 16 bits, 16 represent a number of US can then use only this number N N / 16 th type represents the number of US. A case for the size of the array A [N / 16 + 1],

A [0] represents a number of 1-16 (0-15)

A [1] represents a number of 2-32 (16-31)

...

A [0] = 13, can be seen as 000000001011, A [0] in the first position, the second position, the fourth position of the present number, i.e., the presence of three numbers 0,2,4. 

If a number of input 30, first to determine which position in the array, 30/16 = 1, this time should be in the array A [1], the calculation need only A [1] in the position of 30% of 16 + 1 = 15 , it represents the 15th position.

The following analytical method to achieve a set of book Xiaodong:

A set of basic operations:

// and set operations: the calculation result set A and set B and set 
the Set SetUnion (the Set A, the Set B); 

// intersection calculation: the calculation result of the intersection of set A and set B 
Set SetIntersection (Set A, B sET); 

// assignment operator: B assigned to the set A 
void SetAssign (sET A, B sET); 

// determination calculation: return 1 else return is equal to 0 
int SetEqual (sET A, B sET); 

// member operator : x and a set of the same type, when x is S, 1 otherwise return 0 
int SetMember ( int x, the set sET ); 

// insert operation: the insertion with x set S, when x itself is S, is not changed collection 
void SetInsert ( int x, the set S); 

// delete operation: the elements of the set S x deleted, when x is not S, set without changing the 
void SetDelete ( int x, the set S);

Set a collection of definitions:

typedef struct  SET 
{ 
    int size; // can store element size
     // array size should look as type ushort should v is a number 16 size can save ARR / 16. 1 + || (size + 15) 
    int ArraySize;
     // each vector is capable of storing vectors 16 
    unsigned Short * V; 
} Bitset, * the set;

SetInit function is used to create a new Set:

InitSet SET ( int size) 
{ 
    SET _set = new new Bitset; 
    _set -> size = size;
     // a US type may indicate the size of the array 16 so size +. 1/16
     // book 4 in this movement to the right operation is equivalent to the way / 16 
    _set-> ArraySize = (size + 15 ) >> 4 ;
     // book size size v personally think that is wrong, there are different opinions of friends are welcome to explore 
    _set-> v = new new unsigned Short [_set- > ArraySize];
     for ( int I = 0 ; I <size; I ++ ) 
    { 
        _set -> V [I] = 0  ;
    }
    return _set;
}

SetAssign function for assignment:

//把B赋值给A
void SetAsign(Set A, Set B)
{
    if (A->size != B->size)
        return;
    for (int i = 0; i < A->size; i++)
    {
        A->v [i] = B->v[i];
    }
}

ArrayIndex find the location of the number in the array:

// calculate the size of the other type of position UShort size so as to be 16/16 bit words / 4 to 
int ArrayIndex ( int X) {
     return X >> 4 ; 
}

BitMask function to find the first of several locations in the US:

// compute the number of which is located in a first few ushort position modulo 16 in addition to the +1
 // input is binary 10 151 111 1010 & 1010 denotes a right shift calculated after 10 
unsigned Short BitMask ( int X) {
     return  . 1 << (X & 15 ); 
}

SetInsert Insert Function:

// bit inserting operation to find the array to find the position x% 16 placed by | OP
 @ inserted as 17, then the array v [1] as 00100000, showing a seventh position something, 17% 16 + 1 = 2 | operation after v [1] is 00100010 
void SetInsert ( int X, the Set S) {
     IF (X < 0 || S-> size < X)
         return ; 
    S -> V [ArrayIndex (X)] | = BitMask (X); 
}

SetDelte delete function:

// delete operation element to go get & negated
 // The 17 deleted, then the array v [1] as 00100010, position 2,7 represents something, 17% 16 + 1 = 2 is the inverse 1-2 & after calculation 11111101 v [1] is 00100000 
void SetDelete ( int X, the Set S) {
      IF (X < 0 || S-> size < X)
         return ; 
     S -> V [ArrayIndex (X)] = ~ & BitMask (X); 
}

SetMember function to see whether there are elements:

// detecting element exists 
int SetMember ( int x, the Set SET ) {
     IF (x < 0 || x> SET -> size)
         return  0 ;
     // first lookup array corresponding to the position opposite to the position of the x
     // claim 3 at this time, in the first position BitMask array 1000 represents a value of 4, the first one is 0 
    return  SET -> V [ArrayIndex (X)] & BitMask (X); 
}

SetEqual function to determine whether the sets A and B are equal:

int SetEqual(Set A,Set B){
    if(A->size! B->size)
        return 0;
    for (int i = 0; i < A->arraysize; i++)
    {
        if(A->v[i]! B->v[i])
            return 0;
    }
    return 1;
}

SetUnion and set operations:

// get and set as A = 1100 B = 0110 for the case of three, four and set 
the Set SetUnion (the Set A, the Set B) { 
    the Set S = InitSet (A-> size);
     for ( int I = 0 ; I <S-> ArraySize; I ++ ) 
    { 
        // bitwise | 
        S-> V [I] = A-> V [I] the B-> V [I]; 
    } 
}

SetIntersection intersection operator:

// on the intersection as A = 1100 B = 0100 => 0100 is an intersection of the third case 
the Set SetIntersection (the Set A, the Set B) { 
    the Set S = InitSet (A-> size);
     for ( int I = 0 ; I <S-> ArraySize; I ++ ) 
    { 
        // bitwise & 
        S-> V [I] = A-> V [I] the B-> V [I]; 
    } 
}

SetDifference difference set operation:

// taking the difference set as A = 111000 B = 001000 => & taken to re XOR 110000 001000 5.6 In this case the first set of the difference 
the Set SetDifference (the Set A, the Set B) { 
    the Set S = InitSet (A-> size) ;
     for ( int I = 0 ; I <S-> ArraySize; I ++ ) 
    { 
        // bitwise & again ^ 
        S-> V [I] = A-> V [I] (A-> V [I] ^ B -> V [I]); 
    } 
}

Guess you like

Origin www.cnblogs.com/dlvguo/p/11516412.html