<commit_before>from sympy.core import Basic

class Subset(Basic):
    """
    Represents a basic subset object.

    We generate subsets using essentially two techniques,
    binary enumeration and lexicographic enumeration.
    """

    _rank_binary = None
    _rank_lex = None

    def next_binary(self):
        """
        Generates the next binary ordered subset.

        Examples:
        >>> from sympy.combinatorics.subsets import Subset
        >>> a = Subset(['c','d'],['a','b','c','d'])
        >>> a.next_binary()
        ['b']
        >>> a = Subset(['a','b','c','d'],['a','b','c','d'])
        >>> a.next_binary()
        []
        """
        bin_list = get_bitlist_from_subset(self.subset, self.superset)
        next_bin_list = list(bin((int(reduce(lambda x, y:
                                             x + y, bin_list), 2) + 1)
                                 % 2**self.superset_size))[2:]
        next_bin_list = [0] * (self.superset_size - len(next_bin_list)) + \
                        next_bin_list
        return get_subset_from_bitlist(self.superset, next_bin_list)

    def prev_binary(self):
        """
        Generates the previous binary ordered subset.

        Examples:
        >>> from sympy.combinatorics.subsets import Subset
        >>> a = Subset([],['a','b','c','d'])
        >>> a.prev_binary()
        ['a', 'b', 'c', 'd']
        >>> a = Subset(['c','d'],['a','b','c','d'])
        >>> a.prev_binary()
        ['c']
        """
        bin_list = get_bitlist_from_subset(self.subset, self.superset)
        next_bin_list = list(bin((int(reduce(lambda x, y:
                                             x + y, bin_list), 2) - 1)
                                 % 2**self.superset_size))[2:]
        next_bin_list = [0] * (self.superset_size - len(next_bin_list)) + \
                        next_bin_list
        return get_subset_from_bitlist(self.superset, next_bin_list)

    def next_lexicographic(self):
        """
        Generates the next lexicographically ordered
        subset.

        Examples:
        """
        raise NotImplementedError()

    def prev_lexicographic(self):
        """
        Generates the previous lexicographically ordered
        subset.

        Examples:
        """
        raise NotImplementedError()

    def next_graycode(self):
        """
        Generates the next gray code ordered subset.

        Examples:
        """
        raise NotImplementedError()

    def prev_graycode(self):
        """
        Generates the previous gray code ordered subset.

        Examples:
        """
        raise NotImplementedError()

    @property
    def rank_binary(self):
        """
        Computes the binary ordered rank.

        Examples:
        >>> from sympy.combinatorics.subsets import Subset
        >>> a = Subset([],['a','b','c','d'])
        >>> a.rank_binary
        0
        >>> a = Subset(['c','d'],['a','b','c','d'])
        >>> a.rank_binary
        3
        """
        if self._rank_binary == None:
            self._rank_binary = int("".join(
                get_bitlist_from_subset(self.subset,
                                        self.superset)), 2)
        return self._rank_binary

    @property
    def rank_lexicographic(self):
        """
        Computes the lexicographic ranking of the subset.

        Examples:
        >>> from sympy.combinatorics.subsets import Subset
        >>> a = Subset(['c','d'],['a','b','c','d'])
        >>> a.rank_lexicographic
        14
        >>> a = Subset([2,4,5],[1,2,3,4,5,6])
        >>> a.rank_lexicographic
        43
        """
        if self._rank_lex == None:
            superset_index = list(xrange(self.superset_size))
            subset_index = [self.superset.index(j) for j in self.subset]
            def ranklex(self, subset_index, i, n):
                if subset_index == [] or i > n:
                    return 0
                if i in subset_index:
                    subset_index.remove(i)
                    return 1 + ranklex(self, subset_index, i + 1, n)
                return 2**(n - i - 1) + ranklex(self, subset_index, i + 1, n)
            self._rank_lex = ranklex(self, subset_index, 0, self.superset_size)
        return self._rank_lex

    @property
    def subset(self):
        """
        Gets the subset represented by the current instance.
        """
        return self.args[0]

    @property
    def size(self):
        """
        Gets the size of the subset.
        """
        return len(self.subset)

    @property
    def superset(self):
        """
        Gets the superset of the subset.
        """
        return self.args[1]

    @property<commit_msg>combinatorics/subsets: Added gray code ranking of subsets.<commit_after>from sympy.core import Basic
from sympy.combinatorics.graycode import GrayCode

