| 588 | |
| 589 | template <typename T, typename InternalTypes> |
| 590 | SparseBitmap<T, InternalTypes>** |
| 591 | SparseBitmap<T, InternalTypes>::bit_or(SparseBitmap<T, InternalTypes>** bitmap1, |
| 592 | SparseBitmap<T, InternalTypes>** bitmap2 |
| 593 | ) |
| 594 | { |
| 595 | SparseBitmap *map1, *map2; |
| 596 | |
| 597 | // Handle the case when one or the other of the bitmaps is NULL |
| 598 | if (!bitmap1 || !(map1 = *bitmap1)) { |
| 599 | return bitmap2; |
| 600 | } |
| 601 | |
| 602 | if (!bitmap2 || !(map2 = *bitmap2)) { |
| 603 | return bitmap1; |
| 604 | } |
| 605 | |
| 606 | // Make sure we work on 2 different bitmaps |
| 607 | fb_assert(map1 != map2); |
| 608 | |
| 609 | // First bitmap is singular. Set appropriate bit in second and return it |
| 610 | if (map1->singular) |
| 611 | { |
| 612 | map2->set(map1->singular_value); |
| 613 | return bitmap2; |
| 614 | } |
| 615 | |
| 616 | // Second bitmap is singular. Set appropriate bit in first and return it |
| 617 | if (map2->singular) |
| 618 | { |
| 619 | map1->set(map2->singular_value); |
| 620 | return bitmap1; |
| 621 | } |
| 622 | |
| 623 | SparseBitmap *source, *dest, **result; |
| 624 | |
| 625 | // If second bitmap seems larger then use it as a target |
| 626 | if (map2->tree.seemsBiggerThan(map1->tree)) |
| 627 | { |
| 628 | dest = map2; |
| 629 | source = map1; |
| 630 | result = bitmap2; |
| 631 | } |
| 632 | else |
| 633 | { |
| 634 | dest = map1; |
| 635 | source = map2; |
| 636 | result = bitmap1; |
| 637 | } |
| 638 | |
| 639 | bool sourceFound = source->tree.getFirst(); |
| 640 | |
| 641 | // Source bitmap is empty. We have nothing to do thus return. |
| 642 | if (!sourceFound) { |
| 643 | return result; |
| 644 | } |
| 645 | |
| 646 | // Both source and destination are empty. Return destination FWIW. |
| 647 | bool destFound = dest->tree.getFirst(); |