column_pack 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d3392aff0cd883b18b176bc81e864c6eb575dcb7
4
- data.tar.gz: 4973088e62dac89bc4f9096cec5e76431254af0e
3
+ metadata.gz: ea52916a285ff3137674ec892b04097564ef5b51
4
+ data.tar.gz: f88ca4053df9079852c7c665ea0092dc644b3330
5
5
  SHA512:
6
- metadata.gz: 39fab94e8b81156dbe61ef7a7fd0f09c14f1445f0b234c07e9c2c3555af5d147342bac049fbabd5943c47349037df1b7a32286ab6e1f49184242141d2c599c94
7
- data.tar.gz: be2914d3adc9180b9fe12dc9f3e90fe7d8fc4ebe2ee08344ff88fa8844acae0150457fa81cedbe6f2dcd3b598eca8c4e72adede90ac86cff98170fbf9f1aef68
6
+ metadata.gz: bc117743b86cb42061d308e96b66ed4b7c7b7f127784e40e8d41a035f183f0c7813dbd7167fdd816979a283cc633c8a5c63006db9e9a06eb61f56f63af031a72
7
+ data.tar.gz: cab6dbf8e18dfd3576a3944eabf3c6cf871e33fd111729b63c8e5997386e05cb0b42c46b17edc5721d9f8eb0689ab22b4aab03944f674596d6166d127da18d8c
@@ -1,20 +1,20 @@
1
- Copyright 2014 YOURNAME
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
@@ -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
- @sizes.each { |size| space += max - size }
50
- return space
51
- end
52
-
53
- private
54
- def pack_all
55
- @bins = Array.new(@total_bins) {Array.new}
56
- @sizes = Array.new(@total_bins, 0)
57
-
58
- self.send(@algorithm)
59
- tall_to_middle
60
- shuffle_within_cols if @shuffle_in_col
61
-
62
- @needs_packing = false
63
- end
64
-
65
- def best_fit_decreasing
66
- @elements.sort_by! { |e| e[:size] }
67
- @elements.reverse!
68
- best_fit
69
- end
70
-
71
- def best_fit_increasing
72
- @elements.sort_by! { |e| e[:size] }
73
- best_fit
74
- end
75
-
76
- def best_fit
77
- @elements.each do |element|
78
- size, col = @sizes.each_with_index.min
79
- pack(col, element)
80
- end
81
- end
82
-
83
- def shuffle_within_cols
84
- @bins.each { |bin| bin.shuffle! }
85
- end
86
-
87
- # moves the tallest bin to the middle
88
- def tall_to_middle
89
- if (@total_bins > 1) && ((@total_bins % 2) != 0)
90
- size, max_col = @sizes.each_with_index.max
91
- mid_col = @total_bins / 2
92
-
93
- temp = @bins[mid_col].clone
94
- @bins[mid_col] = @bins[max_col]
95
- @bins[max_col] = temp
96
- end
97
- end
98
-
99
- def pack(col, element)
100
- @bins[col] << element[:content]
101
- @sizes[col] += element[:size].to_i
102
- end
103
- end
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