Skip to content

Commit 9ffc834

Browse files
committed
[perf] 性能优化,SectionInfo的end作为属性,因为contains调用相对频繁,不用每次判断contains都做加法
1 parent b3cf7fa commit 9ffc834

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

library/src/main/java/com/xiangning/sectionadapter/core/SectionInfo.kt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,24 @@ package com.xiangning.sectionadapter.core
33
/**
44
* 分组信息封装
55
*/
6-
data class SectionInfo<T>
6+
class SectionInfo<T>
77
@JvmOverloads
8-
constructor(val section: T, var start: Int, var count: Int = 0) {
9-
val end: Int
10-
get() = start + count
8+
constructor(val section: T, start: Int, count: Int = 0) {
9+
var start: Int = start
10+
set(value) {
11+
end += value - start
12+
field = value
13+
}
14+
var end: Int = start + count
15+
private set
1116

12-
operator fun contains(fullPos: Int): Boolean = fullPos in start until start + count
17+
var count: Int
18+
get() = end - start
19+
set(value) {
20+
end = start + value
21+
}
22+
23+
operator fun contains(fullPos: Int): Boolean = fullPos in start until end
1324

1425
fun toSectionPos(fullPos: Int) = fullPos - start
1526
fun toFullPosition(sectionPos: Int) = start + sectionPos

library/src/main/java/com/xiangning/sectionadapter/core/SectionListCounter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class SectionListCounter<T : Any> {
1313

1414
private val hitInfo = HitInfo<T>()
1515

16-
private val indexes = LinkedHashMap<T, Int>()
16+
private val indexes = HashMap<T, Int>()
1717
private val sectionInfoList = mutableListOf<SectionInfo<T>>()
1818

1919
@JvmOverloads

0 commit comments

Comments
 (0)