{"name":"heap","version":"0.2.7","description":"binary heap (priority queue) algorithms (ported from Python's heapq module)","homepage":"https://github.com/qiao/heap.js","keywords":["algorithm","data structure","heap"],"author":{"name":"Xueqiao Xu","email":"xueqiaoxu@gmail.com"},"main":"./index.js","devDependencies":{"coffee-script":"1.3.x","mocha":"2.1.x","should":"0.6.x"},"scripts":{"test":"make test"},"repository":{"type":"git","url":"git://github.com/qiao/heap.js.git"},"license":"MIT","gitHead":"21701c0b9c22039de6f948ed5f6d44e53ae79231","bugs":{"url":"https://github.com/qiao/heap.js/issues"},"_id":"heap@0.2.7","_nodeVersion":"17.0.1","_npmVersion":"8.1.0","dist":{"integrity":"sha512-2bsegYkkHO+h/9MGbn6KWcE45cHZgPANo5LXF7EvWdT0yT2EguSVO1nDgU5c8+ZOPwp2vMNa7YFsJhVcDR9Sdg==","shasum":"1e6adf711d3f27ce35a81fe3b7bd576c2260a8fc","tarball":"http://registry-npm.ywwl.com/heap/download/heap-0.2.7.tgz","fileCount":10,"unpackedSize":23093,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhqICJCRA9TVsSAnZWagAAZHgP+wWdPeqMxVG3Kl0STO4n\ni4fu0mjzget+KR79uwqozeCHjc/M08zLDG+fsqyEENfQuT99Uxg94tyDejtR\nZTn4IH2GMYh+uO7Cu3pTNULs8eMCxym/sx21xwDdm7vUJRp4XJWdNg8StsKs\ncY46tr5ESThqMPfBHnu+z57ubndbPD6prkNyjZ5luc7RjwtLb2wcCsjXjMnV\nExWcIiThjWMM/l2TNQVB4J3DL15s8FPAdV5MGYPsQ7aJkf4HNMDX6CLtLJq3\ndS1vEMNVbnrezzV4/U1I9xawbj4b2GbZ2N6Qq+nRId/fmNsa//wj6QlidClF\n+OJd5bwuj1biY5RSXy/BwqgMedhmkED9nyN9f8EpQ0fUx0oFmhQ0UWkYafaU\n7tsIG6ksZoSqH9uSB5iQEYO4tFSuPDLJNMJe72iTZa1mvUqyFR4XPSxl9DvC\nWuUHKyL7fz1A/wkRxq1ONO99AnCHGcmEophZK6HoL1yEYb+xooQqtHfTMUx7\nQweYIsyGTyuRQm6AeryiZH79iB6oJMVwKrETBOnXP5BbHcPX0DoN8DvFliHf\nSHRndolJ30+Zc0RX1wbHO4BbPQWPhaYcMM6V2dCpzcnxUoYkeRieEPC0SPDv\n0hTSMKRx5tRitgSkzo6Vu29XO2NJVjvu6BowfPG6mxlyCHxZVo6dCH6IaQ42\nrLs1\r\n=P+NH\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCel0A68eJLQHZQt3pRKL0SWv6jIDi4WBlKXFf5LhmhfAIhAMxcnoZ2uscoYuDi09lYqXvDJDGHae+gf6UpajY0x44v"}],"size":6718,"noattachment":false,"key":"/heap/-/heap-0.2.7.tgz"},"_npmUser":{"name":"qiao","email":"xueqiaoxu@gmail.com"},"directories":{},"maintainers":[{"name":"qiao","email":"xueqiaoxu@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/heap_0.2.7_1638432905165_0.29257309632662976"},"_hasShrinkwrap":false,"readme":"Heap.js\n=======\n\n[![Build Status](https://travis-ci.org/qiao/heap.js.svg?branch=master)](https://travis-ci.org/qiao/heap.js)\n\nA binary heap implementation in CoffeeScript/JavaScript. Ported from Python's [heapq](http://docs.python.org/library/heapq.html) module.\n\n\nDownload\n--------\n\nThis module can be used in either the browser or node.js.\n\nfor browser use, you may [download the script](https://raw.github.com/qiao/heap.js/master/lib/heap.js) and include it in you web page.\n\n```html\n<script type=\"text/javascript\" src=\"./heap.js\"></script>\n```\n\nfor node.js, you may install it via npm:\n\n```bash\nnpm install heap\n```\n\nthen require it:\n\n```\nvar Heap = require('heap');\n```\n\nExamples\n-------\n\n\npush and pop\n\n```js\nvar heap = new Heap();\nheap.push(3);\nheap.push(1);\nheap.push(2);\nheap.pop(); // 1\n```\n\ncustom comparison function\n\n```js\nvar heap = new Heap(function(a, b) {\n    return a.foo - b.foo;\n});\nheap.push({foo: 3});\nheap.push({foo: 1});\nheap.push({foo: 2});\nheap.pop(); // {foo: 1}\n```\n\nfind 3 largest/smallest items in an array\n\n```js\nvar array = [1, 3, 4, 2, 5];\nHeap.nlargest(array, 3);  // [5, 4, 3]\nHeap.nsmallest(array, 3); // [1, 2, 3]\n```\n\nDocument\n--------\n\nThis module exposes only one object, namely the Heap class.\n\n### Constructor: Heap([cmp]) ###\n\nThe constructor receives a comparison function as an optional parameter. If omitted, the heap is built as a min-heap, which means that the smallest element will be popped out first.\n\nIf the comparison function is supplied, the heap will be built according to the \nreturn value of the comparison function.\n\n* if cmp(a, b) < 0, then item a will come prior to b\n* if cmp(a, b) > 0, then item b will come prior to a\n\nSo, the comparison function has the following form:\n\n```js\nfunction cmp(a, b) {\n  if (a is prior to b) {\n    return -1;\n  } \n  if (b is prior to a) {\n    return 1;\n  }\n  return 0;\n}\n```\n\nTo compare numbers, simply: \n\n```js\nfunction cmp(a, b) {\n  return a - b;\n}\n```\n\n### Instance Methods ###\n\n**push(item)** (alias: **insert**) \n\nPush item onto heap.\n\n**pop()**\n\nPop the smallest item off the heap and return it.\n\n**peek()** (alias: **top** / **front**)\n\nReturn the smallest item of the heap.\n\n**replace(item)**\n\nPop and return the current smallest value, and add the new item.\n\nThis is more efficient than pop() followed by push(), and can be \nmore appropriate when using a fixed size heap. Note that the value\nreturned may be larger than item! \n\n**pushpop(item)**\n\nFast version of a push followed by a pop.\n\n**heapify()**\n\nRebuild the heap. This method may come handy when the priority of the \ninternal data is being modified.\n\n**updateItem(item)**\n\nUpdate the position of the given item in the heap.\nThis function should be called every time the item is being modified.\n\n**empty()**\n\nDetermine whether the heap is empty.\n\n**size()**\n\nGet the number of elements stored in the heap.\n\n**toArray()**\n\nReturn the array representation of the heap. (note: the array is a shallow copy of the heap's internal nodes)\n\n**clone()** (alias: **copy**)\n\nReturn a clone of the heap. (note: the internal data is a shallow copy of the original one)\n\n### Static Methods ###\n\nNOTE: All the static methods are designed to be applied on arrays.\n\n**push(array, item, [cmp])** \n\nPush item onto array, maintaining the heap invariant.\n\n**pop(array, [cmp])**\n\nPop the smallest item off the array, maintaining the heap invariant.\n\n**replace(array, item, [cmp])**\n\nPop and return the current smallest value, and add the new item.\n\nThis is more efficient than heappop() followed by heappush(), and can be \nmore appropriate when using a fixed size heap. Note that the value\nreturned may be larger than item! \n\n**pushpop(array, item, [cmp])**\n\nFast version of a heappush followed by a heappop.\n\n**heapify(array, [cmp])**\n\nBuild the heap.\n\n**updateItem(array, item, [cmp])**\n\nUpdate the position of the given item in the heap.\nThis function should be called every time the item is being modified.\n\n**nlargest(array, n, [cmp])**\n\nFind the n largest elements in a dataset.\n\n**nsmallest(array, n, [cmp])**\n\nFind the n smallest elements in a dataset.\n","publish_time":1638432905314,"_cnpm_publish_time":1638432905314,"dist-tags":{"latest":"0.2.7"}}