Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions coil-base/src/main/java/coil/collection/SparseIntArraySet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,51 @@ import coil.util.growAndInsert
class SparseIntArraySet @JvmOverloads constructor(initialCapacity: Int = 10) {

private var elements = IntArray(initialCapacity)
private var size = 0
private var _size = 0

/** Adds an element to the set. */
/** Returns the number of elements that this set currently stores. */
val size: Int get() = _size

/** Adds the element to the set. */
fun add(element: Int): Boolean {
val i = elements.binarySearch(element, toIndex = size)
val i = elements.binarySearch(element, toIndex = _size)
val absent = i < 0
if (absent) {
elements = elements.growAndInsert(i.inv(), element, size)
size++
elements = elements.growAndInsert(i.inv(), element, _size)
_size++
}
return absent
}

/** Removes the element from the set. Return true if it was present. */
fun remove(element: Int): Boolean {
val i = elements.binarySearch(element, toIndex = size)
val i = elements.binarySearch(element, toIndex = _size)
val present = i >= 0
if (present) {
removeAt(i)
}
return present
}

/** Return true if the SparseIntArraySet contains this element. */
operator fun contains(element: Int): Boolean = elements.binarySearch(element, toIndex = size) >= 0
/** Return true if the set contains this element. */
operator fun contains(element: Int): Boolean = elements.binarySearch(element, toIndex = _size) >= 0

/** Removes the element at the given index. */
fun removeAt(index: Int) {
elements.copyInto(elements, destinationOffset = index, startIndex = index + 1, endIndex = size)
size--
elements.copyInto(elements, destinationOffset = index, startIndex = index + 1, endIndex = _size)
_size--
}

/** Returns the number of elements that this SparseIntArraySet currently stores. */
fun size(): Int = size
/** @see size */
@Deprecated(
message = "Replace with val.",
replaceWith = ReplaceWith("size")
)
fun size(): Int = _size

/**
* Given an index in the range `[0, size)`, returns
* the element from the `index`th key-value mapping that this
* SparseIntArraySet stores.
* Given an index in the range `[0, size)`, returns the element from the
* `index`th key-value mapping that this set stores.
*
* The elements corresponding to indices in ascending order are guaranteed to
* be in ascending order, e.g., `elementAt(0)` will return the
Expand All @@ -67,10 +73,10 @@ class SparseIntArraySet @JvmOverloads constructor(initialCapacity: Int = 10) {
* specified element, or a negative number if the specified
* element is not mapped.
*/
fun indexOfElement(key: Int): Int = elements.binarySearch(key, toIndex = size)
fun indexOfElement(element: Int): Int = elements.binarySearch(element, toIndex = _size)

/** Removes all elements from this SparseIntArraySet. */
/** Removes all elements from this set. */
fun clear() {
size = 0
_size = 0
}
}
25 changes: 21 additions & 4 deletions coil-base/src/main/java/coil/extension/SparseIntArraySets.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,48 @@ package coil.extension

import coil.collection.SparseIntArraySet

/** Returns the number of elements that this set currently stores. */
inline fun SparseIntArraySet.count(): Int = size

/** Adds the element to the set. */
inline operator fun SparseIntArraySet.plusAssign(element: Int) {
add(element)
}

/** Removes the element from the set. */
inline operator fun SparseIntArraySet.minusAssign(element: Int) {
remove(element)
}

inline fun SparseIntArraySet.isEmpty(): Boolean = size() == 0
/** Return true when the set contains no elements. */
inline fun SparseIntArraySet.isEmpty(): Boolean = size == 0

inline fun SparseIntArraySet.isNotEmpty(): Boolean = size() != 0
/** Return true when the set contains elements. */
inline fun SparseIntArraySet.isNotEmpty(): Boolean = size != 0

/** Create and return a new set that contains the elements of [this] plus the elements of [other]. */
operator fun SparseIntArraySet.plus(other: SparseIntArraySet): SparseIntArraySet {
val new = SparseIntArraySet(size() + other.size())
val new = SparseIntArraySet(size + other.size)
new.addAll(this)
new.addAll(other)
return new
}

/** Add all elements from [other] to [this]. */
fun SparseIntArraySet.addAll(other: SparseIntArraySet) {
other.forEach { add(it) }
}

/** Performs the given [action] for each element in the set. */
inline fun SparseIntArraySet.forEach(action: (element: Int) -> Unit) {
for (index in 0 until size()) {
for (index in 0 until size) {
action(elementAt(index))
}
}

/** Return an iterator over the set's values. */
operator fun SparseIntArraySet.iterator(): IntIterator = object : IntIterator() {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this as SparseIntArray ktx has it.

var index = 0
override fun hasNext() = index < size
override fun nextInt() = elementAt(index++)
}
16 changes: 8 additions & 8 deletions coil-base/src/test/java/coil/collection/SparseIntArraySetTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SparseIntArraySetTest {
@Test
fun `can add element`() {
assertTrue(set.add(1))
assertEquals(1, set.size())
assertEquals(1, set.size)
}

@Test
Expand Down Expand Up @@ -57,10 +57,10 @@ class SparseIntArraySetTest {
set.add(1)
set.add(2)
set.add(3)
assertEquals(3, set.size())
assertEquals(3, set.size)

set.clear()
assertEquals(0, set.size())
assertEquals(0, set.size)
assertFalse(set.contains(1))
assertFalse(set.contains(2))
assertFalse(set.contains(3))
Expand All @@ -69,11 +69,11 @@ class SparseIntArraySetTest {
@Test
fun `adding the same element does not increment size`() {
assertTrue(set.add(100))
assertEquals(1, set.size())
assertEquals(1, set.size)
assertFalse(set.add(100))
assertEquals(1, set.size())
assertEquals(1, set.size)
assertTrue(set.add(200))
assertEquals(2, set.size())
assertEquals(2, set.size)
}

@Test
Expand Down Expand Up @@ -108,7 +108,7 @@ class SparseIntArraySetTest {
set.add(value)
}

assertEquals(numValues, set.size())
assertEquals(numValues, set.size)
for (value in values) {
assertTrue(set.contains(value), "Set does not contain $value.")
}
Expand All @@ -120,7 +120,7 @@ class SparseIntArraySetTest {
set.remove(value)
}

assertEquals(numValues / 2, set.size())
assertEquals(numValues / 2, set.size)

val keptValues = shuffledValues.subList(numValues / 2, numValues)
for (value in keptValues.shuffled()) {
Expand Down