Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c0ac1d37 authored by Songchun Fan's avatar Songchun Fan
Browse files

[SparseSetArray] optimize copy constructor

BUG: 232347307
Test: atest android.util.SparseSetArrayTest
Change-Id: Ie046f557098ddc168b86523700cc06feede97e5e
parent 96ad5cca
Loading
Loading
Loading
Loading
+1 −4
Original line number Diff line number Diff line
@@ -38,10 +38,7 @@ public class SparseSetArray<T> {
        for (int i = 0; i < arraySize; i++) {
            final int key = src.keyAt(i);
            final ArraySet<T> set = src.get(key);
            final int setSize = set.size();
            for (int j = 0; j < setSize; j++) {
                add(key, set.valueAt(j));
            }
            addAll(key, set);
        }
    }

+23 −0
Original line number Diff line number Diff line
@@ -49,4 +49,27 @@ public class SparseSetArrayTest {

        assertThat(sparseSetArray.size()).isEqualTo(5);
    }

    @Test
    public void testCopyConstructor() {
        final SparseSetArray<Integer> sparseSetArray = new SparseSetArray<>();

        for (int i = 0; i < 10; ++i) {
            for (int j = 100; j < 110; ++j) {
                sparseSetArray.add(i, j);
            }
        }
        final SparseSetArray<Integer> sparseSetArrayCopy = new SparseSetArray<>(sparseSetArray);
        assertThat(sparseSetArray.size()).isEqualTo(sparseSetArrayCopy.size());
        for (int i = 0; i < sparseSetArray.size(); ++i) {
            final ArraySet<Integer> array = sparseSetArray.get(i);
            final ArraySet<Integer> arrayCopy = sparseSetArrayCopy.get(i);
            assertThat(array).isNotNull();
            assertThat(arrayCopy).isNotNull();
            assertThat(array.size()).isEqualTo(arrayCopy.size());
            for (int j = 0; j < array.size(); j++) {
                assertThat(array.valueAt(j)).isEqualTo(arrayCopy.valueAt(j));
            }
        }
    }
}