hungarian_algorithm_c 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 481b09c8e7febe93966818cf4e41e67147a8f816
4
- data.tar.gz: ac1209fde8671eb74130a3f7739b89937b68f256
3
+ metadata.gz: 93a7a92bf3729fb33bc7c669b574d0b8d6745c76
4
+ data.tar.gz: 8b0dbc726cbbdd0ae011506864969a63a3dde4d3
5
5
  SHA512:
6
- metadata.gz: 8719af8a5237221c1f640caa6383f728d7dc0a6c2f9f8e4a9beaaef7c46f2da97136ee0f33a533333b423b5f2a50d1947682031cc0975e8ac63657acc668c8e3
7
- data.tar.gz: 5263ef767f25795c945e2a274ef2cc8e415b971b8ed1d9f64c385e27a876deaafa5c402a9efc63fb0c2af2f6e26c2278c15aedcd30c60892173784f3a387b3aa
6
+ metadata.gz: 22b1b5a810bc278a4631d76ee3e5e66304c3aac95df4521599a243688087f4b324bc1d7966e3031c910ed28c4dbf1876e8439a1c2f6802bf5082038f81d2b65f
7
+ data.tar.gz: 059bf391ceddd1cbff317559a50892c180f206759658d482b16003931d90181d6d97e5c896b67169716e5e5dcd189251fd267383530d587cef03bce749aa9cc3
data/README.md CHANGED
@@ -8,7 +8,7 @@ Add the following to your [Gemfile](http://tosbourn.com/what-is-the-gemfile/):
8
8
 
9
9
  ```ruby
10
10
  # Gemfile
11
- gem 'hungarian_algorithm_c', github: 'deliveroo/hungarian_algorithm_c'
11
+ gem 'hungarian_algorithm_c'
12
12
  ```
13
13
 
14
14
  Run `bundle install`.
@@ -44,7 +44,7 @@ HungarianAlgorithmC.find_pairings(costs)
44
44
  #=> [[0, 0], [1, 2], [2, 1]]
45
45
  ```
46
46
 
47
- **Note**: Your cost matrix should not have `Float::INFINITY` or _very very large_ numbers as those will not be interpreted appropriately [here](ext/hungarian_algorithm_c/hungarian_algorithm_c.c#L56).
47
+ **Note**: Costs are held in the C code using `signed long long int`. If your matrix has values outside the range of the data type (e.g. `Float::INFINITY`, `BigNumber::INFINITY`, `-1e19`), they will be capped to the closest limit i.e. positive or negative 9_223_372_036_854_775_807.
48
48
 
49
49
  ## Acknowledgements
50
50
 
@@ -53,8 +53,7 @@ VALUE pairs(VALUE self, VALUE flattened_array_ruby, VALUE row_size_val) {
53
53
 
54
54
  int index;
55
55
  for (index = 0; index < array_size; index++) {
56
- long double element = NUM2DBL(rb_ary_entry(flattened_array_ruby, index));
57
- long long int rounded_element = element;
56
+ signed long long int rounded_element = NUM2LL(rb_ary_entry(flattened_array_ruby, index));
58
57
  array_c[index] = rounded_element;
59
58
  }
60
59
 
@@ -1,3 +1,3 @@
1
1
  module HungarianAlgorithmC
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
@@ -1,22 +1,48 @@
1
1
  require_relative './hungarian_algorithm_c.so'
2
2
 
3
3
  module HungarianAlgorithmC
4
+ # Limits for 'signed long long int' C data type
5
+ LIMITS = {
6
+ positive: +9_223_372_036_854_775_807,
7
+ negative: -9_223_372_036_854_775_807
8
+ }
9
+ private_constant :LIMITS
10
+
4
11
  class << self
5
- def find_pairings(array)
6
- validate!(array)
7
- pairs(array.flatten, array.size)
12
+ def find_pairings(matrix)
13
+ validate!(matrix)
14
+ array = value_capped_flattened_array(matrix)
15
+ pairs(array, matrix.size)
16
+ end
17
+
18
+ def value_capped_flattened_array(matrix)
19
+ [].tap do |array|
20
+ matrix.each do |row|
21
+ row.each do |element|
22
+ array << begin
23
+ if element > LIMITS[:positive]
24
+ LIMITS[:positive]
25
+ elsif element < LIMITS[:negative]
26
+ LIMITS[:negative]
27
+ else
28
+ element
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
8
34
  end
9
35
 
10
- def validate!(array)
11
- return if rectangular?(array)
12
- raise ArgumentError.new('array must be rectangular')
36
+ def validate!(matrix)
37
+ return if rectangular?(matrix)
38
+ raise ArgumentError.new('matrix must be rectangular')
13
39
  end
14
40
 
15
- def rectangular?(array)
16
- row_size = array.size
17
- array.all? { |column| row_size == column.size }
41
+ def rectangular?(matrix)
42
+ row_size = matrix.size
43
+ matrix.all? { |column| row_size == column.size }
18
44
  end
19
45
 
20
- private :pairs, :validate!, :rectangular?
46
+ private :pairs, :validate!, :rectangular?, :value_capped_flattened_array
21
47
  end
22
48
  end
@@ -26,7 +26,7 @@ RSpec.describe HungarianAlgorithmC do
26
26
  ])
27
27
  end
28
28
 
29
- context 'matrix contains Floa::INFINITY' do
29
+ context 'matrix contains Float::INFINITY' do
30
30
  let(:matrix_with_costs) { [
31
31
  [4, 3],
32
32
  [3, Float::INFINITY]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hungarian_algorithm_c
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Syed Humza Shah
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-08 00:00:00.000000000 Z
11
+ date: 2017-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler