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

Last change on this file since 1284 was 1169, checked in by wouter, 3 months ago

#368 utilitiespy is now py.typed

File size: 1007 bytes
RevLine 
[309]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
[1169]17 def __init__(self, lst:ImmutableList[T] ) :
18 self._lst = lst
[309]19
20 def get(self, index:int) -> ImmutableList[T] :
21 if index >= self.size():
22 raise IndexError("Index out of bounds:" + str(index))
[1169]23 return SubList[T](self._lst, index)
[309]24
25 def size(self)->int:
[1169]26 return 1 << self._lst.size()
[309]27
28 def __hash__(self):
[1169]29 return 17*hash(self._lst)
[309]30
31
32 def __eq__(self, other):
33 return isinstance(other, self.__class__) \
[1169]34 and self._lst==other._lst
Note: See TracBrowser for help on using the repository browser.