source: utilitiespy/tudelft/utilities/immutablelist/PowerSet.py

Last change on this file was 1169, checked in by wouter, 3 weeks ago

#368 utilitiespy is now py.typed

File size: 1007 bytes
Line 
1from typing import TypeVar
2from tudelft.utilities.immutablelist.AbstractImmutableList import AbstractImmutableList
3from tudelft.utilities.immutablelist.ImmutableList import ImmutableList
4from tudelft.utilities.immutablelist.SubList import SubList
5T=TypeVar("T")
6
7class PowerSet(AbstractImmutableList[ImmutableList[T]]):
8 '''
9 the power set (or powerset) of any set S is the set of all subsets of S,
10 including the empty set and S itself. The given list is assumed to be a set,
11 so without duplicates.
12
13 @param T the element type of all lists that we receive.
14 '''
15
16
17 def __init__(self, lst:ImmutableList[T] ) :
18 self._lst = lst
19
20 def get(self, index:int) -> ImmutableList[T] :
21 if index >= self.size():
22 raise IndexError("Index out of bounds:" + str(index))
23 return SubList[T](self._lst, index)
24
25 def size(self)->int:
26 return 1 << self._lst.size()
27
28 def __hash__(self):
29 return 17*hash(self._lst)
30
31
32 def __eq__(self, other):
33 return isinstance(other, self.__class__) \
34 and self._lst==other._lst
Note: See TracBrowser for help on using the repository browser.