Advertisement
Guest User

Untitled

a guest
Mar 5th, 2014
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.92 KB | None | 0 0
  1. /**
  2.  *
  3.  * @author    Denis Zharkov
  4.  */
  5.  
  6. import java.util.LinkedList
  7. import java.util.HashSet
  8.  
  9. object Constants {
  10.     public val DEFAULT_BUCKETS_COUNT: Int = 1
  11. }
  12.  
  13. class MyHashMap<K, V>() : MutableMap<K,V> {
  14.  
  15.     public val size : Int
  16.         get() {
  17.             return _size;
  18.         }
  19.  
  20.     override public fun isEmpty() : Boolean = _size > 0
  21.  
  22.     override public fun containsKey(key: Any?) : Boolean {
  23.         return findEntry(key) != null
  24.     }
  25.  
  26.     override public fun containsValue(value : Any?) : Boolean {
  27.         return table.count { l -> l.count { e -> e.value.equals(value) } > 0 } > 0
  28.     }
  29.  
  30.     override fun size(): Int {
  31.         return size
  32.     }
  33.  
  34.     override public fun get(key: Any?) : V? {
  35.         return findEntry(key)?.value
  36.     }
  37.  
  38.     // Modification Operations
  39.     override public fun put(key : K, value : V) : V? {
  40.         val bucket = findBucket(key)
  41.         val entry = findEntry(key, bucket)
  42.  
  43.         if (entry != null) {
  44.             entry.setValue(value)
  45.         } else {
  46.             bucket.add(Entry(key, value))
  47.             _size++
  48.         }
  49.  
  50.         return value
  51.     }
  52.  
  53.     public fun set(key : K, value : V) {
  54.         put(key, value)
  55.     }
  56.  
  57.     override public fun remove(key: Any?) : V? {
  58.         val bucket = findBucket(key)
  59.         val entry = findEntry(key, bucket)
  60.  
  61.         if (entry != null) {
  62.             bucket.remove(entry)
  63.             _size--
  64.         }
  65.  
  66.         return entry?.value
  67.     }
  68.  
  69.     override public fun putAll(m : Map<out K, V>) {
  70.         m.entrySet().forEach { e -> Entry(e.key, e.value) }
  71.     }
  72.  
  73.     override public fun clear() {
  74.         table.forEach { l -> l.clear() }
  75.     }
  76.  
  77.     override fun keySet() : MutableSet<K> {
  78.         return entrySet().map { e -> e.getKey() }.toCollection(HashSet<K>())
  79.     }
  80.  
  81.     override fun values() : MutableCollection<V> {
  82.         return entrySet().map { e -> e.getValue() }.toLinkedList()
  83.     }
  84.  
  85.     override fun entrySet() : MutableSet<MutableMap.MutableEntry<K, V>> {
  86.         return table.flatMap { l -> l }.toCollection(HashSet<MutableMap.MutableEntry<K, V>>())
  87.     }
  88.  
  89.     private inner class Entry<K,V>(private val _key: K, private var _value: V) : MutableMap.MutableEntry<K,V> {
  90.         override fun hashCode(): Int {
  91.             return _key.hashCode()
  92.         }
  93.         override fun equals(other: Any?): Boolean {
  94.             return when (other) {
  95.                 is Entry<*,*> -> other.getKey().equals(_key) && other.getValue().equals(_value)
  96.                 else -> false
  97.             }
  98.         }
  99.         override fun getKey(): K {
  100.             return _key
  101.         }
  102.         override fun getValue(): V {
  103.             return _value
  104.         }
  105.         override fun setValue(value: V): V {
  106.             this._value = value
  107.             return value
  108.         }
  109.     }
  110.  
  111.     private fun findBucket(key: Any?) : LinkedList<Entry<K,V>> {
  112.         return table[(key?.hashCode() ?: 0) % table.size]
  113.     }
  114.  
  115.     private fun findEntry(key: Any?, bucket : LinkedList<Entry<K,V>>) : Entry<K,V>? {
  116.         return bucket.filter { e -> e.key.equals(key) } .firstOrNull()
  117.     }
  118.  
  119.     private fun findEntry(key: Any?) : Entry<K,V>? {
  120.         return findEntry(key, findBucket(key))
  121.     }
  122.  
  123.     private val table : Array<LinkedList<Entry<K,V>>>
  124.             = Array<LinkedList<Entry<K,V>>>(Constants.DEFAULT_BUCKETS_COUNT)
  125.     {
  126.         LinkedList<Entry<K,V>>()
  127.     }
  128.  
  129.     private var _size : Int = 0;
  130. }
  131.  
  132. /**
  133.  *
  134.  * @author    Denis Zharkov
  135.  */
  136.  
  137. fun main(args: Array<String>): Unit {
  138.     var q : Int? = null
  139.  
  140.     println(q)
  141.     val myMap = MyHashMap<Int,Int>()
  142.  
  143.     myMap[1] = 1;
  144.  
  145.     for (i in 1..100) {
  146.         myMap[i * 100] = i;
  147.     }
  148.  
  149.     println(myMap.size)
  150.  
  151.     for (i in 1..50) {
  152.         myMap.remove(i * 100)
  153.     }
  154.  
  155.     println(myMap.size)
  156.  
  157.     for (i in 51..100) {
  158.         println(myMap[i * 100])
  159.     }
  160.  
  161.     println(myMap.containsKey(100))
  162.     println(myMap.containsKey(51 * 100))
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement