{"id":13736282,"url":"https://github.com/StefanSalewski/minmaxheap","last_synced_at":"2025-05-08T12:32:30.677Z","repository":{"id":109492581,"uuid":"175826186","full_name":"StefanSalewski/minmaxheap","owner":"StefanSalewski","description":"Heap with minimum and maximum access","archived":false,"fork":false,"pushed_at":"2019-03-15T13:39:48.000Z","size":7,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-30T23:24:24.265Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Nim","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/StefanSalewski.png","metadata":{"files":{"readme":"README.adoc","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}},"created_at":"2019-03-15T13:33:20.000Z","updated_at":"2023-09-11T00:25:45.000Z","dependencies_parsed_at":"2023-07-17T17:56:43.096Z","dependency_job_id":null,"html_url":"https://github.com/StefanSalewski/minmaxheap","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StefanSalewski%2Fminmaxheap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StefanSalewski%2Fminmaxheap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StefanSalewski%2Fminmaxheap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StefanSalewski%2Fminmaxheap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/StefanSalewski","download_url":"https://codeload.github.com/StefanSalewski/minmaxheap/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253068924,"owners_count":21848890,"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":[],"created_at":"2024-08-03T03:01:18.814Z","updated_at":"2025-05-08T12:32:30.367Z","avatar_url":"https://github.com/StefanSalewski.png","language":"Nim","funding_links":[],"categories":["Data"],"sub_categories":["Data Structures"],"readme":"= Heap with minimum and maximum access\n(c) Stefan Salewski\n:experimental:\n:source-highlighter: pygments\n:pygments-style: monokai\n:icons: font\n\nNim implementation of a Minimum-Maximum Heap as described in\n\nhttps://en.wikipedia.org/wiki/Min-max_heap\n\nhttp://www.akira.ruc.dk/~keld/teaching/algoritmedesign_f03/Artikler/02/Atkinson86.pdf\n\nNOTE: See also https://nim-lang.github.io/Nim/heapqueue.html\n\nNOTE: Nim generated API documentation is available here: http://ssalewski.de/minmaxheap.html\n\nThe implementation follow very closely these papers, writing the Nim code\nwas straight forward. The heap is generic, this means it works for most data elements\nwhen \"\u003c\" relation is defined. Add(), popMin() and popMax() operations are O(log N), access\nto min and max is O(1) according to Wikipedia. Build time of initial heap is linear O(N)\nand read access to min or max element is O(1).\n\nUse cases for such type of a heap are rare. One application is a ID-Number generator,\nwhere IDs are generated in increasing order, but can be returned and reused. Reused is always the smallest\navailable ID, but at the same time we need to know which is the largest ID still in use. That can be\ndone with popMin() and max() operations. In the original paper external quicksort was mentioned as a use case.\n\nInitial heap can be generated from an array or a seq. Pop() operations will fail when len()\nis already zero.\n\nThe elements in heap are stored in a seq, so there is nearly no space overhead involved.\n\n.Cost in big O notation as a function of number of elements (N)\n[options=\"header\"]\n|===\n| proc name | operation | costs |\n|toMinMaxHeap() | creation  | O(N) |\n|initMinMaxHeap() | create empty heap  | O(1) |\n|min() | get minimum  | O(1) |\n|max() | get maximum  | O(1) |\n|clear() | reset  | O(1) |\n|len() | get number of elements  | O(1) |\n|add() | insert an element  | O(log(n)) |\n|popMin() | delete minimum  | O(log(n)) |\n|popMax() | delete maximum  | O(log(n)) |\n|===\n\nInstall:\n\n----\nnimble install https://github.com/StefanSalewski/minmaxheap\n----\n\nCurrently these procs are available:\n\n[[API]]\n[source,nim]\n.API\n----\ntype MinMaxHeap*[T] = distinct seq[T]\n\nproc initMinMaxHeap*[T](): MinMaxHeap[T]\n\nproc toMinMaxHeap*[T](h: openarray[T]): MinMaxHeap[T]\n\nproc clear*[T](h: var MinMaxHeap[T])\n\nproc len*[T](h: MinMaxHeap[T]): int\n\nproc empty*[T](h: MinMaxHeap[T]): bool\n\nproc add*[T](h: var MinMaxHeap[T]; i: T)\n\nproc min*[T](h: var MinMaxHeap[T]): T\n\nproc max*[T](h: var MinMaxHeap[T]): T\n\nproc popMin*[T](h: var MinMaxHeap[T]): T\n\nproc popMax*[T](h: var MinMaxHeap[T]): T\n\nproc `$`*[T](h: MinMaxHeap[T]): string =\n----\n\nHere is an example how the heap can be used:\n\n[[t.nim]]\n[source,nim]\n.t.nim\n----\nimport minmaxheap\n\nvar\n  # h = toMinMaxHeap[int](@[5, 3, 9, 1, 0])\n  h = toMinMaxHeap([5, 3, 9, 1, 0])\n\necho $(h) # @[0, 5, 9, 1, 3]\necho h.min # 0\necho h.max # 9\necho h.len # 5\necho h.popMin # 0\necho h.popMax # 9\necho h.len # 3\necho h.min # 1\necho h.max # 5\nh.add(8)\necho h.len # 4\necho h.popMax # 8\necho h.len # 3\nh.clear\necho h.len # 0\n\ntype\n  O = object\n    key: float\n\n# we need \u003c operator defines for elements\nproc `\u003c`(a, b: O): bool =\n  a.key \u003c b.key\n\nvar\n  l = initMinMaxHeap[O]()\nl.add(O(key: 1.2))\nl.add(O(key: 0.9))\nl.add(O(key: 3.3))\necho l.popMax # (key: 3.3)\necho l.popMin # (key: 0.9)\n----\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FStefanSalewski%2Fminmaxheap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FStefanSalewski%2Fminmaxheap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FStefanSalewski%2Fminmaxheap/lists"}