• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LearnLib / automatalib / 13138848026

04 Feb 2025 02:53PM UTC coverage: 92.108% (+2.2%) from 89.877%
13138848026

push

github

mtf90
[maven-release-plugin] prepare release automatalib-0.12.0

16609 of 18032 relevant lines covered (92.11%)

1.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.97
/commons/util/src/main/java/net/automatalib/common/util/collection/AllCombinationsIterator.java
1
/* Copyright (C) 2013-2025 TU Dortmund University
2
 * This file is part of AutomataLib <https://automatalib.net>.
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 net.automatalib.common.util.collection;
17

18
import java.util.ArrayList;
19
import java.util.Iterator;
20
import java.util.List;
21
import java.util.NoSuchElementException;
22

23
/**
24
 * An iterator that iterates over the cartesian product of its given source domains. Each intermediate combination of
25
 * elements is computed lazily.
26
 * <p>
27
 * <b>Note:</b> Subsequent calls to the {@link #next()} method return a reference to the same list, and only update the
28
 * contents of the list. If you plan to reuse intermediate results, you'll need to explicitly copy them.
29
 *
30
 * @param <T>
31
 *         type of elements
32
 */
33
final class AllCombinationsIterator<T> implements Iterator<List<T>> {
34

35
    private final Iterable<? extends T>[] iterables;
36
    private final Iterator<? extends T>[] iterators;
37
    private final List<T> current;
38
    private boolean first = true;
2✔
39
    private boolean empty;
40

41
    @SuppressWarnings("unchecked")
42
    @SafeVarargs
43
    AllCombinationsIterator(Iterable<? extends T>... iterables) {
2✔
44
        this.iterables = iterables;
2✔
45
        this.iterators = new Iterator[iterables.length];
2✔
46
        this.current = new ArrayList<>(iterables.length);
2✔
47
        for (int i = 0; i < iterators.length; i++) {
2✔
48
            Iterator<? extends T> it = iterables[i].iterator();
2✔
49
            if (!it.hasNext()) {
2✔
50
                empty = true;
2✔
51
                break;
2✔
52
            }
53
            this.iterators[i] = it;
2✔
54
            this.current.add(it.next());
2✔
55
        }
56
    }
2✔
57

58
    @Override
59
    public boolean hasNext() {
60
        if (empty) {
2✔
61
            return false;
2✔
62
        }
63

64
        for (Iterator<? extends T> it : iterators) {
2✔
65
            if (it.hasNext()) {
2✔
66
                return true;
2✔
67
            }
68
        }
69

70
        return first;
2✔
71
    }
72

73
    @Override
74
    public List<T> next() {
75
        if (empty) {
2✔
76
            throw new NoSuchElementException();
2✔
77
        } else if (first) {
2✔
78
            first = false;
2✔
79
            return current;
2✔
80
        }
81

82
        for (int i = 0; i < iterators.length; i++) {
2✔
83
            Iterator<? extends T> it = iterators[i];
2✔
84

85
            if (iterators[i].hasNext()) {
2✔
86
                current.set(i, it.next());
2✔
87
                return current;
2✔
88
            }
89

90
            it = iterables[i].iterator();
2✔
91
            iterators[i] = it;
2✔
92
            current.set(i, it.next());
2✔
93
        }
94

95
        throw new NoSuchElementException();
×
96
    }
97

98
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc