-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinearmap.py
95 lines (79 loc) · 2.66 KB
/
linearmap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Implementation of Map ADT using a single list.
class myMap:
# Creates an empty map instance.
def __init__( self ):
self._entryList = list()
# Returns the number of entries in the map.
def __len__( self ):
return len( self._entryList )
# Determines if the map contains the given key.
def __contains__( self, key ):
ndx = self._findPosition( key )
return ndx is not None
# Adds a new entry to the map if the key does exist. Otherwise, the
# new value replaces the current value assocaited with the key.
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
# Returns the value associated with the key.
def Valueof( self, key ):
ndx = self._findPosition( key )
assert ndx is not None, "Invalid map key. "
return self._entryList[ndx].value
# Removes the entry associated with the key.
def remove( self, key ):
ndx = self._findPosition( key )
assert ndx is not None, "Invalid map key. "
self._entryList.pop( ndx )
# Returns an iterator for traversing the keys in the map.
def __iter__( self ):
return _MapIterator( self._entryList )
# Helper method used to find the index position of a category. If the
# key is not found, None is returned.
def _findPosition( self, key ):
for i in range( len(self) ):
if self._entryList[i].key == key:
return i
return None
# Storage class for holding the key/value pairs.
class _MapEntry:
def __init__( self, key, value ):
self.key = key
self.value = value
# An iterator for the Map ADT
class _MapIterator:
def __init__( self, theEntryList ):
self._entryItems = theEntryList
self._curItem = 0
def __iter__( self ):
return self
def next( self ):
if self._curItem < len( self._entryItems ):
entry = self._entryItems[ self._curItem ]
self._curItem += 1
return entry
else:
raise StopIteration
if __name__ == '__main__':
StuMap = myMap()
print StuMap
StuMap.add('0301', 'Bell')
StuMap.add('0302', 'Tang')
StuMap.add('0303', 'Liu')
StuMap.add('0304', 'James')
print StuMap
for entry in StuMap:
print entry.key,
print entry.value
print ""
StuMap.add('0303', 'Ronadol')
for entry in StuMap:
print entry.key,
print entry.value
print ""