|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 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 | +package com.google.wear.watchface.dfx.memory |
| 17 | + |
| 18 | +/** |
| 19 | + * A lazy iterator, allowing to generate all combinations of a set without getting into stack |
| 20 | + * overflows. |
| 21 | + */ |
| 22 | +internal interface SizedIterator<T> : Iterator<T> { |
| 23 | + /** The size of the collection of elements that is generated */ |
| 24 | + fun getSize(): Long |
| 25 | + |
| 26 | + companion object { |
| 27 | + /** Constructs a sized iterator from a plain java iterator and a size. */ |
| 28 | + fun <T> fromIterator(iterator: Iterator<T>, size: Long): SizedIterator<T> { |
| 29 | + return object : SizedIterator<T> { |
| 30 | + override fun getSize(): Long { |
| 31 | + return size |
| 32 | + } |
| 33 | + |
| 34 | + override fun hasNext(): Boolean { |
| 35 | + return iterator.hasNext() |
| 36 | + } |
| 37 | + |
| 38 | + override fun next(): T { |
| 39 | + return iterator.next() |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Lazily combines an existing iterator with a new collection of elements by taking every |
| 46 | + * element of the new collection and combining it with every element of the existing |
| 47 | + * iterator. The resulting iterator will have a size of iterator.size() * elements.size(). |
| 48 | + * |
| 49 | + * @param iterator the existing iterator |
| 50 | + * @param elements the collection providing the new elements that are combined with the |
| 51 | + * already generated ones. |
| 52 | + * @param combineFn the function used to combine each element of the elements argument with |
| 53 | + * each element of the existing iterator. |
| 54 | + * @param mapFn the function used to map a single element of the elements collection to |
| 55 | + * elements of the iterator, used when the existing iterator is empty and combineFn cannot |
| 56 | + * be called. |
| 57 | + * @return a new iterator with the combined elements. |
| 58 | + * @param <T> the elements of the iterator |
| 59 | + * @param <U> the elements of the collection |
| 60 | + */ |
| 61 | + fun <T, U> combine( |
| 62 | + iterator: SizedIterator<T>, |
| 63 | + elements: Collection<U>, |
| 64 | + combineFn: (T, U) -> T, |
| 65 | + mapFn: (U) -> T |
| 66 | + ): SizedIterator<T> { |
| 67 | + // if the current key does not have any configuration values, then ignore it |
| 68 | + // and return the next iterator. |
| 69 | + if (elements.isEmpty()) { |
| 70 | + return iterator |
| 71 | + } |
| 72 | + |
| 73 | + // if the rest iterator has no configuration values, then start a new iterator |
| 74 | + // from the current keys. |
| 75 | + if (!iterator.hasNext()) { |
| 76 | + return fromIterator(elements.stream().map(mapFn).iterator(), elements.size.toLong()) |
| 77 | + } |
| 78 | + |
| 79 | + // lazily consume each partial config from the rest iterator and append each value for |
| 80 | + // the |
| 81 | + // current key, producing new partial configs. |
| 82 | + return object : SizedIterator<T> { |
| 83 | + private val _size = elements.size * iterator.getSize() |
| 84 | + private var crtValuesIter = elements.iterator() |
| 85 | + private var currentFromTail = iterator.next() |
| 86 | + override fun getSize(): Long { |
| 87 | + return _size |
| 88 | + } |
| 89 | + |
| 90 | + override fun hasNext(): Boolean { |
| 91 | + return crtValuesIter.hasNext() || iterator.hasNext() |
| 92 | + } |
| 93 | + |
| 94 | + override fun next(): T { |
| 95 | + if (!crtValuesIter.hasNext()) { |
| 96 | + crtValuesIter = elements.iterator() |
| 97 | + currentFromTail = iterator.next() |
| 98 | + } |
| 99 | + return combineFn(currentFromTail, crtValuesIter.next()) |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments