aboutsummaryrefslogtreecommitdiff
path: root/src/ch/epfl/xblast/Lists.java
blob: 9bd527b0e12254d564f7c4189fcac887b4847f3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package ch.epfl.xblast;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Lists utility class providing common operations on lists.
 *
 * @author Pacien TRAN-GIRARD (261948)
 * @author Timothée FLOURE (257420)
 */
public final class Lists {

    /**
     * Returns an immutable copy of a list.
     *
     * @param l   a list
     * @param <T> the element type
     * @return the immutable list
     */
    public static <T> List<T> immutableList(List<T> l) {
        return Collections.unmodifiableList(new ArrayList<>(l));
    }

    /**
     * Returns an immutable copy of a map.
     *
     * @param m   a mab
     * @param <K> the key type
     * @param <V> the value type
     * @return the immutable copy
     */
    public static <K, V> Map<K, V> immutableMap(Map<K, V> m) {
        return Collections.unmodifiableMap(new HashMap<>(m));
    }

    /**
     * Returns a copy of the given list with element e prepended.
     *
     * @param l   the list
     * @param e   the element to prepend
     * @param <T> the type of the list's elements
     * @return a copy of the list with the element prepended
     */
    public static <T> List<T> prepended(List<T> l, T e) {
        return Lists.inserted(l, 0, e);
    }

    /**
     * Returns a copy of the given list with element e appended.
     *
     * @param l   the list
     * @param e   the element to append
     * @param <T> the type of the list's elements
     * @return a copy of the list with the element appended
     */
    public static <T> List<T> appended(List<T> l, T e) {
        return Lists.inserted(l, l.size(), e);
    }

    /**
     * Returns an immutable copy of the given list, sorted using the supplied comparator.
     *
     * @param l   the list to sort
     * @param cmp the Comparator to use
     * @param <T> the type of the list's elements
     * @return the sorted list
     */
    public static <T> List<T> sorted(List<T> l, Comparator<T> cmp) {
        List<T> r = new ArrayList<>(l);
        Collections.sort(r, cmp);
        return Collections.unmodifiableList(r);
    }

    /**
     * Returns a copy of the given list surrounded with element e.
     *
     * @param l   the list
     * @param e   the element to insert
     * @param <T> the type of the list's elements
     * @return a copy of the list with the element inserted at start and end
     */
    public static <T> List<T> surrounded(List<T> l, T e) {
        return Lists.appended(Lists.prepended(l, e), e);
    }

    /**
     * Returns a symmetric version of the list, without repeating the last element of the input list.
     * For instance, mirrored([kay]) will return [kayak].
     *
     * @param l   the input list
     * @param <T> the type of the list's elements
     * @return the mirrored list
     * @throws IllegalArgumentException if the given list is empty
     */
    public static <T> List<T> mirrored(List<T> l) {
        if (l == null || l.size() == 0) throw new IllegalArgumentException();

        return Stream
                .concat(l.stream(), Lists.reversed(l).stream().skip(1))
                .collect(Collectors.toList());
    }

    /**
     * Returns all the permutations of the elements of the given list.
     *
     * @param l   given list
     * @param <T> the type of the list's elements
     * @return a list of all the permutations of the list
     * @throws IllegalArgumentException if the given list is null
     */
    public static <T> List<List<T>> permutations(List<T> l) {
        if (l == null) throw new IllegalArgumentException();
        if (l.size() <= 1) return Collections.singletonList(l);
        if (l.size() == 2) return Arrays.asList(l, Lists.reversed(l));

        List<List<T>> p = new LinkedList<>();
        for (List<T> sp : Lists.permutations(l.subList(1, l.size())))
            for (int i = 0; i <= sp.size(); ++i)
                p.add(Lists.inserted(sp, i, l.get(0)));

        return p;
    }

    /**
     * Returns a copy of the given list with the first n items dropped.
     *
     * @param l   given list
     * @param n   number of items to drop
     * @param <T> the type of the list's elements
     * @return a copy of the list with the n first element dropped
     */
    public static <T> List<T> firstDropped(List<T> l, int n) {
        if (Objects.isNull(l))
            return Collections.emptyList();

        return new ArrayList<>(l.subList(n, l.size()));
    }

