heap.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // Generated by CoffeeScript 1.8.0
  2. (function() {
  3. var Heap, defaultCmp, floor, heapify, heappop, heappush, heappushpop, heapreplace, insort, min, nlargest, nsmallest, updateItem, _siftdown, _siftup;
  4. floor = Math.floor, min = Math.min;
  5. /*
  6. Default comparison function to be used
  7. */
  8. defaultCmp = function(x, y) {
  9. if (x < y) {
  10. return -1;
  11. }
  12. if (x > y) {
  13. return 1;
  14. }
  15. return 0;
  16. };
  17. /*
  18. Insert item x in list a, and keep it sorted assuming a is sorted.
  19. If x is already in a, insert it to the right of the rightmost x.
  20. Optional args lo (default 0) and hi (default a.length) bound the slice
  21. of a to be searched.
  22. */
  23. insort = function(a, x, lo, hi, cmp) {
  24. var mid;
  25. if (lo == null) {
  26. lo = 0;
  27. }
  28. if (cmp == null) {
  29. cmp = defaultCmp;
  30. }
  31. if (lo < 0) {
  32. throw new Error('lo must be non-negative');
  33. }
  34. if (hi == null) {
  35. hi = a.length;
  36. }
  37. while (lo < hi) {
  38. mid = floor((lo + hi) / 2);
  39. if (cmp(x, a[mid]) < 0) {
  40. hi = mid;
  41. } else {
  42. lo = mid + 1;
  43. }
  44. }
  45. return ([].splice.apply(a, [lo, lo - lo].concat(x)), x);
  46. };
  47. /*
  48. Push item onto heap, maintaining the heap invariant.
  49. */
  50. heappush = function(array, item, cmp) {
  51. if (cmp == null) {
  52. cmp = defaultCmp;
  53. }
  54. array.push(item);
  55. return _siftdown(array, 0, array.length - 1, cmp);
  56. };
  57. /*
  58. Pop the smallest item off the heap, maintaining the heap invariant.
  59. */
  60. heappop = function(array, cmp) {
  61. var lastelt, returnitem;
  62. if (cmp == null) {
  63. cmp = defaultCmp;
  64. }
  65. lastelt = array.pop();
  66. if (array.length) {
  67. returnitem = array[0];
  68. array[0] = lastelt;
  69. _siftup(array, 0, cmp);
  70. } else {
  71. returnitem = lastelt;
  72. }
  73. return returnitem;
  74. };
  75. /*
  76. Pop and return the current smallest value, and add the new item.
  77. This is more efficient than heappop() followed by heappush(), and can be
  78. more appropriate when using a fixed size heap. Note that the value
  79. returned may be larger than item! That constrains reasonable use of
  80. this routine unless written as part of a conditional replacement:
  81. if item > array[0]
  82. item = heapreplace(array, item)
  83. */
  84. heapreplace = function(array, item, cmp) {
  85. var returnitem;
  86. if (cmp == null) {
  87. cmp = defaultCmp;
  88. }
  89. returnitem = array[0];
  90. array[0] = item;
  91. _siftup(array, 0, cmp);
  92. return returnitem;
  93. };
  94. /*
  95. Fast version of a heappush followed by a heappop.
  96. */
  97. heappushpop = function(array, item, cmp) {
  98. var _ref;
  99. if (cmp == null) {
  100. cmp = defaultCmp;
  101. }
  102. if (array.length && cmp(array[0], item) < 0) {
  103. _ref = [array[0], item], item = _ref[0], array[0] = _ref[1];
  104. _siftup(array, 0, cmp);
  105. }
  106. return item;
  107. };
  108. /*
  109. Transform list into a heap, in-place, in O(array.length) time.
  110. */
  111. heapify = function(array, cmp) {
  112. var i, _i, _j, _len, _ref, _ref1, _results, _results1;
  113. if (cmp == null) {
  114. cmp = defaultCmp;
  115. }
  116. _ref1 = (function() {
  117. _results1 = [];
  118. for (var _j = 0, _ref = floor(array.length / 2); 0 <= _ref ? _j < _ref : _j > _ref; 0 <= _ref ? _j++ : _j--){ _results1.push(_j); }
  119. return _results1;
  120. }).apply(this).reverse();
  121. _results = [];
  122. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  123. i = _ref1[_i];
  124. _results.push(_siftup(array, i, cmp));
  125. }
  126. return _results;
  127. };
  128. /*
  129. Update the position of the given item in the heap.
  130. This function should be called every time the item is being modified.
  131. */
  132. updateItem = function(array, item, cmp) {
  133. var pos;
  134. if (cmp == null) {
  135. cmp = defaultCmp;
  136. }
  137. pos = array.indexOf(item);
  138. if (pos === -1) {
  139. return;
  140. }
  141. _siftdown(array, 0, pos, cmp);
  142. return _siftup(array, pos, cmp);
  143. };
  144. /*
  145. Find the n largest elements in a dataset.
  146. */
  147. nlargest = function(array, n, cmp) {
  148. var elem, result, _i, _len, _ref;
  149. if (cmp == null) {
  150. cmp = defaultCmp;
  151. }
  152. result = array.slice(0, n);
  153. if (!result.length) {
  154. return result;
  155. }
  156. heapify(result, cmp);
  157. _ref = array.slice(n);
  158. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  159. elem = _ref[_i];
  160. heappushpop(result, elem, cmp);
  161. }
  162. return result.sort(cmp).reverse();
  163. };
  164. /*
  165. Find the n smallest elements in a dataset.
  166. */
  167. nsmallest = function(array, n, cmp) {
  168. var elem, i, los, result, _i, _j, _len, _ref, _ref1, _results;
  169. if (cmp == null) {
  170. cmp = defaultCmp;
  171. }
  172. if (n * 10 <= array.length) {
  173. result = array.slice(0, n).sort(cmp);
  174. if (!result.length) {
  175. return result;
  176. }
  177. los = result[result.length - 1];
  178. _ref = array.slice(n);
  179. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  180. elem = _ref[_i];
  181. if (cmp(elem, los) < 0) {
  182. insort(result, elem, 0, null, cmp);
  183. result.pop();
  184. los = result[result.length - 1];
  185. }
  186. }
  187. return result;
  188. }
  189. heapify(array, cmp);
  190. _results = [];
  191. for (i = _j = 0, _ref1 = min(n, array.length); 0 <= _ref1 ? _j < _ref1 : _j > _ref1; i = 0 <= _ref1 ? ++_j : --_j) {
  192. _results.push(heappop(array, cmp));
  193. }
  194. return _results;
  195. };
  196. _siftdown = function(array, startpos, pos, cmp) {
  197. var newitem, parent, parentpos;
  198. if (cmp == null) {
  199. cmp = defaultCmp;
  200. }
  201. newitem = array[pos];
  202. while (pos > startpos) {
  203. parentpos = (pos - 1) >> 1;
  204. parent = array[parentpos];
  205. if (cmp(newitem, parent) < 0) {
  206. array[pos] = parent;
  207. pos = parentpos;
  208. continue;
  209. }
  210. break;
  211. }
  212. return array[pos] = newitem;
  213. };
  214. _siftup = function(array, pos, cmp) {
  215. var childpos, endpos, newitem, rightpos, startpos;
  216. if (cmp == null) {
  217. cmp = defaultCmp;
  218. }
  219. endpos = array.length;
  220. startpos = pos;
  221. newitem = array[pos];
  222. childpos = 2 * pos + 1;
  223. while (childpos < endpos) {
  224. rightpos = childpos + 1;
  225. if (rightpos < endpos && !(cmp(array[childpos], array[rightpos]) < 0)) {
  226. childpos = rightpos;
  227. }
  228. array[pos] = array[childpos];
  229. pos = childpos;
  230. childpos = 2 * pos + 1;
  231. }
  232. array[pos] = newitem;
  233. return _siftdown(array, startpos, pos, cmp);
  234. };
  235. Heap = (function() {
  236. Heap.push = heappush;
  237. Heap.pop = heappop;
  238. Heap.replace = heapreplace;
  239. Heap.pushpop = heappushpop;
  240. Heap.heapify = heapify;
  241. Heap.updateItem = updateItem;
  242. Heap.nlargest = nlargest;
  243. Heap.nsmallest = nsmallest;
  244. function Heap(cmp) {
  245. this.cmp = cmp != null ? cmp : defaultCmp;
  246. this.nodes = [];
  247. }
  248. Heap.prototype.push = function(x) {
  249. return heappush(this.nodes, x, this.cmp);
  250. };
  251. Heap.prototype.pop = function() {
  252. return heappop(this.nodes, this.cmp);
  253. };
  254. Heap.prototype.peek = function() {
  255. return this.nodes[0];
  256. };
  257. Heap.prototype.contains = function(x) {
  258. return this.nodes.indexOf(x) !== -1;
  259. };
  260. Heap.prototype.replace = function(x) {
  261. return heapreplace(this.nodes, x, this.cmp);
  262. };
  263. Heap.prototype.pushpop = function(x) {
  264. return heappushpop(this.nodes, x, this.cmp);
  265. };
  266. Heap.prototype.heapify = function() {
  267. return heapify(this.nodes, this.cmp);
  268. };
  269. Heap.prototype.updateItem = function(x) {
  270. return updateItem(this.nodes, x, this.cmp);
  271. };
  272. Heap.prototype.clear = function() {
  273. return this.nodes = [];
  274. };
  275. Heap.prototype.empty = function() {
  276. return this.nodes.length === 0;
  277. };
  278. Heap.prototype.size = function() {
  279. return this.nodes.length;
  280. };
  281. Heap.prototype.clone = function() {
  282. var heap;
  283. heap = new Heap();
  284. heap.nodes = this.nodes.slice(0);
  285. return heap;
  286. };
  287. Heap.prototype.toArray = function() {
  288. return this.nodes.slice(0);
  289. };
  290. Heap.prototype.insert = Heap.prototype.push;
  291. Heap.prototype.top = Heap.prototype.peek;
  292. Heap.prototype.front = Heap.prototype.peek;
  293. Heap.prototype.has = Heap.prototype.contains;
  294. Heap.prototype.copy = Heap.prototype.clone;
  295. return Heap;
  296. })();
  297. (function(root, factory) {
  298. if (typeof define === 'function' && define.amd) {
  299. return define([], factory);
  300. } else if (typeof exports === 'object') {
  301. return module.exports = factory();
  302. } else {
  303. return root.Heap = factory();
  304. }
  305. })(this, function() {
  306. return Heap;
  307. });
  308. }).call(this);