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

Last change on this file since 831 was 309, checked in by wouter, 3 years ago

#103 added PowerSet and SubList

File size: 1009 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, list:ImmutableList[T] ) :
18 self._list = list
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._list, index)
24
25 def size(self)->int:
26 return 1 << self._list.size()
27
28 def __hash__(self):
29 return 17*hash(list)
30
31
32 def __eq__(self, other):
33 return isinstance(other, self.__class__) \
34 and self._list==other._list
Note: See TracBrowser for help on using the repository browser.