Skip to content

Commit 0f3049a

Browse files
authored
Merge pull request #10 from sjanarth/dev
Added CollatingIterator Sample
2 parents b6ec097 + e3fde7a commit 0f3049a

File tree

4 files changed

+69
-41
lines changed

4 files changed

+69
-41
lines changed

.idea/encodings.xml

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dsutils.iml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
33
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
4-
<output url="file://$MODULE_DIR$/target/classes" />
5-
<output-test url="file://$MODULE_DIR$/target/test-classes" />
4+
<output url="file://$MODULE_DIR$/${project.build.directory}/classes" />
5+
<output-test url="file://$MODULE_DIR$/${project.build.directory}/test-classes" />
66
<content url="file://$MODULE_DIR$">
77
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8+
<excludeFolder url="file://$MODULE_DIR$/${project.build.directory}/classes" />
9+
<excludeFolder url="file://$MODULE_DIR$/${project.build.directory}/test-classes" />
810
<excludeFolder url="file://$MODULE_DIR$/target" />
911
</content>
1012
<orderEntry type="inheritedJdk" />

src/main/java/com/sjanarth/dsutils/CollatingIterator.java

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,38 @@
22

33
import java.util.*;
44

5+
/**
6+
* Given a list of Iterators that produce an non-decreasing sequence, the CollatingIterator
7+
* can produce a collated, non-decreasing sequence without dropping any values from the
8+
* underling iterators.
9+
*
10+
* @param <E> Type - could be a primitive or a custom class
11+
*/
512
public class CollatingIterator<E> implements Iterator<E>
613
{
7-
private PriorityQueue<E> pq;
8-
private List<Iterator<E>> allIterators;
9-
private Map<E, Iterator<E>> waitingValues;
10-
14+
/**
15+
* Constructs an empty instance.
16+
*/
1117
public CollatingIterator () {
1218
pq = new PriorityQueue<E>();
13-
allIterators = new ArrayList<Iterator<E>>();
14-
waitingValues = new HashMap<E, Iterator<E>>();
19+
allIterators = new ArrayList<>();
20+
waitingValues = new HashMap<>();
1521
}
1622

23+
/**
24+
* Constructs an empty instance with a Comparator
25+
* @param comparator Comparator instance to compare elements.
26+
*/
1727
public CollatingIterator (Comparator<? super E> comparator) {
1828
pq = new PriorityQueue<E>(comparator);
19-
allIterators = new ArrayList<Iterator<E>>();
20-
waitingValues = new HashMap<E, Iterator<E>>();
29+
allIterators = new ArrayList<>();
30+
waitingValues = new HashMap<>();
2131
}
2232

33+
/**
34+
* Adds the given iterator to an internal list of iterators.
35+
* @param it Iterator instance to add to the list.
36+
*/
2337
public void addIterator(Iterator<E> it) {
2438
allIterators.add(it);
2539
}
@@ -59,33 +73,7 @@ private void pullFrom(Iterator<E> it) {
5973
}
6074
}
6175

62-
public static void main(String[] args) {
63-
CollatingIterator<Integer> ci = new CollatingIterator<>();
64-
ci.addIterator(new RandomIncreasingIterator());
65-
ci.addIterator(new RandomIncreasingIterator());
66-
ci.addIterator(new RandomIncreasingIterator());
67-
for (int i = 0; i < 10; i++)
68-
System.out.println(ci.next());
69-
}
70-
71-
private static class RandomIncreasingIterator implements Iterator<Integer> {
72-
public RandomIncreasingIterator() {}
73-
private Integer lastValue = null;
74-
@Override
75-
public boolean hasNext() {
76-
return true;
77-
}
78-
@Override
79-
public Integer next() {
80-
Integer value = (int) (Math.random() * 10.0);
81-
//System.out.println("value="+value+", lastValue="+lastValue);
82-
if (lastValue == null) {
83-
lastValue = value;
84-
return value;
85-
} else {
86-
lastValue = lastValue + value;
87-
return lastValue;
88-
}
89-
}
90-
}
91-
}
76+
private PriorityQueue<E> pq;
77+
private List<Iterator<E>> allIterators;
78+
private Map<E, Iterator<E>> waitingValues;
79+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.sjanarth.dsutils.samples;
2+
3+
import java.util.Iterator;
4+
5+
public class CollatingIterator
6+
{
7+
public static void main (String[] args) {
8+
com.sjanarth.dsutils.CollatingIterator<Integer> ci = new com.sjanarth.dsutils.CollatingIterator<>();
9+
ci.addIterator(new RandomIncreasingIterator());
10+
ci.addIterator(new RandomIncreasingIterator());
11+
ci.addIterator(new RandomIncreasingIterator());
12+
for (int i = 0; i < 10; i++)
13+
System.out.println(ci.next());
14+
}
15+
16+
private static class RandomIncreasingIterator implements Iterator<Integer> {
17+
public RandomIncreasingIterator() {}
18+
private Integer lastValue = null;
19+
@Override
20+
public boolean hasNext() {
21+
return true;
22+
}
23+
@Override
24+
public Integer next() {
25+
Integer value = (int) (Math.random() * 10.0);
26+
//System.out.println("value="+value+", lastValue="+lastValue);
27+
if (lastValue == null) {
28+
lastValue = value;
29+
return value;
30+
} else {
31+
lastValue = lastValue + value;
32+
return lastValue;
33+
}
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)