Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @author Denis Zharkov
- */
- import java.util.LinkedList
- import java.util.HashSet
- object Constants {
- public val DEFAULT_BUCKETS_COUNT: Int = 1
- }
- class MyHashMap<K, V>() : MutableMap<K,V> {
- public val size : Int
- get() {
- return _size;
- }
- override public fun isEmpty() : Boolean = _size > 0
- override public fun containsKey(key: Any?) : Boolean {
- return findEntry(key) != null
- }
- override public fun containsValue(value : Any?) : Boolean {
- return table.count { l -> l.count { e -> e.value.equals(value) } > 0 } > 0
- }
- override fun size(): Int {
- return size
- }
- override public fun get(key: Any?) : V? {
- return findEntry(key)?.value
- }
- // Modification Operations
- override public fun put(key : K, value : V) : V? {
- val bucket = findBucket(key)
- val entry = findEntry(key, bucket)
- if (entry != null) {
- entry.setValue(value)
- } else {
- bucket.add(Entry(key, value))
- _size++
- }
- return value
- }
- public fun set(key : K, value : V) {
- put(key, value)
- }
- override public fun remove(key: Any?) : V? {
- val bucket = findBucket(key)
- val entry = findEntry(key, bucket)
- if (entry != null) {
- bucket.remove(entry)
- _size--
- }
- return entry?.value
- }
- override public fun putAll(m : Map<out K, V>) {
- m.entrySet().forEach { e -> Entry(e.key, e.value) }
- }
- override public fun clear() {
- table.forEach { l -> l.clear() }
- }
- override fun keySet() : MutableSet<K> {
- return entrySet().map { e -> e.getKey() }.toCollection(HashSet<K>())
- }
- override fun values() : MutableCollection<V> {
- return entrySet().map { e -> e.getValue() }.toLinkedList()
- }
- override fun entrySet() : MutableSet<MutableMap.MutableEntry<K, V>> {
- return table.flatMap { l -> l }.toCollection(HashSet<MutableMap.MutableEntry<K, V>>())
- }
- private inner class Entry<K,V>(private val _key: K, private var _value: V) : MutableMap.MutableEntry<K,V> {
- override fun hashCode(): Int {
- return _key.hashCode()
- }
- override fun equals(other: Any?): Boolean {
- return when (other) {
- is Entry<*,*> -> other.getKey().equals(_key) && other.getValue().equals(_value)
- else -> false
- }
- }
- override fun getKey(): K {
- return _key
- }
- override fun getValue(): V {
- return _value
- }
- override fun setValue(value: V): V {
- this._value = value
- return value
- }
- }
- private fun findBucket(key: Any?) : LinkedList<Entry<K,V>> {
- return table[(key?.hashCode() ?: 0) % table.size]
- }
- private fun findEntry(key: Any?, bucket : LinkedList<Entry<K,V>>) : Entry<K,V>? {
- return bucket.filter { e -> e.key.equals(key) } .firstOrNull()
- }
- private fun findEntry(key: Any?) : Entry<K,V>? {
- return findEntry(key, findBucket(key))
- }
- private val table : Array<LinkedList<Entry<K,V>>>
- = Array<LinkedList<Entry<K,V>>>(Constants.DEFAULT_BUCKETS_COUNT)
- {
- LinkedList<Entry<K,V>>()
- }
- private var _size : Int = 0;
- }
- /**
- *
- * @author Denis Zharkov
- */
- fun main(args: Array<String>): Unit {
- var q : Int? = null
- println(q)
- val myMap = MyHashMap<Int,Int>()
- myMap[1] = 1;
- for (i in 1..100) {
- myMap[i * 100] = i;
- }
- println(myMap.size)
- for (i in 1..50) {
- myMap.remove(i * 100)
- }
- println(myMap.size)
- for (i in 51..100) {
- println(myMap[i * 100])
- }
- println(myMap.containsKey(100))
- println(myMap.containsKey(51 * 100))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement