Skip to content

Commit

Permalink
GroupBy.rowNumber() #16
Browse files Browse the repository at this point in the history
* adding Series.concat functionality to concatenate series
  • Loading branch information
andrus committed Mar 31, 2019
1 parent cbe7f62 commit 1cc0a58
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
13 changes: 13 additions & 0 deletions dflib/src/main/java/com/nhl/dflib/Series.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.nhl.dflib;

import com.nhl.dflib.concat.SeriesConcat;
import com.nhl.dflib.series.ArraySeries;
import com.nhl.dflib.series.EmptySeries;
import com.nhl.dflib.series.RangeSeries;
Expand Down Expand Up @@ -42,4 +43,16 @@ default Series<T> openClosedRange(int fromInclusive, int toExclusive) {
Series<T> fillNullsBackwards();

Series<T> fillNullsForward();

default Series<T> concat(Series<? extends T>... other) {
if (other.length == 0) {
return this;
}

Series<? extends T>[] combined = new Series[other.length + 1];
combined[0] = this;
System.arraycopy(other, 0, combined, 1, other.length);

return SeriesConcat.concat(combined);
}
}
25 changes: 25 additions & 0 deletions dflib/src/main/java/com/nhl/dflib/concat/SeriesConcat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.nhl.dflib.concat;

import com.nhl.dflib.Series;
import com.nhl.dflib.series.ArraySeries;

public class SeriesConcat {

public static <T> Series<T> concat(Series<? extends T>... concat) {

int h = 0;
int n = concat.length;
for (Series<?> s : concat) {
h += s.size();
}

T[] data = (T[]) new Object[h];
for (int i = 0, ai = 0; i < n; i++) {
int len = concat[i].size();
concat[i].copyTo(data, 0, ai, len);
ai += len;
}

return new ArraySeries<>(data);
}
}
47 changes: 47 additions & 0 deletions dflib/src/test/java/com/nhl/dflib/Series_ConcatTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.nhl.dflib;

import com.nhl.dflib.unit.SeriesAsserts;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Collection;

import static org.junit.Assert.*;

@RunWith(Parameterized.class)
public class Series_ConcatTest extends BaseSeriesTest {

public Series_ConcatTest(SeriesTypes seriesType) {
super(seriesType);
}

@Parameterized.Parameters
public static Collection<Object[]> data() {
return ALL_SERIES_TYPES;
}

@Test
public void testConcat_None() {
Series<String> s = createSeries("a", "b");
assertSame(s, s.concat());
}

@Test
public void testConcat_Self() {
Series<String> s = createSeries("a", "b");
Series<String> c = s.concat(s);
new SeriesAsserts(c).expectData("a", "b", "a", "b");
}

@Test
public void testConcat() {
Series<String> s1 = createSeries("m", "n");
Series<String> s2 = createSeries("a", "b");
Series<String> s3 = createSeries("d", "c");

Series<String> c = s1.concat(s2, s3);
new SeriesAsserts(c).expectData("m", "n", "a", "b", "d", "c");
}
}

0 comments on commit 1cc0a58

Please sign in to comment.