-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDict.java
More file actions
213 lines (116 loc) · 4.05 KB
/
Dict.java
File metadata and controls
213 lines (116 loc) · 4.05 KB
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
package javaiterables;
import java.util.NoSuchElementException;
public class Dict {
List keys = new List();
List values = new List();
int Index = 0;
int nextIndex = 0;
public Dict() {
// Nothing to do when class is initialized
}
private int find(String key) {
int index = 0;
for (Object Key: this.keys.arrayIterate()) {
if (key.equals(Key + "")) {
break;
}
index++;
}
if (!(this.keys.get(index).equals(key))) {
System.err.printf("There's no key with the name %s in the Dict", (String) key);
throw new NoSuchElementException();
} else {
return index;
}
}
public String findKey(Object value) {
int index = 0;
for (Object Value: this.values.arrayIterate()) {
if (value.equals(Value)) {
break;
}
index++;
}
if (!(this.values.get(index).equals(value))) {
System.err.printf("There's no value with the name %s in the Dict", (String) value);
throw new NoSuchElementException();
} else {
return (String) this.keys.get(index);
}
}
public void add(String key, Object value) {
this.keys.append(key);
this.values.append(value);
this.Index++;
}
public void set(String key, Object value) {
int index = 0;
index += this.find(key);
this.values.insert(index, value);
}
public Object get(String key) { return this.values.get(this.find(key)); }
public void remove(String key) {
int index = 0;
index += this.find(key);
this.keys.remove(index);
this.values.remove(index);
}
public void anullValue(String key) { this.set(key, null); }
public void sortByKeys(boolean ascending) {
List sortedKeys = new List();
List localValues = new List();
sortedKeys.extend(this.keys);
sortedKeys.sort(ascending);
for (Object sortedKey : sortedKeys.arrayIterate()) {
localValues.append(this.get("" + sortedKey));
}
this.keys.clear();
this.keys.extend(sortedKeys);
this.values.clear();
this.values.extend(localValues);
}
public void sortByValues(boolean ascending) {
List localKeys = new List();
List sortedValues = new List();
sortedValues.extend(this.keys);
sortedValues.sort(ascending);
for (Object sortedValue : (Object[]) sortedValues.arrayIterate()) {
localKeys.append(this.findKey(sortedValue));
}
this.keys.clear();
this.keys.extend(localKeys);
this.values.clear();
this.values.extend(sortedValues);
}
public List keys() { return this.keys; }
public List values() { return this.values; }
public void restart() { this.nextIndex = 0; }
public void clear() {
this.Index = 0;
this.keys.clear();
this.values.clear();
this.restart();
}
public List next(int step) {
List actualStep = new List();
this.nextIndex += step;
if (nextIndex >= this.keys.len()) {
System.err.println("Can't iter anymore, no more values in the dict");
throw new IndexOutOfBoundsException();
}
actualStep.append(this.keys.get(this.nextIndex));
actualStep.append(this.values.get(this.nextIndex));
return actualStep;
}
public int size() {
return this.keys.len();
}
public boolean containsKey(String key) { return this.keys.contains(key); }
public boolean containsValue(Object value) { return this.values.contains(value); }
public boolean contains(Object any) {
return (this.containsKey(String.valueOf(any)) || this.containsValue(any));
}
public boolean isEqualTo(Dict other) {
return (this.keys.isEqualTo(other.keys) && this.values.isEqualTo(other.values));
}
}