schulze-vote 2.0.4 → 2.0.5

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: 0f1a89377c52c746f04598f5af4202130d6276e4
4
- data.tar.gz: 55fab398da85310889bee1304c3b099d9a6a6554
3
+ metadata.gz: d14c1791421988a4b47d01a1a1d534a178447f4e
4
+ data.tar.gz: 970a191bb923a4469fea996a4d78537425b13d77
5
5
  SHA512:
6
- metadata.gz: 0ee33a934d33321ea22e2f35bad75fa1c9b462b0b4e9c141b7e0709eabe8a3d547fba852526b70a9de395afa12fa55a30b3e4b60dc553b3aeaf95cb2e96a8fee
7
- data.tar.gz: b7475ff5206ebd7f19166aaf89a4cb88c3e37bd7a2ae15e60d3b4b7b35bc91cab5da1e56541b7feba5b1aebf568c805bbf2e9864d2bd7bb0acfbf7912aa2e289
6
+ metadata.gz: bc0aab70b8a74efc664067693f2553e9a9c983be783ff1be51fc40d31eb112f79b3c435b7ed60963ac90214e24da0b7a1cd265a929a9b68978016e1a0363e301
7
+ data.tar.gz: 382412f15f3d6b445be9d8a49d01118b0a41db71cdb3247505bad3a7f5a76bba52b924e9da7d1196be8f6628684f7e69039cfa0c80c9156db884ea8e9ac3d35d
data/README.md CHANGED
@@ -192,6 +192,13 @@ end
192
192
 
193
193
  which is the same result of the reference above.
194
194
 
195
+ ## Classifications
196
+
197
+ You have a `classifications(limit_results = false)` that you can call.
198
+ If the number of results is greater then the `limit_results` parameter then a `TooManyClassificationsException`
199
+ is raised.
200
+ If you set this parameter to any value other then `false` be careful to catch and manage the exception properly.
201
+
195
202
  ## Contributing to schulze-vote
196
203
 
197
204
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
@@ -49,8 +49,13 @@ module Vote
49
49
  # return all possible solutions to the votation
50
50
  attr_reader :winners_array
51
51
 
52
- def classifications
53
- @classifications ||= calculate_classifications
52
+ # compute all possible solutions
53
+ # since this can take days, there is an option to limit the numeber of calculated classifications
54
+ # the default is 10. if the system is calculating more then 10 possible classifications it will stop
55
+ # raising a TooManyClassifications exception
56
+ # you can set it to false to disable the limit
57
+ def classifications(limit_results = false)
58
+ @classifications ||= calculate_classifications(limit_results)
54
59
  end
55
60
 
56
61
  private
@@ -139,7 +144,7 @@ module Vote
139
144
  rank
140
145
  end
141
146
 
142
- def calculate_classifications
147
+ def calculate_classifications(limit_results)
143
148
  calculate_potential_winners
144
149
  calculate_beat_couples
145
150
 
@@ -147,29 +152,35 @@ module Vote
147
152
  start_list.sort! { |e1, e2| rank_element(e1) <=> rank_element(e2) }
148
153
 
149
154
  classifications = []
150
- compute_classifications(classifications, [], @potential_winners, @beat_couples, start_list)
155
+ compute_classifications(classifications, [], @potential_winners, @beat_couples, start_list, limit_results)
151
156
  classifications
152
157
  end
153
158
 
154
- def compute_classifications(classifications, classif = [], potential_winners, beated_list, start_list)
159
+ def compute_classifications(classifications, classif = [], potential_winners,
160
+ beated_list, start_list, limit_results)
155
161
  if beated_list.empty?
156
162
  start_list.permutation.each do |array|
157
163
  classifications << classif + array
164
+ check_limits(classifications, limit_results)
158
165
  end
159
166
  else
160
167
  if classif.empty? && potential_winners.any?
161
168
  potential_winners.each do |element|
162
- add_element(classifications, classif, nil, beated_list, start_list, element)
169
+ add_element(classifications, classif, nil, beated_list, start_list, element, limit_results)
163
170
  end
164
171
  else
165
172
  start_list.each do |element|
166
- add_element(classifications, classif, nil, beated_list, start_list, element)
173
+ add_element(classifications, classif, nil, beated_list, start_list, element, limit_results)
167
174
  end
168
175
  end
169
176
  end
170
177
  end
171
178
 
172
- def add_element(classifications, classif, _potential_winners, beated_list, start_list, element)
179
+ def check_limits(classifications, limit_results)
180
+ raise TooManyClassificationsException if limit_results && classifications.size > limit_results
181
+ end
182
+
183
+ def add_element(classifications, classif, _potential_winners, beated_list, start_list, element, limit_results)
173
184
  return if beated_list.any? { |c| c[1] == element }
174
185
  classification = classif.clone
175
186
  classification << element
@@ -178,11 +189,15 @@ module Vote
178
189
  next_start_list.delete(element)
179
190
  if next_start_list.empty?
180
191
  classifications << classification
192
+ check_limits(classifications, limit_results)
181
193
  else
182
- compute_classifications(classifications, classification, nil, next_beated_list, next_start_list)
194
+ compute_classifications(classifications, classification, nil, next_beated_list, next_start_list, limit_results)
183
195
  end
184
196
  end
185
197
  end
186
198
  end
187
199
  end
188
200
  end
201
+
202
+ class TooManyClassificationsException < StandardError
203
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schulze-vote
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.4
4
+ version: 2.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alessandro Rodi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-10 00:00:00.000000000 Z
11
+ date: 2015-12-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This gem is a Ruby implementation of the Schulze voting method (with
14
14
  help of the Floyd-Warshall algorithm), a type of the Condorcet voting methods.