An application scenario of the bitset class -- acts as a mask

Source: Internet
Author: User
Tags bitset

Author: Zhu Jincan

I usually seldom use the bitset class in STL. Today, it is very suitable to use it as a mask. The mask here refers to an int value to cover multiple situations. In fact, we often encounter this mask. For example, the window CWnd class in the MFC program moves the message processing function with the mouse:


Afx_msg void OnMouseMove (
UINT nFlags,
CPoint point
);

 


The first UINT nFlags parameter of this function can be called a mask. This mask covers whether the user simultaneously presses the left mouse button and right mouse button while moving the mouse. In fact, this is simple, because it is only a key combination problem. There are multiple possible combinations. For example, in a 2D image editing software, you can select only one image at a time by clicking and editing the image with the mouse. Now, all vertices are placed in the dot array, and all wired images are placed in the online array. To select a chart, you must set a threshold to ensure that the image is selected within a certain range. A common practice is:
First, traverse the Dot Array to find out that the distance between the mouse and a certain point is the smallest and less than the threshold value. Meanwhile, traverse the line array to find out that the distance between the mouse and a line is the smallest and less than the threshold value. In this way, there are four situations:


1. No matching point or line is selected by the mouse

2. Find a qualified vertex in the vertex group, but no line in the online group that matches the condition is found.

3. The online group finds a line that meets the condition, but does not find a point that meets the condition in the point group.

4. At the same time, locate the qualified point in the point group and find the line that matches the condition in the online group. In this case, compare the closer the mouse is to the point or the closer it is to the offline one.

How can we cover these four situations? If we define an int variable to save these four cases, we have to write a lot of if and else statements:


UINT nFlag;
If (if no matching image is found)
NFlag = 0;
Else if (if only the vertex is found) // you need to determine whether to locate the line when finding the vertex.
NFlag = 1;
Else if (if only the line is found)
NFlag = 2;
Else if (locate points and lines at the same time)
NFlag = 3;

There are few of the four cases. If there are more combinations, you may be confused and the code is not clear.

 


Now let's use the bitset class to handle this situation.
// Define a bitset class object with the initial binary value: 00
Bitset <2> RetBit;

// If a vertex is found, set the first binary bit to 1. Here, you only need to determine whether a vertex is found.
If (if a vertex is found)
RetBit. set (0 );

// If the line is found, set the second binary bit to 1. Here, you only need to determine whether the line is found.
If (if line is found)
RetBit. set (1 );

The code is much simpler and clearer.

Related Article

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.