# lru

An LRU (Least-Recently-Used) cache backed by a **doubly-linked list + HashMap**. All core operations are O(1). Iteration is in MRU → LRU order (most recently used first).

---

## `LRU<K, V>`

```typescript
new LRU<K, V>(capacity?: number, entries?: [K, V][])
```

- `capacity` defaults to `100`.
- `entries` is an optional array of `[key, value]` pairs to pre-populate the cache (entries that exceed `capacity` are evicted in order).

---

## Methods

### `get(key): V | undefined`

Return the value and promote `key` to MRU. Returns `undefined` if not found.

### `peek(key): V | undefined`

Return the value **without** updating LRU order. Returns `undefined` if not found.

### `set(key, value): [K, V] | null`

Insert or update. If the key already exists its value is updated and it is promoted to MRU. If at capacity the least-recently-used entry is evicted.

Returns the evicted `[key, value]` pair, or `null` if nothing was evicted.

### `has(key): boolean`

Membership check. Does **not** update LRU order.

### `delete(key): boolean`

Remove an entry. Returns `true` if it existed, `false` otherwise.

### `clear(): void`

Remove all entries.

### `size` (getter)

Current number of cached entries.

### `keys(): K[]`

All keys in MRU → LRU order.

### `values(): V[]`

All values in MRU → LRU order.

### `entries(): [K, V][]`

All `[key, value]` pairs in MRU → LRU order.

### `forEach(callback): void`

Iterate all entries in MRU → LRU order.

```typescript
lru.forEach((value, key, lru) => { ... });
```

---

## Example

```typescript
const cache = new LRU<string, number>(3);

cache.set("a", 1);
cache.set("b", 2);
cache.set("c", 3);

cache.get("a");            // promote "a" to MRU → order: a, c, b

const evicted = cache.set("d", 4);  // "b" evicted (LRU)
// evicted === ["b", 2]

cache.entries();  // [["d",4],["a",1],["c",3]]

cache.peek("c");  // 3 — LRU order unchanged
cache.has("b");   // false
cache.delete("a"); // true
cache.size;        // 2
```
