Here’s my problem:
- I have two classes:
ClassObjandClassProperty; - One of the properties of
ClassObj(let’s call itlstProperty) is aListofClassProperty(representing the property that current instance of the object have); - At runtime I have stored somewhere an
Arrayof all possibleClassProperty, we will call thisArrayarrPossibleProperty.
My issue is to find a REALLY fast way of checking if an instance of ClassObj match a particular set of ClassProperty (if he have in his lstProperty all the ClassProperty of a given set).
I was thinking about creating an array of Bit representing the sequence of ClassProperty that the ClassObj posesses. Using as reference the Array arrPossibleProperty, and the index of his Property.
So if for example we have 10 property and a InstanceA of ClassObj have the 1st the 4th and the 9th I would generate this array of bit:
1001000010
My question is, how can I check for example (Fastest and Most performing solution) that the array of bit have (for example) the 3rd and the 4th Property?
Of course if you need there’s a more performing way of doing this let me know.
You’ll want to use bitwise-operations, such as
&,|,^or~(depending on your needs)Taking your example of 1001000010, to find out if the 3rd bit is set, you’d want to do this:
1001000010 & 0000000100 != 0000000000, or ratherFor more information about bitwise operations: http://en.wikipedia.org/wiki/Bitwise_operation
Alternatively and a lot more easy to implement, would be using enums.
Then, you’d use the enum like this