@@ -28,6 +28,7 @@ def __init__(self, sequence):
2828 self .sequence = \
2929 sequence_tools .ensure_iterable_is_immutable_sequence (sequence )
3030 self .sequence_length = len (self .sequence )
31+ self ._sequence_set = set (self .sequence )
3132 self .length = 2 ** self .sequence_length
3233
3334
@@ -51,41 +52,37 @@ def __getitem__(self, i):
5152
5253 assert len (binary_i ) == self .sequence_length
5354
54- return tuple (item for (is_included , item ) in
55- zip (map (int , binary_i ), self .sequence ) if is_included )
55+ return set (item for (is_included , item ) in
56+ zip (map (int , binary_i ), self .sequence ) if is_included )
5657
5758
5859 _reduced = property (lambda self : (type (self ), self .sequence ))
5960
6061 __eq__ = lambda self , other : (isinstance (other , SelectionSpace ) and
6162 self ._reduced == other ._reduced )
6263
63- # def __contains__(self, given_sequence ):
64- # try:
65- # self.index(given_sequence )
66- # except ValueError:
67- # return False
68- # else:
69- # return True
64+ def __contains__ (self , given_iterable ):
65+ try :
66+ self .index (given_iterable )
67+ except ValueError :
68+ return False
69+ else :
70+ return True
7071
71- # def index(self, given_sequence):
72- # if not isinstance(given_sequence, collections.Sequence) or \
73- # not len(given_sequence) == len(self.sequences):
74- # raise ValueError
72+ def index (self , given_iterable ):
73+ if not isinstance (given_iterable , collections .Iterable ):
74+ raise ValueError
7575
76- # reverse_indices = []
77- # current_radix = 1
76+ given_iterable_set = set (given_iterable )
7877
79- # wip_index = 0
80-
81- # for item, sequence in reversed(tuple(zip(given_sequence, self.sequences))):
82- # wip_index += sequence.index(item) # Propagating `ValueError`
83- # current_radix *= misc.get_length(sequence)
84-
85- # return wip_index
78+ if not given_iterable_set <= self ._sequence_set :
79+ raise ValueError
80+
81+ return sum ((2 ** i ) for i , item in enumerate (reversed (self .sequence ))
82+ if item in given_iterable_set )
8683
8784
88- # __bool__ = lambda self: bool(self.length)
85+ __bool__ = lambda self : bool (self .length )
8986
9087
9188
0 commit comments