[PATCH V2] smartset: add a "toset" method

Augie Fackler raf at durin42.com
Sat Jun 3 19:07:41 EDT 2017


> On Jun 3, 2017, at 5:06 PM, Jun Wu <quark at fb.com> wrote:
> 
> # HG changeset patch
> # User Jun Wu <quark at fb.com>
> # Date 1496515319 25200
> #      Sat Jun 03 11:41:59 2017 -0700
> # Node ID a5f77662c4f22467b84fc3ce494998d23b0daa82
> # Parent  783394c0c97807e83daad9da561179bd0719e159
> # Available At https://bitbucket.org/quark-zju/hg-draft
> #              hg pull https://bitbucket.org/quark-zju/hg-draft -r a5f77662c4f2
> smartset: add a "toset" method
> 
> This allows us to convert a smartset to a Python set using a customized
> approach.  Namely, baseset may have a "_set" property already which could be
> used directly to avoid __iter__ overhead.
> 
> diff --git a/mercurial/smartset.py b/mercurial/smartset.py
> --- a/mercurial/smartset.py
> +++ b/mercurial/smartset.py
> @@ -156,4 +156,8 @@ class abstractsmartset(object):
>         return filteredset(self, condition, condrepr)
> 
> +    def toset(self):
> +        """Convert to unordered Python set"""
> +        return set(self)
> +
> class baseset(abstractsmartset):
>     """Basic data structure that represents a revset and contains the basic
> @@ -169,5 +173,6 @@ class baseset(abstractsmartset):
> 
>     Construct by a set:
> -    >>> xs = baseset(set(x))
> +    >>> xset = set(x)
> +    >>> xs = baseset(xset)
>>>> ys = baseset(set(y))
>>>> [list(i) for i in [xs + ys, xs & ys, xs - ys]]
> @@ -175,4 +180,6 @@ class baseset(abstractsmartset):
>>>> [type(i).__name__ for i in [xs + ys, xs & ys, xs - ys]]
>     ['addset', 'baseset', 'baseset']
> +    >>> xset is xs.toset()
> +    True
> 
>     Construct by a list-like:
> @@ -237,4 +244,7 @@ class baseset(abstractsmartset):
>         return set(self._list)
> 
> +    def toset(self):
> +        return self._set

It freaks me out just a little (maybe too much Rust today?) to leak self._set mutably like this. What do you think of making a copy? Should we just strongly admonish in the “convert to a set” docstring that callers of toset() _must not_ mutate the returned set?

> +
>     @util.propertycache
>     def _asclist(self):
> @@ -584,4 +594,8 @@ class addset(abstractsmartset):
>>>> [x for x in rs]
>     [5, 4, 3, 2, 0]
> +
> +    convert to Python set:
> +    >>> sorted(rs.toset())
> +    [0, 2, 3, 4, 5]
>     """
>     def __init__(self, revs1, revs2, ascending=None):
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel at mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel



More information about the Mercurial-devel mailing list