mirror of
				https://github.com/pocketpy/pocketpy
				synced 2025-10-30 16:30:16 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			95 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			95 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| class set:
 | |
|     def __init__(self, iterable=None):
 | |
|         iterable = iterable or []
 | |
|         self._a = {}
 | |
|         for item in iterable:
 | |
|             self.add(item)
 | |
| 
 | |
|     def add(self, elem):
 | |
|         self._a[elem] = None
 | |
|         
 | |
|     def discard(self, elem):
 | |
|         if elem in self._a:
 | |
|             del self._a[elem]
 | |
| 
 | |
|     def remove(self, elem):
 | |
|         del self._a[elem]
 | |
|         
 | |
|     def clear(self):
 | |
|         self._a.clear()
 | |
| 
 | |
|     def update(self,other):
 | |
|         for elem in other:
 | |
|             self.add(elem)
 | |
|         return self
 | |
| 
 | |
|     def __len__(self):
 | |
|         return len(self._a)
 | |
|     
 | |
|     def copy(self):
 | |
|         return set(self._a.keys())
 | |
|     
 | |
|     def __and__(self, other):
 | |
|         ret = set()
 | |
|         for elem in self:
 | |
|             if elem in other:
 | |
|                 ret.add(elem)
 | |
|         return ret
 | |
|     
 | |
|     def __or__(self, other):
 | |
|         ret = self.copy()
 | |
|         for elem in other:
 | |
|             ret.add(elem)
 | |
|         return ret
 | |
| 
 | |
|     def __sub__(self, other):
 | |
|         ret = set() 
 | |
|         for elem in self:
 | |
|             if elem not in other: 
 | |
|                 ret.add(elem) 
 | |
|         return ret
 | |
|     
 | |
|     def __xor__(self, other): 
 | |
|         ret = set() 
 | |
|         for elem in self: 
 | |
|             if elem not in other: 
 | |
|                 ret.add(elem) 
 | |
|         for elem in other: 
 | |
|             if elem not in self: 
 | |
|                 ret.add(elem) 
 | |
|         return ret
 | |
| 
 | |
|     def union(self, other):
 | |
|         return self | other
 | |
| 
 | |
|     def intersection(self, other):
 | |
|         return self & other
 | |
| 
 | |
|     def difference(self, other):
 | |
|         return self - other
 | |
| 
 | |
|     def symmetric_difference(self, other):      
 | |
|         return self ^ other
 | |
|     
 | |
|     def __eq__(self, other):
 | |
|         return self.__xor__(other).__len__() == 0
 | |
| 
 | |
|     def isdisjoint(self, other):
 | |
|         return self.__and__(other).__len__() == 0
 | |
|     
 | |
|     def issubset(self, other):
 | |
|         return self.__sub__(other).__len__() == 0
 | |
|     
 | |
|     def issuperset(self, other):
 | |
|         return other.__sub__(self).__len__() == 0
 | |
| 
 | |
|     def __contains__(self, elem):
 | |
|         return elem in self._a
 | |
|     
 | |
|     def __repr__(self):
 | |
|         if len(self) == 0:
 | |
|             return 'set()'
 | |
|         return '{'+ ', '.join([repr(i) for i in self._a.keys()]) + '}'
 | |
|     
 | |
|     def __iter__(self):
 | |
|         return iter(self._a.keys()) |