Skip to content
This repository was archived by the owner on Jan 20, 2023. It is now read-only.

Commit 6ba6702

Browse files
committed
判定をABSENT_VALUEで書き直し
1 parent 71edbcd commit 6ba6702

File tree

1 file changed

+20
-36
lines changed

1 file changed

+20
-36
lines changed

src/main/kotlin/com/mapk/fastkfunction/argumentbucket/ArgumentBucket.kt

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,74 +6,58 @@ import kotlin.reflect.KParameter
66
class ArgumentBucket(
77
private val keyList: List<KParameter>,
88
private val valueArray: Array<Any?>,
9-
private val initializationStatuses: BooleanArray,
9+
private var count: Int,
1010
private val valueArrayGetter: (Array<Any?>) -> Array<Any?>
1111
) : Map<KParameter, Any?> {
12-
fun isFullInitialized(): Boolean = initializationStatuses.all { it }
12+
fun isFullInitialized(): Boolean = count == keyList.size
1313

1414
// getValueArrayは内部処理でしか利用しないためinternal化
1515
internal fun getValueArray(): Array<Any?> = valueArrayGetter(valueArray)
1616

17-
operator fun set(key: KParameter, value: Any?): Any? {
18-
return valueArray[key.index].apply {
19-
valueArray[key.index] = value
20-
initializationStatuses[key.index] = true
21-
}
22-
}
17+
operator fun set(key: KParameter, value: Any?): Any? = set(key.index, value)
2318

24-
operator fun set(index: Int, value: Any?): Any? {
25-
return valueArray[index].apply {
26-
valueArray[index] = value
27-
initializationStatuses[index] = true
28-
}
19+
operator fun set(index: Int, value: Any?): Any? = valueArray[index].apply {
20+
if (this === ABSENT_VALUE) count++
21+
valueArray[index] = value
2922
}
3023

3124
/**
3225
* If the specified key is not already associated with a value associates it with the given value and returns
3326
* {@code null}, else returns the current value.
3427
*/
35-
fun setIfAbsent(key: KParameter, value: Any?): Any? {
36-
return if (initializationStatuses[key.index])
37-
valueArray[key.index]
38-
else
39-
null.apply {
40-
valueArray[key.index] = value
41-
initializationStatuses[key.index] = true
42-
}
43-
}
28+
fun setIfAbsent(key: KParameter, value: Any?): Any? = setIfAbsent(key.index, value)
4429

4530
/**
4631
* If the specified key is not already associated with a value associates it with the given value and returns
4732
* {@code null}, else returns the current value.
4833
*/
49-
fun setIfAbsent(index: Int, value: Any?): Any? {
50-
return if (initializationStatuses[index])
51-
valueArray[index]
52-
else
53-
null.apply {
54-
valueArray[index] = value
55-
initializationStatuses[index] = true
56-
}
34+
fun setIfAbsent(index: Int, value: Any?): Any? = valueArray[index].apply {
35+
if (this === ABSENT_VALUE) {
36+
count++
37+
valueArray[index] = value
38+
}
5739
}
5840

41+
private fun isInitialized(index: Int): Boolean = valueArray[index] !== ABSENT_VALUE
42+
5943
override val entries: Set<Map.Entry<KParameter, Any?>>
6044
get() = keyList.fold(HashSet()) { acc, cur ->
61-
acc.apply { if (initializationStatuses[cur.index]) add(Entry(cur, valueArray[cur.index])) }
45+
acc.apply { if (isInitialized(cur.index)) add(Entry(cur, valueArray[cur.index])) }
6246
}
6347
override val keys: Set<KParameter>
6448
get() = keyList.fold(HashSet()) { acc, cur ->
65-
acc.apply { if (initializationStatuses[cur.index]) add(cur) }
49+
acc.apply { if (isInitialized(cur.index)) add(cur) }
6650
}
67-
override val size: Int get() = initializationStatuses.count { it }
51+
override val size: Int get() = count
6852
override val values: Collection<Any?>
69-
get() = valueArray.filterIndexed { idx, _ -> initializationStatuses[idx] }
53+
get() = valueArray.filter { it !== ABSENT_VALUE }
7054

7155
// keyはインスタンスの一致と初期化状態を見る
7256
override fun containsKey(key: KParameter): Boolean =
73-
keyList[key.index] === key && initializationStatuses[key.index]
57+
keyList[key.index] === key && isInitialized(key.index)
7458

7559
override fun containsValue(value: Any?): Boolean = valueArray.withIndex()
76-
.any { initializationStatuses[it.index] && it.value == value }
60+
.any { isInitialized(it.index) && it.value == value }
7761

7862
override fun get(key: KParameter): Any? = valueArray[key.index]
7963
operator fun get(index: Int): Any? = valueArray[index]

0 commit comments

Comments
 (0)