search_space_splitter 1.0.0

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.
Files changed (3) hide show
  1. data/README.md +51 -0
  2. data/lib/search_space_splitter.rb +33 -0
  3. metadata +94 -0
@@ -0,0 +1,51 @@
1
+ ## Search Space Splitter
2
+
3
+ Splits a search space into n pieces.
4
+
5
+
6
+ ## Usage
7
+
8
+ Splits a search space, represented by an array of ranges, into similarly sized sections.
9
+
10
+ ```ruby
11
+ require 'search_splace_splitter'
12
+
13
+ SearchSpaceSplitter.split([1..2, 1..3])
14
+ #=> [[1..1, 1..3], [3..3, 1..3]]
15
+
16
+ SearchSpaceSplitter.split([1..3, 1..5])
17
+ #=> [[1..3, 1..3], [1..3, 4..5]]
18
+
19
+ SearchSpaceSplitter.split([1..3, 1..4, 1..5])
20
+ #=> [[1..3, 1..2, 1..5], [1..3, 3..4, 1..5]]
21
+
22
+ SearchSpaceSplitter.split([-1..1, 0..0, 2..3])
23
+ #=> [[-1..1, 0..0, 2..2], [-1..1, 0..0, 3..3]]
24
+
25
+ SearchSpaceSplitter.split([1..3, 1..2], :into => 3)
26
+ #=> [[1..1, 1..2], [2..2, 1..2], [3..3, 1..2]]
27
+
28
+ SearchSpaceSplitter.split([1..3, 1..4, 1..5], :into => 6)
29
+ #=> [[1..1, 1..2, 1..5],
30
+ # [1..1, 3..4, 1..5],
31
+ # [2..2, 1..2, 1..5],
32
+ # [2..2, 3..4, 1..5],
33
+ # [3..3, 1..2, 1..5],
34
+ # [3..3, 3..4, 1..5]]
35
+
36
+ SearchSpaceSplitter.split([1..3, 1..4, 1..5], :into => 11)
37
+ #=> [[1..3, 1..2, 1..1],
38
+ # [1..3, 3..4, 1..1],
39
+ # [1..3, 1..2, 2..2],
40
+ # [1..3, 3..4, 2..2],
41
+ # [1..3, 1..2, 3..3],
42
+ # [1..3, 3..4, 3..3],
43
+ # [1..3, 1..2, 4..4],
44
+ # [1..3, 3..4, 4..4],
45
+ # [1..1, 1..4, 5..5],
46
+ # [2..2, 1..4, 5..5],
47
+ # [3..3, 1..4, 5..5]]
48
+
49
+ SearchSpaceSplitter.split([1..2], :into => 999)
50
+ #=> [[1..1], [2..2]]
51
+ ```
@@ -0,0 +1,33 @@
1
+ require 'rational'
2
+ require 'range_splitter'
3
+
4
+ class SearchSpaceSplitter
5
+ def self.split(ranges, params = {})
6
+ into = params[:into] || 2
7
+
8
+ atomic = ranges.all? { |r| r.count == 1 }
9
+ return ranges if into == 1 || atomic
10
+
11
+ ri = ranges.each_with_index
12
+ divisible = lambda { |c| (into % c).zero? || (c % into).zero? }
13
+
14
+ range, index = ri.detect { |r, i| r.count != 1 && divisible[r.count] }
15
+ range, index = ri.max_by { |r, i| r.count } unless index
16
+
17
+ splits = range.split(:into => into).map do |r|
18
+ dup = ranges.dup
19
+ dup[index] = r
20
+ dup
21
+ end
22
+
23
+ current = splits.size
24
+ div, mod = into.divmod(current)
25
+
26
+ splits.each_with_index.map do |r, i|
27
+ into = div + (i < (current - mod) ? 0 : 1)
28
+ p = params.merge(:into => into)
29
+
30
+ split(r, p)
31
+ end.flatten.each_slice(ranges.size).to_a
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: search_space_splitter
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Christopher Patuzzo
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-11-02 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: range_splitter
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: rspec
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id002
48
+ description: Splits a search space into n sections.
49
+ email: chris@patuzzo.co.uk
50
+ executables: []
51
+
52
+ extensions: []
53
+
54
+ extra_rdoc_files: []
55
+
56
+ files:
57
+ - README.md
58
+ - lib/search_space_splitter.rb
59
+ homepage: https://github.com/cpatuzzo/search_space_splitter
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ requirements: []
86
+
87
+ rubyforge_project:
88
+ rubygems_version: 1.8.24
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Search Space Splitter
92
+ test_files: []
93
+
94
+ has_rdoc: