hamming 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: de61c8fcbfd38c34801656f383cb34bc1baff7058a61117af043838f8b10daa8
4
+ data.tar.gz: 9421e1ccca66a2817f4c421409c34aa1933dd17f5498440c84be07ccf97dbc58
5
+ SHA512:
6
+ metadata.gz: 3b3447a06164f785058398bf0b18ddaa16d035d78cfb6845b18e91291e83e14cb24870ead0103aacae8f30c0fe6d99e6399da9ea5a07de4131ac6723a27aac46
7
+ data.tar.gz: 3e65c9e21e4b1d561ffb0e7fc3972b26ae10c05e0dba9fe3ad4c6d81bc49e262f31c0f7544ecb2dea07c9a26e87ab0929bb0eddac5e43720cadeb2ba4902ca72
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.gem
data/Makefile ADDED
@@ -0,0 +1,16 @@
1
+ .PHONY: *
2
+
3
+ default: test
4
+
5
+ build:
6
+ rm -f *.gem
7
+ gem build hamming.gemspec
8
+
9
+ publish: build
10
+ gem push *.gem
11
+
12
+ console:
13
+ irb -Ilib -rhamming
14
+
15
+ test:
16
+ ruby -Ilib:spec spec/*_spec.rb
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # Hamming
2
+
3
+ ## Install
4
+
5
+ ```bash
6
+ gem install hamming
7
+ ```
8
+
9
+ ## Usage
10
+
11
+ Hamming calculates the distance between two hashes or two vectors.
12
+
13
+ ### Distance
14
+
15
+ Since hashes exist on a metric space you can measure how far a hash is from another.
16
+
17
+ ```ruby
18
+ hash_a = "859091ce633aaebb"
19
+ hash_b = "859091ce633aaeba"
20
+ Hamming.distance(hash_a, hash_b)
21
+
22
+ # You can also transform hashes based on your storage:
23
+ Hamming.vector_to_hash(hash)
24
+ Hamming.hash_to_vector(vector)
25
+ ```
data/hamming.gemspec ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "hamming"
5
+ s.version = "0.1.0"
6
+ s.summary = "Hamming calculations"
7
+ s.authors = ["elcuervo"]
8
+ s.licenses = %w[MIT]
9
+ s.email = ["elcuervo@elcuervo.net"]
10
+ s.homepage = "http://github.com/elcuervo/hamming"
11
+ s.files = `git ls-files`.split("\n")
12
+ s.test_files = `git ls-files test`.split("\n")
13
+ end
data/lib/hamming.rb ADDED
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ class Hamming
4
+ class << self
5
+ def vector_to_hash(vector)
6
+ vector.join.to_i(2).to_s(16)
7
+ end
8
+
9
+ def hash_to_vector(hash)
10
+ hash.hex.to_s(2).split("").map(&:to_i)
11
+ end
12
+
13
+ def distance(a, b)
14
+ a = a.kind_of?(Array) ? vector_to_hash(a) : a
15
+ b = b.kind_of?(Array) ? vector_to_hash(b) : b
16
+
17
+ n = a.hex ^ b.hex
18
+
19
+ ones = 0
20
+
21
+ while n > 0
22
+ n &= n - 1
23
+ ones += 1
24
+ end
25
+
26
+ ones
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,35 @@
1
+ require "spec_helper"
2
+
3
+ describe Hamming do
4
+ describe "comparing hashes" do
5
+ let(:hash_a) { "859091ce633aaebb" }
6
+ let(:hash_b) { "859091ce633aaeba" }
7
+
8
+ let(:vector_a) do
9
+ [
10
+ 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1,
11
+ 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0,
12
+ 1, 1
13
+ ]
14
+ end
15
+
16
+ let(:vector_b) do
17
+ [
18
+ 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1,
19
+ 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0,
20
+ 1, 0
21
+ ]
22
+ end
23
+
24
+ it "conversion" do
25
+ assert_equal vector_a, Hamming.hash_to_vector(hash_a)
26
+ assert_equal hash_b, Hamming.vector_to_hash(vector_b)
27
+ end
28
+
29
+ it "#distance" do
30
+ assert_equal 0, Hamming.distance(vector_b, vector_b)
31
+ assert_equal 0, Hamming.distance(hash_a, hash_a)
32
+ assert_equal 1, Hamming.distance(hash_a, vector_b)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,2 @@
1
+ require "minitest/autorun"
2
+ require "hamming"
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hamming
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - elcuervo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-04-29 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - elcuervo@elcuervo.net
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - Makefile
22
+ - README.md
23
+ - hamming.gemspec
24
+ - lib/hamming.rb
25
+ - spec/hamming_spec.rb
26
+ - spec/spec_helper.rb
27
+ homepage: http://github.com/elcuervo/hamming
28
+ licenses:
29
+ - MIT
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubygems_version: 3.0.3
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: Hamming calculations
50
+ test_files: []