summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/esodata/NestList.java
blob: c1544ca95a56a976dc6c173d0c1620f3d6ad1fc5 (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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* 
 * esodata - data structures and other things, of varying utility
 * Copyright 2022, Ben Culkin
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *   
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package bjc.esodata;

import static bjc.functypes.Combinators.*;

import java.util.*;
import java.util.function.*;

import bjc.data.*;

/**
 * A list which can contain sublists of itself.
 * 
 * N.B: Be careful if you form a recursive list, as there is no form of detection
 * in place for that. Some operations may work, but those that do a deep traversal
 * of the list will not.
 * 
 * @author Ben Culkin
 *
 * @param <Element> The type contained in the list.
 */
public class NestList<Element> extends AbstractList<Either<Element, NestList<Element>>>
{
	private final List<Either<Element, NestList<Element>>> backing;

	/**
	 * Create a new empty nesting list.
	 */
	public NestList() {
		backing = new ArrayList<>();
	}	
	
	/**
	 * Create a new empty nesting list with the given capacity.
	 * 
	 * @param cap The capacity for the nesting list.
	 */
	public NestList(int cap) {
		backing = new ArrayList<>(cap);
	}
	
	/**
	 * Add an element to this list.
	 * 
	 * @param element The element to add to the list.
	 * 
	 * @return Whether we could add the element.
	 */
	public boolean addItem(Element element) {
		return backing.add(Either.left(element));
	}
	
	/**
	 * Add a sublist to this list.
	 * 
	 * @param element The sublist to add to this list.
	 * 
	 * @return Whether we could add the sublist.
	 */
	public boolean addItem(NestList<Element> element) {
		return backing.add(Either.right(element));
	}
	/**
	 * Add elements to this list.
	 * 
	 * @param elements The elements to add to the list.
	 * 
	 * @return Whether we could add each element.
	 */
	public boolean[] addItems(@SuppressWarnings("unchecked") Element... elements) {
		boolean[] vals = new boolean[elements.length];
		
		for (int i = 0; i < vals.length; i++)
		{
			vals[i] = addItem(elements[i]);
		}
		
		return vals;
	}
	
	/**
	 * Add sublists to this list.
	 * 
	 * @param elements The sublists to add to this list.
	 * 
	 * @return Whether we could add each sublist.
	 */
	public boolean[] addItems(@SuppressWarnings("unchecked") NestList<Element>... elements) {
		boolean[] vals = new boolean[elements.length];
		
		for (int i = 0; i < vals.length; i++)
		{
			vals[i] = addItem(elements[i]);
		}
		
		return vals;
	}
	
	/**
	 * Add a sublist with the given elements to this list.
	 * 
	 * @param elements The elements of the sublist.
	 * 
	 * @return Whether or not we could add the sublist.
	 */
	public boolean addSublist(@SuppressWarnings("unchecked") Element... elements) {
		NestList<Element> container = new NestList<>(elements.length);
		
		for (Element ele : elements) {
			container.addItem(ele);
		}
		
		return addItem(container);
	}
	
	/**
	 * Return an iterator over a flattened version of this list.
	 * 
	 * N.B: In certain cases involving empty sublists, the hasNext() operation\
	 * may not be 100% accurate. Be warned.
	 * 
	 * @return An iterator over a flattened variant of this list.
	 */
	public ListIterator<Element> flatIterator() {
		return new FlatNestIterator<>(listIterator());
	}
	
	/**
	 * Flatten one level of nesting from this list.
	 * 
	 * @return The list with one level of nesting flattened.
	 */
	public NestList<Element> flatten() {
		NestList<Element> flatterList = new NestList<>(size());
		
		backing.forEach((element) -> 
			element.pick(flatterList::addItem,	flatterList::addAll)
		);
		
		return flatterList;
	}
	
	/**
	 * Flatten this list recursively.
	 * 
	 * @return A flattened form of this list.
	 */
	public List<Element> deepFlatten() {
		List<Element> flatList = new ArrayList<>();
		
		flatIterator().forEachRemaining(flatList::add);
		
		return flatList;
	}
	
	/**
	 * Get the total number of elements contained in this list and all sublists.
	 * 
	 * @return The total number of elements contained in this list.
	 */
	public int deepSize() {
		int size = 0;
		
		for (Either<Element, NestList<Element>> element : backing)
		{
			size += element.extract((ele) -> 1, (lst) -> lst.deepSize());
		}
		
		return size;
	}
	
	/**
	 * Replace all of the elements in this list in-place with transformed versions.
	 * 
	 * @param elementOperator The operator to apply to elements.
	 * @param listOperator The operator to apply to sublists.
	 */
	public void replace(
			UnaryOperator<Element> elementOperator,
			UnaryOperator<NestList<Element>> listOperator) {
		backing.replaceAll((ele) -> ele.map(elementOperator, listOperator));
	}
	
	/**
	 * Perform a shallow mapping over this list.
	 * 
	 * @param <NewElement> The new element type.
	 * 
	 * @param elementMapper The function to map elements.
	 * @param listMapper The function to map lists.
	 * 
	 * @return A new list containing the mapped elements.
	 */
	public <NewElement> NestList<NewElement> map(
			Function<Element, NewElement> elementMapper,
			Function<NestList<Element>, NestList<NewElement>> listMapper)
	{
		NestList<NewElement> nest = new NestList<>(backing.size());
		
		for (Either<Element, NestList<Element>> element : backing)
		{
			nest.add(element.map(elementMapper, listMapper));
		}
		
		return nest;
	}
	
	/**
	 * Perform a recursive mapping over this list.
	 * 
	 * @param <NewElement> The new element type.
	 * 
	 * @param mapper The element mapper.
	 * 
	 * @return A new list with the same structure, but transformed elements.
	 */
	public <NewElement> NestList<NewElement> deepMap(
			Function<Element, NewElement> mapper)
	{
		return map(mapper, (lst) -> lst.deepMap(mapper));
	}
	
	/**
	 * Perform a mapping on the list with controllable recursion.
	 * 
	 * Inspired by the function of the same name from Raku.
	 * 
	 * @param <NewElement> The new element type.
	 * 
	 * @param recurPredicate Determines whether to recur into a list or not.
	 * @param elementMapper The mapper on elements.
	 * @param listMapper The mapper on lists we aren't recursing into.
	 * 
	 * @return A new list with its elements mapped.
	 */
	public <NewElement> NestList<NewElement> duckMap(
			Predicate<NestList<Element>> recurPredicate,
			Function<Element, NewElement> elementMapper,
			Function<NestList<Element>, NestList<NewElement>> listMapper)
	{
		return map(
				elementMapper, 
				iftt(recurPredicate,
						(list) -> list.duckMap(
								recurPredicate, elementMapper, listMapper),
						listMapper
				)
		);
	}
	
	/**
	 * Perform a reduction over this list.
	 * 
	 * @param <Output> The type of the output value.
	 * 
	 * @param initial The initial state of the output value.
	 * @param elementFolder The function to fold elements with.
	 * @param listFolder The function to fold lists with.
	 * 
	 * @return The result of reducing the list.
	 */
	public <Output> Output reduce(
			Output initial,
			BiFunction<Output, Element, Output> elementFolder,
			BiFunction<Output, NestList<Element>, Output> listFolder)
	{
		Holder<Output> out = Holder.of(initial);
		for (Either<Element, NestList<Element>> item : backing)
		{
			out.transform((state) -> item.extract(
					(ele) -> elementFolder.apply(state, ele),
					(lst) -> listFolder.apply(state, lst))
			);
		}
		
		return out.getValue();
	}
	
	/**
	 * Perform a recursive reduction over this list.
	 * 
	 * @param <Output> The type of the output value.
	 * 
	 * @param initial The initial state of the output value.
	 * @param elementFolder The function to fold elements with.
	 * 
	 * @return The result of recursively reducing the list.
	 */
	public <Output> Output deepReduce(
			Output initial,
			BiFunction<Output, Element, Output> elementFolder)
	{
		return reduce(
				initial,
				elementFolder,
				(state, lst) -> lst.deepReduce(state, elementFolder));
	}
	
	/**
	 * Conditionally expand elements of this list into the provided list.
	 * 
	 * @param nest The list to expand elements into.
	 * @param expandPicker Picks whether or not to expand a list.
	 * @param recur Whether or not to recursively expand lists.
	 * 
	 * @return The provided list.
	 */
	public NestList<Element> expandInto(
			NestList<Element> nest,
			Predicate<NestList<Element>> expandPicker,
			boolean recur)
	{
		return reduce(nest,
				(state, item) -> with(state, (stat) -> stat.addItem(item)),
				(state, list) -> {	
					if (expandPicker.test(list)) {
						return list.reduce(nest,
								(substate, subitem) 
									-> with(substate,
											(subst) -> subst.addItem(subitem)),
								(substate, sublist) 
									-> with(substate, (subst) -> {												
										if (recur) {
											sublist.expandInto(
													subst,
													expandPicker,
													recur);
										} else {
											subst.addItem(sublist);
										}
								}));
					} else {
						state.addItem(list);
						return state;
					}
		});
	}
	// List methods and other things.
	
	@Override
	public boolean add(Either<Element, NestList<Element>> e) {
		return backing.add(e);
	}

	@Override
	public void add(int index, Either<Element, NestList<Element>> element) {
		backing.add(index, element);
	}
	
	@Override
	public Either<Element, NestList<Element>> get(int index) {
		return backing.get(index);
	}

	@Override
	public boolean remove(Object o) {
		return backing.remove(o);
	}


	@Override
	public Either<Element, NestList<Element>> remove(int index) {
		return backing.remove(index);
	}

	@Override
	public int size() {
		return backing.size();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + Objects.hash(backing);
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)                  return true;
		if (!super.equals(obj))           return false;
		if (getClass() != obj.getClass()) return false;

		NestList<?> other = (NestList<?>) obj;
		
		return Objects.equals(backing, other.backing);
	}
}