{"name":"heap","version":"0.2.0","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","dependencies":{},"devDependencies":{"coffee-script":">= 1.3.0","mocha":">= 0.14.0","should":">= 0.6.0"},"repository":{"type":"git","url":"git://github.com/qiao/heap.js.git"},"licenses":[{"type":"PSF","url":"http://docs.python.org/license.html"}],"_npmUser":{"name":"qiao","email":"xueqiaoxu@gmail.com"},"_id":"heap@0.2.0","optionalDependencies":{},"engines":{"node":"*"},"_engineSupported":true,"_npmVersion":"1.1.18","_nodeVersion":"v0.6.15","_defaultsLoaded":true,"dist":{"shasum":"8ea3bbd03f9cc17509dc0aed4502523eb4963486","tarball":"http://registry-npm.ywwl.com/heap/download/heap-0.2.0.tgz","integrity":"sha512-YscTQk3H1aAk2g/qGK/o+UylzP+IgkW7GxT/7cDHkdkz4/DJdAvXJnbnRs/9yJ5n/kT+AQ3SbL57apBY3D/Ecg==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIHAci6B57RTTnhKe09Ee9R5QUKqn97qaLoS5Vrlu9NFzAiBkBYobaXq3Kv5pYbmVC4U2VJT4OiruCJKxGXaCHZn7BA=="}],"size":6694,"noattachment":false,"key":"/heap/-/heap-0.2.0.tgz"},"maintainers":[{"name":"qiao","email":"xueqiaoxu@gmail.com"}],"directories":{},"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":1335113406403,"_cnpm_publish_time":1335113406403,"dist-tags":{"latest":"0.2.7"}}