munkres_ru 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a9d5a7d8be0a0bea21da9178e20a6101b580b310
4
+ data.tar.gz: 2d3ec35715ccafa1bc24ebbca5034fea4fe58eba
5
+ SHA512:
6
+ metadata.gz: ebcb44fdfd40c042f862bff2bcad0392fac891cf1faa15f8b7b1b424c8087ae9e7e9097ecf8a7d99ece81fe3db22fec6a6f43e47ae05469356f6a5c709a1e0c0
7
+ data.tar.gz: 58e0800b9c3f1e1dba993e7626856aaaec7f19f826d022942bd1ac1f786f5a9e1edf3c01ce4baf5df3983d36cb658dcac30ae799d9725c2636b09001f42aa496
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /rust/target
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in munkres_ru.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Oleg Antonyan
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Kuhn-Munkres in Rust as a Ruby gem
2
+
3
+ This is a wrapper of https://github.com/mneumann/munkres-rs for Ruby.
4
+
5
+ ## Usage
6
+
7
+ Add to your Gemfile
8
+
9
+ ```
10
+ gem 'munkres_ru'
11
+ ```
12
+
13
+ Then you can do:
14
+
15
+ ```
16
+ problem = [
17
+ [1.0, 1.0, 1.0, 1.0, 1.0],
18
+ [1.0, 1.0, 1.0, 1.0, 1.0],
19
+ [1.0, 1.0, 1.0, 1.0, 1.0],
20
+ [0.0, 0.0, 0.0, 0.0, 0.0],
21
+ [0.0, 0.0, 0.0, 0.0, 0.0]
22
+ ]
23
+ solution = MunkresRu.solve(problem)
24
+
25
+ # => [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4]]
26
+ ```
27
+
28
+ Note: only square matrixes are handled.
29
+
30
+ ## Building
31
+
32
+ Install Rust and Cargo; then run
33
+
34
+ ```
35
+ $ rake compile
36
+ $ rake spec
37
+ ```
38
+
39
+ Build the Ruby gem with
40
+
41
+ ```
42
+ $ rake build
43
+ ```
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
7
+
8
+ task :compile do
9
+ sh "cd #{File.dirname(__FILE__)}/rust && make"
10
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "munkres_ru"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module MunkresRu
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/lib/munkres_ru.rb ADDED
@@ -0,0 +1,33 @@
1
+ require 'munkres_ru/version'
2
+ require 'ffi'
3
+ require 'json'
4
+
5
+ if RUBY_PLATFORM.include?('darwin')
6
+ EXT = 'dylib'
7
+ else
8
+ EXT = 'so'
9
+ end
10
+
11
+ module MunkresRu
12
+ extend FFI::Library
13
+ ffi_lib "#{File.dirname(__FILE__)}/../rust/target/release/libmunkres_ru.#{EXT}"
14
+
15
+ class ResultArray < FFI::Struct
16
+ layout :len, :size_t,
17
+ :data, :pointer
18
+
19
+ def to_a
20
+ self[:data].get_array_of_int(0, self[:len]).compact
21
+ end
22
+ end
23
+
24
+ attach_function :solve_munkres, [:size_t, :pointer], ResultArray.by_value
25
+
26
+ def self.solve(array)
27
+ flattened = array.flatten
28
+ pointer = FFI::MemoryPointer.new :double, flattened.size
29
+ pointer.autorelease = false # Rust will take ownership of that memory
30
+ pointer.put_array_of_double 0, flattened
31
+ MunkresRu.solve_munkres(array.size, pointer).to_a.each_slice(2).to_a
32
+ end
33
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'munkres_ru/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'munkres_ru'
8
+ spec.version = MunkresRu::VERSION
9
+ spec.authors = ['Ivan Pirlik']
10
+ spec.email = ['ivan.pirlik@deliveroo.co.uk']
11
+
12
+ spec.summary = 'Kuhn-Munkres implemented in Rust'
13
+ spec.homepage = 'https://github.com/deliveroo/munkres_ru'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.extensions = Dir['rust/extconf.rb']
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.12'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'rspec', '~> 3.0'
26
+ end
data/rust/Cargo.lock ADDED
@@ -0,0 +1,30 @@
1
+ [root]
2
+ name = "munkres_ru"
3
+ version = "0.1.0"
4
+ dependencies = [
5
+ "libc 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)",
6
+ "munkres 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
7
+ ]
8
+
9
+ [[package]]
10
+ name = "fixedbitset"
11
+ version = "0.1.6"
12
+ source = "registry+https://github.com/rust-lang/crates.io-index"
13
+
14
+ [[package]]
15
+ name = "libc"
16
+ version = "0.2.23"
17
+ source = "registry+https://github.com/rust-lang/crates.io-index"
18
+
19
+ [[package]]
20
+ name = "munkres"
21
+ version = "0.3.0"
22
+ source = "registry+https://github.com/rust-lang/crates.io-index"
23
+ dependencies = [
24
+ "fixedbitset 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
25
+ ]
26
+
27
+ [metadata]
28
+ "checksum fixedbitset 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "fcf4412e2d11115c5ed81c2fbdaba8028de0c92553497aa771fc5f4e0c5c8793"
29
+ "checksum libc 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)" = "e7eb6b826bfc1fdea7935d46556250d1799b7fe2d9f7951071f4291710665e3e"
30
+ "checksum munkres 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "84ca023813adf830e2537250f471f9928ff581527fe87d08a641f68b1b46c3e7"
data/rust/Cargo.toml ADDED
@@ -0,0 +1,11 @@
1
+ [package]
2
+ name = "munkres_ru"
3
+ version = "0.1.0"
4
+ authors = ["Ivan Pirlik <ivan.pirlik@deliveroo.co.uk>"]
5
+
6
+ [lib]
7
+ crate-type = ["dylib"]
8
+
9
+ [dependencies]
10
+ libc = "0.2"
11
+ munkres = "0.3.0"
data/rust/Makefile ADDED
@@ -0,0 +1,7 @@
1
+ all:
2
+ cargo build --release
3
+
4
+ clean:
5
+ rm -rf target
6
+
7
+ install: ;
data/rust/extconf.rb ADDED
@@ -0,0 +1 @@
1
+ raise 'You have to install Rust with Cargo (https://www.rust-lang.org/)' if !system('cargo --version') || !system('rustc --version')
data/rust/src/lib.rs ADDED
@@ -0,0 +1,36 @@
1
+ extern crate munkres;
2
+ extern crate libc;
3
+
4
+ use munkres::WeightMatrix;
5
+ use munkres::solve_assignment;
6
+
7
+ #[repr(C)]
8
+ pub struct Array {
9
+ len: libc::size_t,
10
+ data: *const libc::c_int,
11
+ }
12
+
13
+ impl Array {
14
+ fn from_vec<T>(mut vec: Vec<T>) -> Array {
15
+ vec.shrink_to_fit();
16
+ let array = Array { data: vec.as_ptr() as *const libc::c_int, len: vec.len() as libc::size_t };
17
+ std::mem::forget(vec);
18
+ array
19
+ }
20
+ }
21
+
22
+
23
+ #[no_mangle]
24
+ pub extern fn solve_munkres(n: libc::size_t, array: *mut libc::c_double) -> Array {
25
+ let size = n as usize;
26
+ let len = size*size;
27
+ let vec = unsafe { Vec::from_raw_parts(array, len, len) };
28
+ let mut weights: WeightMatrix<libc::c_double> = WeightMatrix::from_row_vec(size, vec);
29
+ let matching = solve_assignment(&mut weights);
30
+ let mut res = Vec::new();
31
+ for &(row, col) in &matching[..] {
32
+ res.push(row as libc::c_int);
33
+ res.push(col as libc::c_int);
34
+ }
35
+ Array::from_vec(res)
36
+ }
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: munkres_ru
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Pirlik
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-12 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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - ivan.pirlik@deliveroo.co.uk
58
+ executables: []
59
+ extensions:
60
+ - rust/extconf.rb
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/munkres_ru.rb
73
+ - lib/munkres_ru/version.rb
74
+ - munkres_ru.gemspec
75
+ - rust/Cargo.lock
76
+ - rust/Cargo.toml
77
+ - rust/Makefile
78
+ - rust/extconf.rb
79
+ - rust/src/lib.rs
80
+ homepage: https://github.com/deliveroo/munkres_ru
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.4.5.1
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Kuhn-Munkres implemented in Rust
104
+ test_files: []