column_pack 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIT-LICENSE +20 -20
- data/Rakefile +32 -32
- data/lib/column_pack.rb +9 -9
- data/lib/column_pack/bin_packer.rb +106 -104
- data/lib/column_pack/column_packer.rb +54 -54
- data/lib/column_pack/railtie.rb +11 -11
- data/lib/column_pack/version.rb +3 -3
- data/lib/column_pack/view_helpers.rb +51 -51
- data/test/bin_packer_test.rb +139 -139
- data/test/bp_perf.rb +26 -26
- data/test/column_packer_test.rb +16 -16
- data/test/fixtures/files/five.txt +5 -5
- data/test/fixtures/files/hundred.txt +100 -100
- data/test/fixtures/files/pack.html.erb +11 -11
- data/test/fixtures/files/ten.txt +10 -10
- data/test/fixtures/files/thirty.txt +31 -31
- data/test/fixtures/files/twenty.txt +20 -20
- data/test/test_helper.rb +13 -13
- data/test/view_helper_test.rb +45 -45
- data/vendor/assets/stylesheets/column_pack.css +30 -30
- metadata +16 -13
- data/lib/tasks/column_pack_tasks.rake +0 -4
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/test.log +0 -8820
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea52916a285ff3137674ec892b04097564ef5b51
|
4
|
+
data.tar.gz: f88ca4053df9079852c7c665ea0092dc644b3330
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc117743b86cb42061d308e96b66ed4b7c7b7f127784e40e8d41a035f183f0c7813dbd7167fdd816979a283cc633c8a5c63006db9e9a06eb61f56f63af031a72
|
7
|
+
data.tar.gz: cab6dbf8e18dfd3576a3944eabf3c6cf871e33fd111729b63c8e5997386e05cb0b42c46b17edc5721d9f8eb0689ab22b4aab03944f674596d6166d127da18d8c
|
data/MIT-LICENSE
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
-
Copyright 2014
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
-
a copy of this software and associated documentation files (the
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
9
|
-
the following conditions:
|
10
|
-
|
11
|
-
The above copyright notice and this permission notice shall be
|
12
|
-
included in all copies or substantial portions of the Software.
|
13
|
-
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
1
|
+
Copyright 2014 Justin Tanner
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
CHANGED
@@ -1,32 +1,32 @@
|
|
1
|
-
begin
|
2
|
-
require 'bundler/setup'
|
3
|
-
rescue LoadError
|
4
|
-
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
-
end
|
6
|
-
|
7
|
-
require 'rdoc/task'
|
8
|
-
|
9
|
-
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
-
rdoc.rdoc_dir = 'rdoc'
|
11
|
-
rdoc.title = 'ColumnPack'
|
12
|
-
rdoc.options << '--line-numbers'
|
13
|
-
rdoc.rdoc_files.include('README.rdoc')
|
14
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
-
end
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
Bundler::GemHelper.install_tasks
|
21
|
-
|
22
|
-
require 'rake/testtask'
|
23
|
-
|
24
|
-
Rake::TestTask.new(:test) do |t|
|
25
|
-
t.libs << 'lib'
|
26
|
-
t.libs << 'test'
|
27
|
-
t.pattern = 'test/**/*_test.rb'
|
28
|
-
t.verbose = false
|
29
|
-
end
|
30
|
-
|
31
|
-
|
32
|
-
task default: :test
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'ColumnPack'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
Bundler::GemHelper.install_tasks
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
|
24
|
+
Rake::TestTask.new(:test) do |t|
|
25
|
+
t.libs << 'lib'
|
26
|
+
t.libs << 'test'
|
27
|
+
t.pattern = 'test/**/*_test.rb'
|
28
|
+
t.verbose = false
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
task default: :test
|
data/lib/column_pack.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
require 'column_pack/railtie' if defined? Rails
|
2
|
-
|
3
|
-
module ColumnPack
|
4
|
-
module Rails
|
5
|
-
class Engine < ::Rails::Engine
|
6
|
-
# Get rails to add app, lib, vendor to load path
|
7
|
-
end
|
8
|
-
end
|
9
|
-
end
|
1
|
+
require 'column_pack/railtie' if defined? Rails
|
2
|
+
|
3
|
+
module ColumnPack
|
4
|
+
module Rails
|
5
|
+
class Engine < ::Rails::Engine
|
6
|
+
# Get rails to add app, lib, vendor to load path
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
@@ -1,104 +1,106 @@
|
|
1
|
-
module ColumnPack
|
2
|
-
|
3
|
-
# Arranges elements into bins using a simple one dimensional bin packing algorithm.
|
4
|
-
class BinPacker
|
5
|
-
|
6
|
-
# Uses a fixed number of bins (total_bins).
|
7
|
-
#
|
8
|
-
# Options:
|
9
|
-
# :algorithm specify a different bin packing algorithm (default :best_fit_decreasing)
|
10
|
-
# available algorithms are :best_fit_decreasing, :best_fit_increasing
|
11
|
-
#
|
12
|
-
# :shuffle_in_col after packing columns, shuffle the elements in each column (defaults to true)
|
13
|
-
#
|
14
|
-
def initialize(total_bins, options = {})
|
15
|
-
raise ArgumentError.new("Must choose a number of bins greater than zero") if total_bins <= 0
|
16
|
-
|
17
|
-
@total_bins = total_bins
|
18
|
-
@algorithm = options[:algorithm] || :best_fit_decreasing
|
19
|
-
|
20
|
-
if options.has_key? :shuffle_in_col
|
21
|
-
@shuffle_in_col = options[:shuffle_in_col]
|
22
|
-
else
|
23
|
-
@shuffle_in_col = true
|
24
|
-
end
|
25
|
-
|
26
|
-
@elements = []
|
27
|
-
@needs_packing = true
|
28
|
-
end
|
29
|
-
|
30
|
-
# Adds element to be packed.
|
31
|
-
def add(size, content)
|
32
|
-
raise ArgumentError.new("Bin size must be greater than zero") if size <= 0
|
33
|
-
|
34
|
-
@elements << {:size => size.to_i, :content => content}
|
35
|
-
@needs_packing = true
|
36
|
-
end
|
37
|
-
|
38
|
-
# Returns a packed multi-dimensional array of elements.
|
39
|
-
def bins
|
40
|
-
pack_all if @needs_packing
|
41
|
-
@bins
|
42
|
-
end
|
43
|
-
|
44
|
-
# Total empty space left over by uneven packing.
|
45
|
-
def empty_space
|
46
|
-
pack_all if @needs_packing
|
47
|
-
max = @sizes.each.max
|
48
|
-
space = 0
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
@bins[
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
end
|
1
|
+
module ColumnPack
|
2
|
+
|
3
|
+
# Arranges elements into bins using a simple one dimensional bin packing algorithm.
|
4
|
+
class BinPacker
|
5
|
+
|
6
|
+
# Uses a fixed number of bins (total_bins).
|
7
|
+
#
|
8
|
+
# Options:
|
9
|
+
# :algorithm specify a different bin packing algorithm (default :best_fit_decreasing)
|
10
|
+
# available algorithms are :best_fit_decreasing, :best_fit_increasing
|
11
|
+
#
|
12
|
+
# :shuffle_in_col after packing columns, shuffle the elements in each column (defaults to true)
|
13
|
+
#
|
14
|
+
def initialize(total_bins, options = {})
|
15
|
+
raise ArgumentError.new("Must choose a number of bins greater than zero") if total_bins <= 0
|
16
|
+
|
17
|
+
@total_bins = total_bins
|
18
|
+
@algorithm = options[:algorithm] || :best_fit_decreasing
|
19
|
+
|
20
|
+
if options.has_key? :shuffle_in_col
|
21
|
+
@shuffle_in_col = options[:shuffle_in_col]
|
22
|
+
else
|
23
|
+
@shuffle_in_col = true
|
24
|
+
end
|
25
|
+
|
26
|
+
@elements = []
|
27
|
+
@needs_packing = true
|
28
|
+
end
|
29
|
+
|
30
|
+
# Adds element to be packed.
|
31
|
+
def add(size, content)
|
32
|
+
raise ArgumentError.new("Bin size must be greater than zero") if size <= 0
|
33
|
+
|
34
|
+
@elements << {:size => size.to_i, :content => content}
|
35
|
+
@needs_packing = true
|
36
|
+
end
|
37
|
+
|
38
|
+
# Returns a packed multi-dimensional array of elements.
|
39
|
+
def bins
|
40
|
+
pack_all if @needs_packing
|
41
|
+
@bins
|
42
|
+
end
|
43
|
+
|
44
|
+
# Total empty space left over by uneven packing.
|
45
|
+
def empty_space
|
46
|
+
pack_all if @needs_packing
|
47
|
+
max = @sizes.each.max
|
48
|
+
space = 0
|
49
|
+
|
50
|
+
@sizes.each { |size| space += max - size }
|
51
|
+
|
52
|
+
space
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
def pack_all
|
57
|
+
@bins = Array.new(@total_bins) {Array.new}
|
58
|
+
@sizes = Array.new(@total_bins, 0)
|
59
|
+
|
60
|
+
self.send(@algorithm)
|
61
|
+
tall_to_middle
|
62
|
+
shuffle_within_cols if @shuffle_in_col
|
63
|
+
|
64
|
+
@needs_packing = false
|
65
|
+
end
|
66
|
+
|
67
|
+
def best_fit_decreasing
|
68
|
+
@elements.sort_by! { |e| e[:size] }
|
69
|
+
@elements.reverse!
|
70
|
+
best_fit
|
71
|
+
end
|
72
|
+
|
73
|
+
def best_fit_increasing
|
74
|
+
@elements.sort_by! { |e| e[:size] }
|
75
|
+
best_fit
|
76
|
+
end
|
77
|
+
|
78
|
+
def best_fit
|
79
|
+
@elements.each do |element|
|
80
|
+
_, col = @sizes.each_with_index.min
|
81
|
+
pack(col, element)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def shuffle_within_cols
|
86
|
+
@bins.each { |bin| bin.shuffle! }
|
87
|
+
end
|
88
|
+
|
89
|
+
# moves the tallest bin to the middle
|
90
|
+
def tall_to_middle
|
91
|
+
if (@total_bins > 1) && ((@total_bins % 2) != 0)
|
92
|
+
_, max_col = @sizes.each_with_index.max
|
93
|
+
mid_col = @total_bins / 2
|
94
|
+
|
95
|
+
temp = @bins[mid_col].clone
|
96
|
+
@bins[mid_col] = @bins[max_col]
|
97
|
+
@bins[max_col] = temp
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def pack(col, element)
|
102
|
+
@bins[col] << element[:content]
|
103
|
+
@sizes[col] += element[:size].to_i
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -1,54 +1,54 @@
|
|
1
|
-
module ColumnPack
|
2
|
-
# Arranges HTML elements into a fixed number of columns.
|
3
|
-
class ColumnPacker
|
4
|
-
include ActionView::Context
|
5
|
-
include ActionView::Helpers::TagHelper
|
6
|
-
include ActionView::Helpers::TextHelper
|
7
|
-
|
8
|
-
# Uses a fixed number of columns (total_columns).
|
9
|
-
#
|
10
|
-
# Options:
|
11
|
-
# :algorithm specify a different bin packing algorithm (default :best_fit_decreasing)
|
12
|
-
# available algorithms are :best_fit_decreasing, :best_fit_increasing
|
13
|
-
#
|
14
|
-
# :shuffle_in_col after packing columns, shuffle the elements in each column (defaults to true)
|
15
|
-
#
|
16
|
-
def initialize(total_columns, options = {})
|
17
|
-
@bin_packer = BinPacker.new(total_columns, options)
|
18
|
-
end
|
19
|
-
|
20
|
-
def add(height, content)
|
21
|
-
@bin_packer.add(height.to_i, content)
|
22
|
-
end
|
23
|
-
|
24
|
-
# Renders all elements into columns.
|
25
|
-
def render
|
26
|
-
wrap(@bin_packer.bins).html_safe
|
27
|
-
end
|
28
|
-
|
29
|
-
private
|
30
|
-
def wrap(bins)
|
31
|
-
content_tag :div, :class => "column-pack-wrap" do
|
32
|
-
capture do
|
33
|
-
bins.each do |bin|
|
34
|
-
concat column(bin)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def column(bin)
|
41
|
-
content_tag :div, :class => "column-pack-col" do
|
42
|
-
capture do
|
43
|
-
bin.each do |element|
|
44
|
-
concat element(element)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def element(element)
|
51
|
-
content_tag :div, element.html_safe, :class => "column-pack-element"
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
1
|
+
module ColumnPack
|
2
|
+
# Arranges HTML elements into a fixed number of columns.
|
3
|
+
class ColumnPacker
|
4
|
+
include ActionView::Context
|
5
|
+
include ActionView::Helpers::TagHelper
|
6
|
+
include ActionView::Helpers::TextHelper
|
7
|
+
|
8
|
+
# Uses a fixed number of columns (total_columns).
|
9
|
+
#
|
10
|
+
# Options:
|
11
|
+
# :algorithm specify a different bin packing algorithm (default :best_fit_decreasing)
|
12
|
+
# available algorithms are :best_fit_decreasing, :best_fit_increasing
|
13
|
+
#
|
14
|
+
# :shuffle_in_col after packing columns, shuffle the elements in each column (defaults to true)
|
15
|
+
#
|
16
|
+
def initialize(total_columns, options = {})
|
17
|
+
@bin_packer = BinPacker.new(total_columns, options)
|
18
|
+
end
|
19
|
+
|
20
|
+
def add(height, content)
|
21
|
+
@bin_packer.add(height.to_i, content)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Renders all elements into columns.
|
25
|
+
def render
|
26
|
+
wrap(@bin_packer.bins).html_safe
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
def wrap(bins)
|
31
|
+
content_tag :div, :class => "column-pack-wrap" do
|
32
|
+
capture do
|
33
|
+
bins.each do |bin|
|
34
|
+
concat column(bin)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def column(bin)
|
41
|
+
content_tag :div, :class => "column-pack-col" do
|
42
|
+
capture do
|
43
|
+
bin.each do |element|
|
44
|
+
concat element(element)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def element(element)
|
51
|
+
content_tag :div, element.html_safe, :class => "column-pack-element"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|