Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
next up previous contents index

Union, Intersection and Difference

The implementations of the union, intersection, and difference operators (+, *, and -, respectively) for operands of type SetAsBitVector are shown in Program gif. The code is quite similar to that for the SetAsArray class given in Program gif.

   program28441
Program: SetAsBitVector Class Union, Intersection and Difference Operator Definitions

Instead of using the Boolean operators &&, || and !, we have used the bitwise operators &, | and . By using the bitwise operators, tex2html_wrap_inline67410 bits of the result are computed in each iteration of the loop. Therefore, the number of iterations required is tex2html_wrap_inline67408 instead of N. The worst-case running time of each of these operations is tex2html_wrap_inline67412.

Notice that the asymptotic performance of these SetAsBitVector class operations is the same as the asymptotic performance of the SetAsArray class operations. I.e., both of them are O(N). Nevertheless, the SetAsBitVector class operations are faster. In fact, the bit-vector approach is asymptotically faster than the the array approach by the factor w.


next up previous contents index

Bruno Copyright © 1997 by Bruno R. Preiss, P.Eng. All rights reserved.