weighted-select 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 65b74a6522a9f394a9bccfdf927003ff60d820d2
4
+ data.tar.gz: 48fc387a42699f63bb0adac747d70175af7c2b53
5
+ SHA512:
6
+ metadata.gz: cdf931867cd003ac179896861e33feb97c3faef34fd6bd64b9693f4fcf2f9605901e27ade0acbcfb5991c54b62c572878f6ecd33b32e2955e9098c35731be3dd
7
+ data.tar.gz: dca2be1bae2d651b9bc2652d4b2f05b17758b3aaaef875b60982a467d3be8ed17f7e33aacf1a089fc6c4ae7aa9ef06188694f900948ac839e1bbe17aff6e642b
@@ -0,0 +1,11 @@
1
+ -----BEGIN PGP SIGNATURE-----
2
+ Version: GnuPG v1
3
+
4
+ iQEcBAABAgAGBQJWNUU9AAoJEGf2hZJktszzgJAH/A6iHA3AKuuk/oqTJ79aKa5E
5
+ 7Txkf52yk236fS2RAhgSVzdJYkJ5xxcfHGEYvTs0JBzCU477g9hOk8HtZrbQNn3n
6
+ j30K3w4xi6djLkaZuhH0+qxmbFKe1tcDwdfPvzphio7eidOEXWudeF+oPW+qRAdj
7
+ oIFrznRiGTTJXJXp/TiIAFRqhMd8UXtoAVZLs/3mlA0T978vzX4RRyqyEgNOv5Ud
8
+ KIOHzojdy34Q91NW3NiZ08lEz9vRCjzRyZcVZ1WhR3h6GVGtIcsqFLcW9DaG0N3Y
9
+ na2ibVg97TvTTL4JDQA4JqAJBLl7Mq2IFCF/KvVfM6d70VWGp9sgLirCNCeR8wM=
10
+ =fxJK
11
+ -----END PGP SIGNATURE-----
@@ -0,0 +1,11 @@
1
+ -----BEGIN PGP SIGNATURE-----
2
+ Version: GnuPG v1
3
+
4
+ iQEcBAABAgAGBQJWNUU5AAoJEGf2hZJktszzHhMIAInhzhbCXCEGQAYGR7/OxAVZ
5
+ zMzhSg/B1+s1oMHlUWo0gUzHDvQ7fbxAVkatC+7AB4h3rukSiVgIOWxAm2bjCnJH
6
+ 8jHMYGpmrPVJcWMy3/tQeke+OD6m6BNeilrm26cqlTeBCdqTKMdnS3WIc1zuo7jB
7
+ 5uDJmW3LnQ68K9ZKpu+qHigLYX2L9PYIssTcpD/aREgdxEl0JyPNImRoYTKLxU/B
8
+ mKMQvA89Mv1wVyRSdRqGAawaFDljMEnZQVb+wibhGfEXOgc9cp2Na+JkmB5/3IZ/
9
+ rgJEyNvU8IA+izPeya4FdgC4Y/APZJQ1WrtSX2gtRvKxO+jEayZYo94CK+nasqE=
10
+ =5F7l
11
+ -----END PGP SIGNATURE-----
@@ -0,0 +1 @@
1
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Maxim Khan-Magomedov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,37 @@
1
+ Weighted select
2
+ ===============
3
+
4
+ Allows you to store random items with their weight and select them later based on weights.
5
+
6
+ Usage
7
+ -----
8
+
9
+ require 'weighted-select'
10
+
11
+ # a/b/c/d relate as 10/4/1/16
12
+ weights = { a: 10, b: 4, c: 1, d: 16 }
13
+ selector = WeightedSelect::Selector.new weights
14
+
15
+ # One can add some more items later
16
+ selector.add :e, 5
17
+ selector.add :f, 3
18
+
19
+ # One can increase weight by adding the same item again
20
+ selector.add :c, 4
21
+
22
+ selector.weights
23
+ # {
24
+ # (0...10) => :a,
25
+ # (10...14) => :b,
26
+ # (14...15) => :c,
27
+ # (15...31) => :d,
28
+ # (31...36) => :e,
29
+ # (36...39) => :f,
30
+ # (39...43) => :c
31
+ # }
32
+
33
+ # Returns random item based on weights
34
+ selector.select
35
+
36
+ Anything can be an item. Weight must be positive integer.
37
+ Total weight of all items is stored in `selector.total_weight`.
@@ -0,0 +1,51 @@
1
+ require_relative 'weighted-select/version'
2
+
3
+ module WeightedSelect
4
+ class Selector
5
+ attr_reader :weights, :total_weight
6
+
7
+ # Sets initial weights
8
+ #
9
+ # Accepts Hash as an argument, where keys are items and corresponding values
10
+ # are their weights (positive integers).
11
+ #
12
+ # @param [Hash] weights
13
+ def initialize(weights = Hash.new)
14
+ @weights = Hash.new
15
+ @total_weight = 0
16
+ weights.each { |item, weight| add item, weight }
17
+ end
18
+
19
+ # Add item with weight
20
+ #
21
+ # @param [Object] item
22
+ # @param [Integer] weight
23
+ def add(item, weight)
24
+ delta = Integer(weight)
25
+ if delta > 0
26
+ new_weight = @total_weight + delta
27
+ weights[@total_weight...new_weight] = item
28
+ @total_weight = new_weight
29
+ end
30
+ end
31
+
32
+ # Select weighted-random item if where are any in weights hash
33
+ #
34
+ # @return [Object|nil]
35
+ def select
36
+ extract_item unless @weights.empty?
37
+ end
38
+
39
+ private
40
+
41
+ # Extract item based on weights distribution
42
+ #
43
+ # @return [Object]
44
+ def extract_item
45
+ weight = Random.rand(@total_weight)
46
+ @weights.each do |range, item|
47
+ return item if range === weight
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module WeightedSelect
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,21 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'weighted-select/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'weighted-select'
7
+ spec.version = WeightedSelect::VERSION
8
+ spec.authors = ['Maxim Khan-Magomedov']
9
+ spec.email = ['maxim.km@gmail.com']
10
+
11
+ spec.summary = %q{Gem for selecting items by weight}
12
+ spec.homepage = 'https://github.com/ozgg/weighted-select'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.require_paths = ["lib"]
17
+
18
+ spec.add_development_dependency "bundler", "~> 1.10"
19
+ spec.add_development_dependency "rake", "~> 10.0"
20
+ spec.add_development_dependency "rspec"
21
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: weighted-select
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Maxim Khan-Magomedov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - maxim.km@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - lib/weighted-select.rb
68
+ - lib/weighted-select/version.rb
69
+ - weighted-select.gemspec
70
+ homepage: https://github.com/ozgg/weighted-select
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.4.5.1
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Gem for selecting items by weight
94
+ test_files: []
@@ -0,0 +1,11 @@
1
+ -----BEGIN PGP SIGNATURE-----
2
+ Version: GnuPG v1
3
+
4
+ iQEcBAABAgAGBQJWNUU1AAoJEGf2hZJktszzjxcIALyFsK81kXV9XI9XognPQmf7
5
+ +yu5NYa/fngVi0S0ymra3hArWNqHfAmNmcclwpQMbDcmj5O81ytz8DVx+YF9dcCi
6
+ dss5FE3PhkWK5FIxFysoR7+6V4uHBrgDU00ff44r1OdrSoVdPwVlXBUYyhI7Fgs6
7
+ bQOKS1uEcurfbTdsNRopNgENCim6j8wP9CDxK15tLYb515/ZJhTd7tpsdT+QiGfI
8
+ s2YmUAxHJ3tB2E/H/pHyEn7I6a7A/8OtCf+5XOEAwpYuTk7b3kzKQ7tOrNcOcY8K
9
+ 5mon7MyxQl20xCaYhYLktW4r+mdwM+jBL1gTF10aMKJGVGKfOzqOakTk9704mw4=
10
+ =tcUW
11
+ -----END PGP SIGNATURE-----