kastner-hasher 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Erik Kastner
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gemspec|
4
+ gemspec.name = "hasher"
5
+ gemspec.summary = "Hash strings just like AppGen does!"
6
+ gemspec.email = "kastner@gmail.com"
7
+ gemspec.homepage = "http://github.com/kastner/hasher"
8
+ gemspec.description = "A library that uses the same hash function as AppGen 4GL"
9
+ gemspec.authors = ["Erik Kastner"]
10
+ gemspec.rubyforge_project = "hasher"
11
+ end
12
+ rescue LoadError
13
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
14
+ end
15
+
16
+ task :default => :features
17
+
18
+ begin
19
+ require 'cucumber/rake/task'
20
+ Cucumber::Rake::Task.new(:features)
21
+ rescue LoadError
22
+ task :features do
23
+ abort "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
24
+ end
25
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
data/cucumber.yml ADDED
@@ -0,0 +1,3 @@
1
+ default: features
2
+ autotest: --format profile
3
+
@@ -0,0 +1,34 @@
1
+ Feature: Consistent Hashing
2
+ As a l33t hax0r
3
+ I want to use the language of my choice
4
+ So that I can extend our backend system MY way.
5
+
6
+ Scenario: Hashing
7
+ Given a hash container size of 90917
8
+ When the method hash is invoked with 17614
9
+ Then the number 1 is returned
10
+
11
+ Scenario: Hashing Redux
12
+ Given a hash container size of 1009
13
+ When the method hash is invoked with O*519938
14
+ Then the number 1 is returned
15
+
16
+ Scenario: Hashing The Third
17
+ Given a hash container size of 1009
18
+ When the method hash is invoked with O*259631T
19
+ Then the number 789 is returned
20
+
21
+ Scenario Outline: Hashing a bunch
22
+ Given a hash container size of <size>
23
+ When the method hash is invoked with <key>
24
+ Then the number <bucket> is returned
25
+
26
+ Examples:
27
+ | size | key | bucket |
28
+ | 90917 | 17614 | 1 |
29
+ | 1009 | O*259631T | 789 |
30
+ | 1009 | O*519938 | 1 |
31
+ | 1009 | C*375193 | 936 |
32
+ | 1009 | O*576156 | 1001 |
33
+ | 1009 | C*535806 | 814 |
34
+ | 55711 | 071529PG0013798*01 | 0 |
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../lib/hasher.rb')
2
+ require 'cucumber'
3
+
4
+ require 'test/unit/assertions'
5
+ require 'test/spec'
6
+
7
+ World(Test::Unit::Assertions)
@@ -0,0 +1,23 @@
1
+ require 'cucumber/formatter/unicode'
2
+
3
+ Given /^a hash container size of (\d+)$/ do |size|
4
+ @hasher = Hasher.new(size.to_i)
5
+ end
6
+
7
+ When /^the method hash is invoked with (.+?)$/ do |string|
8
+ @output = @hasher.hash(string)
9
+ end
10
+
11
+ Then /^the number (\d+) is returned$/ do |result|
12
+ @output.should == result.to_i
13
+ end
14
+
15
+ Given /^a hash container with a non\-prime size$/ do
16
+ @size = 55
17
+ end
18
+
19
+ Then /^a (.+?) error should be raised$/ do |error|
20
+ klass, exception = error.split(/::/)
21
+ klass = Object.const_get(klass)
22
+ lambda { Hasher.new(@size) }.should.raise(klass.const_get(exception))
23
+ end
data/hasher.gemspec ADDED
@@ -0,0 +1,47 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{hasher}
5
+ s.version = "0.2.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Erik Kastner"]
9
+ s.date = %q{2009-05-24}
10
+ s.description = %q{A library that uses the same hash function as AppGen 4GL}
11
+ s.email = %q{kastner@gmail.com}
12
+ s.extra_rdoc_files = [
13
+ "LICENSE"
14
+ ]
15
+ s.files = [
16
+ "LICENSE",
17
+ "Rakefile",
18
+ "VERSION",
19
+ "cucumber.yml",
20
+ "features/hashing.feature",
21
+ "features/steps/env.rb",
22
+ "features/steps/hasher_steps.rb",
23
+ "hasher.gemspec",
24
+ "lib/hasher.rb",
25
+ "pkg/hasher-0.1.0.gem",
26
+ "test/spec_hasher.rb"
27
+ ]
28
+ s.homepage = %q{http://github.com/kastner/hasher}
29
+ s.rdoc_options = ["--charset=UTF-8"]
30
+ s.require_paths = ["lib"]
31
+ s.rubyforge_project = %q{hasher}
32
+ s.rubygems_version = %q{1.3.3}
33
+ s.summary = %q{Hash strings just like AppGen does!}
34
+ s.test_files = [
35
+ "test/spec_hasher.rb"
36
+ ]
37
+
38
+ if s.respond_to? :specification_version then
39
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
43
+ else
44
+ end
45
+ else
46
+ end
47
+ end
data/lib/hasher.rb ADDED
@@ -0,0 +1,18 @@
1
+ class Hasher
2
+ def initialize(size)
3
+ @size = size
4
+ end
5
+
6
+ def hash(input, length=input.length)
7
+ hash = 0
8
+ length.times do |i|
9
+ new_hash = hash << 0x7 ^ input[i]
10
+ overflow = hash >> 0x15 & 0x1fc
11
+ hash = new_hash ^ overflow
12
+ end
13
+ hash &= 0x7fffffff
14
+ hash % @size
15
+ end
16
+
17
+ class NonPrimeContainer < StandardError; end
18
+ end
Binary file
@@ -0,0 +1,8 @@
1
+ # require 'test/spec'
2
+ #
3
+ # describe "Hasher" do
4
+ # it "should hash 17614 to 1 with a hash size of 90917" do
5
+ # h = Hasher.new(90917)
6
+ # h.hash(17614).should == 1
7
+ # end
8
+ # end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kastner-hasher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Erik Kastner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-24 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A library that uses the same hash function as AppGen 4GL
17
+ email: kastner@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ files:
25
+ - LICENSE
26
+ - Rakefile
27
+ - VERSION
28
+ - cucumber.yml
29
+ - features/hashing.feature
30
+ - features/steps/env.rb
31
+ - features/steps/hasher_steps.rb
32
+ - hasher.gemspec
33
+ - lib/hasher.rb
34
+ - pkg/hasher-0.1.0.gem
35
+ - test/spec_hasher.rb
36
+ has_rdoc: false
37
+ homepage: http://github.com/kastner/hasher
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --charset=UTF-8
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project: hasher
58
+ rubygems_version: 1.2.0
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Hash strings just like AppGen does!
62
+ test_files:
63
+ - test/spec_hasher.rb