1.0.0 • Published 2 years ago

cache-mapset v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

cache-mapset

deno land deno doc GitHub release (latest by date) codecov License

test NPM standard-readme compliant semantic-release: angular

Maps and Sets with cache replacement policies, TC39 proposal-policy-map-set implementation. This can be used as a cache for TC39 proposal-function-memo and its implementation.

Usage

All Map-like constructors specify capacity.

When the limit is reached, the cache is adjusted according to the cache replacement policy.

import { LRUMap } from "https://deno.land/x/cache_mapset@$VERSION/mod.ts";
import { assert, assertEquals } from "https://deno.land/std/testing/asserts.ts";

declare const capacity: 2;

const map = new LRUMap<number, string>(capacity);

map.set(200, "Ok");
map.set(201, "Created");

assertEquals(map.size, 2);

map.set(202, "Accepted");

assertEquals(map.size, 2);
assert(map.has(201));
assert(map.has(202));

It provides a Map-like constructor with the following cache-replacement-policy:

Set like

SetLike is a set-like constructor, with the same cache-replacement-policy.

LFUSet preferentially removes item with fewer references (by has or add).

import { LFUSet } from "https://deno.land/x/cache_mapset@$VERSION/mod.ts";
import { assert, assertEquals } from "https://deno.land/std/testing/asserts.ts";

declare const capacity: 2;

const set = new LFUSet<number>(capacity);

set.add(200);
set.add(201);

assertEquals(set.size, 2);
assert(set.has(200));

set.add(202);

assert(set.has(200));
assert(set.has(202));

Initial value

Accepts an initial value, like Map or Set. If overcapacity occurs, the cache is adjusted according to the policy.

import { FIFOSet } from "https://deno.land/x/cache_mapset@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

const set = new FIFOSet<number>(3, [0, 1, 2, 3, 4, 5]);
assertEquals(set.size, 3);

Errors

All constructors specify a capacity as their first argument.

If it is a negative number, an error is thrown.

import { FIFOMap } from "https://deno.land/x/cache_mapset@$VERSION/mod.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";

assertThrows(() => new FIFOMap(-1));

Difference from Map and Set

MapLike and SetLike are not Iterable.

The following members are not implemented.

  • Symbol.iterator
  • forEach
  • entries
  • keys
  • values

Currently, these are outside the scope of the specification. For more information, check Data iteration and order.

API

See deno doc for all APIs.

Contributing

See CONTRIBUTING.md

License

MIT © 2023 Tomoki Miyauchi