Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
jsheap を更新
  • Loading branch information
shogo82148 committed May 18, 2013
1 parent 4619911 commit 42fa373
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 128 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
@@ -0,0 +1,3 @@
[submodule "lib/jsheap"]
path = lib/jsheap
url = https://github.com/shogo82148/jsheap.git
2 changes: 1 addition & 1 deletion Makefile
@@ -1,4 +1,4 @@
LIB = lib/jsheap.js
LIB = lib/jsheap/jsheap.js
SRC = src/tagger.js src/dictionary.js src/trie.js src/util.js;

all: build/igo.js
Expand Down
2 changes: 1 addition & 1 deletion build/build.js
Expand Up @@ -4,7 +4,7 @@ var fs = require('fs');
var jsp = require("./uglify-js").parser;
var pro = require("./uglify-js").uglify;

var liblist = ["jsheap.js"];
var liblist = ["jsheap/jsheap.js"];
var srclist = ["tagger.js", "dictionary.js", "trie.js", "util.js"];

var orig_code = "";
Expand Down
129 changes: 67 additions & 62 deletions build/igo.js
@@ -1,69 +1,74 @@



/********** lib/jsheap.js **********/
/********** lib/jsheap/jsheap.js **********/
(function(global) {
global.jsheap = function(cmp) {
//Compare function
this.cmp = cmp || function(a, b) {
return a<b;
};

// heap[0] is dummpy
this.heap = [null];
};

function swap(heap, a, b) {
var tmp = heap[a];
heap[a] = heap[b];
heap[b] = tmp;
}

global.jsheap.prototype.push = function(item) {
var heap = this.heap;
var cmp = this.cmp;
var i = heap.length;

heap.push(item);
while(i>1 && cmp(heap[i], heap[Math.floor(i/2)])) {
var j = Math.floor(i/2);
swap(heap, i, j);
i = j;
}
return this;
};

global.jsheap.prototype.top = function() {
return this.heap[1];
};

global.jsheap.prototype.pop = function() {
var heap = this.heap;
var cmp = this.cmp;
var i;
var item = heap.pop();
if(this.empty()) return this;
i = 1;
heap[1] = item;

while(i*2 < heap.length) {
var j = i*2;
if(j+1 < heap.length && cmp(heap[j+1], heap[j])) {
++j;
}
if(!cmp(heap[j], heap[i])) {
break;
}
swap(heap, i, j);
i = j;
}

return this;
};

global.jsheap.prototype.empty = function() {
return this.heap.length <= 1;
};
function default_cmp(a, b) {
return a < b;
}

var jsheap = global.jsheap = function(cmp) {
//Compare function
this.cmp = cmp || default_cmp;

// heap[0] is dummpy
this.heap = [null];
};

function swap(heap, a, b) {
var tmp = heap[a];
heap[a] = heap[b];
heap[b] = tmp;
}

jsheap.prototype.push = function(item) {
var heap = this.heap;
var cmp = this.cmp;
var i = heap.length;
var j;

heap.push(item);
while(i>1 && cmp(heap[i], heap[ j = i/2 | 0 ])) {
swap(heap, i, j);
i = j;
}
return this;
};

jsheap.prototype.top = function() {
return this.heap[1];
};

jsheap.prototype.pop = function() {
if(this.empty()) throw new Error('heap is empty');
var heap = this.heap;
var cmp = this.cmp;
var i;
var item = heap.pop();
var length = heap.length;
if(length == 1) return this;

i = 1;
heap[1] = item;

while(i*2 < length) {
var j = i*2;
if(j+1 < length && cmp(heap[j+1], heap[j])) {
++j;
}
if(!cmp(heap[j], heap[i])) {
break;
}
swap(heap, i, j);
i = j;
}

return this;
};

jsheap.prototype.empty = function() {
return this.heap.length <= 1;
};
})(this);


Expand Down
2 changes: 1 addition & 1 deletion build/igo.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/jsheap
Submodule jsheap added at bd5fd3
63 changes: 0 additions & 63 deletions lib/jsheap.js

This file was deleted.

0 comments on commit 42fa373

Please sign in to comment.