Skip to content

Commit 348f8a6

Browse files
saving executing files
1 parent 5eb1331 commit 348f8a6

File tree

3 files changed

+67
-56
lines changed

3 files changed

+67
-56
lines changed

app/src/main/java/com/leetcode_kotlin/Executing.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.leetcode_kotlin
22

3+
import java.util.concurrent.ConcurrentHashMap
4+
35

46
/**
57
* Executing
@@ -8,11 +10,10 @@ package com.leetcode_kotlin
810

911
fun main() {
1012

13+
val concurrentHashMap = ConcurrentHashMap<Int, Int>()
14+
15+
concurrentHashMap.put(1, 5)
1116

12-
val arr = intArrayOf(1, 4, 6, 7, 3, 7, 7)
13-
arr.drop(1).forEachIndexed { index, num ->
14-
println("$index $num")
15-
}
1617

1718
}
1819

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,15 @@
11
package com.github.contest
22

3-
import com.github.contest.backtracking.punishmentNumberProdVariant
4-
import com.github.contest.dp.countVowels
5-
import com.github.contest.graph.findJudge
6-
import com.github.contest.heap.customStructure.CustomMinHeap
7-
83

94
/**
105
* Stand
116
*/
127

138
fun main() {
149

15-
punishmentNumberProdVariant(10).apply { println(this) }
16-
17-
}
1810

19-
private fun IntArray.printArray() {
20-
var s = "["
21-
this.forEach { s += "$it, " }
22-
s += "]"
23-
println(s)
2411
}
2512

26-
fun findJudgeData() {
27-
findJudge(
28-
5,
29-
arrayOf(
30-
intArrayOf(1, 3),
31-
intArrayOf(2, 3),
32-
intArrayOf(4, 3),
33-
intArrayOf(4, 1),
34-
intArrayOf(5, 3),
35-
intArrayOf(5, 1),
36-
intArrayOf(5, 4)
37-
)
38-
)
39-
}
4013

41-
fun vowels() {
4214

43-
/**
44-
* a a a
45-
* aa
46-
* aa
47-
* aaa
48-
* count - 10
49-
*/
50-
51-
countVowels("aaa")
52-
53-
}
5415

55-
56-
fun heapWork() {
57-
58-
val heap = CustomMinHeap<Int>()
59-
heap.offer(3)
60-
heap.offer(1)
61-
heap.offer(2)
62-
heap.offer(5)
63-
64-
65-
println(heap.poll())
66-
67-
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.github.contest.hashTable.customStructure
2+
3+
import java.util.concurrent.locks.ReentrantLock
4+
import kotlin.concurrent.withLock
5+
6+
class CustomConcurrentHashMap<T> {
7+
8+
9+
}
10+
11+
12+
class MyConcurrentHashMap<K, V>(private val segmentCount: Int = 16) {
13+
14+
private class Segment<K, V> {
15+
val entries = mutableListOf<Entry<K, V>>()
16+
val lock = ReentrantLock()
17+
18+
fun get(key: K): V? = lock.withLock {
19+
entries.find { it.key == key }?.value
20+
}
21+
22+
fun put(key: K, value: V): V? = lock.withLock {
23+
entries.find { it.key == key }?.let {
24+
val oldValue = it.value
25+
it.value = value
26+
oldValue
27+
} ?: run {
28+
entries.add(Entry(key, value))
29+
null
30+
}
31+
}
32+
33+
fun remove(key: K): V? = lock.withLock {
34+
entries.find { it.key == key }?.let {
35+
entries.remove(it)
36+
it.value
37+
}
38+
}
39+
40+
fun size(): Int = lock.withLock { entries.size }
41+
42+
fun clear() = lock.withLock { entries.clear() }
43+
}
44+
45+
private data class Entry<K, V>(val key: K, var value: V)
46+
47+
private val segments: Array<Segment<K, V>> = Array(segmentCount) { Segment() }
48+
49+
private fun getSegmentIndex(key: K): Int = key.hashCode().let { Math.abs(it % segmentCount) }
50+
51+
fun get(key: K): V? = segments[getSegmentIndex(key)].get(key)
52+
53+
fun put(key: K, value: V): V? = segments[getSegmentIndex(key)].put(key, value)
54+
55+
fun remove(key: K): V? = segments[getSegmentIndex(key)].remove(key)
56+
57+
fun size(): Int = segments.sumOf { it.size() }
58+
59+
fun clear() {
60+
segments.forEach { it.clear() }
61+
}
62+
}

0 commit comments

Comments
 (0)