Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hashing
- import src.HashingInterface
- import tools.ArrayGenerator
- import tools.Tester
- abstract class BaseHashingInt(private var arrayGenerator: ArrayGenerator, val percentFill: Float) : HashingInterface<Int> {
- private val array : Array<Int> by lazy {
- val tempArray = Array(HashingInterface.arraySize) {0}
- for (i in 0 until (HashingInterface.arraySize * percentFill).toInt()){
- insert(arrayGenerator.generatedArrayInt[i],tempArray)
- }
- tempArray
- }
- private var tester = Tester()
- init {
- runSearchHit()
- runSearchMiss()
- }
- protected abstract fun hashFunction(value: Int, elementIndex: Int): Int
- override fun search(searchedValue: Int) {
- var index : Int
- for (i in 0 until HashingInterface.arraySize) {
- index = Math.abs(hashFunction(searchedValue, i))
- tester.increaseCount()
- if (array[index] == searchedValue || array[index] == 0) {
- break
- }
- }
- }
- override fun insert(valueToAdd: Int) {
- var index : Int
- for (i in 0 until HashingInterface.arraySize) {
- index = Math.abs(hashFunction(valueToAdd, i))
- if (array[index] == 0) {
- array[index] = valueToAdd
- break
- }
- }
- }
- override fun insert(valueToAdd: Int, array: Array<Int>) {
- var index : Int
- for (i in 0 until HashingInterface.arraySize) {
- index = Math.abs(hashFunction(valueToAdd, i))
- if (array[index] == 0) {
- array[index] = valueToAdd
- break
- }
- }
- }
- override fun runSearchHit() {
- tester.cleanCount()
- for (i in 0 until ((HashingInterface.testSize * percentFill).toInt()) step 10) {
- search(arrayGenerator.generatedArrayInt[i])
- }
- println("Hit int: " + (tester.count.toDouble() / (HashingInterface.testSize * percentFill).toDouble()))
- }
- override fun runSearchMiss() {
- tester.cleanCount()
- for (i in 0 until ((HashingInterface.testSize * percentFill).toInt()) step 10) {
- search(i)
- }
- println("Miss int: " + (tester.count.toDouble() / (HashingInterface.testSize * percentFill).toDouble()))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement