-
Notifications
You must be signed in to change notification settings - Fork 6
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
Showing
5 changed files
with
121 additions
and
26 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
...t/bancaditalia/oss/vtl/coverage/tests/10 Aggregate and Analytic operators/median/ex_1.csv
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
id_2,me_1,me_2 | ||
XX,20,17 | ||
YY,5,4 | ||
XX,2,14 | ||
YY,5,3 |
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
109 changes: 109 additions & 0 deletions
109
vtl-types/src/main/java/it/bancaditalia/oss/vtl/impl/types/operators/MedianCollector.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,109 @@ | ||
/* | ||
* Copyright © 2020 Banca D'Italia | ||
* | ||
* Licensed under the EUPL, Version 1.2 (the "License"); | ||
* You may not use this work except in compliance with the | ||
* License. | ||
* You may obtain a copy of the License at: | ||
* | ||
* https://joinup.ec.europa.eu/sites/default/files/custom-page/attachment/2020-03/EUPL-1.2%20EN.txt | ||
* | ||
* Unless required by applicable law or agreed to in | ||
* writing, software distributed under the License is | ||
* distributed on an "AS IS" basis, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. | ||
* | ||
* See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
package it.bancaditalia.oss.vtl.impl.types.operators; | ||
|
||
import static java.util.Collections.reverseOrder; | ||
|
||
import java.io.Serializable; | ||
import java.util.EnumSet; | ||
import java.util.Optional; | ||
import java.util.PriorityQueue; | ||
|
||
import it.bancaditalia.oss.vtl.model.data.ScalarValue; | ||
import it.bancaditalia.oss.vtl.util.SerCollector; | ||
|
||
public class MedianCollector extends SerCollector<ScalarValue<?, ?, ?, ?>, MedianCollector.MedianAcc, Optional<ScalarValue<?, ?, ?, ?>>> | ||
{ | ||
private static final long serialVersionUID = 1L; | ||
|
||
public static class MedianAcc implements Serializable | ||
{ | ||
private static final long serialVersionUID = 1L; | ||
|
||
private final Class<?> repr; | ||
private PriorityQueue<ScalarValue<?, ?, ?, ?>> left = new PriorityQueue<>(reverseOrder()); | ||
private PriorityQueue<ScalarValue<?, ?, ?, ?>> right = new PriorityQueue<>(); | ||
|
||
public MedianAcc(Class<?> repr) | ||
{ | ||
this.repr = repr; | ||
} | ||
|
||
public void accumulate(ScalarValue<?, ?, ?, ?> value) | ||
{ | ||
if (left.isEmpty() || value.compareTo(left.peek()) <= 0) | ||
{ | ||
left.add(value); | ||
if (left.size() > right.size() + 1) | ||
right.add(left.poll()); | ||
} | ||
else | ||
{ | ||
right.add(value); | ||
if (right.size() > left.size()) | ||
left.add(right.poll()); | ||
} | ||
} | ||
|
||
public MedianAcc merge(MedianAcc other) | ||
{ | ||
left.addAll(other.left); | ||
right.addAll(other.right); | ||
|
||
while (left.size() > right.size() + 1) | ||
right.add(left.poll()); | ||
while (right.size() > left.size()) | ||
left.add(right.poll()); | ||
|
||
return this; | ||
} | ||
|
||
public Optional<ScalarValue<?, ?, ?, ?>> finish() | ||
{ | ||
while (left.size() > right.size() + 1) | ||
right.add(left.poll()); | ||
while (right.size() > left.size()) | ||
left.add(right.poll()); | ||
|
||
ScalarValue<?, ?, ?, ?> l = left.peek(); | ||
ScalarValue<?, ?, ?, ?> r = right.peek(); | ||
|
||
if (l != null && r != null) | ||
return Optional.of(l.compareTo(r) <= 0 ? l : r); | ||
else | ||
return Optional.ofNullable(l); | ||
} | ||
|
||
public Class<?> getRepr() | ||
{ | ||
return repr; | ||
} | ||
} | ||
|
||
public MedianCollector(Class<?> repr) | ||
{ | ||
super(() -> new MedianAcc(repr), MedianAcc::accumulate, MedianAcc::merge, MedianAcc::finish, EnumSet.noneOf(Characteristics.class)); | ||
} | ||
|
||
public static SerCollector<ScalarValue<?, ?, ?, ?>, ?, Optional<ScalarValue<?, ?, ?, ?>>> medianCollector(Class<?> repr) | ||
{ | ||
return new MedianCollector(repr); | ||
} | ||
} |