1.1 --- a/lib/__builtins__/sequence.py Wed Jan 11 12:26:04 2017 +0100
1.2 +++ b/lib/__builtins__/sequence.py Wed Jan 11 17:22:32 2017 +0100
1.3 @@ -19,6 +19,7 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 +from __builtins__.int import maxint
1.8 from native import isinstance as _isinstance
1.9
1.10 class itemaccess:
1.11 @@ -156,6 +157,30 @@
1.12
1.13 return 0
1.14
1.15 +class hashable(itemaccess):
1.16 +
1.17 + "An abstract class providing support for hashable sequences."
1.18 +
1.19 + _p = maxint / 32
1.20 + _a = 31
1.21 +
1.22 + def _hashvalue(self, fn):
1.23 +
1.24 + """
1.25 + Return a value for hashing purposes for the sequence using the given
1.26 + 'fn' on each item.
1.27 + """
1.28 +
1.29 + result = 0
1.30 + l = self.__len__()
1.31 + i = 0
1.32 +
1.33 + while i < l:
1.34 + result = (result * self._a + fn(self.__get_single_item__(i))) % self._p
1.35 + i += 1
1.36 +
1.37 + return result
1.38 +
1.39 class sequence(itemaccess):
1.40
1.41 "A common base class for sequence types."
1.42 @@ -214,6 +239,18 @@
1.43
1.44 "Return whether this sequence is equal to 'other'."
1.45
1.46 + try:
1.47 + return self._eq(other)
1.48 + except TypeError:
1.49 + return NotImplemented
1.50 +
1.51 + def _eq(self, other):
1.52 +
1.53 + """
1.54 + Return whether this sequence is equal to 'other' sequence. Note that
1.55 + this method will raise a TypeError if 'other' is not a sequence.
1.56 + """
1.57 +
1.58 # Sequences must have equal lengths to be equal.
1.59
1.60 n = self.__len__()