    /**
     * Returns an immutable copy of the given lists, concatenated.
     *
     * @param lists the lists to concatenate
     * @param <T>   the type of the list's elements
     * @return the list result of the concatenation
     * @deprecated Use List<T> concatenated(List<List<T>> lists) instead to avoid parametrized varargs.
     */
    public static <T> List<T> concatenated(List<T>... lists) {
        return concatenated(Arrays.asList(lists));
    }

    /**
     * Returns an immutable copy of the given lists, concatenated.
     *
     * @param lists the lists to concatenate
     * @param <T>   the type of the list's elements
     * @return the list result of the concatenation
     */
    public static <T> List<T> concatenated(List<List<T>> lists) {
        return Collections.unmodifiableList(
                lists.stream()
                        .map(List::stream)
                        .reduce(Stream::concat).orElseGet(Stream::empty)
                        .collect(Collectors.toList()));
    }

    /**
     * Returns a reversed copy of the given list, leaving the original one unmodified.
     *
     * @param l   the list to reverse
     * @param <T> the type of the list's elements
     * @return a reversed copy of the list
     */
    private static <T> List<T> reversed(List<T> l) {
        List<T> r = new ArrayList<>(l);
        Collections.reverse(r);
        return r;
    }

    /**
     * Returns a copy of the given list with element e inserted at index i.
     *
     * @param l   the list
     * @param i   the insertion index
     * @param e   the element to insert
     * @param <T> the type of the list's elements
     * @return a copy of the list with the element inserted
     */
    private static <T> List<T> inserted(List<T> l, int i, T e) {
        List<T> r = new LinkedList<>(l);
        r.add(i, e);
        return r;
    }

    /**
     * Maps linearly two lists.
     *
     * @param kl  the keys list
     * @param vl  the values list
     * @param <K> the key type
     * @param <V> the value type
     * @return the map
     */
    public static <K, V> Map<K, V> linearMap(List<K> kl, List<V> vl) {
        if (Objects.isNull(kl) || Objects.isNull(vl) || kl.size() != vl.size())
            throw new IllegalArgumentException();

        Map<K, V> m = new HashMap<>();
        Iterator<K> ki = kl.iterator();
        Iterator<V> vi = vl.iterator();

        while (ki.hasNext() && vi.hasNext())
            m.put(ki.next(), vi.next());

        return Collections.unmodifiableMap(m);
    }

    /**
     * Maps linearly two lists, adjusting their respective size by truncating the key list or completing the value list
     * with null values.
     *
     * @param kl  the keys list
     * @param vl  the values list
     * @param <K> the key type
     * @param <V> the value type
     * @return the map
     */
    public static <K, V> Map<K, V> linearAdjustedMap(List<K> kl, List<V> vl) {
        if (Objects.isNull(kl) || Objects.isNull(vl))
            throw new IllegalArgumentException();

        if (kl.size() <= vl.size())
            return Lists.linearMap(kl, vl.subList(0, kl.size()));
        else
            return Lists.linearMap(
                    kl,
                    concatenated(Arrays.asList(vl, Collections.nCopies(kl.size() - vl.size(), null))));
    }

    /**
     * Merges two maps Map<K1, IK> and Map<IK, V> -> Map<K1, V>.
     *
     * @param m1   key maps
     * @param m2   values maps
     * @param <K>  the keys type
     * @param <IK> the intermediate keys type
     * @param <V>  the values type
     * @return the traversing map
     */
    public static <K, IK, V> Map<K, V> traverseMaps(Map<K, IK> m1, Map<IK, V> m2) {
        return Collections.unmodifiableMap(m1.entrySet().stream()
                .filter(e -> Objects.nonNull(e.getValue()))
                .filter(e -> Objects.nonNull(m2.get(e.getValue())))
                .collect(Collectors.toMap(Map.Entry::getKey, e -> m2.get(e.getValue()))));
    }

    /**
     * Returns an inverted copy of a bijective map.
     *
     * @param m   the map to invert
     * @param <K> the keys type
     * @param <V> the values type
     * @return the inverted map
     */
    public static <K, V> Map<K, V> invertMap(Map<V, K> m) {
        return Collections.unmodifiableMap(m.entrySet().stream()
                .filter(e -> Objects.nonNull(e.getKey()))
                .filter(e -> Objects.nonNull(e.getValue()))
                .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)));
    }

}