{"id":17725625,"url":"https://github.com/bacali95/sweet-collections","last_synced_at":"2025-09-05T20:37:18.669Z","repository":{"id":65509429,"uuid":"308765334","full_name":"bacali95/sweet-collections","owner":"bacali95","description":"Typescript implementations of in-memory cache data-structures for Node and Browser.","archived":false,"fork":false,"pushed_at":"2023-07-20T17:52:06.000Z","size":35176,"stargazers_count":4,"open_issues_count":1,"forks_count":1,"subscribers_count":0,"default_branch":"main","last_synced_at":"2024-09-23T05:50:41.118Z","etag":null,"topics":["avl-tree","collections","heap","lfu-cache","lru-cache","sorted-array","sorted-map","sorted-set"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bacali95.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-10-30T23:30:01.000Z","updated_at":"2024-03-30T23:45:03.000Z","dependencies_parsed_at":"2024-06-19T00:37:16.207Z","dependency_job_id":null,"html_url":"https://github.com/bacali95/sweet-collections","commit_stats":{"total_commits":35,"total_committers":1,"mean_commits":35.0,"dds":0.0,"last_synced_commit":"e32033e0dd0c045a8712e3e7efb7a70676e8196f"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bacali95%2Fsweet-collections","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bacali95%2Fsweet-collections/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bacali95%2Fsweet-collections/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bacali95%2Fsweet-collections/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bacali95","download_url":"https://codeload.github.com/bacali95/sweet-collections/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246620518,"owners_count":20806784,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["avl-tree","collections","heap","lfu-cache","lru-cache","sorted-array","sorted-map","sorted-set"],"created_at":"2024-10-25T16:05:07.178Z","updated_at":"2025-04-01T16:30:51.049Z","avatar_url":"https://github.com/bacali95.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Sweet Collections\n\n![Build](https://github.com/bacali95/sweet-collections/workflows/Build/badge.svg)\n[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)\n![Coverage](coverage/badge.svg)\n[![npm version](https://badge.fury.io/js/sweet-collections.svg)](https://badge.fury.io/js/sweet-collections)\n\nTypescript implementations of in-memory cache data-structures for Node and Browser. These data-structures are:\n\n- **LruMap**: A fixed size `Map` which removes the least recently used entry.\n- **LruSet**: A fixed size `Set` which removes the least recently used entry. (Backed by a `LruMap`)\n- **LfuMap**: A fixed size `Map` which removes the least frequently used entry.\n- **LfuSet**: A fixed size `Set` which removes the least frequently used entry. (Backed by a `LfuMap`)\n- **SortedArray**: An `Array` that stays sorted after any modification.\n- **SortedMap**: A `Map` with entries stays sorted by key after any modification. (Backed by a `SortedArray`)\n- **SortedSet**: A `Set` with entries stays sorted after any modification. (Backed by a `SortedArray`)\n- **Heap**: A collection that have always the largest, smallest or most relevant value on top.\n- **Stack**: A collection that have always the last added value on top.\n- **Queue**: A collection that have always the first added value on top.\n\n## Install\n\n```sh\nnpm install --save sweet-collections\n```\n\nor\n\n```sh\nyarn add sweet-collections\n```\n\n## Usage\n\n\u003cdetails\u003e\n    \u003csummary\u003eLruMap\u003c/summary\u003e\n\n```Typescript\nimport { LruMap } from 'sweet-collections';\n\nconst map = new LruMap\u003cnumber, number\u003e(3);\nmap.set(1, 1);\nmap.set(2, 2);\nmap.set(3, 3);              // least recent used: 1\nconsole.log(map.has(1))     // true, least recent used: 2\nconsole.log(map.get(2))     // 1, least recent used: 3\n\nmap.set(4, 4);\nconsole.log(map.has(3))     // false\nconsole.log(map.get(3))     // undefined\n\nconsole.log(map.isFull())   // true\nmap.delete(1);\nconsole.log(map.size)       // 2\n\nmap.clear();\nconsole.log(map.size)       // 0\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n    \u003csummary\u003eLruSet\u003c/summary\u003e\n\n```Typescript\nimport { LruSet } from 'sweet-collections';\n\nconst set = new LruSet\u003cnumber\u003e(3);\nset.add(1);\nset.add(2);\nset.add(3);                 // least recent used: 1\nconsole.log(set.has(1))     // true, least recent used: 2\n\nset.add(4);\nconsole.log(set.has(2))     // false\n\nconsole.log(set.isFull())   // true\nset.delete(1);\nconsole.log(set.size)       // 2\n\nset.clear();\nconsole.log(set.size)       // 0\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n    \u003csummary\u003eLfuMap\u003c/summary\u003e\n\n```Typescript\nimport { LfuMap } from 'sweet-collections';\n\nconst map = new LfuMap\u003cnumber, number\u003e(3);\nmap.set(1, 1);\nmap.set(2, 2);\nmap.set(3, 3);              // least frequently used: 1\nconsole.log(map.has(1))     // true, least frequently used: 2\nconsole.log(map.get(2))     // 1, least frequently used: 3\n\nmap.set(4, 4);\nconsole.log(map.has(3))     // false\nconsole.log(map.get(3))     // undefined\n\nconsole.log(map.isFull())   // true\nmap.delete(1);\nconsole.log(map.size)       // 2\n\nmap.clear();\nconsole.log(map.size)       // 0\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n    \u003csummary\u003eLfuSet\u003c/summary\u003e\n\n```Typescript\nimport { LfuSet } from 'sweet-collections';\n\nconst set = new LfuSet\u003cnumber\u003e(3);\nset.add(1);\nset.add(2);\nset.add(3);                 // least frequently used: 1\nconsole.log(set.has(1))     // true, least frequently used: 2\nconsole.log(set.has(2))     // true, least frequently used: 3\n\nset.add(4);\nconsole.log(set.has(3))     // false\n\nconsole.log(set.isFull())   // true\nset.delete(1);\nconsole.log(set.size)       // 2\n\nset.clear();\nconsole.log(set.size)       // 0\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n    \u003csummary\u003eSortedArray\u003c/summary\u003e\n\n```Typescript\nimport { SortedArray } from 'sweet-collections';\n\n// Increasing order sorted array\nconst array = new SortedArray\u003cnumber\u003e((a: number, b: number) =\u003e a - b);\narray.push(4);\narray.push(1);\narray.push(2);\narray.push(3);\narray.push(5);\nconsole.log(array.toArray());       // [1, 2, 3, 4, 5]\nconsole.log(array.get(2));          // 3\nconsole.log(array.get(4));          // 5\nconsole.log(array.length);          // 5\n\narray.delete(4);\nconsole.log(array.toArray());       // [1, 2, 3, 5]\nconsole.log(array.includes(4));     // false\n\narray.push(1);\nconsole.log(array.toArray());       // [1, 1, 2, 3, 5]\nconsole.log(array.count(1));        // 2\nconsole.log(array.firstIndexOf(1)); // 0\nconsole.log(array.lastIndexOf(1));  // 1\n\nconsole.log(array.shift());         // 1\nconsole.log(array.pop());           // 1\nconsole.log(array.min());           // 1\nconsole.log(array.max());           // 3\nconsole.log(array.toArray());       // [1, 2, 3]\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n    \u003csummary\u003eSortedMap\u003c/summary\u003e\n\n```Typescript\nimport { SortedMap } from 'sweet-collections';\n\n// Increasing order sorted map\nconst map = new SortedMap\u003cnumber, string\u003e((a: number, b: number) =\u003e a - b);\nmap.set(3, 'c');\nmap.set(2, 'd');\nmap.set(5, 'a');\nmap.set(4, 'b');\nmap.set(1, 'e');\nconsole.log([...map.keys()]);       // [1, 2, 3, 4, 5]\nconsole.log([...map.values()]);     // [\"e\", \"d\", \"c\", \"b\", \"a\"]\nconsole.log(map.get(2));            // \"d\"\nconsole.log(map.get(4));            // \"b\"\nconsole.log(map.size);              // 5\n\nmap.delete(4);\nconsole.log([...map.keys()]);       // [1, 2, 3, 5]\nconsole.log([...map.values()]);     // [\"e\", \"d\", \"c\", \"a\"]\nconsole.log(map.has(4));            // false\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n    \u003csummary\u003eSortedSet\u003c/summary\u003e\n\n```Typescript\nimport { SortedSet } from 'sweet-collections';\n\n// Increasing order sorted set\nconst set = new SortedSet\u003cnumber\u003e((a: number, b: number) =\u003e a - b);\nset.add(3);\nset.add(2);\nset.add(5);\nset.add(4);\nset.add(1);\nconsole.log([...set.keys()]);       // [1, 2, 3, 4, 5]\nconsole.log(set.has(2));            // true\nconsole.log(set.has(4));            // true\nconsole.log(set.size);              // 5\n\nset.delete(4);\nconsole.log([...set.keys()]);       // [1, 2, 3, 5]\nconsole.log(set.has(4));            // false\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n    \u003csummary\u003eHeap\u003c/summary\u003e\n\n```Typescript\nimport { Heap } from 'sweet-collections';\n\n// Heap with the maximum value on top\nconst heap = new Heap\u003cnumber\u003e((a: number, b: number) =\u003e a \u003e b);\nheap.push(3);\nheap.push(2);\nheap.push(5);\nheap.push(4);\nheap.push(1);\nconsole.log(heap.peek());        // 5\nconsole.log(heap.pop());         // 5\nconsole.log(heap.peek());        // 4\nconsole.log(heap.replace(0));    // 4\nconsole.log(heap.peek());        // 3\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n    \u003csummary\u003eStack\u003c/summary\u003e\n\n```Typescript\nimport { Stack } from 'sweet-collections';\n\nconst stack = new Stack\u003cnumber\u003e();\nstack.push(3);\nstack.push(2);\nconsole.log(stack.top());         // 2\nstack.push(5, 4, 1);\nconsole.log(stack.pop());         // 1\nconsole.log(stack.top());         // 4\nconsole.log(stack.size);          // 4\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n    \u003csummary\u003eQueue\u003c/summary\u003e\n\n```Typescript\nimport { Queue } from 'sweet-collections';\n\nconst queue = new Queue\u003cnumber\u003e();\nqueue.push(3);\nqueue.push(2);\nconsole.log(queue.pop());         // 3\nqueue.push(5, 4, 1);\nconsole.log(queue.pop());         // 2\nconsole.log(queue.peek());        // 5\nconsole.log(queue.size);          // 3\n```\n\n\u003c/details\u003e\n\n## Author\n\n👤 **Nasreddine Bac Ali**\n\n- Website: [nasreddinebacali.info](https://nasreddinebacali.info)\n- Github: [@bacali95](https://github.com/bacali95)\n- LinkedIn: [@bacali](https://linkedin.com/in/bacali)\n\n## Show your support\n\nGive a ⭐️ if this project helped you!\n\n## 📝 License\n\nCopyright © 2021 [Nasreddine Bac Ali](https://github.com/bacali95). This project\nis [ISC](https://github.com/bacali95/sweet-collections/blob/master/LICENSE) licensed.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbacali95%2Fsweet-collections","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbacali95%2Fsweet-collections","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbacali95%2Fsweet-collections/lists"}