AndNot computes the difference between two bitmaps and returns the result
(x1, x2 *Bitmap)
| 1871 | |
| 1872 | // AndNot computes the difference between two bitmaps and returns the result |
| 1873 | func AndNot(x1, x2 *Bitmap) *Bitmap { |
| 1874 | if x1 == x2 { |
| 1875 | return NewBitmap() |
| 1876 | } |
| 1877 | answer := NewBitmap() |
| 1878 | pos1 := 0 |
| 1879 | pos2 := 0 |
| 1880 | length1 := x1.highlowcontainer.size() |
| 1881 | length2 := x2.highlowcontainer.size() |
| 1882 | |
| 1883 | main: |
| 1884 | for { |
| 1885 | if pos1 < length1 && pos2 < length2 { |
| 1886 | s1 := x1.highlowcontainer.getKeyAtIndex(pos1) |
| 1887 | s2 := x2.highlowcontainer.getKeyAtIndex(pos2) |
| 1888 | for { |
| 1889 | if s1 < s2 { |
| 1890 | answer.highlowcontainer.appendCopy(x1.highlowcontainer, pos1) |
| 1891 | pos1++ |
| 1892 | if pos1 == length1 { |
| 1893 | break main |
| 1894 | } |
| 1895 | s1 = x1.highlowcontainer.getKeyAtIndex(pos1) |
| 1896 | } else if s1 == s2 { |
| 1897 | c1 := x1.highlowcontainer.getContainerAtIndex(pos1) |
| 1898 | c2 := x2.highlowcontainer.getContainerAtIndex(pos2) |
| 1899 | diff := c1.andNot(c2) |
| 1900 | if !diff.isEmpty() { |
| 1901 | answer.highlowcontainer.appendContainer(s1, diff, false) |
| 1902 | } |
| 1903 | pos1++ |
| 1904 | pos2++ |
| 1905 | if (pos1 == length1) || (pos2 == length2) { |
| 1906 | break main |
| 1907 | } |
| 1908 | s1 = x1.highlowcontainer.getKeyAtIndex(pos1) |
| 1909 | s2 = x2.highlowcontainer.getKeyAtIndex(pos2) |
| 1910 | } else { // s1 > s2 |
| 1911 | pos2 = x2.highlowcontainer.advanceUntil(s1, pos2) |
| 1912 | if pos2 == length2 { |
| 1913 | break main |
| 1914 | } |
| 1915 | s2 = x2.highlowcontainer.getKeyAtIndex(pos2) |
| 1916 | } |
| 1917 | } |
| 1918 | } else { |
| 1919 | break |
| 1920 | } |
| 1921 | } |
| 1922 | if pos2 == length2 { |
| 1923 | answer.highlowcontainer.appendCopyMany(x1.highlowcontainer, pos1, length1) |
| 1924 | } |
| 1925 | return answer |
| 1926 | } |
| 1927 | |
| 1928 | // AddMany add all of the values in dat |
| 1929 | func (rb *Bitmap) AddMany(dat []uint32) { |
searching dependent graphs…