annotate cachingseq.py @ 0:5397da1ef896 draft

Uploaded
author gianmarco_piccinno
date Tue, 21 May 2019 05:05:15 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
1 #!/usr/bin/env python2
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
2 #
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
3 # Copyright 2011-2016 Google Inc.
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
4 #
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
5 # Licensed under the Apache License, Version 2.0 (the "License");
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
6 # you may not use this file except in compliance with the License.
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
7 # You may obtain a copy of the License at
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
8 #
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
9 # http://www.apache.org/licenses/LICENSE-2.0
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
10 #
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
11 # Unless required by applicable law or agreed to in writing, software
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
12 # distributed under the License is distributed on an "AS IS" BASIS,
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
14 # See the License for the specific language governing permissions and
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
15 # limitations under the License.
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
16 #
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
17 # vim: sw=2 sts=2 et
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
18
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
19 class CachingFuncSequence(object):
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
20 def __init__(self, func, length, inc_func=None):
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
21 """
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
22 length: Length of this sequence.
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
23 func: function(index)
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
24 inc_func: function(index, value_of_previous)
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
25 """
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
26
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
27 self.func = func
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
28 self.inc_func = inc_func
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
29 self.length = length
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
30 self._cache = {}
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
31
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
32 def __getitem__(self, i):
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
33 if i < 0:
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
34 i += self.length
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
35 if i < 0 or i >= self.length:
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
36 raise IndexError()
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
37
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
38 v = self._cache.get(i)
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
39 if v is not None:
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
40 return v
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
41
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
42 if self.inc_func and i-1 in self._cache:
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
43 v = self.inc_func(i, self._cache[i-1])
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
44 else:
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
45 v = self.func(i)
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
46
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
47 self._cache[i] = v
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
48 return v
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
49
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
50 def __len__(self):
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
51 return self.length
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
52
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
53 def __iter__(self):
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
54 for i in range(self.length):
5397da1ef896 Uploaded
gianmarco_piccinno
parents:
diff changeset
55 yield self[i]