forked from heroic/Rucksack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rucksack.js
54 lines (51 loc) · 1.36 KB
/
rucksack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
$.fn.rucksack = function(options){
options = options || {}
var self = this, els = self.children(), b = parseInt(options.width,10) || 960, cl = options.class || 'rucksack';
function knapsack(a, b){
for(var k = 1; k <= a.length; k++)
for (var y = 1; y <= b; y++)
if (y < $(a[k-1]).outerWidth())
knap[k][y-1] = knap[k-1][y-1];
else if (y > $(a[k-1]).outerWidth())
knap[k][y-1] = Math.max(knap[k-1][y-1], knap[k-1][y-1-$(a[k-1]).outerWidth()] + 1);
else
knap[k][y-1] = Math.max(knap[k-1][y-1], 1);
return knap[a.length][b-1];
};
function findKnap(knap, b, a){
k = a.length;
var soln = new Array(k);
for (var j = 0; j < k; j++)
soln[j] = 0
while (k > 0) {
while(k > 0 && knap[k-1][b-1] == knap[k][b-1])
k--;
soln[k-1] = 1;
b -= $(a[k-1]).outerWidth();
k--;
}
return soln;
};
var knap
function init(a, b){
knap = new Array(a.length+1);
var i, found = -1, soln, div;
for(i = 0; i < a.length + 1; i++)
knap[i] = new Array(b)
for(i = 0; i < b; i++)
knap[0][i] = 0;
knapsack(a, b);
soln = findKnap(knap, b, a);
div = $('<div class="'+cl+'"></div')
for(i = soln.length - 1; i >= 0; i--)
if(1 === soln[i]){
div.append(a[i])
a.splice(i,1)
found++;
}
self.get(0).insertBefore(div.get(0), self.get(0).lastChild.nextSibling);
if(-1 !== found && a.length)
init(a, b)
}
init(els, b)
}