1 Star 0 Fork 0

4.9527/Data-Structures-and-Algorithms-Using-Python

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
linearmap.py 1.52 KB
一键复制 编辑 原始数据 按行查看 历史
4.9527 提交于 2019-12-04 10:41 +08:00 . read chapter 9
class Map:
def __init__(self):
self._entryList = list()
def __len__(self):
return len(self._entryList)
def __contains__(self, key):
ndx = self._findPosition(key)
return ndx is not None
def add(self, key, value):
ndx = self._findPosition(key)
if ndx is not None :
self._entryList[ndx].value = value
return False
else :
entry = _MapEntry(key, value)
self._entryList.append(entry)
return True
def _findPosition(self, key):
for i in range(len(self)) :
if self._entryList[i].key == key :
return i
return None
def valueOf(self, key):
ndx = self._findPosition(key)
assert ndx is not None, "Invalid map key"
return self._entryList[ndx].value
def remove(self, key):
ndx = self._findPosition(key)
assert ndx is not None, "Invalid map key"
self._entryList.pop(ndx)
def __iter__(self):
return _MapIterator(self._entryList)
class _MapEntry:
def __init__(self, key, value):
self.key = key
self.value = value
class _MapIterator:
def __init__(self, entryList):
self._entryList = entryList
self.ndx = 0
def __iter__(self):
return self
def __next__(self):
if self.ndx in range(len(self._entryList)) :
value = self._entryList[self.ndx].value
self.ndx += 1
return value
else :
raise StopIteration
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/nicolas4d/Data-Structures-and-Algorithms-Using-Python.git
git@gitee.com:nicolas4d/Data-Structures-and-Algorithms-Using-Python.git
nicolas4d
Data-Structures-and-Algorithms-Using-Python
Data-Structures-and-Algorithms-Using-Python
master

搜索帮助