Advertisement
Guest User

Untitled

a guest
May 24th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.33 KB | None | 0 0
  1. package hashing
  2.  
  3. import src.HashingInterface
  4. import tools.ArrayGenerator
  5. import tools.Tester
  6.  
  7. abstract class BaseHashingInt(private var arrayGenerator: ArrayGenerator, val percentFill: Float) : HashingInterface<Int> {
  8.  
  9.     private val array : Array<Int> by lazy {
  10.       val tempArray = Array(HashingInterface.arraySize) {0}
  11.         for (i in 0 until (HashingInterface.arraySize * percentFill).toInt()){
  12.             insert(arrayGenerator.generatedArrayInt[i],tempArray)
  13.         }
  14.         tempArray
  15.     }
  16.     private var tester = Tester()
  17.  
  18.     init {
  19.         runSearchHit()
  20.         runSearchMiss()
  21.     }
  22.  
  23.     protected abstract fun hashFunction(value: Int, elementIndex: Int): Int
  24.  
  25.  
  26.     override fun search(searchedValue: Int) {
  27.         var index : Int
  28.         for (i in 0 until HashingInterface.arraySize) {
  29.             index = Math.abs(hashFunction(searchedValue, i))
  30.             tester.increaseCount()
  31.             if (array[index] == searchedValue || array[index] == 0) {
  32.                 break
  33.             }
  34.         }
  35.     }
  36.  
  37.     override fun insert(valueToAdd: Int) {
  38.         var index : Int
  39.         for (i in 0 until HashingInterface.arraySize) {
  40.             index = Math.abs(hashFunction(valueToAdd, i))
  41.             if (array[index] == 0) {
  42.                 array[index] = valueToAdd
  43.                 break
  44.             }
  45.         }
  46.     }
  47.  
  48.     override fun insert(valueToAdd: Int, array: Array<Int>) {
  49.         var index : Int
  50.         for (i in 0 until HashingInterface.arraySize) {
  51.             index = Math.abs(hashFunction(valueToAdd, i))
  52.             if (array[index] == 0) {
  53.                 array[index] = valueToAdd
  54.                 break
  55.             }
  56.         }
  57.     }
  58.  
  59.     override fun runSearchHit() {
  60.         tester.cleanCount()
  61.         for (i in 0 until ((HashingInterface.testSize * percentFill).toInt()) step 10) {
  62.             search(arrayGenerator.generatedArrayInt[i])
  63.         }
  64.         println("Hit int: " + (tester.count.toDouble() / (HashingInterface.testSize * percentFill).toDouble()))
  65.     }
  66.  
  67.     override fun runSearchMiss() {
  68.         tester.cleanCount()
  69.         for (i in 0 until ((HashingInterface.testSize * percentFill).toInt()) step 10) {
  70.             search(i)
  71.         }
  72.         println("Miss int: " + (tester.count.toDouble() / (HashingInterface.testSize * percentFill).toDouble()))
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement