-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstractCollection.py
78 lines (62 loc) · 2.1 KB
/
abstractCollection.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
"""
Author: Harry Pinkerton, James Lawson, Laurie Jones
File: abstractCollection.py
Project: 5
"""
class AbstractCollection(object):
"""An abstract collection implementation."""
# Constructor
def __init__(self, sourceCollection = None):
"""Sets the initial state of self, which includes the
contents of sourceCollection, if it's present."""
self._size = self._modCount = 0
if sourceCollection:
for item in sourceCollection:
self.add(item)
# Accessor methods
def __str__(self):
"""Returns the string representation of self, using []
as delimiters."""
return "[" + ", ".join(map(str, self)) + "]"
def __eq__(self, other):
"""Returns True if self equals other,
or False otherwise.
Compares pairs of items in the two sequences
generated by the iterators on self and other."""
if self is other: return True
if type(self) != type(other) or \
len(self) != len(other):
return False
otherIter = iter(other)
for item in self:
if item != next(otherIter):
return False
return True
def getModCount(self):
"""Returns the number of mutations to this collection."""
return self._modCount
def incModCount(self):
"""Increments the number of mutations by one."""
self._modCount += 1
def resetSizeAndModCount(self):
"""Resets the numbers of items and mutations to 0."""
self._size = 0
self._modCount = 0
def isEmpty(self):
return len(self) == 0
def __len__(self):
return self._size
def __str__(self):
return "{" + ", ".join(map(str, self)) + "}"
def __add__(self, other):
result = type(self)(self)
for item in other:
result.add(item)
return result
def count(self, item):
''' this notes each time that a certain value is in a linked structre '''
count = 0
for i in self:
if i == item:
count += 1
return count