-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5d5566c
commit 937728e
Showing
8 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.kingdoms.utils | ||
|
||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
class AverageStats<K : Any, V : Number> { | ||
private val data: MutableMap<K, Avg> = ConcurrentHashMap() | ||
|
||
/** | ||
* Instead of keeping all the data set, we update the | ||
* values we need in order to take an average from | ||
* a data set with a much, much smaller memory. | ||
*/ | ||
private class Avg(var sum: Double, var count: Long) | ||
|
||
fun addData(key: K, data: V) { | ||
val avg = this.data.getOrPut(key) { Avg(0.0, 0L) } | ||
avg.apply { | ||
sum += data.toDouble() | ||
count++ | ||
|
||
if (sum >= Double.MAX_VALUE) { | ||
// Time to free up some memory. | ||
sum = Double.MAX_VALUE / count | ||
count = 1 | ||
} | ||
} | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") fun getAverage(key: K): V? { | ||
val avg = this.data[key] ?: return null | ||
return (avg.sum / avg.count) as V | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
shared/src/main/java/org/kingdoms/utils/internal/iterator/SharedSubIterable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.kingdoms.utils.internal.iterator; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Iterator; | ||
|
||
public class SharedSubIterable<E> implements Iterable<E> { | ||
private final SubIterator<E> subIterable; | ||
|
||
public SharedSubIterable(Iterable<E> iterable, int limit) { | ||
this.subIterable = new SubIterator<>(iterable.iterator(), limit); | ||
} | ||
|
||
public SubIterator<E> getSharedIterator() { | ||
return subIterable; | ||
} | ||
|
||
public boolean hasRemaining() { | ||
return subIterable.hasNext(); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public Iterator<E> iterator() { | ||
return subIterable; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
shared/src/main/java/org/kingdoms/utils/internal/iterator/SubIterable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.kingdoms.utils.internal.iterator; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Iterator; | ||
|
||
public class SubIterable<E> implements Iterable<E> { | ||
private final Iterable<E> iterable; | ||
private final int limit; | ||
|
||
public SubIterable(Iterable<E> iterable, int limit) { | ||
this.iterable = iterable; | ||
this.limit = limit; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public Iterator<E> iterator() { | ||
return new SubIterator<>(iterable.iterator(), limit); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
shared/src/main/java/org/kingdoms/utils/internal/iterator/SubIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.kingdoms.utils.internal.iterator; | ||
|
||
import java.util.Iterator; | ||
import java.util.function.Consumer; | ||
|
||
public class SubIterator<E> implements Iterator<E> { | ||
private final Iterator<E> iterator; | ||
private final int limit; | ||
private int counted; | ||
|
||
public SubIterator(Iterator<E> iterator, int limit) { | ||
this.iterator = iterator; | ||
this.limit = limit; | ||
} | ||
|
||
public void resetIndex() { | ||
this.counted = 0; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return counted < limit && iterator.hasNext(); | ||
} | ||
|
||
@Override | ||
public E next() { | ||
counted++; | ||
return iterator.next(); | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
iterator.remove(); | ||
} | ||
|
||
@Override | ||
public void forEachRemaining(Consumer<? super E> action) { | ||
iterator.forEachRemaining(action); | ||
} | ||
} |