def merge(self, gk):
        """Merge sketch with another sketch gk."""
        if not self.mergeable(gk):
            raise Exception("gk does not share signature.")

        if gk._count == 0:
            return

        if self._count == 0:
            self.copy(gk)
            return

        entries = []
        spread = int(gk.eps * (gk.n - 1))
        gk.merge_compress()

        # upper bound elements(gk.v0, gk.v1) - spread
        g = gk.entries[0].g + gk.entries[0].delta - 1 - spread

        if g > 0:
            entries.append(Entry(gk._min, g, 0))

        n_gk = len(gk)
        for i in range(n_gk - 1):
            tp1 = gk.entries[i + 1]
            t = gk.entries[i]
            g = tp1.g + (tp1.delta - t.delta)
            if g > 0:
                entries.append(Entry(t.value, g, 0))

        last_t = gk.entries[n_gk - 1]
        g = spread + 1 - last_t.delta
        if g > 0:
            entries.append(Entry(last_t.value, g, 0))

        self._count += gk._count
        self._min = min(self._min, gk._min)
        self._max = max(self._max, gk._max)
        self._sum += gk._sum

        self.merge_compress(entries)
Merge sketch with another sketch gk.
optbinning/binning/distributed/gk.py