Created
October 27, 2009 08:35
-
-
Save fumokmm/219418 to your computer and use it in GitHub Desktop.
ハッシュによるランダムアクセスのテスト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String getSHA1(String source) { | |
java.security.MessageDigest md = java.security.MessageDigest.getInstance('SHA-1') | |
md.update(source.getBytes('UTF-8')) | |
md.digest().collect{ String.format('%02x', it) }.join() | |
} | |
def list = (1..10).collect{ | |
[it, getSHA1(it.toString())] | |
} | |
println list | |
println collect(list, 3) | |
def collect(list, num) { | |
def hash = getSHA1(Runtime.runtime.freeMemory().toString()) | |
def result = [] | |
result.addAll list.findAll{ it[1] <= hash } | |
if (result.size() >= num) { | |
return result[0..<num] | |
} | |
result.addAll list.findAll{ it[1] > hash } | |
println "2: ${result.size()}個" | |
if (result.size() >= num) { | |
return result[0..<num] | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment