splashy 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -50,6 +50,7 @@ buckets.select( :random => true )
50
50
  Changelog
51
51
  =========
52
52
 
53
+ * *0.1.2* - A few small bugs fixed in few-objects situations. (Thanks to Mars Hall)
53
54
  * *0.1.1* - Add support for random selection from buckets.
54
55
  * *0.1.0* - Several bug fixes, add `#neediest_buckets` method to `Buckets` to
55
56
  allow you to choose which buckets to add to first if an element can be put in
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -84,7 +84,7 @@ module Splashy
84
84
  # distribution, sorted descending by how much more they need.
85
85
  def neediest_buckets
86
86
  multipliers = self.needed_multipliers( self._select_all, @wanted_distribution ).to_a
87
- multipliers.sort! { |a, b| b[1] <=> a[1] } # Sort on multiplier ascending
87
+ multipliers.sort! { |a, b| b[1] <=> a[1] } # Sort on multiplier descending
88
88
  multipliers.map{ |bucket_name, multiplier| bucket_name }
89
89
  end
90
90
 
@@ -169,10 +169,11 @@ module Splashy
169
169
  bucket_size = current_selections[bucket_name].size
170
170
  desired_pct = wanted_distribution[bucket_name]
171
171
  current_pct = bucket_size.to_f / total_size
172
+ current_pct = 0 if current_pct.nan?
172
173
  if current_pct > 0
173
- memo[bucket_name] = desired_pct / current_pct
174
+ memo[bucket_name] = desired_pct - current_pct
174
175
  else
175
- memo[bucket_name] = 1 / 0.0 # Infinity
176
+ memo[bucket_name] = desired_pct
176
177
  end
177
178
  memo
178
179
  end
@@ -194,7 +195,7 @@ module Splashy
194
195
  # supplied, we can't meet the requirements.
195
196
  def estimated_final_count
196
197
  limiter_bucket = self.limiter_bucket
197
- final_count = ( limiter_bucket.count / @wanted_distribution[limiter_bucket.name] ).floor
198
+ final_count = ( limiter_bucket.count / @wanted_distribution[limiter_bucket.name] ).ceil # go upward here to avoid missing elements in low-quantity situations
198
199
  final_count = [@wanted_count, final_count].min if @wanted_count
199
200
  final_count
200
201
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "splashy"
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tyson Tate"]
12
- s.date = "2011-12-13"
12
+ s.date = "2012-01-19"
13
13
  s.description = "Simple distribution-based sampling of arbitrary objects from any number of buckets. Splashy. Buckets. Get it!?"
14
14
  s.email = "tyson@tysontate.com"
15
15
  s.extra_rdoc_files = [
@@ -123,7 +123,7 @@ describe Splashy::Buckets do
123
123
  fill_with_counts( 3, 3, 3 )
124
124
  assert @buckets.satisfied?
125
125
  assert_equal(
126
- {:a=>["10"], :b=>["20"], :c=>["30", "31"]},
126
+ {:a=>["10"], :b=>["20"], :c=>["30", "31", "32"]},
127
127
  @buckets.select
128
128
  )
129
129
  end
@@ -163,7 +163,19 @@ describe Splashy::Buckets do
163
163
  fill_with_counts( 10, 2, 40 )
164
164
  assert @buckets.satisfied?
165
165
  assert_equal(
166
- {:a=>["10"], :b=>["20", "21"], :c=>["30", "31", "32", "33", "34", "35", "36", "37"]},
166
+ {:a=>["10"], :b=>["20", "21"], :c=>["30", "31", "32", "33", "34", "35", "36", "37", "38"]},
167
+ @buckets.select
168
+ )
169
+ end
170
+
171
+ it "selects from a very small pool" do
172
+ @buckets = Splashy::Buckets.new( {:a => 0.33, :b => 0.67} )
173
+ @buckets.add( :a, "hoy" )
174
+ @buckets.add( :b, "zoy" )
175
+ @buckets.add( :b, "moy" )
176
+ assert @buckets.satisfied?
177
+ assert_equal(
178
+ {:a=>["hoy"], :b=>["zoy", "moy"]},
167
179
  @buckets.select
168
180
  )
169
181
  end
@@ -281,6 +293,13 @@ describe Splashy::Buckets do
281
293
  end
282
294
  end
283
295
 
296
+ describe "neediness" do
297
+ it "provides descending list" do
298
+ @buckets = Splashy::Buckets.new( {:a => 0.33, :b => 0.67} )
299
+ assert_equal( [:b, :a], @buckets.neediest_buckets )
300
+ end
301
+ end
302
+
284
303
  describe "performance" do
285
304
  it "grows linearly with more elements" do
286
305
  puts # Formatting...
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: splashy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-13 00:00:00.000000000 Z
12
+ date: 2012-01-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
16
- requirement: &70277083322720 !ruby/object:Gem::Requirement
16
+ requirement: &70280060104260 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70277083322720
24
+ version_requirements: *70280060104260
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &70277083321600 !ruby/object:Gem::Requirement
27
+ requirement: &70280060101400 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.0.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70277083321600
35
+ version_requirements: *70280060101400
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: jeweler
38
- requirement: &70277083320380 !ruby/object:Gem::Requirement
38
+ requirement: &70280060099660 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: 1.6.4
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70277083320380
46
+ version_requirements: *70280060099660
47
47
  description: Simple distribution-based sampling of arbitrary objects from any number
48
48
  of buckets. Splashy. Buckets. Get it!?
49
49
  email: tyson@tysontate.com
@@ -80,7 +80,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
80
80
  version: '0'
81
81
  segments:
82
82
  - 0
83
- hash: 4086114583339023412
83
+ hash: -2094145325101002950
84
84
  required_rubygems_version: !ruby/object:Gem::Requirement
85
85
  none: false
86
86
  requirements: