@@ -167,7 +167,7 @@ static void
167167lru_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+
335363static PyObject *
336364LRU_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
0 commit comments