Add lru.keys iterator (#6)

* Add lru.keys iterator
* Fix compilation on nim <1.4
* Add keys iterator test
This commit is contained in:
Tanguy 2022-11-14 10:28:01 +01:00 committed by GitHub
parent 717abe4e61
commit 3ead7db4fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 11 deletions

View File

@ -229,6 +229,17 @@ window.addEventListener('DOMContentLoaded', main);
</ul>
</li>
<li>
<a class="reference reference-toplevel" href="#15" id="65">Iterators</a>
<ul class="simple simple-toc-section">
<ul class="simple nested-toc-section">keys
<li><a class="reference" href="#keys.i%2CLruCache%5BK%2CT%5D"
title="keys[K, T](cache: LruCache[K, T]): lent K">keys[K, T](cache: LruCache[K, T]): lent K</a></li>
</ul>
</ul>
</li>
</ul>
@ -349,8 +360,8 @@ Return least recently used key. Raise <tt class="docutils literal"><span class="
</dd>
</div>
<div id="getLruValue,LruCache[K,T]">
<dt><pre><span class="Keyword">proc</span> <a href="#getLruValue%2CLruCache%5BK%2CT%5D"><span class="Identifier">getLruValue</span></a><span class="Other">[</span><span class="Identifier">K</span><span class="Other">,</span> <span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">cache</span><span class="Other">:</span> <a href="lrucache.html#LruCache"><span class="Identifier">LruCache</span></a><span class="Other">[</span><span class="Identifier">K</span><span class="Other">,</span> <span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><a href="lrucache.html#EmptyLruCacheError"><span class="Identifier">EmptyLruCacheError</span></a><span class="Other">]</span><span class="Other">,</span>
<span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">Defect</span><span class="Other">]</span></span>.}</pre></dt>
<dt><pre><span class="Keyword">proc</span> <a href="#getLruValue%2CLruCache%5BK%2CT%5D"><span class="Identifier">getLruValue</span></a><span class="Other">[</span><span class="Identifier">K</span><span class="Other">,</span> <span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">cache</span><span class="Other">:</span> <a href="lrucache.html#LruCache"><span class="Identifier">LruCache</span></a><span class="Other">[</span><span class="Identifier">K</span><span class="Other">,</span> <span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span> {.
<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">Defect</span><span class="Other">,</span> <a href="lrucache.html#EmptyLruCacheError"><span class="Identifier">EmptyLruCacheError</span></a><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">Defect</span><span class="Other">]</span></span>.}</pre></dt>
<dd>
Return least recently used value. Raise <tt class="docutils literal"><span class="pre"><span class="Identifier">EmptyLruCacheError</span></span></tt> if <tt class="docutils literal"><span class="pre"><span class="Identifier">cache</span></span></tt> is empty.
@ -358,8 +369,8 @@ Return least recently used value. Raise <tt class="docutils literal"><span class
</dd>
</div>
<div id="getMruKey,LruCache[K,T]">
<dt><pre><span class="Keyword">proc</span> <a href="#getMruKey%2CLruCache%5BK%2CT%5D"><span class="Identifier">getMruKey</span></a><span class="Other">[</span><span class="Identifier">K</span><span class="Other">,</span> <span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">cache</span><span class="Other">:</span> <a href="lrucache.html#LruCache"><span class="Identifier">LruCache</span></a><span class="Other">[</span><span class="Identifier">K</span><span class="Other">,</span> <span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">K</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><a href="lrucache.html#EmptyLruCacheError"><span class="Identifier">EmptyLruCacheError</span></a><span class="Other">]</span><span class="Other">,</span>
<span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">Defect</span><span class="Other">]</span></span>.}</pre></dt>
<dt><pre><span class="Keyword">proc</span> <a href="#getMruKey%2CLruCache%5BK%2CT%5D"><span class="Identifier">getMruKey</span></a><span class="Other">[</span><span class="Identifier">K</span><span class="Other">,</span> <span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">cache</span><span class="Other">:</span> <a href="lrucache.html#LruCache"><span class="Identifier">LruCache</span></a><span class="Other">[</span><span class="Identifier">K</span><span class="Other">,</span> <span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">K</span> {.
<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">Defect</span><span class="Other">,</span> <a href="lrucache.html#EmptyLruCacheError"><span class="Identifier">EmptyLruCacheError</span></a><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">Defect</span><span class="Other">]</span></span>.}</pre></dt>
<dd>
Return most recently used key. Raise <tt class="docutils literal"><span class="pre"><span class="Identifier">EmptyLruCacheError</span></span></tt> if <tt class="docutils literal"><span class="pre"><span class="Identifier">cache</span></span></tt> is empty.
@ -367,8 +378,8 @@ Return most recently used key. Raise <tt class="docutils literal"><span class="p
</dd>
</div>
<div id="getMruValue,LruCache[K,T]">
<dt><pre><span class="Keyword">proc</span> <a href="#getMruValue%2CLruCache%5BK%2CT%5D"><span class="Identifier">getMruValue</span></a><span class="Other">[</span><span class="Identifier">K</span><span class="Other">,</span> <span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">cache</span><span class="Other">:</span> <a href="lrucache.html#LruCache"><span class="Identifier">LruCache</span></a><span class="Other">[</span><span class="Identifier">K</span><span class="Other">,</span> <span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><a href="lrucache.html#EmptyLruCacheError"><span class="Identifier">EmptyLruCacheError</span></a><span class="Other">]</span><span class="Other">,</span>
<span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">Defect</span><span class="Other">]</span></span>.}</pre></dt>
<dt><pre><span class="Keyword">proc</span> <a href="#getMruValue%2CLruCache%5BK%2CT%5D"><span class="Identifier">getMruValue</span></a><span class="Other">[</span><span class="Identifier">K</span><span class="Other">,</span> <span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">cache</span><span class="Other">:</span> <a href="lrucache.html#LruCache"><span class="Identifier">LruCache</span></a><span class="Other">[</span><span class="Identifier">K</span><span class="Other">,</span> <span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span> {.
<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">Defect</span><span class="Other">,</span> <a href="lrucache.html#EmptyLruCacheError"><span class="Identifier">EmptyLruCacheError</span></a><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">Defect</span><span class="Other">]</span></span>.}</pre></dt>
<dd>
Return most recently used value. Raise <tt class="docutils literal"><span class="pre"><span class="Identifier">EmptyLruCacheError</span></span></tt> if <tt class="docutils literal"><span class="pre"><span class="Identifier">cache</span></span></tt> is empty.
@ -457,6 +468,19 @@ Alias of <tt class="docutils literal"><span class="pre"><span class="Identifier"
Return least recently used key value pair as tuple. Raise <tt class="docutils literal"><span class="pre"><span class="Identifier">EmptyLruCacheError</span></span></tt> if <tt class="docutils literal"><span class="pre"><span class="Identifier">cache</span></span></tt> is empty.
</dd>
</div>
</dl></div>
<div class="section" id="15">
<h1><a class="toc-backref" href="#15">Iterators</a></h1>
<dl class="item">
<div id="keys.i,LruCache[K,T]">
<dt><pre><span class="Keyword">iterator</span> <a href="#keys.i%2CLruCache%5BK%2CT%5D"><span class="Identifier">keys</span></a><span class="Other">[</span><span class="Identifier">K</span><span class="Other">,</span> <span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">cache</span><span class="Other">:</span> <a href="lrucache.html#LruCache"><span class="Identifier">LruCache</span></a><span class="Other">[</span><span class="Identifier">K</span><span class="Other">,</span> <span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">lent</span> <span class="Identifier">K</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">Defect</span><span class="Other">]</span></span>.}</pre></dt>
<dd>
</dd>
</div>
@ -469,7 +493,7 @@ Return least recently used key value pair as tuple. Raise <tt class="docutils li
<div class="twelve-columns footer">
<span class="nim-sprite"></span>
<br/>
<small style="color: var(--hint);">Made with Nim. Generated: 2022-02-21 00:20:13 UTC</small>
<small style="color: var(--hint);">Made with Nim. Generated: 2022-11-14 09:21:30 UTC</small>
</div>
</div>
</div>

View File

@ -155,14 +155,14 @@ proc isFull*[K,T](cache: LruCache[K,T]): bool {.inline.} =
## Raise `EmptyLruCacheError` if `cache` is empty.
cache.len == cache.capacity
proc getMruKey*[K,T](cache: LruCache[K,T]): K {.raises: [EmptyLruCacheError].} =
proc getMruKey*[K,T](cache: LruCache[K,T]): K {.raises: [Defect, EmptyLruCacheError].} =
## Return most recently used key.
## Raise `EmptyLruCacheError` if `cache` is empty.
if cache.isEmpty:
raise newException(EmptyLruCacheError, "Cannot get most recently used key from empty cache")
cache.list.head.value.key
proc getMruValue*[K,T](cache: LruCache[K,T]): T {.raises: [EmptyLruCacheError].} =
proc getMruValue*[K,T](cache: LruCache[K,T]): T {.raises: [Defect, EmptyLruCacheError].} =
## Return most recently used value.
## Raise `EmptyLruCacheError` if `cache` is empty.
if cache.isEmpty:
@ -180,7 +180,7 @@ proc getLruKey*[K,T](cache: LruCache[K,T]): K
cache.list.tail.value.key
proc getLruValue*[K,T](cache: LruCache[K,T]): T
{.raises: [EmptyLruCacheError].} =
{.raises: [Defect, EmptyLruCacheError].} =
## Return least recently used value.
## Raise `EmptyLruCacheError` if `cache` is empty.
@ -204,3 +204,9 @@ proc removeLru*[K,T](cache: LruCache[K,T]): T
# under no circumstances should `val` be Option.none, given that the cache is
# not empty, we should always have an LRU key
return val.get
iterator keys*[K,T](cache: LruCache[K,T]): lent K =
var it = cache.list.head
while not isNil(it):
yield it.value.key
it = it.next

View File

@ -1,3 +1,4 @@
import sequtils
import unittest, options
import lrucache
@ -22,6 +23,11 @@ suite "LruCache":
cache.len == 0
cache.del(0) == int.none
test "keys iterator":
let cache = newLruCache[int, int](5)
for i in 1..10: cache[i] = i
check: toSeq(cache.keys) == toSeq(countdown(10, 6))
test "remove items if capacity exceeded":
let cache = newLruCache[int, int](5)