Commits

Kenneth Jørgensen committed b774ab7 Draft

Stuff

Comments (0)

Files changed (7)

+# Tovikov Library
+The purpose of `tovikov` is to provide a comprehensive set of math, hash, and cryptographic algorithms.
+The project is still in very early development.
+The project is [hosted on bitbucket](https://bitbucket.org/kennethjor/tovikov).

spec/hash/StringHashCodeSpec.coffee

+_ = require "underscore"
+
+StringHashCode = require "../../src/hash/StringHashCode"
+
+TESTS = [
+	["", 0],
+	["a", 97],
+	["abc", 96354],
+	["abcdef", -1424385949],
+	["abcdefghijklmnopqrstuvwxyz", 958031277]
+]
+
+#describe "SringHashCode", ->
+#	it "should match Java", ->
+#		hash = new StringHashCode()
+#		for test in TESTS
+#			result = hash.getHash test[0]
+#			expect(result).toBe test[1]

spec/utils/IntegerUtilSpec.coffee

+IntegerUtil = require "../../src/utils/IntegerUtil"
+
+#describe "IntegerUtil", ->
+#	describe "powMod", ->
+#		it "should calculate correctly when modding well inside the 32-bit limit", ->
+#			expect(IntegerUtil.powMod(5, 100, 65535)).toBe 35815
+#
+#		it "should calculate correctly when modding close to the 32-bit limit", ->
+#			# 5^16 = 0x71afd498d, we want the lower 32 bits
+#			expect(IntegerUtil.powMod(5, 16, 0x100000000)).toBe 0x1afd498d

src/grunt.js

-var fs = require("fs")
-var exec = require("child_process").exec
-var browserify = require("browserify");
-var templatify = require("templatify");
-
-module.exports = function(grunt) {
-	grunt.initConfig({
-		pkg: '<json:package.json>',
-
-//		copy: {
-//			html: {
-//				files: {
-//					"build/client/": "src/client/index.html"
-//				}
-//			}
-//		},
-
-		watch: {
-			files: [
-				"src/**",
-				"test/**"
-			],
-			tasks: "default"
-		}
-	});
-
-	// Load grunt plugins.
-//	grunt.loadNpmTasks("grunt-contrib-copy");
-
-	grunt.registerTask("jessie", "Runs Jasmine specs through Jessie.", function() {
-		done = this.async();
-		command = "./node_modules/jessie/bin/jessie spec"
-		exec(command, function(err, stdout, stderr) {
-			console.log(stdout);
-			if (err) {
-				grunt.warn(err);
-				done(false);
-			}
-			else {
-				done(true);
-			}
-		});
-	});
-
-	// Default task.
-//	grunt.registerTask("default", "coffee handlebars templateFix browserify copy:html");
-//	grunt.registerTask("default", "browserify copy:html");
-	grunt.registerTask("default", "coffee jessie");
-};

src/hash/StringHashCode.coffee

+# Simple hash algorithm to convert a string of any length to an integer.
+# Based on Java's String.hashCode.
+# http://en.wikipedia.org/wiki/Java_hashCode()
+# DOESN'T WORK YET
+module.exports = class StringHashCode
+	getHash: (input) ->
+		unless typeof input is "string" then throw new Error "Input for StringHashCode must be a string"
+		return 0 if input.length is 0
+		hash = 0
+		console.log "hash :: #{input}"
+		for i in [0..input.length-1]
+			code = input.charCodeAt i
+			exponent = input.length - 1 - i
+			val = code * Math.pow 31, exponent
+			hash += val & 0xffffffff
+			console.log "\t#{hash}"
+		return hash

src/package.json

-{
-	"fullname": "namesrandom",
-	"name": "namedrandom",
-	"private": true,
-	"version": "0.1.0",
-	"description": "Random library.",
-	"keywords": [],
-	"homepage": "https://bitbucket.org/kennethjor/namedrandom",
-	"bugs": "https://bitbucket.org/kennethjor/namedrandom/issues",
-	"repository": {
-		"type": "hg",
-		"url": "ssh://hg@bitbucket.org/kennethjor/namedrandom"
-	},
-	"main": "build/client/init.js",
-	"dependencies": {
-		"underscore": "~ 1.4.3",
-		"calamity": "~ 0.2.0",
-		"jquery-browser": "~ 1.8.2",
-		"backbone": "~ 0.9.9",
-		"handlebars": "~ 1.0.7",
-		"rivets": "~ 0.3.4",
-		"crossroads": "~ 0.11"
-	},
-	"devDependencies": {
-		"coffee-script": "~ 1.4.0",
-		"grunt-contrib-copy": "~ 0.3.1",
-		"templatify": "~ 0.0.2",
-		"browserify": "~ 1.16.6",
-		"jessie": "~ 0.4.2",
-		"sinon": "~ 1.5.2"
-	}
-}

src/utils/IntegerUtil.coffee

+module.exports = class IntegerUtil
+
+# 32-bit version of `Math.pow(base, exp) % mod`.
+# http://stackoverflow.com/questions/5989429/pow-and-mod-function-optimization
+# http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2.6
+powMod = IntegerUtil.powMod = ( base, pow, mod ) ->
+#	if exp is 0 then return 1
+#	if exp % 2 is 0
+#		return Math.pow( powMod( base, (exp / 2), mod), 2) % mod
+#	else
+#		return (base * powMod( base, (exp - 1), mod)) % mod
+
+	result = 1
+	for i in [0..pow-1]
+		result *= base
+		result %= mod
+	return result