123456789101112131415161718192021222324252627282930313233343536373839 |
- # -*- coding: utf-8 -*-
- class BinaryRelationItem(object):
- def __init__(self, item1, item2):
- self.first = item1
- self.second = item2
- def get_another(self, item):
- if item == self.first:
- return self.second
- if item == self.second:
- return self.first
- class BinaryRelationCollection(object):
- def __init__(self):
- self.__m_Dic = {}
- def update(self, item):
- if not self.is_exist(item):
- key = self.create_key(item)
- self.__m_Dic[key] = item
- def clear(self):
- self.__m_Dic.clear()
- @staticmethod
- def create_key(item):
- lis = [item.first, item.second]
- lis.sort()
- return '%s@%s' % (lis[0], lis[1])
- def is_exist(self, item):
- key = self.create_key(item)
- return key in self.__m_Dic
- def __iter__(self):
- return iter(self.__m_Dic)
|