Skip to content

Commit 220b787

Browse files
authored
Merge pull request #14 from normanyahq/master
Add peek_first_item() and peek_last_item()
2 parents ee61aea + 300e84b commit 220b787

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

README.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@ This can be used to build a LRU cache. Usage is almost like a dict.
1616
1717
from lru import LRU
1818
l = LRU(5) # Create an LRU container that can hold 5 items
19+
20+
print l.peek_first_item(), l.peek_last_item() #return the MRU key and LRU key
21+
# Would print None None
22+
1923
for i in range(5):
2024
l[i] = str(i)
2125
print l.items() # Prints items in MRU order
2226
# Would print [(4, '4'), (3, '3'), (2, '2'), (1, '1'), (0, '0')]
2327
28+
print l.peek_first_item(), l.peek_last_item() #return the MRU key and LRU key
29+
# Would print (4, '4') (0, '0')
30+
2431
l[5] = '5' # Inserting one more item should evict the old item
2532
print l.items()
2633
# Would print [(5, '5'), (4, '4'), (3, '3'), (2, '2'), (1, '1')]
@@ -87,4 +94,3 @@ be similar with other python implementations as well).
8794
Time : 3.31 s, Memory : 453672 Kb
8895
$ python bench.py lru.LRU
8996
Time : 0.23 s, Memory : 124328 Kb
90-

lru.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ static void
167167
lru_delete_last(LRU *self)
168168
{
169169
Node* n = self->last;
170-
170+
171171
if (!self->last)
172172
return;
173173

@@ -238,7 +238,7 @@ LRU_get(LRU *self, PyObject *args)
238238

239239
if (!PyArg_ParseTuple(args, "O|O", &key, &instead))
240240
return NULL;
241-
241+
242242
result = lru_subscript(self, key);
243243
PyErr_Clear(); /* GET_NODE sets an exception on miss. Shut it up. */
244244
if (result)
@@ -332,6 +332,34 @@ get_key(Node *node)
332332
return node->key;
333333
}
334334

335+
static PyObject *
336+
LRU_peek_first_item(LRU *self)
337+
{
338+
if (self->first) {
339+
PyObject *tuple = PyTuple_New(2);
340+
Py_INCREF(self->first->key);
341+
PyTuple_SET_ITEM(tuple, 0, self->first->key);
342+
Py_INCREF(self->first->value);
343+
PyTuple_SET_ITEM(tuple, 1, self->first->value);
344+
return tuple;
345+
}
346+
else Py_RETURN_NONE;
347+
}
348+
349+
static PyObject *
350+
LRU_peek_last_item(LRU *self)
351+
{
352+
if (self->last) {
353+
PyObject *tuple = PyTuple_New(2);
354+
Py_INCREF(self->last->key);
355+
PyTuple_SET_ITEM(tuple, 0, self->last->key);
356+
Py_INCREF(self->last->value);
357+
PyTuple_SET_ITEM(tuple, 1, self->last->value);
358+
return tuple;
359+
}
360+
else Py_RETURN_NONE;
361+
}
362+
335363
static PyObject *
336364
LRU_keys(LRU *self) {
337365
return collect(self, get_key);
@@ -451,6 +479,10 @@ static PyMethodDef LRU_methods[] = {
451479
PyDoc_STR("L.clear() -> clear LRU")},
452480
{"get_stats", (PyCFunction)LRU_get_stats, METH_NOARGS,
453481
PyDoc_STR("L.get_stats() -> returns a tuple with cache hits and misses")},
482+
{"peek_first_item", (PyCFunction)LRU_peek_first_item, METH_NOARGS,
483+
PyDoc_STR("L.peek_first_item() -> returns the MRU item (key,value) without changing key order")},
484+
{"peek_last_item", (PyCFunction)LRU_peek_last_item, METH_NOARGS,
485+
PyDoc_STR("L.peek_last_item() -> returns the LRU item (key,value) without changing key order")},
454486
{NULL, NULL},
455487
};
456488

test/test_lru.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,20 @@ def test_access(self):
106106
self.assertEqual(l[i], str(i))
107107
self.assertEqual(l.get(i,None), str(i))
108108

109+
def test_peek_first_item(self):
110+
l = LRU(2)
111+
self.assertEqual(None, l.peek_first_item())
112+
l[1] = '1'
113+
l[2] = '2'
114+
self.assertEqual((2, '2'), l.peek_first_item())
115+
116+
def test_peek_last_item(self):
117+
l = LRU(2)
118+
self.assertEqual(None, l.peek_last_item())
119+
l[1] = '1'
120+
l[2] = '2'
121+
self.assertEqual((1, '1'), l.peek_last_item())
122+
109123
def test_overwrite(self):
110124
l = LRU(1)
111125
l[1] = '2'

0 commit comments

Comments
 (0)