Skip to content

Instantly share code, notes, and snippets.

@fumokmm
Created October 26, 2009 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fumokmm/218685 to your computer and use it in GitHub Desktop.
Save fumokmm/218685 to your computer and use it in GitHub Desktop.
リストにハッシュをつけてランダムアクセスするデモ
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