class Subset(Basic):
    """
    Represents a basic subset object.

    We generate subsets using essentially two techniques,
    binary enumeration and lexicographic enumeration.
    """

    _rank_binary = None
    _rank_lex = None
    _rank_graycode = None

    def next_binary(self):
        """
        Generates the next binary ordered subset.

        Examples:
        >>> from sympy.combinatorics.subsets import Subset
        >>> a = Subset(['c','d'],['a','b','c','d'])
        >>> a.next_binary()
        ['b']
        >>> a = Subset(['a','b','c','d'],['a','b','c','d'])
        >>> a.next_binary()
        []
        """
        bin_list = get_bitlist_from_subset(self.subset, self.superset)
        next_bin_list = list(bin((int(reduce(lambda x, y:
                                             x + y, bin_list), 2) + 1)
                                 % 2**self.superset_size))[2:]
        next_bin_list = [0] * (self.superset_size - len(next_bin_list)) + \
                        next_bin_list
        return get_subset_from_bitlist(self.superset, next_bin_list)

    def prev_binary(self):
        """
        Generates the previous binary ordered subset.

        Examples:
        >>> from sympy.combinatorics.subsets import Subset
        >>> a = Subset([],['a','b','c','d'])
        >>> a.prev_binary()
        ['a', 'b', 'c', 'd']
        >>> a = Subset(['c','d'],['a','b','c','d'])
        >>> a.prev_binary()
        ['c']
        """
        bin_list = get_bitlist_from_subset(self.subset, self.superset)
        next_bin_list = list(bin((int(reduce(lambda x, y:
                                             x + y, bin_list), 2) - 1)
                                 % 2**self.superset_size))[2:]
        next_bin_list = [0] * (self.superset_size - len(next_bin_list)) + \
                        next_bin_list
        return get_subset_from_bitlist(self.superset, next_bin_list)

    def next_lexicographic(self):
        """
        Generates the next lexicographically ordered
        subset.

        Examples:
        """
        raise NotImplementedError()

    def prev_lexicographic(self):
        """
        Generates the previous lexicographically ordered
        subset.

        Examples:
        """
        raise NotImplementedError()

    def next_graycode(self):
        """
        Generates the next gray code ordered subset.

        Examples:
        """
        raise NotImplementedError()

    def prev_graycode(self):
        """
        Generates the previous gray code ordered subset.

        Examples:
        """
        raise NotImplementedError()

    @property
    def rank_binary(self):
        """
        Computes the binary ordered rank.

        Examples:
        >>> from sympy.combinatorics.subsets import Subset
        >>> a = Subset([],['a','b','c','d'])
        >>> a.rank_binary
        0
        >>> a = Subset(['c','d'],['a','b','c','d'])
        >>> a.rank_binary
        3
        """
        if self._rank_binary == None:
            self._rank_binary = int("".join(
                get_bitlist_from_subset(self.subset,
                                        self.superset)), 2)
        return self._rank_binary

    @property
    def rank_lexicographic(self):
        """
        Computes the lexicographic ranking of the subset.

        Examples:
        >>> from sympy.combinatorics.subsets import Subset
        >>> a = Subset(['c','d'],['a','b','c','d'])
        >>> a.rank_lexicographic
        14
        >>> a = Subset([2,4,5],[1,2,3,4,5,6])
        >>> a.rank_lexicographic
        43
        """
        if self._rank_lex == None:
            superset_index = list(xrange(self.superset_size))
            subset_index = [self.superset.index(j) for j in self.subset]
            def ranklex(self, subset_index, i, n):
                if subset_index == [] or i > n:
                    return 0
                if i in subset_index:
                    subset_index.remove(i)
                    return 1 + ranklex(self, subset_index, i + 1, n)
                return 2**(n - i - 1) + ranklex(self, subset_index, i + 1, n)
            self._rank_lex = ranklex(self, subset_index, 0, self.superset_size)
        return self._rank_lex

    @property
    def rank_graycode(self):
        """
        Computes the gray code ranking of the subset.

        Examples:
        >>> from sympy.combinatorics.subsets import Subset
        >>> a = Subset(['c','d'],['a','b','c','d'])
        >>> a.rank_graycode
        8
        >>> a = Subset([2,4,5],[1,2,3,4,5,6])
        >>> a.rank_graycode
        19
        """
        if self._rank_graycode == None:
            bits = get_bitlist_from_subset(self.subset, self.superset)
            self._rank_graycode = GrayCode(start = bits).rank
        return self._rank_graycode

    @property
    def subset(self):
        """
        Gets the subset represented by the current instance.
        """
        return self.args[0]

    @property
    def size(self):
        """
        Gets the size of the subset.
        """
        return len(self.subset)

    @property
    def superset(self):
        """
        Gets the superset of the subset.
        """
        return self.args[1]

    @property
{'fraction_non_alphanumeric': 0.132636815920398, 'fraction_numerical': 0.008159203980099502, 'mean_word_length': 1.7454247473367932, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 140, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '753428', 'n_tokens_mistral': 3020, 'n_tokens_neox': 2923, 'n_words': 646}