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
|
/*
* 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 java.util.*;
public class KeyedList<Key, Val> implements Iterable<Val> {
private List<Val> backing;
private Map<Key, Integer> indices;
private int currIdx = 0;
public KeyedList() {
backing = new ArrayList<>();
indices = new HashMap<>();
}
/**
* Add a item to this list.
*
* If an item already exists with the given key, the current one will not be
* added. Use <code>set</code> to handle that.
*
* @param key The key for this item.
* @param val The value for this item
*
* @return Whether or not this item was added to the list
*/
public boolean add(Key key, Val val) {
// TODO: Determine if this is the desired behavior
if (indices.containsKey(key))
return false;
backing.add(val);
indices.put(key, currIdx++);
return true;
}
/**
* Set the item associated with a given key.
*
* @param key The key to set
* @param newVal The new value for the key
*
* @return The previous value for the key, if there was one
*/
public Val set(Key key, Val newVal) {
if (indices.containsKey(key)) {
return backing.set(indices.get(key), newVal);
}
add(key, newVal);
return null;
}
/**
* Retrieve all of the keys for this list.
*
* @return An immutable set of the keys for this list
*/
public Set<Key> keys() {
// TODO: write mutable wrapper which will update the list appropriately
return Collections.unmodifiableSet(indices.keySet());
}
/**
* Retrieve the value associated with the given key.
*
* @param key The key to look up.
*
* @return The value for the given key.
*/
public Val get(Key key) {
return backing.get(indices.get(key));
}
/**
* Check if this list contains a value for a given key.
*
* @param key The key to look up.
*
* @return Whether this list contains a value for the given key.
*/
public boolean containsKey(Key key) {
return indices.containsKey(key);
}
@Override
public Iterator<Val> iterator() {
return backing.iterator();
}
/**
* Return an iterator that starts at the value for the given key.'
*
* @param key The key to start at.
*
* @return An iterator starting at the given key, or null if the key isn't
* present.
*/
public ListIterator<Val> iteratorFrom(Key key) {
if (indices.containsKey(key)) {
return backing.listIterator(indices.get(key));
}
return null;
}
}
|