1 /* 2 * Copyright 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /** Static utils for working with [BitSet]s. */ 18 @file:JvmName("BitSets") 19 20 package com.android.cts.backportedfixes.bitset 21 22 import java.util.BitSet 23 import java.util.SortedSet 24 25 /** 26 * Returns a sorted set of indices for which this {@code BitSet} 27 * contains a bit in the set state. The indices are returned 28 * in order, from lowest to highest. 29 * 30 * @return a sorted set of integers representing set indices 31 */ BitSetnull32fun BitSet.toIndices(): SortedSet<Int> { 33 // bs.stream is not available until SDK 23 so extract aliases by hand. 34 if (size() == 0) { 35 return emptySet<Int>().toSortedSet() 36 } 37 val result = 38 buildSet(size()) { 39 var next = 0 40 while (next >= 0) { 41 if (get(next)) { 42 add(next) 43 } 44 if (next == Integer.MAX_VALUE) { 45 break 46 } 47 next = nextSetBit(next + 1) 48 } 49 } 50 return result.toSortedSet() 51 } 52