Skip to content
This repository was archived by the owner on May 6, 2021. It is now read-only.

Commit 3efe920

Browse files
authored
Merge pull request #52 from comroid-git/dev
Updated TrieMap inheritance
2 parents aca1639 + a9a9ccb commit 3efe920

7 files changed

Lines changed: 138 additions & 26 deletions

File tree

gradle/global.gradle

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,22 @@ task javadocJar(type: Jar) {
4242
}
4343

4444
compileJava.options.encoding = 'UTF-8'
45-
repositories.jcenter()
45+
46+
repositories {
47+
mavenLocal()
48+
maven {
49+
url "https://maven.jetbrains.space/comroid/release"
50+
credentials.username "Anonymous.User"
51+
credentials.password "anonymous"
52+
}
53+
maven {
54+
url "https://maven.jetbrains.space/comroid/repo"
55+
credentials.username "Anonymous.User"
56+
credentials.password "anonymous"
57+
}
58+
jcenter()
59+
maven { url 'https://www.jitpack.io' }
60+
}
4661

4762
dependencies {
4863
if (project.name != 'interfaces')

gradle/javadoc.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ javadoc {
22
dependsOn += subprojects.collect { it.classes }
33
source = subprojects.collect { it.sourceSets.main.allJava }
44
options {
5-
destinationDir = file("$rootProject.projectDir/docs/$artifactName")
65
encoding = 'UTF-8'
76
}
87
}

src/mutatio/main/java/org/comroid/mutatio/pipe/Pipeable.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,11 @@
1414
public interface Pipeable<T> {
1515
Pipe<?, T> pipe();
1616

17-
@Deprecated
1817
default Pump<?, T> pump() {
1918
return pump(Runnable::run);
2019
}
2120

22-
@Deprecated
23-
default Pump<?, T> pump(Executor executor) {
24-
Executor exe = Stream.<Callable<Executor>>of(() -> executor, ((ExecutorBound) this)::getExecutor, () -> Runnable::run)
25-
.map(FunctionUtil::executeRethrow)
26-
.findAny()
27-
.orElseThrow(AssertionError::new);
28-
return pipe().pump(exe); //todo this will throw stackoverflow errors
29-
}
21+
Pump<?, T> pump(Executor executor);
3022

3123
interface From<T> extends Pipeable<T> {
3224
@Override

src/mutatio/main/java/org/comroid/mutatio/ref/ReferenceIndex.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
package org.comroid.mutatio.ref;
22

3+
import org.comroid.mutatio.pipe.BasicPipe;
34
import org.comroid.mutatio.pipe.Pipe;
45
import org.comroid.mutatio.pipe.Pipeable;
6+
import org.comroid.mutatio.pump.BasicPump;
7+
import org.comroid.mutatio.pump.Pump;
58
import org.jetbrains.annotations.NotNull;
69
import org.jetbrains.annotations.Nullable;
710

811
import java.util.*;
12+
import java.util.concurrent.Executor;
13+
import java.util.function.IntBinaryOperator;
14+
import java.util.function.IntFunction;
15+
import java.util.function.Predicate;
16+
import java.util.function.Supplier;
917
import java.util.stream.Stream;
1018

1119
public interface ReferenceIndex<T> extends Pipeable<T> {
@@ -54,7 +62,14 @@ default Stream<T> stream() {
5462
}
5563

5664
@Override
57-
Pipe<?, T> pipe();
65+
default Pipe<?, T> pipe() {
66+
return new BasicPipe<>(this);
67+
}
68+
69+
@Override
70+
default Pump<?, T> pump(Executor executor) {
71+
return new BasicPump<>(executor, this);
72+
}
5873

5974
Reference<T> getReference(int index);
6075

src/mutatio/main/java/org/comroid/mutatio/ref/ReferenceMap.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
package org.comroid.mutatio.ref;
22

3+
import org.comroid.mutatio.pipe.BiPipe;
4+
import org.comroid.mutatio.pipe.Pipe;
5+
import org.comroid.mutatio.pipe.Pipeable;
6+
import org.comroid.mutatio.pump.Pump;
7+
import org.comroid.util.Pair;
38
import org.jetbrains.annotations.NotNull;
49
import org.jetbrains.annotations.Nullable;
510

11+
import java.util.Map;
612
import java.util.Optional;
13+
import java.util.concurrent.Executor;
714
import java.util.function.Function;
815
import java.util.function.Supplier;
916

10-
public interface ReferenceMap<K, V, REF extends Reference<V>> {
17+
public interface ReferenceMap<K, V, REF extends Reference<V>> extends Pipeable<V> {
1118
default REF getReference(K key) {
1219
return getReference(key, false);
1320
}
1421

1522
REF getReference(K key, boolean createIfAbsent);
1623

24+
ReferenceIndex<Map.Entry<K, V>> entryIndex();
25+
1726
default V get(K key) {
1827
return getReference(key).get();
1928
}
@@ -30,6 +39,25 @@ default Optional<V> wrap(K key) {
3039
return getReference(key).requireNonNull(message);
3140
}
3241

42+
@Override
43+
default Pipe<?, V> pipe() {
44+
return entryIndex()
45+
.pipe()
46+
.map(Map.Entry::getValue);
47+
}
48+
49+
default BiPipe<?, ?, K, V> biPipe() {
50+
return entryIndex()
51+
.pipe()
52+
.bi(Map.Entry::getValue)
53+
.mapFirst(Map.Entry::getKey);
54+
}
55+
56+
@Override
57+
default Pump<?, V> pump(Executor executor) {
58+
return pipe().pump(executor);
59+
}
60+
3361
interface Settable<K, V, REF extends Reference.Settable<V>> extends ReferenceMap<K, V, REF> {
3462
default @Nullable V set(K key, V newValue) {
3563
return getReference(key).set(newValue);

src/trie/main/java/org/comroid/trie/TrieMap.java

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import org.comroid.api.Junction;
44
import org.comroid.api.Polyfill;
55
import org.comroid.mutatio.ref.Reference;
6+
import org.comroid.mutatio.ref.ReferenceIndex;
7+
import org.comroid.mutatio.ref.ReferenceMap;
68
import org.jetbrains.annotations.NotNull;
79
import org.jetbrains.annotations.Nullable;
810

@@ -12,7 +14,7 @@
1214
import java.util.stream.Collectors;
1315
import java.util.stream.Stream;
1416

15-
public interface TrieMap<K, V> extends Map<K, V> {
17+
public interface TrieMap<K, V> extends ReferenceMap<K, V, Reference.Settable<V>>, Map<K, V> {
1618
Junction<K, String> getKeyConverter();
1719

1820
@Override
@@ -39,6 +41,11 @@ default void putAll(@NotNull Map<? extends K, ? extends V> map) {
3941

4042
Stage<V> getStage(String key);
4143

44+
default V get(Object key) {
45+
//noinspection unchecked
46+
return getReference((K) key).get();
47+
}
48+
4249
final class Stage<V> implements Map.Entry<String, V> {
4350
private final Map<Character, Stage<V>> storage = new ConcurrentHashMap<>();
4451
private final Reference.Settable<V> reference = Reference.Settable.create();
@@ -90,48 +97,53 @@ private Stream<Stage<V>> streamPresentStages() {
9097
);
9198
}
9299

93-
private Optional<V> getValue(char[] chars, int cIndex) {
100+
private Optional<Reference.Settable<V>> getReference(char[] chars, int cIndex) {
94101
if (cIndex >= chars.length)
95-
return reference.wrap();
102+
return Optional.of(reference);
96103

97-
return Optional.ofNullable(storage.getOrDefault(chars[cIndex], null))
98-
.flatMap(stage -> stage.getValue(chars, cIndex + 1));
104+
return getStageByChar(chars[cIndex])
105+
.flatMap(stage -> stage.getReference(chars, cIndex + 1));
99106
}
100107

101108
private Optional<V> putValue(char[] chars, int cIndex, @Nullable V value) {
102109
if (cIndex >= chars.length)
103110
return Optional.ofNullable(setValue(value));
104111

105112
// expect existing stages
106-
return Optional.ofNullable(storage.getOrDefault(chars[cIndex], null))
113+
return getStageByChar(chars[cIndex])
107114
.flatMap(stage -> stage.putValue(chars, cIndex + 1, value));
108115
}
109116

110117
private Optional<V> remove(char[] chars, int cIndex) {
111118
if (cIndex >= chars.length)
112119
return Optional.ofNullable(remove());
113120

114-
return Optional.ofNullable(storage.getOrDefault(chars[cIndex], null))
121+
return getStageByChar(chars[cIndex])
115122
.flatMap(stage -> stage.remove(chars, cIndex));
116123
}
117124

118125
public boolean containsKey(char[] chars, int cIndex) {
119126
if (cIndex >= chars.length)
120127
return false;
121128

122-
return Optional.ofNullable(storage.getOrDefault(chars[cIndex], null))
129+
return getStageByChar(chars[cIndex])
123130
.map(stage -> stage.containsKey(chars, cIndex + 1))
124131
.orElse(false);
125132
}
126133

127-
private Stage<V> requireStage(char[] chars, int cIndex) {
134+
public Stage<V> requireStage(char[] chars, int cIndex) {
128135
if (cIndex < chars.length) {
129136
return storage.computeIfAbsent(chars[cIndex], key -> {
130137
String converted = new String(Arrays.copyOfRange(chars, 0, cIndex + 1));
131138
return new Stage<>(converted);
132139
}).requireStage(chars, cIndex + 1);
133140
} else return this;
134141
}
142+
143+
@NotNull
144+
public Optional<Stage<V>> getStageByChar(char aChar) {
145+
return Optional.ofNullable(storage.getOrDefault(aChar, null));
146+
}
135147
}
136148

137149
class Basic<K, V> implements TrieMap<K, V> {
@@ -175,13 +187,58 @@ public boolean containsValue(Object value) {
175187
}
176188

177189
@Override
178-
public V get(Object key) {
190+
public Reference.Settable<V> getReference(K key, boolean createIfAbsent) {
179191
final char[] convertKey = convertKey(key);
180192

181193
if (convertKey.length == 0)
182-
return null;
194+
return Reference.Settable.create();
195+
196+
return baseStage.getReference(convertKey, 0)
197+
.orElseGet(Reference.Settable::create);
198+
}
183199

184-
return baseStage.getValue(convertKey, 0).orElse(null);
200+
@Override
201+
public ReferenceIndex<Entry<K, V>> entryIndex() {
202+
class RemoteIndex implements ReferenceIndex<Entry<K, V>> {
203+
private final ArrayList<Entry<K, V>> entries = new ArrayList<>(entrySet());
204+
205+
@Override
206+
public List<Entry<K, V>> unwrap() {
207+
return entries;
208+
}
209+
210+
@Override
211+
public int size() {
212+
return entries.size();
213+
}
214+
215+
@Override
216+
public boolean add(Entry<K, V> entry) {
217+
Basic.this.put(entry.getKey(), entry.getValue());
218+
return Basic.this.containsKey(entry.getKey());
219+
}
220+
221+
@Override
222+
public boolean remove(Entry<K, V> entry) {
223+
Basic.this.remove(entry.getKey());
224+
return !Basic.this.containsKey(entry.getKey());
225+
}
226+
227+
@Override
228+
public void clear() {
229+
Basic.this.clear();
230+
}
231+
232+
@Override
233+
public Reference<Entry<K, V>> getReference(int index) {
234+
return Reference.conditional(
235+
() -> entries.size() < index,
236+
() -> entries.get(index)
237+
);
238+
}
239+
}
240+
241+
return new RemoteIndex();
185242
}
186243

187244
@Nullable

src/trie/test/java/org/comroid/test/trie/TrieMapTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void testBasic() {
4040
// amount of calls made
4141
// multiplied by
4242
// 75 as the maximum average timeout
43-
@Test(timeout = (TEST_SIZE / 50) * 75)
43+
@Test(timeout = (TEST_SIZE / 50) * 150)
4444
public void testPerformance() {
4545
System.out.printf("Starting TrieMap performance test at %d with %d ms timeout\n", nanoTime(), (TEST_SIZE / 50) * 75);
4646

@@ -61,6 +61,12 @@ public void testPerformance() {
6161
private void assertions() {
6262
Assert.assertEquals(TEST_SIZE, trie.size());
6363

64+
final int equal = trie.biPipe()
65+
.filter((str, id) -> id.toString().equals(str))
66+
.span()
67+
.size();
68+
Assert.assertEquals(trie.size(), equal);
69+
6470
ids.forEach(uuid -> {
6571
final String str = uuid.toString();
6672

0 commit comments

Comments
 (0)