]> git.ipfire.org Git - thirdparty/git.git/commitdiff
ewah: implement `ewah_bitmap_is_subset()`
authorTaylor Blau <me@ttaylorr.com>
Thu, 23 May 2024 21:26:20 +0000 (17:26 -0400)
committerJunio C Hamano <gitster@pobox.com>
Fri, 24 May 2024 18:40:41 +0000 (11:40 -0700)
In order to know whether a given pseudo-merge (comprised of a "parents"
and "objects" bitmaps) is "satisfied" and can be OR'd into the bitmap
result, we need to be able to quickly determine whether the "parents"
bitmap is a subset of the current set of objects reachable on either
side of a traversal.

Implement a helper function to prepare for that, which determines
whether an EWAH bitmap (the parents bitmap from the pseudo-merge) is a
subset of a non-EWAH bitmap (in this case, the results bitmap from
either side of the traversal).

This function makes use of the EWAH iterator to avoid inflating any part
of the EWAH bitmap after we determine it is not a subset of the non-EWAH
bitmap. This "fail-fast" allows us to avoid a potentially large amount
of wasted effort.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
ewah/bitmap.c
ewah/ewok.h

index ac7e0af622a8fc74b678b57f464698d1bc9a45e7..d352fec54cef70cafa21959b3e804a1ea254d21f 100644 (file)
@@ -138,6 +138,49 @@ void bitmap_or(struct bitmap *self, const struct bitmap *other)
                self->words[i] |= other->words[i];
 }
 
+int ewah_bitmap_is_subset(struct ewah_bitmap *self, struct bitmap *other)
+{
+       struct ewah_iterator it;
+       eword_t word;
+       size_t i;
+
+       ewah_iterator_init(&it, self);
+
+       for (i = 0; i < other->word_alloc; i++) {
+               if (!ewah_iterator_next(&word, &it)) {
+                       /*
+                        * If we reached the end of `self`, and haven't
+                        * rejected `self` as a possible subset of
+                        * `other` yet, then we are done and `self` is
+                        * indeed a subset of `other`.
+                        */
+                       return 1;
+               }
+               if (word & ~other->words[i]) {
+                       /*
+                        * Otherwise, compare the next two pairs of
+                        * words. If the word from `self` has bit(s) not
+                        * in the word from `other`, `self` is not a
+                        * subset of `other`.
+                        */
+                       return 0;
+               }
+       }
+
+       /*
+        * If we got to this point, there may be zero or more words
+        * remaining in `self`, with no remaining words left in `other`.
+        * If there are any bits set in the remaining word(s) in `self`,
+        * then `self` is not a subset of `other`.
+        */
+       while (ewah_iterator_next(&word, &it))
+               if (word)
+                       return 0;
+
+       /* `self` is definitely a subset of `other` */
+       return 1;
+}
+
 void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)
 {
        size_t original_size = self->word_alloc;
index c11d76c6f336930e0ea540a5b40c594bdb9f5be2..2b6c4ac499c7dafe87a433d26454eb5963752450 100644 (file)
@@ -179,7 +179,13 @@ void bitmap_unset(struct bitmap *self, size_t pos);
 int bitmap_get(struct bitmap *self, size_t pos);
 void bitmap_free(struct bitmap *self);
 int bitmap_equals(struct bitmap *self, struct bitmap *other);
+
+/*
+ * Both `bitmap_is_subset()` and `ewah_bitmap_is_subset()` return 1 if the set
+ * of bits in 'self' are a subset of the bits in 'other'. Returns 0 otherwise.
+ */
 int bitmap_is_subset(struct bitmap *self, struct bitmap *other);
+int ewah_bitmap_is_subset(struct ewah_bitmap *self, struct bitmap *other);
 
 struct ewah_bitmap * bitmap_to_ewah(struct bitmap *bitmap);
 struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah);