• 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

88.24
/commons/util/src/main/java/net/automatalib/common/util/comparison/CmpUtil.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.comparison;
17

18
import java.util.Comparator;
19
import java.util.Iterator;
20
import java.util.List;
21

22
/**
23
 * Various methods for dealing with the comparison of objects.
24
 */
25
public final class CmpUtil {
26

27
    private CmpUtil() {
28
        // prevent instantiation
29
    }
30

31
    /**
32
     * Compares two {@link List}s with respect to canonical ordering.
33
     * <p>
34
     * In canonical ordering, a sequence {@code o1} is less than a sequence {@code o2} if {@code o1} is shorter than
35
     * {@code o2}, or if they have the same length and {@code o1} is lexicographically smaller than {@code o2}.
36
     *
37
     * @param o1
38
     *         the first list
39
     * @param o2
40
     *         the second list
41
     * @param elemComparator
42
     *         the comparator for comparing the single elements
43
     * @param <U>
44
     *         element type
45
     *
46
     * @return the result of the comparison
47
     */
48
    public static <U> int canonicalCompare(List<? extends U> o1,
49
                                           List<? extends U> o2,
50
                                           Comparator<? super U> elemComparator) {
51
        int siz1 = o1.size(), siz2 = o2.size();
2✔
52

53
        if (siz1 != siz2) {
2✔
54
            return siz1 - siz2;
2✔
55
        }
56

57
        return lexCompare(o1, o2, elemComparator);
2✔
58
    }
59

60
    public static int canonicalCompare(int[] a1, int[] a2) {
61
        int ldiff = a1.length - a2.length;
2✔
62
        if (ldiff != 0) {
2✔
63
            return ldiff;
2✔
64
        }
65
        return lexCompare(a1, a2);
2✔
66
    }
67

68
    /**
69
     * Compares two {@link List}s of {@link Comparable} elements with respect to canonical ordering.
70
     * <p>
71
     * In canonical ordering, a sequence {@code o1} is less than a sequence {@code o2} if {@code o1} is shorter than
72
     * {@code o2}, or if they have the same length and {@code o1} is lexicographically smaller than {@code o2}.
73
     *
74
     * @param o1
75
     *         the first list
76
     * @param o2
77
     *         the second list
78
     * @param <U>
79
     *         element type
80
     *
81
     * @return the result of the comparison
82
     */
83
    public static <U extends Comparable<? super U>> int canonicalCompare(List<? extends U> o1, List<? extends U> o2) {
84
        int siz1 = o1.size(), siz2 = o2.size();
2✔
85
        if (siz1 != siz2) {
2✔
86
            return siz1 - siz2;
2✔
87
        }
88

89
        return lexCompare(o1, o2);
×
90
    }
91

92
    /**
93
     * Lexicographically compares two {@link Iterable}s. Comparison of the elements is done using the specified
94
     * comparator.
95
     *
96
     * @param o1
97
     *         the first iterable.
98
     * @param o2
99
     *         the second iterable.
100
     * @param elemComparator
101
     *         the comparator.
102
     * @param <U>
103
     *         element type
104
     *
105
     * @return {@code < 0} iff o1 is lexicographically smaller, {@code 0} if o1 equals o2 and {@code > 0} otherwise.
106
     */
107
    public static <U> int lexCompare(Iterable<? extends U> o1,
108
                                     Iterable<? extends U> o2,
109
                                     Comparator<? super U> elemComparator) {
110
        Iterator<? extends U> it1 = o1.iterator(), it2 = o2.iterator();
2✔
111

112
        while (it1.hasNext() && it2.hasNext()) {
2✔
113
            int cmp = elemComparator.compare(it1.next(), it2.next());
2✔
114
            if (cmp != 0) {
2✔
115
                return cmp;
2✔
116
            }
117
        }
2✔
118

119
        if (it1.hasNext()) {
2✔
120
            return 1;
×
121
        } else if (it2.hasNext()) {
2✔
122
            return -1;
×
123
        }
124
        return 0;
2✔
125
    }
126

127
    public static int lexCompare(int[] a1, int[] a2) {
128
        int i = 0;
2✔
129
        int len1 = a1.length, len2 = a2.length;
2✔
130

131
        while (i < len1 && i < len2) {
2✔
132
            int cmp = a1[i] - a2[i];
2✔
133
            if (cmp != 0) {
2✔
134
                return cmp;
2✔
135
            }
136
            i++;
2✔
137
        }
2✔
138

139
        if (i < len1) {
2✔
140
            return 1;
×
141
        }
142
        if (i < len2) {
2✔
143
            return -1;
×
144
        }
145
        return 0;
2✔
146
    }
147

148
    /**
149
     * Lexicographically compares two {@link Iterable}s using the inert comparability of their elements.
150
     *
151
     * @param o1
152
     *         the first iterable.
153
     * @param o2
154
     *         the second iterable.
155
     * @param <U>
156
     *         element type
157
     *
158
     * @return {@code < 0} iff o1 is lexicographically smaller, {@code 0} if o1 equals o2 and {@code > 0} otherwise.
159
     */
160
    public static <U extends Comparable<? super U>> int lexCompare(Iterable<? extends U> o1, Iterable<? extends U> o2) {
161
        Iterator<? extends U> it1 = o1.iterator(), it2 = o2.iterator();
2✔
162

163
        while (it1.hasNext() && it2.hasNext()) {
2✔
164
            int cmp = it1.next().compareTo(it2.next());
2✔
165
            if (cmp != 0) {
2✔
166
                return cmp;
1✔
167
            }
168
        }
2✔
169

170
        if (it1.hasNext()) {
1✔
171
            return 1;
1✔
172
        } else if (it2.hasNext()) {
1✔
173
            return -1;
1✔
174
        }
175
        return 0;
1✔
176
    }
177

178
    /**
179
     * Retrieves a lexicographical comparator for the given type.
180
     *
181
     * @param elemComp
182
     *         the comparator to use for comparing the elements.
183
     * @param <U>
184
     *         element type
185
     * @param <T>
186
     *         iterable type
187
     *
188
     * @return a comparator for comparing iterables of type {@code U} based on lexicographical ordering.
189
     */
190
    public static <T extends Iterable<U>, U> Comparator<T> lexComparator(Comparator<? super U> elemComp) {
191
        return new LexComparator<>(elemComp);
2✔
192
    }
193

194
    /**
195
     * Retrieves a lexicographical comparator for the given type, which has to be an {@link Iterable} of
196
     * {@link Comparable} types.
197
     *
198
     * @param <U>
199
     *         element type
200
     * @param <T>
201
     *         iterable type
202
     *
203
     * @return the lexicographical comparator.
204
     */
205
    public static <U extends Comparable<U>, T extends Iterable<U>> Comparator<T> lexComparator() {
206
        return CmpUtil::lexCompare;
1✔
207
    }
208

209
    /**
210
     * Retrieves a canonical comparator for the given list type.
211
     *
212
     * @param elemComp
213
     *         the comparator to use for comparing the elements.
214
     * @param <U>
215
     *         element type
216
     * @param <T>
217
     *         list type
218
     *
219
     * @return a comparator for comparing iterables of type {@code U} based on canonical ordering.
220
     */
221
    public static <T extends List<? extends U>, U> Comparator<T> canonicalComparator(Comparator<? super U> elemComp) {
222
        return new CanonicalComparator<>(elemComp);
2✔
223
    }
224

225
    /**
226
     * Retrieves a canonical comparator for the given type, which has to be a {@link List} of {@link Comparable} types.
227
     *
228
     * @param <U>
229
     *         element type
230
     * @param <T>
231
     *         list type
232
     *
233
     * @return the canonical comparator
234
     *
235
     * @see #canonicalCompare(List, List)
236
     */
237
    public static <T extends List<U>, U extends Comparable<U>> Comparator<T> canonicalComparator() {
238
        return CmpUtil::canonicalCompare;
×
239
    }
240

241
}
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